Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731// utils/response/errors_test.ts/** * Comprehensive unit tests for error response utilities * * Test Structure (Effect-TS inspired - precise behavioral contracts): * * 1. PROBLEM DETAILS CONSTRUCTION * - baseProblem() - RFC 7807 compliance * - Return tuple structure * * 2. CONVENIENCE FACTORIES * - All status-specific factories (badRequest, notFound, etc.) * - Factories with required extensions (methodNotAllowed, rateLimitExceeded) * * 3. MULTI-ERROR RESPONSES * - validationFailed() - 422 with field errors * - errs() - generic multi-error builder * * 4. TYPE GUARDS AND HELPERS * - isErrorResponse() - discriminates error from success * - exception() - HTTPException wrapper * - extraProblemHeaders() - additional headers for certain statuses */
import { describe, it } from '@std/testing/bdd'import { expect } from '@std/expect'
import { baseProblem, badRequest, unauthorized, forbidden, notFound, methodNotAllowed, notAcceptable, requestTimeout, conflict, gone, preconditionFailed, payloadTooLarge, uriTooLong, unsupportedMediaType, rangeNotSatisfiable, unprocessableEntity, preconditionRequired, rateLimitExceeded, requestHeaderFieldsTooLarge, unavailableForLegalReasons, internalServerError, notImplemented, badGateway, serviceUnavailable, gatewayTimeout, err, errs, validationFailed, isErrorResponse, exception, extraProblemHeaders, ERROR_TYPES, ERROR_DOCS, STATUS_TITLES, titleFor,} from './errors.ts'
import { ok, paginate } from './success.ts'import type { ErrorResult, ValidationErrorDetail } from './schemas.ts'
// ============================================================================// TEST FIXTURES// ============================================================================
const TEST_INSTANCE = '/api/test/123'
function makeValidationErrors(count: number): ValidationErrorDetail[] { return Array.from({ length: count }, (_, i) => ({ field: `field${i}`, message: `Error message ${i}` }))}
// ============================================================================// 1. PROBLEM DETAILS CONSTRUCTION// ============================================================================
describe('baseProblem', () => { describe('RFC 7807 compliance', () => { it('includes type URI', () => { const [body] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error')
expect(body.type).toBe(ERROR_TYPES.BAD_REQUEST) })
it('includes title from STATUS_TITLES', () => { const [body] = baseProblem(404, ERROR_TYPES.NOT_FOUND, TEST_INSTANCE, 'Test error')
expect(body.title).toBe('Not Found') })
it('includes status code', () => { const [body] = baseProblem(500, ERROR_TYPES.INTERNAL_SERVER_ERROR, TEST_INSTANCE, 'Test error')
expect(body.status).toBe(500) })
it('includes detail message', () => { const [body] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Custom detail message')
expect(body.detail).toBe('Custom detail message') })
it('includes instance (request path)', () => { const [body] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, '/api/users/456', 'Test error')
expect(body.instance).toBe('/api/users/456') })
it('includes timestamp', () => { const before = new Date() const [body] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error') const after = new Date()
expect(body.timestamp).toBeInstanceOf(Date) expect(body.timestamp.getTime()).toBeGreaterThanOrEqual(before.getTime()) expect(body.timestamp.getTime()).toBeLessThanOrEqual(after.getTime()) })
it('includes docs URL when provided', () => { const [body] = baseProblem( 400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error', ERROR_DOCS.BAD_REQUEST )
expect(body.docs).toBe(ERROR_DOCS.BAD_REQUEST) })
it('omits docs when not provided', () => { const [body] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error')
expect(body.docs).toBeUndefined() })
it('includes extensions when provided', () => { const [body] = baseProblem( 400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error', undefined, { customField: 'custom value', count: 42 } )
expect((body as Record<string, unknown>).customField).toBe('custom value') expect((body as Record<string, unknown>).count).toBe(42) }) })
describe('return tuple structure', () => { it('returns [body, status, headers] tuple', () => { const result = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error')
expect(result).toHaveLength(3) expect(typeof result[0]).toBe('object') expect(result[1]).toBe(400) expect(typeof result[2]).toBe('object') })
it('sets Content-Type to application/problem+json', () => { const [, , headers] = baseProblem(400, ERROR_TYPES.BAD_REQUEST, TEST_INSTANCE, 'Test error')
expect(headers['Content-Type']).toBe('application/problem+json') }) })})
describe('titleFor', () => { it('returns title for known status', () => { expect(titleFor(200)).toBe('OK') expect(titleFor(404)).toBe('Not Found') expect(titleFor(500)).toBe('Internal Server Error') })
it('returns "Error" for unknown status', () => { expect(titleFor(999)).toBe('Error') })})
// ============================================================================// 2. CONVENIENCE FACTORIES// ============================================================================
describe('convenience error factories', () => { // All factories follow the same pattern as baseProblem() // We test the pattern once thoroughly, then spot-check representatives
describe('factory pattern verification', () => { // Test that all factories return the expected tuple structure const allFactories = [ { fn: badRequest, status: 400 }, { fn: unauthorized, status: 401 }, { fn: forbidden, status: 403 }, { fn: notFound, status: 404 }, { fn: notAcceptable, status: 406 }, { fn: requestTimeout, status: 408 }, { fn: conflict, status: 409 }, { fn: gone, status: 410 }, { fn: preconditionFailed, status: 412 }, { fn: uriTooLong, status: 414 }, { fn: rangeNotSatisfiable, status: 416 }, { fn: unprocessableEntity, status: 422 }, { fn: preconditionRequired, status: 428 }, { fn: requestHeaderFieldsTooLarge, status: 431 }, { fn: unavailableForLegalReasons, status: 451 }, { fn: internalServerError, status: 500 }, { fn: notImplemented, status: 501 }, { fn: badGateway, status: 502 }, { fn: gatewayTimeout, status: 504 }, ] as const
it('all factories return correct status codes', () => { allFactories.forEach(({ fn, status }) => { const [, resultStatus] = fn(TEST_INSTANCE, 'Test error') expect(resultStatus).toBe(status) }) })
it('all factories return [body, status, headers] tuple', () => { allFactories.forEach(({ fn }) => { const result = fn(TEST_INSTANCE, 'Test error') expect(result).toHaveLength(3) expect(result[2]['Content-Type']).toBe('application/problem+json') }) })
it('all factories include RFC 7807 required fields', () => { allFactories.forEach(({ fn }) => { const [body] = fn(TEST_INSTANCE, 'Test error') expect(body.type).toBeDefined() expect(body.title).toBeDefined() expect(body.status).toBeDefined() expect(body.detail).toBeDefined() expect(body.instance).toBeDefined() }) }) })
describe('spot-check: client errors (4xx)', () => { it('badRequest (400) - common validation error', () => { const [body, status] = badRequest(TEST_INSTANCE, 'Invalid input') expect(status).toBe(400) expect(body.type).toBe(ERROR_TYPES.BAD_REQUEST) expect(body.title).toBe('Bad Request') expect(body.detail).toBe('Invalid input') })
it('notFound (404) - resource not found', () => { const [body, status] = notFound('/api/users/999', 'User not found') expect(status).toBe(404) expect(body.instance).toBe('/api/users/999') })
it('unprocessableEntity (422) - semantic validation', () => { const [body, status] = unprocessableEntity(TEST_INSTANCE, 'Email already exists') expect(status).toBe(422) expect(body.detail).toBe('Email already exists') }) })
describe('spot-check: server errors (5xx)', () => { it('internalServerError (500) - unexpected error', () => { const [body, status] = internalServerError(TEST_INSTANCE, 'Unexpected error') expect(status).toBe(500) expect(body.type).toBe(ERROR_TYPES.INTERNAL_SERVER_ERROR) }) })
describe('extensions support', () => { it('all factories accept extensions', () => { const [body] = badRequest(TEST_INSTANCE, 'Test', { customField: 'value' }) expect((body as Record<string, unknown>).customField).toBe('value') }) })})
describe('factories with required extensions', () => { describe('methodNotAllowed', () => { it('returns status 405', () => { const [, status] = methodNotAllowed(TEST_INSTANCE, ['GET', 'POST']) expect(status).toBe(405) })
it('includes allowed methods array in extensions', () => { const [body] = methodNotAllowed(TEST_INSTANCE, ['GET', 'POST', 'PUT']) expect((body as Record<string, unknown>).allowed).toEqual(['GET', 'POST', 'PUT']) })
it('accepts custom detail message', () => { const [body] = methodNotAllowed(TEST_INSTANCE, ['GET'], 'DELETE not allowed here') expect(body.detail).toBe('DELETE not allowed here') }) })
describe('payloadTooLarge', () => { it('returns status 413', () => { const [, status] = payloadTooLarge(TEST_INSTANCE, 1024 * 1024) expect(status).toBe(413) })
it('includes limitBytes in extensions', () => { const [body] = payloadTooLarge(TEST_INSTANCE, 5 * 1024 * 1024) expect((body as Record<string, unknown>).limitBytes).toBe(5 * 1024 * 1024) })
it('works without limitBytes', () => { const [body, status] = payloadTooLarge(TEST_INSTANCE) expect(status).toBe(413) expect((body as Record<string, unknown>).limitBytes).toBeUndefined() }) })
describe('unsupportedMediaType', () => { it('returns status 415', () => { const [, status] = unsupportedMediaType(TEST_INSTANCE, ['application/json']) expect(status).toBe(415) })
it('includes supported media types in extensions', () => { const [body] = unsupportedMediaType(TEST_INSTANCE, ['application/json', 'application/xml']) expect((body as Record<string, unknown>).supported).toEqual(['application/json', 'application/xml']) }) })
describe('rateLimitExceeded', () => { it('returns status 429', () => { const [, status] = rateLimitExceeded(TEST_INSTANCE, 60) expect(status).toBe(429) })
it('includes retryAfter in extensions', () => { const [body] = rateLimitExceeded(TEST_INSTANCE, 120) expect((body as Record<string, unknown>).retryAfter).toBe(120) })
it('includes retryAfter in detail message', () => { const [body] = rateLimitExceeded(TEST_INSTANCE, 60) expect(body.detail).toContain('60') }) })
describe('serviceUnavailable', () => { it('returns status 503', () => { const [, status] = serviceUnavailable(TEST_INSTANCE, 'database') expect(status).toBe(503) })
it('includes service name in extensions', () => { const [body] = serviceUnavailable(TEST_INSTANCE, 'redis-cache') expect((body as Record<string, unknown>).service).toBe('redis-cache') })
it('includes service in default detail message', () => { const [body] = serviceUnavailable(TEST_INSTANCE, 'payment-gateway') expect(body.detail).toContain('payment-gateway') })
it('accepts custom detail message', () => { const [body] = serviceUnavailable(TEST_INSTANCE, 'db', 'Custom unavailable message') expect(body.detail).toBe('Custom unavailable message') }) })})
describe('err (generic factory)', () => { it('delegates to appropriate factory for known status', () => { const [body, status] = err(404, TEST_INSTANCE, 'Resource not found')
expect(status).toBe(404) expect(body.type).toBe(ERROR_TYPES.NOT_FOUND) expect(body.title).toBe('Not Found') })
it('handles 405 with empty allowed array', () => { const [body] = err(405, TEST_INSTANCE, 'Method not allowed') expect((body as Record<string, unknown>).allowed).toEqual([]) })
it('handles 429 with retryAfter from extensions', () => { const [body] = err(429, TEST_INSTANCE, 'Rate limited', { retryAfter: 30 }) expect((body as Record<string, unknown>).retryAfter).toBe(30) })
it('handles 429 with default retryAfter when not in extensions', () => { const [body] = err(429, TEST_INSTANCE, 'Rate limited') expect((body as Record<string, unknown>).retryAfter).toBe(60) })
it('handles 503 with service from extensions', () => { const [body] = err(503, TEST_INSTANCE, 'Service down', { service: 'api' }) expect((body as Record<string, unknown>).service).toBe('api') })
it('handles unknown status with generic fallback', () => { const [body, status] = err(418 as any, TEST_INSTANCE, "I'm a teapot")
expect(status).toBe(418) expect(body.detail).toBe("I'm a teapot") })})
// ============================================================================// 3. MULTI-ERROR RESPONSES// ============================================================================
describe('validationFailed', () => { it('returns 422 status', () => { const [, status] = validationFailed(TEST_INSTANCE, makeValidationErrors(1))
expect(status).toBe(422) })
it('includes errors array', () => { const errors = makeValidationErrors(3) const [body] = validationFailed(TEST_INSTANCE, errors)
expect(body.errors).toEqual(errors) })
it('formats detail as "1 validation error occurred" for single error', () => { const [body] = validationFailed(TEST_INSTANCE, makeValidationErrors(1))
expect(body.detail).toBe('1 validation error occurred') })
it('formats detail as "X validation errors occurred" for multiple errors', () => { const [body] = validationFailed(TEST_INSTANCE, makeValidationErrors(5))
expect(body.detail).toBe('5 validation errors occurred') })
it('accepts custom detail message', () => { const [body] = validationFailed(TEST_INSTANCE, makeValidationErrors(1), 'Custom validation message')
expect(body.detail).toBe('Custom validation message') })
it('uses VALIDATION_ERROR type', () => { const [body] = validationFailed(TEST_INSTANCE, makeValidationErrors(1))
expect(body.type).toBe(ERROR_TYPES.VALIDATION_ERROR) })
it('handles empty validation errors array', () => { // Edge case: what happens with zero errors? // This documents actual behavior - may want to reject empty arrays const [body] = validationFailed(TEST_INSTANCE, [])
expect(body.errors).toEqual([]) expect(body.detail).toBe('0 validation errors occurred') })})
describe('errs', () => { it('accepts custom status and type', () => { const errors = makeValidationErrors(2) const [body, status] = errs( ERROR_TYPES.BAD_REQUEST, 'Bad Request', 400, TEST_INSTANCE, errors )
expect(status).toBe(400) expect(body.type).toBe(ERROR_TYPES.BAD_REQUEST) })
it('includes all validation error details', () => { const errors = [ { field: 'email', message: 'Invalid email format' }, { field: 'password', message: 'Too short' }, { field: 'age', message: 'Must be positive' } ]
const [body] = errs( ERROR_TYPES.UNPROCESSABLE_ENTITY, 'Unprocessable Entity', 422, TEST_INSTANCE, errors )
expect(body.errors).toEqual(errors) })
it('uses default detail based on error count', () => { const [body] = errs( ERROR_TYPES.VALIDATION_ERROR, 'Validation Error', 422, TEST_INSTANCE, makeValidationErrors(3) )
expect(body.detail).toBe('3 errors occurred') })
it('accepts custom detail', () => { const [body] = errs( ERROR_TYPES.VALIDATION_ERROR, 'Validation Error', 422, TEST_INSTANCE, makeValidationErrors(1), 'Custom detail' )
expect(body.detail).toBe('Custom detail') })
it('includes docs when provided', () => { const [body] = errs( ERROR_TYPES.VALIDATION_ERROR, 'Validation Error', 422, TEST_INSTANCE, makeValidationErrors(1), undefined, ERROR_DOCS.VALIDATION_ERROR )
expect(body.docs).toBe(ERROR_DOCS.VALIDATION_ERROR) })})
// ============================================================================// 4. TYPE GUARDS AND HELPERS// ============================================================================
describe('isErrorResponse', () => { it('returns true for error tuples (problem+json)', () => { const errorResult = badRequest(TEST_INSTANCE, 'Test error')
expect(isErrorResponse(errorResult)).toBe(true) })
it('returns false for success tuples (application/json)', () => { const successResult = ok({ id: '123' })
expect(isErrorResponse(successResult)).toBe(false) })
it('returns false for pagination results', () => { const paginationResult = paginate('/api/items', [{ id: '1' }], { hasMore: false, limit: 20, count: 1 })
expect(isErrorResponse(paginationResult)).toBe(false) })
it('works with validationFailed results', () => { const validationResult = validationFailed(TEST_INSTANCE, makeValidationErrors(1))
expect(isErrorResponse(validationResult)).toBe(true) })
it('correctly narrows type', () => { const result: ErrorResult | ReturnType<typeof ok> = badRequest(TEST_INSTANCE, 'error')
if (isErrorResponse(result)) { // TypeScript should know this is ErrorResult const [body, status] = result expect(body.type).toBeDefined() expect(status).toBe(400) } })})
describe('exception', () => { it('creates HTTPException with problem details', () => { const errorResult = notFound(TEST_INSTANCE, 'Resource not found') const httpException = exception(errorResult)
expect(httpException).toBeInstanceOf(Error) expect(httpException.status).toBe(404) })
it('includes response with correct body', async () => { const errorResult = badRequest(TEST_INSTANCE, 'Invalid input') const httpException = exception(errorResult)
// Explicitly verify response exists before accessing expect(httpException.res).toBeDefined() if (!httpException.res) { throw new Error('Expected HTTPException to have a response') }
const body = await httpException.res.json() expect(body.type).toBe(ERROR_TYPES.BAD_REQUEST) expect(body.detail).toBe('Invalid input') })
it('includes problem+json content type', () => { const errorResult = forbidden(TEST_INSTANCE, 'Access denied') const httpException = exception(errorResult)
expect(httpException.res).toBeDefined() expect(httpException.res?.headers.get('Content-Type')).toBe('application/problem+json') })})
describe('extraProblemHeaders', () => { it('returns Retry-After for 429 with retryAfter extension', () => { const headers = extraProblemHeaders(429, { retryAfter: 60 })
expect(headers['Retry-After']).toBe('60') })
it('returns Retry-After for 503 with retryAfter extension', () => { const headers = extraProblemHeaders(503, { retryAfter: 120 })
expect(headers['Retry-After']).toBe('120') })
it('returns empty object for 429 without retryAfter', () => { const headers = extraProblemHeaders(429, {})
expect(headers).toEqual({}) })
it('returns empty object for other status codes', () => { const headers = extraProblemHeaders(400, { retryAfter: 60 })
expect(headers).toEqual({}) })
it('returns empty object when no extensions', () => { const headers = extraProblemHeaders(500)
expect(headers).toEqual({}) })})
// ============================================================================// EDGE CASES// ============================================================================
describe('edge cases', () => { describe('empty and special values', () => { it('handles empty detail message', () => { const [body] = badRequest(TEST_INSTANCE, '')
expect(body.detail).toBe('') })
it('handles empty instance', () => { const [body] = badRequest('', 'Test error')
expect(body.instance).toBe('') })
it('handles very long detail message', () => { const longMessage = 'x'.repeat(10000) const [body] = badRequest(TEST_INSTANCE, longMessage)
expect(body.detail).toBe(longMessage) }) })
describe('special characters', () => { it('handles unicode in detail', () => { const [body] = badRequest(TEST_INSTANCE, '日本語エラーメッセージ 🚫')
expect(body.detail).toBe('日本語エラーメッセージ 🚫') })
it('handles special characters in instance', () => { const [body] = badRequest('/api/users?name=John%20Doe&filter[status]=active', 'Test')
expect(body.instance).toBe('/api/users?name=John%20Doe&filter[status]=active') }) })
describe('extension edge cases', () => { it('handles null in extensions', () => { const [body] = badRequest(TEST_INSTANCE, 'Test', { nullField: null })
expect((body as Record<string, unknown>).nullField).toBeNull() })
it('handles nested objects in extensions', () => { const [body] = badRequest(TEST_INSTANCE, 'Test', { nested: { deep: { value: 42 } } })
expect((body as Record<string, unknown>).nested).toEqual({ deep: { value: 42 } }) })
it('handles arrays in extensions', () => { const [body] = badRequest(TEST_INSTANCE, 'Test', { items: [1, 2, 3] })
expect((body as Record<string, unknown>).items).toEqual([1, 2, 3]) }) })
describe('validation errors edge cases', () => { it('handles empty errors array', () => { const [body] = validationFailed(TEST_INSTANCE, [])
expect(body.errors).toEqual([]) expect(body.detail).toBe('0 validation errors occurred') })
it('handles many errors', () => { const errors = makeValidationErrors(100) const [body] = validationFailed(TEST_INSTANCE, errors)
expect(body.errors).toHaveLength(100) }) })})