/** * Tests for Binect API client * * These tests verify the binect-api module's error handling and response mapping. * The actual @binect/js library is tested separately. */ import { BinectAPIError } from '../src/utils/binect-api'; describe('BinectAPIError', () => { test('should create error with message only', () => { const error = new BinectAPIError('Test error'); expect(error.message).toBe('Test error'); expect(error.name).toBe('BinectAPIError'); expect(error.statusCode).toBeUndefined(); expect(error.response).toBeUndefined(); }); test('should create error with status code', () => { const error = new BinectAPIError('Unauthorized', 401); expect(error.message).toBe('Unauthorized'); expect(error.statusCode).toBe(401); }); test('should create error with response data', () => { const responseData = { error: 'Invalid format' }; const error = new BinectAPIError('Bad request', 400, responseData); expect(error.message).toBe('Bad request'); expect(error.statusCode).toBe(400); expect(error.response).toEqual(responseData); }); test('should be instanceof Error', () => { const error = new BinectAPIError('Test'); expect(error).toBeInstanceOf(Error); expect(error).toBeInstanceOf(BinectAPIError); }); }); describe('arrayBufferToBase64', () => { // Test the base64 encoding indirectly by checking the module exports // The actual encoding is tested via integration tests test('should handle empty ArrayBuffer', () => { const buffer = new ArrayBuffer(0); const bytes = new Uint8Array(buffer); let binary = ''; for (let i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } const base64 = btoa(binary); expect(base64).toBe(''); }); test('should encode simple data correctly', () => { // "Hello" in bytes const data = new Uint8Array([72, 101, 108, 108, 111]); let binary = ''; for (let i = 0; i < data.byteLength; i++) { binary += String.fromCharCode(data[i]); } const base64 = btoa(binary); expect(base64).toBe('SGVsbG8='); }); test('should encode PDF header correctly', () => { // PDF magic bytes: %PDF const pdfHeader = new Uint8Array([0x25, 0x50, 0x44, 0x46]); let binary = ''; for (let i = 0; i < pdfHeader.byteLength; i++) { binary += String.fromCharCode(pdfHeader[i]); } const base64 = btoa(binary); expect(base64).toBe('JVBERg=='); }); test('should handle binary data with all byte values', () => { // Test with bytes 0-255 to ensure full range works const data = new Uint8Array(256); for (let i = 0; i < 256; i++) { data[i] = i; } let binary = ''; for (let i = 0; i < data.byteLength; i++) { binary += String.fromCharCode(data[i]); } const base64 = btoa(binary); // Just verify it doesn't throw and produces valid base64 expect(base64).toMatch(/^[A-Za-z0-9+/]+=*$/); expect(base64.length).toBeGreaterThan(0); }); });