2025-10-25 19:59:31 +08:00
|
|
|
import React, { Suspense } from "react";
|
|
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
|
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
2025-10-25 20:43:47 +08:00
|
|
|
import { http, HttpResponse } from "msw";
|
2025-11-21 11:12:06 +08:00
|
|
|
import { SettingsPage } from "@/components/settings/SettingsPage";
|
|
|
|
|
import {
|
|
|
|
|
resetProviderState,
|
|
|
|
|
getSettings,
|
|
|
|
|
getAppConfigDirOverride,
|
|
|
|
|
} from "../msw/state";
|
2025-10-25 20:43:47 +08:00
|
|
|
import { server } from "../msw/server";
|
2025-10-25 19:59:31 +08:00
|
|
|
|
|
|
|
|
const toastSuccessMock = vi.fn();
|
|
|
|
|
const toastErrorMock = vi.fn();
|
|
|
|
|
|
|
|
|
|
vi.mock("sonner", () => ({
|
|
|
|
|
toast: {
|
|
|
|
|
success: (...args: unknown[]) => toastSuccessMock(...args),
|
|
|
|
|
error: (...args: unknown[]) => toastErrorMock(...args),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/ui/dialog", () => ({
|
2025-11-21 11:12:06 +08:00
|
|
|
Dialog: ({ open, children }: any) =>
|
|
|
|
|
open ? <div data-testid="dialog-root">{children}</div> : null,
|
2025-10-25 19:59:31 +08:00
|
|
|
DialogContent: ({ children }: any) => <div>{children}</div>,
|
|
|
|
|
DialogHeader: ({ children }: any) => <div>{children}</div>,
|
|
|
|
|
DialogFooter: ({ children }: any) => <div>{children}</div>,
|
|
|
|
|
DialogTitle: ({ children }: any) => <h2>{children}</h2>,
|
|
|
|
|
}));
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
const TabsContext = React.createContext<{
|
|
|
|
|
value: string;
|
|
|
|
|
onValueChange?: (value: string) => void;
|
|
|
|
|
}>({
|
|
|
|
|
value: "general",
|
|
|
|
|
});
|
2025-10-25 19:59:31 +08:00
|
|
|
|
|
|
|
|
vi.mock("@/components/ui/tabs", () => {
|
|
|
|
|
return {
|
|
|
|
|
Tabs: ({ value, onValueChange, children }: any) => (
|
2025-11-21 11:12:06 +08:00
|
|
|
<TabsContext.Provider value={{ value, onValueChange }}>
|
|
|
|
|
{children}
|
|
|
|
|
</TabsContext.Provider>
|
2025-10-25 19:59:31 +08:00
|
|
|
),
|
|
|
|
|
TabsList: ({ children }: any) => <div>{children}</div>,
|
|
|
|
|
TabsTrigger: ({ value, children }: any) => {
|
|
|
|
|
const ctx = React.useContext(TabsContext);
|
|
|
|
|
return (
|
|
|
|
|
<button type="button" onClick={() => ctx.onValueChange?.(value)}>
|
|
|
|
|
{children}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
TabsContent: ({ value, children }: any) => {
|
|
|
|
|
const ctx = React.useContext(TabsContext);
|
2025-11-21 11:12:06 +08:00
|
|
|
return ctx.value === value ? (
|
|
|
|
|
<div data-testid={`tab-${value}`}>{children}</div>
|
|
|
|
|
) : null;
|
2025-10-25 19:59:31 +08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/LanguageSettings", () => ({
|
|
|
|
|
LanguageSettings: ({ value, onChange }: any) => (
|
|
|
|
|
<div>
|
|
|
|
|
<span>language:{value}</span>
|
|
|
|
|
<button onClick={() => onChange("en")}>change-language</button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/ThemeSettings", () => ({
|
|
|
|
|
ThemeSettings: () => <div data-testid="theme-settings">theme</div>,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/WindowSettings", () => ({
|
|
|
|
|
WindowSettings: ({ onChange }: any) => (
|
2025-11-21 11:12:06 +08:00
|
|
|
<button onClick={() => onChange({ minimizeToTrayOnClose: false })}>
|
|
|
|
|
window-settings
|
|
|
|
|
</button>
|
2025-10-25 19:59:31 +08:00
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/DirectorySettings", async () => {
|
2025-11-21 11:12:06 +08:00
|
|
|
const actual = await vi.importActual<
|
|
|
|
|
typeof import("@/components/settings/DirectorySettings")
|
|
|
|
|
>("@/components/settings/DirectorySettings");
|
2025-10-25 19:59:31 +08:00
|
|
|
return actual;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/ImportExportSection", () => ({
|
|
|
|
|
ImportExportSection: ({
|
|
|
|
|
status,
|
|
|
|
|
selectedFile,
|
|
|
|
|
errorMessage,
|
|
|
|
|
isImporting,
|
|
|
|
|
onSelectFile,
|
|
|
|
|
onImport,
|
|
|
|
|
onExport,
|
|
|
|
|
onClear,
|
|
|
|
|
}: any) => (
|
|
|
|
|
<div>
|
|
|
|
|
<div data-testid="import-status">{status}</div>
|
|
|
|
|
<div data-testid="selected-file">{selectedFile || "none"}</div>
|
|
|
|
|
<button onClick={onSelectFile}>settings.selectConfigFile</button>
|
|
|
|
|
<button onClick={onImport} disabled={!selectedFile || isImporting}>
|
|
|
|
|
{isImporting ? "settings.importing" : "settings.import"}
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={onExport}>settings.exportConfig</button>
|
|
|
|
|
<button onClick={onClear}>common.clear</button>
|
|
|
|
|
{errorMessage ? <span>{errorMessage}</span> : null}
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/settings/AboutSection", () => ({
|
|
|
|
|
AboutSection: ({ isPortable }: any) => <div>about:{String(isPortable)}</div>,
|
|
|
|
|
}));
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
const renderDialog = (
|
|
|
|
|
props?: Partial<React.ComponentProps<typeof SettingsPage>>,
|
|
|
|
|
) => {
|
2025-10-25 19:59:31 +08:00
|
|
|
const client = new QueryClient();
|
|
|
|
|
return render(
|
|
|
|
|
<QueryClientProvider client={client}>
|
|
|
|
|
<Suspense fallback={<div data-testid="loading">loading</div>}>
|
2025-11-21 11:12:06 +08:00
|
|
|
<SettingsPage open onOpenChange={() => {}} {...props} />
|
2025-10-25 19:59:31 +08:00
|
|
|
</Suspense>
|
|
|
|
|
</QueryClientProvider>,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
resetProviderState();
|
|
|
|
|
toastSuccessMock.mockReset();
|
|
|
|
|
toastErrorMock.mockReset();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.useRealTimers();
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
describe("SettingsPage integration", () => {
|
2025-10-25 19:59:31 +08:00
|
|
|
it("loads default settings from MSW", async () => {
|
|
|
|
|
renderDialog();
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(screen.getByText("language:zh")).toBeInTheDocument(),
|
|
|
|
|
);
|
2025-10-25 19:59:31 +08:00
|
|
|
fireEvent.click(screen.getByText("settings.tabAdvanced"));
|
2025-11-21 11:12:06 +08:00
|
|
|
const appInput = await screen.findByPlaceholderText(
|
|
|
|
|
"settings.browsePlaceholderApp",
|
|
|
|
|
);
|
2025-10-25 19:59:31 +08:00
|
|
|
expect((appInput as HTMLInputElement).value).toBe("/home/mock/.cc-switch");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("imports configuration and triggers success callback", async () => {
|
|
|
|
|
const onImportSuccess = vi.fn();
|
|
|
|
|
renderDialog({ onImportSuccess });
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(screen.getByText("language:zh")).toBeInTheDocument(),
|
|
|
|
|
);
|
2025-10-25 19:59:31 +08:00
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("settings.tabAdvanced"));
|
|
|
|
|
fireEvent.click(screen.getByText("settings.selectConfigFile"));
|
|
|
|
|
await waitFor(() =>
|
2025-11-21 11:12:06 +08:00
|
|
|
expect(screen.getByTestId("selected-file").textContent).toContain(
|
|
|
|
|
"/mock/import-settings.json",
|
|
|
|
|
),
|
2025-10-25 19:59:31 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("settings.import"));
|
|
|
|
|
await waitFor(() => expect(toastSuccessMock).toHaveBeenCalled());
|
|
|
|
|
await waitFor(() => expect(onImportSuccess).toHaveBeenCalled(), {
|
|
|
|
|
timeout: 4000,
|
|
|
|
|
});
|
|
|
|
|
expect(getSettings().language).toBe("en");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("saves settings and handles restart prompt", async () => {
|
|
|
|
|
renderDialog();
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(screen.getByText("language:zh")).toBeInTheDocument(),
|
|
|
|
|
);
|
2025-10-25 19:59:31 +08:00
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("settings.tabAdvanced"));
|
2025-11-21 11:12:06 +08:00
|
|
|
const appInput = await screen.findByPlaceholderText(
|
|
|
|
|
"settings.browsePlaceholderApp",
|
|
|
|
|
);
|
2025-10-25 19:59:31 +08:00
|
|
|
fireEvent.change(appInput, { target: { value: "/custom/app" } });
|
|
|
|
|
fireEvent.click(screen.getByText("common.save"));
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(toastSuccessMock).toHaveBeenCalled());
|
|
|
|
|
await screen.findByText("settings.restartRequired");
|
|
|
|
|
fireEvent.click(screen.getByText("settings.restartLater"));
|
|
|
|
|
await waitFor(() =>
|
2025-11-21 11:12:06 +08:00
|
|
|
expect(
|
|
|
|
|
screen.queryByText("settings.restartRequired"),
|
|
|
|
|
).not.toBeInTheDocument(),
|
2025-10-25 19:59:31 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(getAppConfigDirOverride()).toBe("/custom/app");
|
|
|
|
|
});
|
2025-10-25 20:43:47 +08:00
|
|
|
|
|
|
|
|
it("allows browsing and resetting directories", async () => {
|
|
|
|
|
renderDialog();
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(screen.getByText("language:zh")).toBeInTheDocument(),
|
|
|
|
|
);
|
2025-10-25 20:43:47 +08:00
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("settings.tabAdvanced"));
|
|
|
|
|
|
|
|
|
|
const browseButtons = screen.getAllByTitle("settings.browseDirectory");
|
|
|
|
|
const resetButtons = screen.getAllByTitle("settings.resetDefault");
|
|
|
|
|
|
|
|
|
|
const appInput = (await screen.findByPlaceholderText(
|
|
|
|
|
"settings.browsePlaceholderApp",
|
|
|
|
|
)) as HTMLInputElement;
|
|
|
|
|
expect(appInput.value).toBe("/home/mock/.cc-switch");
|
|
|
|
|
|
|
|
|
|
fireEvent.click(browseButtons[0]);
|
|
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(appInput.value).toBe("/home/mock/.cc-switch/picked"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fireEvent.click(resetButtons[0]);
|
|
|
|
|
await waitFor(() => expect(appInput.value).toBe("/home/mock/.cc-switch"));
|
|
|
|
|
|
|
|
|
|
const claudeInput = (await screen.findByPlaceholderText(
|
|
|
|
|
"settings.browsePlaceholderClaude",
|
|
|
|
|
)) as HTMLInputElement;
|
|
|
|
|
fireEvent.change(claudeInput, { target: { value: "/custom/claude" } });
|
|
|
|
|
await waitFor(() => expect(claudeInput.value).toBe("/custom/claude"));
|
|
|
|
|
|
|
|
|
|
fireEvent.click(browseButtons[1]);
|
|
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(claudeInput.value).toBe("/custom/claude/picked"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fireEvent.click(resetButtons[1]);
|
|
|
|
|
await waitFor(() => expect(claudeInput.value).toBe("/home/mock/.claude"));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("notifies when export fails", async () => {
|
|
|
|
|
renderDialog();
|
|
|
|
|
|
2025-11-21 11:12:06 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(screen.getByText("language:zh")).toBeInTheDocument(),
|
|
|
|
|
);
|
2025-10-25 20:43:47 +08:00
|
|
|
fireEvent.click(screen.getByText("settings.tabAdvanced"));
|
|
|
|
|
|
|
|
|
|
server.use(
|
|
|
|
|
http.post("http://tauri.local/save_file_dialog", () =>
|
|
|
|
|
HttpResponse.json(null),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
fireEvent.click(screen.getByText("settings.exportConfig"));
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(toastErrorMock).toHaveBeenCalled());
|
|
|
|
|
const cancelMessage = toastErrorMock.mock.calls.at(-1)?.[0] as string;
|
2025-11-21 11:12:06 +08:00
|
|
|
expect(cancelMessage).toMatch(
|
|
|
|
|
/settings\.selectFileFailed|选择保存位置失败/,
|
|
|
|
|
);
|
2025-10-25 20:43:47 +08:00
|
|
|
|
|
|
|
|
toastErrorMock.mockClear();
|
|
|
|
|
|
|
|
|
|
server.use(
|
|
|
|
|
http.post("http://tauri.local/save_file_dialog", () =>
|
|
|
|
|
HttpResponse.json("/mock/export-settings.json"),
|
|
|
|
|
),
|
|
|
|
|
http.post("http://tauri.local/export_config_to_file", () =>
|
|
|
|
|
HttpResponse.json({ success: false, message: "disk-full" }),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("settings.exportConfig"));
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(toastErrorMock).toHaveBeenCalled());
|
|
|
|
|
const exportMessage = toastErrorMock.mock.calls.at(-1)?.[0] as string;
|
|
|
|
|
expect(exportMessage).toContain("disk-full");
|
|
|
|
|
expect(toastSuccessMock).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2025-10-25 19:59:31 +08:00
|
|
|
});
|