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
This commit is contained in:
61
tests/msw/tauriMocks.ts
Normal file
61
tests/msw/tauriMocks.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import "cross-fetch/polyfill";
|
||||
import { vi } from "vitest";
|
||||
import { server } from "./server";
|
||||
|
||||
const TAURI_ENDPOINT = "http://tauri.local";
|
||||
|
||||
vi.mock("@tauri-apps/api/core", () => ({
|
||||
invoke: async (command: string, payload: Record<string, unknown> = {}) => {
|
||||
const response = await fetch(`${TAURI_ENDPOINT}/${command}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload ?? {}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(text || `Invoke failed for ${command}`);
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) return undefined;
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const listeners = new Map<
|
||||
string,
|
||||
Set<(event: { payload: unknown }) => void>
|
||||
>();
|
||||
|
||||
const ensureListenerSet = (event: string) => {
|
||||
if (!listeners.has(event)) {
|
||||
listeners.set(event, new Set());
|
||||
}
|
||||
return listeners.get(event)!;
|
||||
};
|
||||
|
||||
export const emitTauriEvent = (event: string, payload: unknown) => {
|
||||
const handlers = listeners.get(event);
|
||||
handlers?.forEach((handler) => handler({ payload }));
|
||||
};
|
||||
|
||||
vi.mock("@tauri-apps/api/event", () => ({
|
||||
listen: async (event: string, handler: (event: { payload: unknown }) => void) => {
|
||||
const set = ensureListenerSet(event);
|
||||
set.add(handler);
|
||||
return () => {
|
||||
set.delete(handler);
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
// Ensure the MSW server is referenced so tree shaking doesn't remove imports
|
||||
void server;
|
||||
|
||||
Reference in New Issue
Block a user