import {describe, expect, it} from 'vitest' import {ProtocolError, isProtocolError, normalizeProtocolError} from '#lib/errors' describe('isProtocolError', () => { it('identifies protocol errors', () => { const protocolError = new ProtocolError('test', 400) const regularError = new Error('test') expect(isProtocolError(protocolError, 400)).toBe(true) expect(isProtocolError(protocolError)).toBe(true) expect(isProtocolError(regularError, 400)).toBe(false) expect(isProtocolError(regularError)).toBe(false) }) it('checks specific status codes', () => { const error400 = new ProtocolError('bad request', 400) const error500 = new ProtocolError('server error', 500) expect(isProtocolError(error400, 400)).toBe(true) expect(isProtocolError(error400, 500)).toBe(false) expect(isProtocolError(error500, 500)).toBe(true) }) }) describe('normalizeProtocolError', () => { it('passes through existing protocol errors', () => { const original = new ProtocolError('test', 400) const normalized = normalizeProtocolError(original) expect(normalized).toBe(original) }) it('converts regular errors to protocol errors', () => { const error = new Error('test error') const normalized = normalizeProtocolError(error) expect(normalized).toBeInstanceOf(ProtocolError) expect(normalized.status).toBe(500) expect(normalized.cause).toBe(error) }) it('handles timeout errors', () => { const timeoutError = new DOMException('timeout', 'TimeoutError') const normalized = normalizeProtocolError(timeoutError) expect(normalized.status).toBe(408) expect(normalized.message).toContain('operation timed out') }) it('handles abort errors', () => { const abortError = new DOMException('aborted', 'AbortError') const normalized = normalizeProtocolError(abortError) expect(normalized.status).toBe(499) expect(normalized.message).toContain('operation was aborted') }) })