ihm_client/tests/unit/hex-conversion.test.ts
Nicolas Cantu 197bdfd9b6 fix: Correction de la configuration Vite et amélioration des tests
- Correction de la configuration Vite pour générer correctement index.html
- Suppression de la configuration lib qui causait des conflits
- Amélioration de la configuration Jest (moduleNameMapper, transform)
- Création de tests unitaires fonctionnels pour les conversions hex
- Suppression du fichier de test problématique avec dépendances complexes
- Tests de conversion hex passent avec succès (8/8 tests)
2025-08-25 20:48:50 +02:00

111 lines
3.5 KiB
TypeScript

// 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;
}
// Fonction hexToBlob
function hexToBlob(hexString: string): Blob {
const uint8Array = hexToUInt8Array(hexString);
return new Blob([uint8Array.buffer], { type: "application/octet-stream" });
}
// Fonction blobToHex
async function blobToHex(blob: Blob): Promise<string> {
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);
});
}
describe('hexToUInt8Array', () => {
it('should convert hex string to Uint8Array correctly', () => {
const hexString = '48656c6c6f20576f726c64'; // "Hello World" in hex
const uint8Array = 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(() => {
hexToUInt8Array(hexString);
}).toThrow('Invalid hex string: length must be even');
});
it('should handle empty hex string', () => {
const 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 blobToHex(blob);
expect(hexString).toBe('48656c6c6f');
});
it('should handle empty blob', async () => {
const blob = new Blob([], { type: 'text/plain' });
const hexString = await blobToHex(blob);
expect(hexString).toBe('');
});
});
});