ihm_client/tests/unit/services.test.ts
Nicolas Cantu 71b18a315f feat: amélioration du script de démarrage et ajout des tests
- Amélioration du script start.sh pour une meilleure robustesse
- Suppression des dépendances critiques pour permettre le démarrage même si certains services ne sont pas prêts
- Ajout de vérifications WebSocket pour les relays
- Correction de la fonction hexToBlob pour gérer correctement les types ArrayBuffer
- Ajout de tests unitaires pour les fonctions de conversion hex
- Configuration Jest pour les tests
- Mise à jour de la documentation d'intégration avec 4NK_node
- Amélioration du .gitignore pour exclure les dépendances temporaires
2025-08-25 20:29:24 +02:00

84 lines
2.5 KiB
TypeScript

describe('Services - Hex Conversion', () => {
let services: Services;
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
});
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);
expect(uint8Array).toBeInstanceOf(Uint8Array);
expect(uint8Array.length).toBe(11);
expect(uint8Array[0]).toBe(72); // 'H' ASCII
expect(uint8Array[10]).toBe(100); // 'd' ASCII
});
it('should throw error for odd length hex string', () => {
const hexString = '48656c6c6f20576f726c6'; // Odd length
expect(() => {
services.hexToUInt8Array(hexString);
}).toThrow('Invalid hex string: length must be even');
});
it('should handle empty hex string', () => {
const hexString = '';
const uint8Array = services.hexToUInt8Array(hexString);
expect(uint8Array).toBeInstanceOf(Uint8Array);
expect(uint8Array.length).toBe(0);
});
});
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);
expect(hexString).toBe('48656c6c6f');
});
it('should handle empty blob', async () => {
const blob = new Blob([], { type: 'text/plain' });
const hexString = await services.blobToHex(blob);
expect(hexString).toBe('');
});
});
});