Files
cc-switch/tests/msw/handlers.ts
YoVinchen f1b0fa2985 test: update test suites to match component refactoring
Comprehensive test updates to align with recent component refactoring
and new auto-launch functionality.

Component Tests:
- AddProviderDialog.test.tsx (10 lines):
  * Updated test cases for new dialog behavior
  * Enhanced mock data for preset selection
  * Improved assertions for validation

- ImportExportSection.test.tsx (16 lines):
  * Updated for new settings page integration
  * Enhanced test coverage for error scenarios
  * Better mock state management

- McpFormModal.test.tsx (60 lines):
  * Extensive updates for form refactoring
  * New test cases for multi-app selection
  * Enhanced validation testing
  * Better coverage of stdio/http server types

- ProviderList.test.tsx (11 lines):
  * Updated for new card layout
  * Enhanced drag-and-drop testing

- SettingsDialog.test.tsx (96 lines):
  * Major updates for SettingsPage migration
  * New test cases for auto-launch functionality
  * Enhanced integration test coverage
  * Better async operation testing

Hook Tests:
- useDirectorySettings.test.tsx (32 lines):
  * Updated for refactored hook logic
  * Enhanced test coverage for edge cases

- useDragSort.test.tsx (36 lines):
  * Simplified test cases
  * Better mock implementation
  * Improved assertions

- useImportExport tests (16 lines total):
  * Updated for new error handling
  * Enhanced test coverage

- useMcpValidation.test.tsx (23 lines):
  * Updated validation test cases
  * Better coverage of error scenarios

- useProviderActions.test.tsx (48 lines):
  * Extensive updates for hook refactoring
  * New test cases for provider operations
  * Enhanced mock data

- useSettings.test.tsx (12 lines):
  * New test cases for auto-launch
  * Enhanced settings state testing
  * Better async operation coverage

Integration Tests:
- App.test.tsx (41 lines):
  * Updated for new routing logic
  * Enhanced navigation testing
  * Better component integration coverage

- SettingsDialog.test.tsx (88 lines):
  * Complete rewrite for SettingsPage
  * New integration test scenarios
  * Enhanced user workflow testing

Mock Infrastructure:
- handlers.ts (117 lines):
  * Major updates for MSW handlers
  * New handlers for auto-launch commands
  * Enhanced error simulation
  * Better request/response mocking

- state.ts (37 lines):
  * Updated mock state structure
  * New state for auto-launch
  * Enhanced state reset functionality

- tauriMocks.ts (10 lines):
  * Updated mock implementations
  * Better type safety

- server.ts & testQueryClient.ts:
  * Minor cleanup (2 lines removed)

Test Infrastructure Improvements:
- Better test isolation
- Enhanced mock data consistency
- Improved async operation testing
- Better error scenario coverage
- Enhanced integration test patterns

Coverage Improvements:
- Net increase of 195 lines of test code
- Better coverage of edge cases
- Enhanced error path testing
- Improved integration test scenarios
- Better mock infrastructure

All tests now pass with the refactored components while maintaining
comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00

240 lines
7.0 KiB
TypeScript

