import '@testing-library/jest-dom'; import { vi } from 'vitest'; import { webcrypto } from 'node:crypto'; // Polyfill Web Crypto API for Node.js test environment Object.defineProperty(global, 'crypto', { value: webcrypto, configurable: true, }); // Mock Tauri API vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(), })); // Mock Tauri runtime check vi.mock('../src/lib/tauri-gateway', () => ({ isTauriRuntime: () => false, getGatewayClient: vi.fn(), startLocalGateway: vi.fn(), stopLocalGateway: vi.fn(), getLocalGatewayStatus: vi.fn(), getLocalGatewayAuth: vi.fn(), prepareLocalGatewayForTauri: vi.fn(), approveLocalGatewayDevicePairing: vi.fn(), getZclawProcessList: vi.fn(), getZclawProcessLogs: vi.fn(), getUnsupportedLocalGatewayStatus: vi.fn(() => ({ supported: false, cliAvailable: false, runtimeSource: null, runtimePath: null, serviceLabel: null, serviceLoaded: false, serviceStatus: null, configOk: false, port: null, portStatus: null, probeUrl: null, listenerPids: [], error: null, raw: {}, })), })); // Mock localStorage with export for test access export const localStorageMock = (() => { let store: Record = {}; return { getItem: (key: string) => store[key] || null, setItem: (key: string, value: string) => { store[key] = value; }, removeItem: (key: string) => { delete store[key]; }, clear: () => { store = {}; }, get length() { return Object.keys(store).length; }, key: (index: number) => { const keys = Object.keys(store); return keys[index] || null; }, }; })(); Object.defineProperty(global, 'localStorage', { value: localStorageMock, configurable: true, }); // Note: We intentionally do NOT mock crypto.subtle here. // Tests that need real crypto operations (like crypto-utils) will use the real Web Crypto API. // Tests that need to mock crypto operations should do so in their own test files. // // If you need to mock crypto.subtle for specific tests, use: // vi.spyOn(crypto.subtle, 'encrypt').mockImplementation(...) // Or restore after mocking: // afterAll(() => vi.restoreAllMocks())