Files
cc-switch/tests/msw/state.ts

146 lines
4.0 KiB
TypeScript
Raw Normal View History

test: migrate to MSW testing architecture for App integration test Major testing infrastructure upgrade from manual mocks to Mock Service Worker (MSW): New MSW infrastructure (tests/msw/): - Add state.ts: In-memory state manager with full CRUD operations - Manage providers and current selections per app type (Claude/Codex) - Auto-switch current provider when deleted - Deep clone to prevent reference pollution - Add handlers.ts: HTTP request handlers for 10 Tauri API endpoints - Mock get_providers, switch_provider, add/update/delete_provider - Mock update_sort_order, update_tray_menu, import_default_config - Support error scenarios (404 for non-existent providers) - Add tauriMocks.ts: Tauri API mock layer - Transparently convert invoke() calls to HTTP requests - Mock event listener system with emitTauriEvent helper - Use cross-fetch for Node.js environment - Add server.ts: MSW server setup for Node.js test environment Refactor App.test.tsx (-170 lines, -43%): - Remove 23 manual mocks (useProvidersQuery, useProviderActions, etc.) - Run real hooks with MSW-backed API calls instead of mock implementations - Test real state changes instead of mock call counts - Add comprehensive flow: duplicate → create → edit → delete → event listening - Verify actual provider list changes and current selection updates Setup integration: - Add MSW server lifecycle to tests/setupTests.ts - Start server before all tests - Reset handlers and state after each test - Close server after all tests complete - Clear all mocks in afterEach for test isolation Dependencies: - Add msw@^2.11.6 for API mocking - Add cross-fetch@^4.1.0 for fetch polyfill in Node.js Type fixes: - Add missing imports (AppType, Provider) in handlers.ts - Fix HttpResponse.json generic constraint with as any (MSW best practice) - Change invalid category "default" to "official" in state.ts Test results: All 50 tests passing across 8 files, 0 TypeScript errors
2025-10-25 16:48:43 +08:00
import type { AppType } from "@/lib/api/types";
import type { Provider, Settings } from "@/types";
test: migrate to MSW testing architecture for App integration test Major testing infrastructure upgrade from manual mocks to Mock Service Worker (MSW): New MSW infrastructure (tests/msw/): - Add state.ts: In-memory state manager with full CRUD operations - Manage providers and current selections per app type (Claude/Codex) - Auto-switch current provider when deleted - Deep clone to prevent reference pollution - Add handlers.ts: HTTP request handlers for 10 Tauri API endpoints - Mock get_providers, switch_provider, add/update/delete_provider - Mock update_sort_order, update_tray_menu, import_default_config - Support error scenarios (404 for non-existent providers) - Add tauriMocks.ts: Tauri API mock layer - Transparently convert invoke() calls to HTTP requests - Mock event listener system with emitTauriEvent helper - Use cross-fetch for Node.js environment - Add server.ts: MSW server setup for Node.js test environment Refactor App.test.tsx (-170 lines, -43%): - Remove 23 manual mocks (useProvidersQuery, useProviderActions, etc.) - Run real hooks with MSW-backed API calls instead of mock implementations - Test real state changes instead of mock call counts - Add comprehensive flow: duplicate → create → edit → delete → event listening - Verify actual provider list changes and current selection updates Setup integration: - Add MSW server lifecycle to tests/setupTests.ts - Start server before all tests - Reset handlers and state after each test - Close server after all tests complete - Clear all mocks in afterEach for test isolation Dependencies: - Add msw@^2.11.6 for API mocking - Add cross-fetch@^4.1.0 for fetch polyfill in Node.js Type fixes: - Add missing imports (AppType, Provider) in handlers.ts - Fix HttpResponse.json generic constraint with as any (MSW best practice) - Change invalid category "default" to "official" in state.ts Test results: All 50 tests passing across 8 files, 0 TypeScript errors
2025-10-25 16:48:43 +08:00
type ProvidersByApp = Record<AppType, Record<string, Provider>>;
type CurrentProviderState = Record<AppType, string>;
const createDefaultProviders = (): ProvidersByApp => ({
claude: {
"claude-1": {
id: "claude-1",
name: "Claude Default",
settingsConfig: {},
category: "official",
sortIndex: 0,
createdAt: Date.now(),
},
"claude-2": {
id: "claude-2",
name: "Claude Custom",
settingsConfig: {},
category: "custom",
sortIndex: 1,
createdAt: Date.now() + 1,
},
},
codex: {
"codex-1": {
id: "codex-1",
name: "Codex Default",
settingsConfig: {},
category: "official",
sortIndex: 0,
createdAt: Date.now(),
},
"codex-2": {
id: "codex-2",
name: "Codex Secondary",
settingsConfig: {},
category: "custom",
sortIndex: 1,
createdAt: Date.now() + 1,
},
},
});
const createDefaultCurrent = (): CurrentProviderState => ({
claude: "claude-1",
codex: "codex-1",
});
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;
test: migrate to MSW testing architecture for App integration test Major testing infrastructure upgrade from manual mocks to Mock Service Worker (MSW): New MSW infrastructure (tests/msw/): - Add state.ts: In-memory state manager with full CRUD operations - Manage providers and current selections per app type (Claude/Codex) - Auto-switch current provider when deleted - Deep clone to prevent reference pollution - Add handlers.ts: HTTP request handlers for 10 Tauri API endpoints - Mock get_providers, switch_provider, add/update/delete_provider - Mock update_sort_order, update_tray_menu, import_default_config - Support error scenarios (404 for non-existent providers) - Add tauriMocks.ts: Tauri API mock layer - Transparently convert invoke() calls to HTTP requests - Mock event listener system with emitTauriEvent helper - Use cross-fetch for Node.js environment - Add server.ts: MSW server setup for Node.js test environment Refactor App.test.tsx (-170 lines, -43%): - Remove 23 manual mocks (useProvidersQuery, useProviderActions, etc.) - Run real hooks with MSW-backed API calls instead of mock implementations - Test real state changes instead of mock call counts - Add comprehensive flow: duplicate → create → edit → delete → event listening - Verify actual provider list changes and current selection updates Setup integration: - Add MSW server lifecycle to tests/setupTests.ts - Start server before all tests - Reset handlers and state after each test - Close server after all tests complete - Clear all mocks in afterEach for test isolation Dependencies: - Add msw@^2.11.6 for API mocking - Add cross-fetch@^4.1.0 for fetch polyfill in Node.js Type fixes: - Add missing imports (AppType, Provider) in handlers.ts - Fix HttpResponse.json generic constraint with as any (MSW best practice) - Change invalid category "default" to "official" in state.ts Test results: All 50 tests passing across 8 files, 0 TypeScript errors
2025-10-25 16:48:43 +08:00
const cloneProviders = (value: ProvidersByApp) =>
JSON.parse(JSON.stringify(value)) as 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;
test: migrate to MSW testing architecture for App integration test Major testing infrastructure upgrade from manual mocks to Mock Service Worker (MSW): New MSW infrastructure (tests/msw/): - Add state.ts: In-memory state manager with full CRUD operations - Manage providers and current selections per app type (Claude/Codex) - Auto-switch current provider when deleted - Deep clone to prevent reference pollution - Add handlers.ts: HTTP request handlers for 10 Tauri API endpoints - Mock get_providers, switch_provider, add/update/delete_provider - Mock update_sort_order, update_tray_menu, import_default_config - Support error scenarios (404 for non-existent providers) - Add tauriMocks.ts: Tauri API mock layer - Transparently convert invoke() calls to HTTP requests - Mock event listener system with emitTauriEvent helper - Use cross-fetch for Node.js environment - Add server.ts: MSW server setup for Node.js test environment Refactor App.test.tsx (-170 lines, -43%): - Remove 23 manual mocks (useProvidersQuery, useProviderActions, etc.) - Run real hooks with MSW-backed API calls instead of mock implementations - Test real state changes instead of mock call counts - Add comprehensive flow: duplicate → create → edit → delete → event listening - Verify actual provider list changes and current selection updates Setup integration: - Add MSW server lifecycle to tests/setupTests.ts - Start server before all tests - Reset handlers and state after each test - Close server after all tests complete - Clear all mocks in afterEach for test isolation Dependencies: - Add msw@^2.11.6 for API mocking - Add cross-fetch@^4.1.0 for fetch polyfill in Node.js Type fixes: - Add missing imports (AppType, Provider) in handlers.ts - Fix HttpResponse.json generic constraint with as any (MSW best practice) - Change invalid category "default" to "official" in state.ts Test results: All 50 tests passing across 8 files, 0 TypeScript errors
2025-10-25 16:48:43 +08:00
};
export const getProviders = (appType: AppType) =>
cloneProviders(providers)[appType] ?? {};
export const getCurrentProviderId = (appType: AppType) => current[appType] ?? "";
export const setCurrentProviderId = (appType: AppType, providerId: string) => {
current[appType] = providerId;
};
export const updateProviders = (appType: AppType, data: Record<string, Provider>) => {
providers[appType] = cloneProviders({ [appType]: data } as ProvidersByApp)[appType];
};
export const setProviders = (appType: AppType, data: Record<string, Provider>) => {
providers[appType] = JSON.parse(JSON.stringify(data)) as Record<string, Provider>;
};
export const addProvider = (appType: AppType, provider: Provider) => {
providers[appType] = providers[appType] ?? {};
providers[appType][provider.id] = provider;
};
export const updateProvider = (appType: AppType, provider: Provider) => {
if (!providers[appType]) return;
providers[appType][provider.id] = {
...providers[appType][provider.id],
...provider,
};
};
export const deleteProvider = (appType: AppType, providerId: string) => {
if (!providers[appType]) return;
delete providers[appType][providerId];
if (current[appType] === providerId) {
const fallback = Object.keys(providers[appType])[0] ?? "";
current[appType] = fallback;
}
};
export const updateSortOrder = (
appType: AppType,
updates: { id: string; sortIndex: number }[],
) => {
if (!providers[appType]) return;
updates.forEach(({ id, sortIndex }) => {
const provider = providers[appType][id];
if (provider) {
providers[appType][id] = { ...provider, sortIndex };
}
});
};
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;
};