import { describe, expect, it } from 'vitest' import { MintApiError, mintApiErrorFromResponse } from '../apiErrors' describe('MintApiError', () => { it('should expose a typed server envelope', () => { const error = mintApiErrorFromResponse(404, { code: 'leaf.ARTIFACT_NOT_FOUND', message: 'Artifact is not available', status: 404, request_id: 'req-typed', details: { artifact_id: 67 }, detail: 'Artifact is not available', }) expect({ type: error.constructor, code: error.code, status: error.status, requestId: error.requestId, details: error.details, }).toEqual({ type: MintApiError, code: 'leaf.ARTIFACT_NOT_FOUND', status: 404, requestId: 'req-typed', details: { artifact_id: 67 }, }) }) it('should retain a legacy FastAPI validation detail list', () => { const detail = [{ type: 'int_parsing', loc: ['body', 'count'] }] const error = mintApiErrorFromResponse(422, { detail }) expect(error.detail).toBe(detail) expect({ message: error.message, code: error.code, details: error.details }).toEqual({ message: 'Validation error', code: 'unknown', details: null, }) }) it('preserves nested SDK errors and outer platform metadata', () => { const detail = { error: 'NOT_FOUND', message: 'Missing artifact', details: { id: 67 }, request_id: 'inner' } const body = { code: 'resource.not_found', message: 'Resource not found', request_id: 'outer', detail } const error = mintApiErrorFromResponse(404, body) expect(error).toMatchObject({ code: 'NOT_FOUND', message: 'Missing artifact', status: 404, requestId: 'outer', details: { id: 67 }, detail, body, }) expect(mintApiErrorFromResponse(404, { ...body, details: [] }).details).toEqual([]) expect(mintApiErrorFromResponse(404, detail).code).toBe('NOT_FOUND') }) it('prefers canonical envelopes over legacy nested SDK error metadata', () => { const body = { status: 426, code: 'plugin.api_version_mismatch', message: 'Plugin API version mismatch', detail: { error: 'PLATFORM_API_VERSION_MISMATCH', message: 'Legacy version mismatch' }, } expect(mintApiErrorFromResponse(426, body)).toMatchObject({ status: 426, code: body.code, message: body.message, detail: body.detail, }) expect(mintApiErrorFromResponse(502, body).status).toBe(502) expect(mintApiErrorFromResponse(426, { ...body, status: '426' }).code) .toBe('PLATFORM_API_VERSION_MISMATCH') }) it('retains platform codes and falls back to legacy detail and header request IDs', () => { expect(mintApiErrorFromResponse(403, { code: 'auth.forbidden', message: '', detail: 'Restricted', request_id: '', }, { requestId: 'header-id' })).toMatchObject({ code: 'auth.forbidden', message: 'Restricted', requestId: 'header-id', }) expect(mintApiErrorFromResponse(502, 'Bad gateway')).toMatchObject({ code: 'unknown', message: 'Bad gateway', body: 'Bad gateway', }) }) })