import { http, HttpResponse } from "msw";
import type { AppId } from "@/lib/api/types";
import type { McpServer, Provider, Settings } from "@/types";
import {
addProvider,
deleteProvider,
getCurrentProviderId,
getProviders,
listProviders,
resetProviderState,
setCurrentProviderId,
updateProvider,
updateSortOrder,
getSettings,
setSettings,
getAppConfigDirOverride,
setAppConfigDirOverrideState,
getMcpConfig,
setMcpServerEnabled,
upsertMcpServer,
deleteMcpServer,
} from "./state";
const TAURI_ENDPOINT = "http://tauri.local";
const withJson = async <T>(request: Request): Promise<T> => {
try {
const body = await request.text();
if (!body) return {} as T;
return JSON.parse(body) as T;
} catch {
return {} as T;
}
};
const success = <T>(payload: T) => HttpResponse.json(payload as any);
export const handlers = [
http.post(`${TAURI_ENDPOINT}/get_providers`, async ({ request }) => {
const { app } = await withJson<{ app: AppId }>(request);
return success(getProviders(app));
}),
http.post(`${TAURI_ENDPOINT}/get_current_provider`, async ({ request }) => {
const { app } = await withJson<{ app: AppId }>(request);
return success(getCurrentProviderId(app));
}),
http.post(
`${TAURI_ENDPOINT}/update_providers_sort_order`,
async ({ request }) => {
const { updates = [], app } = await withJson<{
updates: { id: string; sortIndex: number }[];
app: AppId;
}>(request);
updateSortOrder(app, updates);
return success(true);
},
),
http.post(`${TAURI_ENDPOINT}/update_tray_menu`, () => success(true)),
http.post(`${TAURI_ENDPOINT}/switch_provider`, async ({ request }) => {
const { id, app } = await withJson<{ id: string; app: AppId }>(request);
const providers = listProviders(app);
if (!providers[id]) {
return HttpResponse.json(false, { status: 404 });
}
setCurrentProviderId(app, id);
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/add_provider`, async ({ request }) => {
const { provider, app } = await withJson<{
provider: Provider & { id?: string };
app: AppId;
}>(request);
const newId = provider.id ?? `mock-${Date.now()}`;
addProvider(app, { ...provider, id: newId });
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/update_provider`, async ({ request }) => {
const { provider, app } = await withJson<{
provider: Provider;
app: AppId;
}>(request);
updateProvider(app, provider);
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/delete_provider`, async ({ request }) => {
const { id, app } = await withJson<{ id: string; app: AppId }>(request);
deleteProvider(app, id);
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/import_default_config`, async () => {
resetProviderState();
return success(true);
}),
http.post(`${TAURI_ENDPOINT}/open_external`, () => success(true)),
// MCP APIs
http.post(`${TAURI_ENDPOINT}/get_mcp_config`, async ({ request }) => {
const { app } = await withJson<{ app: AppId }>(request);
return success(getMcpConfig(app));
}),
http.post(`${TAURI_ENDPOINT}/import_mcp_from_claude`, () => success(1)),
http.post(`${TAURI_ENDPOINT}/import_mcp_from_codex`, () => success(1)),
http.post(`${TAURI_ENDPOINT}/set_mcp_enabled`, async ({ request }) => {
const { app, id, enabled } = await withJson<{
app: AppId;
id: string;
enabled: boolean;
}>(request);
setMcpServerEnabled(app, id, enabled);
return success(true);
}),
http.post(
`${TAURI_ENDPOINT}/upsert_mcp_server_in_config`,
async ({ request }) => {
const { app, id, spec } = await withJson<{
app: AppId;
id: string;
spec: McpServer;
}>(request);
upsertMcpServer(app, id, spec);
return success(true);
},
),
http.post(
`${TAURI_ENDPOINT}/delete_mcp_server_in_config`,
async ({ request }) => {
const { app, id } = await withJson<{ app: AppId; id: string }>(request);
deleteMcpServer(app, id);
return 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 } = await withJson<{ app: AppId }>(request);
return success(app === "claude" ? "/default/claude" : "/default/codex");
}),
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
http.post(
`${TAURI_ENDPOINT}/select_config_directory`,
async ({ request }) => {
const { defaultPath, default_path } = await withJson<{
defaultPath?: string;
default_path?: string;
}>(request);
const initial = defaultPath ?? default_path;
return success(initial ? `${initial}/picked` : "/mock/selected-dir");
},
),
http.post(`${TAURI_ENDPOINT}/pick_directory`, async ({ request }) => {
const { defaultPath, default_path } = await withJson<{
defaultPath?: string;
default_path?: string;
}>(request);
const initial = defaultPath ?? default_path;
return success(initial ? `${initial}/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"),
),
// Sync current providers live (no-op success)
http.post(`${TAURI_ENDPOINT}/sync_current_providers_live`, () =>
success({ success: true }),
),
];