test: add SettingsDialog integration tests and enhance MSW infrastructure

- Add comprehensive SettingsDialog integration tests with 3 test cases:
  * Load default settings from MSW
  * Import configuration and trigger success callback
  * Save settings and handle restart prompt
- Extend MSW handlers with settings-related endpoints:
  * get_settings/save_settings for settings management
  * app_config_dir_override for custom config directory
  * apply_claude_plugin_config for plugin integration
  * import/export config file operations
  * file/directory dialog mocks
- Add settings state management to MSW mock state:
  * Settings state with default values
  * appConfigDirOverride state
  * Reset logic in resetProviderState()
- Mock @tauri-apps/api/path for DirectorySettings tests
- Refactor App.test.tsx to focus on happy path scenarios:
  * Remove delete functionality test (covered in useProviderActions unit tests)
  * Reorganize test flow: settings → switch → usage → create → edit → switch → duplicate
  * Remove unnecessary state verifications
  * Simplify event testing

All tests passing: 4 integration tests + 12 unit tests
This commit is contained in:
Jason
2025-10-25 19:59:31 +08:00
parent 96a8712f2d
commit 001ac14c85
5 changed files with 291 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
import { http, HttpResponse } from "msw";
import type { AppType } from "@/lib/api/types";
import type { Provider } from "@/types";
import type { Provider, Settings } from "@/types";
import {
addProvider,
deleteProvider,
@@ -11,6 +11,10 @@ import {
setCurrentProviderId,
updateProvider,
updateSortOrder,
getSettings,
setSettings,
getAppConfigDirOverride,
setAppConfigDirOverrideState,
} from "./state";
const TAURI_ENDPOINT = "http://tauri.local";
@@ -97,4 +101,65 @@ export const handlers = [
http.post(`${TAURI_ENDPOINT}/open_external`, () => success(true)),
http.post(`${TAURI_ENDPOINT}/restart_app`, () => success(true)),
http.post(`${TAURI_ENDPOINT}/get_settings`, () => success(getSettings())),
http.post(`${TAURI_ENDPOINT}/save_settings`, async ({ request }) => {
const { settings } = await withJson<{ settings: Settings }>(request);
setSettings(settings);
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/set_app_config_dir_override`, async ({ request }) => {
const { path } = await withJson<{ path: string | null }>(request);
setAppConfigDirOverrideState(path ?? null);
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/get_app_config_dir_override`, () =>
success(getAppConfigDirOverride()),
),
http.post(`${TAURI_ENDPOINT}/apply_claude_plugin_config`, async ({ request }) => {
const { official } = await withJson<{ official: boolean }>(request);
setSettings({ enableClaudePluginIntegration: !official });
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/get_config_dir`, async ({ request }) => {
const { app_type } = await withJson<{ app_type: AppType }>(request);
return success(app_type === "claude" ? "/default/claude" : "/default/codex");
}),
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
http.post(`${TAURI_ENDPOINT}/select_config_directory`, async ({ request }) => {
const { default_path } = await withJson<{ default_path?: string }>(request);
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
}),
http.post(`${TAURI_ENDPOINT}/open_file_dialog`, () =>
success("/mock/import-settings.json"),
),
http.post(`${TAURI_ENDPOINT}/import_config_from_file`, async ({ request }) => {
const { filePath } = await withJson<{ filePath: string }>(request);
if (!filePath) {
return success({ success: false, message: "Missing file" });
}
setSettings({ language: "en" });
return success({ success: true, backupId: "backup-123" });
}),
http.post(`${TAURI_ENDPOINT}/export_config_to_file`, async ({ request }) => {
const { filePath } = await withJson<{ filePath: string }>(request);
if (!filePath) {
return success({ success: false, message: "Invalid destination" });
}
return success({ success: true, filePath });
}),
http.post(`${TAURI_ENDPOINT}/save_file_dialog`, () =>
success("/mock/export-settings.json"),
),
];

View File

@@ -1,5 +1,5 @@
import type { AppType } from "@/lib/api/types";
import type { Provider } from "@/types";
import type { Provider, Settings } from "@/types";
type ProvidersByApp = Record<AppType, Record<string, Provider>>;
type CurrentProviderState = Record<AppType, string>;
@@ -50,6 +50,15 @@ const createDefaultCurrent = (): CurrentProviderState => ({
let providers = createDefaultProviders();
let current = createDefaultCurrent();
let settingsState: Settings = {
showInTray: true,
minimizeToTrayOnClose: true,
enableClaudePluginIntegration: false,
claudeConfigDir: "/default/claude",
codexConfigDir: "/default/codex",
language: "zh",
};
let appConfigDirOverride: string | null = null;
const cloneProviders = (value: ProvidersByApp) =>
JSON.parse(JSON.stringify(value)) as ProvidersByApp;
@@ -57,6 +66,15 @@ const cloneProviders = (value: ProvidersByApp) =>
export const resetProviderState = () => {
providers = createDefaultProviders();
current = createDefaultCurrent();
settingsState = {
showInTray: true,
minimizeToTrayOnClose: true,
enableClaudePluginIntegration: false,
claudeConfigDir: "/default/claude",
codexConfigDir: "/default/codex",
language: "zh",
};
appConfigDirOverride = null;
};
export const getProviders = (appType: AppType) =>
@@ -114,3 +132,14 @@ export const updateSortOrder = (
export const listProviders = (appType: AppType) =>
JSON.parse(JSON.stringify(providers[appType] ?? {})) as Record<string, Provider>;
export const getSettings = () => JSON.parse(JSON.stringify(settingsState)) as Settings;
export const setSettings = (data: Partial<Settings>) => {
settingsState = { ...settingsState, ...data };
};
export const getAppConfigDirOverride = () => appConfigDirOverride;
export const setAppConfigDirOverrideState = (value: string | null) => {
appConfigDirOverride = value;
};

View File

@@ -59,3 +59,7 @@ vi.mock("@tauri-apps/api/event", () => ({
// Ensure the MSW server is referenced so tree shaking doesn't remove imports
void server;
vi.mock("@tauri-apps/api/path", () => ({
homeDir: async () => "/home/mock",
join: async (...segments: string[]) => segments.join("/"),
}));