import { describe, expect, test } from 'bun:test'; import { MAX_RETRY_WAIT_MS, fieldFromPath, fixFor, pageStatusFor, retryDelayMs, unexplainedMessage } from './errors'; describe('retryDelayMs', () => { test('retries contended conflicts quickly', () => { expect(retryDelayMs(409, 'contended', null)).toBe(250); }); test('does not retry other conflicts', () => { expect(retryDelayMs(409, 'ship_docked', null)).toBeNull(); }); test('honours Retry-After on 429 and 503', () => { expect(retryDelayMs(429, 'rate_limited', '2')).toBe(2000); expect(retryDelayMs(503, 'busy', '0')).toBe(0); }); test('defaults to one second when Retry-After is missing or junk', () => { expect(retryDelayMs(429, 'rate_limited', null)).toBe(1000); expect(retryDelayMs(503, 'busy', 'soon')).toBe(1000); }); test('gives up when the wait is too long', () => { expect(retryDelayMs(429, 'rate_limited', String(MAX_RETRY_WAIT_MS / 1000 + 1))).toBeNull(); }); test('never retries client errors', () => { expect(retryDelayMs(422, 'invalid_body', '1')).toBeNull(); }); }); describe('fixFor', () => { test('maps state refusals to the action that resolves them', () => { expect(fixFor('ship_docked')).toBe('orbit'); expect(fixFor('ship_in_orbit')).toBe('dock'); expect(fixFor('not_enough_fuel')).toBe('refuel'); }); test('returns null for unknown codes', () => { expect(fixFor('internal')).toBeNull(); }); }); describe('fieldFromPath', () => { test('extracts a top-level field', () => { expect(fieldFromPath('$.waypoint')).toBe('waypoint'); }); test('rejects nested or missing paths', () => { expect(fieldFromPath('$.a.b')).toBeNull(); expect(fieldFromPath(undefined)).toBeNull(); }); }); describe('unexplainedMessage', () => { test('names the status and its text', () => { expect(unexplainedMessage(403, 'Forbidden')).toContain('403 Forbidden'); }); test('copes with an empty status text (HTTP/2)', () => { expect(unexplainedMessage(502, '')).toContain('answered 502 without'); }); }); describe('pageStatusFor', () => { test('passes client errors through', () => { expect(pageStatusFor(401)).toBe(401); expect(pageStatusFor(404)).toBe(404); }); test('reports upstream failures as a bad gateway', () => { expect(pageStatusFor(500)).toBe(502); expect(pageStatusFor(503)).toBe(502); expect(pageStatusFor(200)).toBe(502); }); });