test: add Vitest configuration and setup
- Add vitest.config.ts with jsdom environment and path aliases - Add tests/setup.ts with mocks for Tauri API, crypto, and localStorage - Add test:coverage script to package.json Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
76
desktop/tests/setup.ts
Normal file
76
desktop/tests/setup.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
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(),
|
||||
getOpenFangProcessList: vi.fn(),
|
||||
getOpenFangProcessLogs: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock localStorage with export for test access
|
||||
export const localStorageMock = (() => {
|
||||
let store: Record<string, string> = {};
|
||||
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,
|
||||
});
|
||||
|
||||
// Mock crypto.subtle for tests (already set via webcrypto above, but ensure subtle mock works)
|
||||
const subtleMock = {
|
||||
encrypt: vi.fn(),
|
||||
decrypt: vi.fn(),
|
||||
generateKey: vi.fn(),
|
||||
deriveKey: vi.fn(),
|
||||
importKey: vi.fn(),
|
||||
exportKey: vi.fn(),
|
||||
digest: vi.fn(),
|
||||
sign: vi.fn(),
|
||||
verify: vi.fn(),
|
||||
};
|
||||
|
||||
// Override subtle if needed for specific test scenarios
|
||||
Object.defineProperty(global.crypto, 'subtle', {
|
||||
value: subtleMock,
|
||||
configurable: true,
|
||||
});
|
||||
Reference in New Issue
Block a user