generated from coulomb/repo-seed
Add Binect SDK implementation, Explorer, and test suite
SDK (@binect/js): - BinectClient with domain sub-clients (documents, sendings, accounts, attachments, invoices) - HTTP Basic Auth, native fetch only (no runtime dependencies) - TypeScript types matching Binect API vocabulary - Status predicates and polling helpers in helpers.ts - Structured error handling (BinectApiError, BinectAuthError) Explorer: - Standalone browser-based API explorer (explorer/index.html) - Interactive testing without code Tests: - Unit tests for client, types, errors, helpers, http - E2E tests for upload/delete and send/cancel workflows Also includes: - Architecture Decision Records (ADRs) - Example DIN 5008 letter PDFs for testing - API specification research notes Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
89
tests/errors.test.ts
Normal file
89
tests/errors.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { BinectApiError, BinectAuthError } from '../src/errors.js';
|
||||
|
||||
describe('BinectApiError', () => {
|
||||
it('creates error with all properties', () => {
|
||||
const response = { error: 'Not found', message: 'Document not found' };
|
||||
const error = new BinectApiError('Not found', 404, '/documents/123', 'GET', response);
|
||||
|
||||
expect(error.name).toBe('BinectApiError');
|
||||
expect(error.message).toBe('Not found');
|
||||
expect(error.status).toBe(404);
|
||||
expect(error.endpoint).toBe('/documents/123');
|
||||
expect(error.method).toBe('GET');
|
||||
expect(error.response).toEqual(response);
|
||||
});
|
||||
|
||||
it('creates error without response', () => {
|
||||
const error = new BinectApiError('Server error', 500, '/documents', 'POST');
|
||||
|
||||
expect(error.status).toBe(500);
|
||||
expect(error.response).toBeNull();
|
||||
});
|
||||
|
||||
it('has correct inheritance', () => {
|
||||
const error = new BinectApiError('Test', 400, '/test', 'GET');
|
||||
|
||||
expect(error instanceof Error).toBe(true);
|
||||
expect(error instanceof BinectApiError).toBe(true);
|
||||
});
|
||||
|
||||
describe('toDetailedString', () => {
|
||||
it('includes all error details', () => {
|
||||
const response = {
|
||||
error: 'Validation error',
|
||||
message: 'Invalid document format',
|
||||
details: ['Page size incorrect', 'Missing address'],
|
||||
};
|
||||
const error = new BinectApiError('Validation failed', 400, '/documents', 'POST', response);
|
||||
|
||||
const detailed = error.toDetailedString();
|
||||
|
||||
expect(detailed).toContain('BinectApiError: Validation failed');
|
||||
expect(detailed).toContain('Status: 400');
|
||||
expect(detailed).toContain('Endpoint: POST /documents');
|
||||
expect(detailed).toContain('Error: Validation error');
|
||||
expect(detailed).toContain('Message: Invalid document format');
|
||||
expect(detailed).toContain('Details: Page size incorrect, Missing address');
|
||||
});
|
||||
|
||||
it('handles minimal error response', () => {
|
||||
const error = new BinectApiError('Server error', 500, '/test', 'GET');
|
||||
|
||||
const detailed = error.toDetailedString();
|
||||
|
||||
expect(detailed).toContain('BinectApiError: Server error');
|
||||
expect(detailed).toContain('Status: 500');
|
||||
// Should not contain "Error:" as a separate field (only in class name)
|
||||
expect(detailed).not.toContain(' Error:');
|
||||
expect(detailed).not.toContain(' Message:');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('BinectAuthError', () => {
|
||||
it('creates authentication error', () => {
|
||||
const error = new BinectAuthError('/accounts', 'GET');
|
||||
|
||||
expect(error.name).toBe('BinectAuthError');
|
||||
expect(error.message).toBe('Authentication failed: Invalid credentials');
|
||||
expect(error.status).toBe(401);
|
||||
expect(error.endpoint).toBe('/accounts');
|
||||
expect(error.method).toBe('GET');
|
||||
});
|
||||
|
||||
it('includes response when provided', () => {
|
||||
const response = { error: 'Unauthorized', message: 'Invalid credentials' };
|
||||
const error = new BinectAuthError('/accounts', 'GET', response);
|
||||
|
||||
expect(error.response).toEqual(response);
|
||||
});
|
||||
|
||||
it('inherits from BinectApiError', () => {
|
||||
const error = new BinectAuthError('/test', 'GET');
|
||||
|
||||
expect(error instanceof Error).toBe(true);
|
||||
expect(error instanceof BinectApiError).toBe(true);
|
||||
expect(error instanceof BinectAuthError).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user