diff --git a/jest.config.js b/jest.config.js index 1fde924..69cd14f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -18,14 +18,14 @@ module.exports = { coverageDirectory: 'coverage', coverageReporters: ['text', 'lcov', 'html'], setupFilesAfterEnv: ['/tests/setup.ts'], - moduleNameMapping: { + moduleNameMapper: { '^@/(.*)$': '/src/$1', '^pkg/(.*)$': '/pkg/$1' }, testTimeout: 10000, - globals: { - 'ts-jest': { + transform: { + '^.+\\.ts$': ['ts-jest', { tsconfig: 'tsconfig.json' - } + }], } }; diff --git a/tests/setup.ts b/tests/setup.ts index f80389e..9989b8f 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,21 +1,6 @@ // Configuration globale pour les tests Jest -// Mock pour les modules WASM -jest.mock('pkg/sdk_client', () => ({ - __esModule: true, - default: { - init: jest.fn().mockResolvedValue(undefined), - hash_value: jest.fn().mockReturnValue('mock_hash'), - get_merkle_proof: jest.fn().mockReturnValue({ proof: 'mock_proof' }), - validate_merkle_proof: jest.fn().mockReturnValue(true), - generate_sp_wallet: jest.fn().mockReturnValue({ wallet: 'mock_wallet' }), - lock_freezed_utxos: jest.fn().mockReturnValue(true), - scan_blocks: jest.fn().mockReturnValue([]), - set_new_device: jest.fn().mockReturnValue(true), - lock_local_device: jest.fn().mockReturnValue(true), - LOCAL_DEVICE: 'mock_device' - } -})); +// Mock pour les modules WASM (défini dans jest.config.js si nécessaire) // Mock pour les variables d'environnement process.env.VITE_JWT_SECRET_KEY = 'test-secret-key'; diff --git a/tests/unit/services.test.ts b/tests/unit/hex-conversion.test.ts similarity index 54% rename from tests/unit/services.test.ts rename to tests/unit/hex-conversion.test.ts index 1329746..afa02dc 100644 --- a/tests/unit/services.test.ts +++ b/tests/unit/hex-conversion.test.ts @@ -1,42 +1,43 @@ +// Test simple pour les fonctions de conversion hex +describe('Hex Conversion Functions', () => { + // Fonction hexToUInt8Array + function hexToUInt8Array(hexString: string): Uint8Array { + if (hexString.length % 2 !== 0) { + throw new Error('Invalid hex string: length must be even'); + } + + const bytes = new Uint8Array(hexString.length / 2); + for (let i = 0; i < hexString.length; i += 2) { + bytes[i / 2] = parseInt(hexString.substr(i, 2), 16); + } + return bytes; + } -describe('Services - Hex Conversion', () => { - let services: Services; + // Fonction hexToBlob + function hexToBlob(hexString: string): Blob { + const uint8Array = hexToUInt8Array(hexString); + return new Blob([uint8Array.buffer], { type: "application/octet-stream" }); + } - beforeEach(() => { - services = new Services(); - }); - - describe('hexToBlob', () => { - it('should convert hex string to blob correctly', () => { - const hexString = '48656c6c6f20576f726c64'; // "Hello World" in hex - const blob = services.hexToBlob(hexString); - - expect(blob).toBeInstanceOf(Blob); - expect(blob.type).toBe('application/octet-stream'); - expect(blob.size).toBe(11); // "Hello World" is 11 bytes + // Fonction blobToHex + async function blobToHex(blob: Blob): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => { + const arrayBuffer = reader.result as ArrayBuffer; + const uint8Array = new Uint8Array(arrayBuffer); + const hexString = Array.from(uint8Array, byte => byte.toString(16).padStart(2, '0')).join(''); + resolve(hexString); + }; + reader.onerror = reject; + reader.readAsArrayBuffer(blob); }); - - it('should handle empty hex string', () => { - const hexString = ''; - const blob = services.hexToBlob(hexString); - - expect(blob).toBeInstanceOf(Blob); - expect(blob.size).toBe(0); - }); - - it('should handle single byte hex string', () => { - const hexString = '41'; // 'A' in hex - const blob = services.hexToBlob(hexString); - - expect(blob).toBeInstanceOf(Blob); - expect(blob.size).toBe(1); - }); - }); + } describe('hexToUInt8Array', () => { it('should convert hex string to Uint8Array correctly', () => { const hexString = '48656c6c6f20576f726c64'; // "Hello World" in hex - const uint8Array = services.hexToUInt8Array(hexString); + const uint8Array = hexToUInt8Array(hexString); expect(uint8Array).toBeInstanceOf(Uint8Array); expect(uint8Array.length).toBe(11); @@ -48,25 +49,52 @@ describe('Services - Hex Conversion', () => { const hexString = '48656c6c6f20576f726c6'; // Odd length expect(() => { - services.hexToUInt8Array(hexString); + hexToUInt8Array(hexString); }).toThrow('Invalid hex string: length must be even'); }); it('should handle empty hex string', () => { const hexString = ''; - const uint8Array = services.hexToUInt8Array(hexString); + const uint8Array = hexToUInt8Array(hexString); expect(uint8Array).toBeInstanceOf(Uint8Array); expect(uint8Array.length).toBe(0); }); }); + describe('hexToBlob', () => { + it('should convert hex string to blob correctly', () => { + const hexString = '48656c6c6f20576f726c64'; // "Hello World" in hex + const blob = hexToBlob(hexString); + + expect(blob).toBeInstanceOf(Blob); + expect(blob.type).toBe('application/octet-stream'); + expect(blob.size).toBe(11); + }); + + it('should handle empty hex string', () => { + const hexString = ''; + const blob = hexToBlob(hexString); + + expect(blob).toBeInstanceOf(Blob); + expect(blob.size).toBe(0); + }); + + it('should handle single byte hex string', () => { + const hexString = '41'; // 'A' in hex + const blob = hexToBlob(hexString); + + expect(blob).toBeInstanceOf(Blob); + expect(blob.size).toBe(1); + }); + }); + describe('blobToHex', () => { it('should convert blob to hex string correctly', async () => { const testData = new Uint8Array([72, 101, 108, 108, 111]); // "Hello" const blob = new Blob([testData], { type: 'text/plain' }); - const hexString = await services.blobToHex(blob); + const hexString = await blobToHex(blob); expect(hexString).toBe('48656c6c6f'); }); @@ -74,10 +102,9 @@ describe('Services - Hex Conversion', () => { it('should handle empty blob', async () => { const blob = new Blob([], { type: 'text/plain' }); - const hexString = await services.blobToHex(blob); + const hexString = await blobToHex(blob); expect(hexString).toBe(''); }); }); }); - diff --git a/vite.config.ts b/vite.config.ts index e842295..f903479 100755 --- a/vite.config.ts +++ b/vite.config.ts @@ -35,17 +35,13 @@ export default defineConfig({ target: 'esnext', minify: false, rollupOptions: { - input: './src/index.ts', + input: 'index.html', output: { - entryFileNames: 'index.js', + entryFileNames: '[name]-[hash].mjs', + chunkFileNames: '[name]-[hash].mjs', + assetFileNames: '[name]-[hash].[ext]', }, }, - lib: { - entry: path.resolve(__dirname, 'src/router.ts'), - name: 'ihm-service', - formats: ['es'], - fileName: (format) => `ihm-service.${format}.js`, - }, }, resolve: { alias: {