import { expect, describe, it } from 'vitest'; import { hexToBase64, base64ToHex } from './base64'; import { generateKey } from './tmp-secret'; describe('base64 utility', () => { it('should convert hex to base64', () => { const input = '0x48656c6c6f2c20576f726c6421'; const expectedOutput = 'SGVsbG8sIFdvcmxkIQ'; const encoded = hexToBase64(input); expect(encoded).toBe(expectedOutput); }); it('should convert base64 to hex', () => { const input = 'SGVsbG8sIFdvcmxkIQ'; const expectedOutput = '0x48656c6c6f2c20576f726c6421'; const decoded = base64ToHex(input); expect(decoded).toBe(expectedOutput); }); it('should handle empty hex string conversion to base64', () => { const input = '0x'; const expectedOutput = ''; const encoded = hexToBase64(input); expect(encoded).toBe(expectedOutput); }); it('should handle empty base64 string conversion to hex', () => { const input = ''; const expectedOutput = '0x'; const decoded = base64ToHex(input); expect(decoded).toBe(expectedOutput); }); it('should handle special characters in hex to base64 conversion', () => { const input = '0xe79fbce6b8a3e5ad97e7acac'; const expectedOutput = '55-85rij5a2X56ys'; const encoded = hexToBase64(input); expect(encoded).toBe(expectedOutput); }); it('should handle special characters in base64 to hex conversion', () => { const input = '55-85rij5a2X56ys'; const expectedOutput = '0xe79fbce6b8a3e5ad97e7acac'; const decoded = base64ToHex(input); expect(decoded).toBe(expectedOutput); }); it('fuzz', () => { const hex = generateKey(); const base64 = hexToBase64(hex); const decoded = base64ToHex(base64); expect(decoded).toBe(hex); }); it('zero prefix', () => { const hex = '0x000123456789abcdef'; const base64 = hexToBase64(hex); const decoded = base64ToHex(base64); expect(decoded).toBe(hex); }); it('zero suffix', () => { const hex = '0x123456789abcdef000'; const base64 = hexToBase64(hex); const decoded = base64ToHex(base64); expect(decoded).toBe(hex); }); it('0.5 byte', () => { const hex = '0x000123456789abcde'; expect(() => hexToBase64(hex)).toThrow(Error); }); });