- Add arrayToBase64/base64ToArray conversion functions - Add deriveKey for PBKDF2 key derivation - Add encrypt/decrypt using AES-GCM - Add generateMasterKey for random key generation - Update setup.ts to use real Web Crypto API instead of mock - Add comprehensive unit tests for all crypto functions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
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,
|
|
});
|
|
|
|
// 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())
|