import { describe, it, expect } from 'vitest'; import { uploadOutcome } from './upload.adapter'; /** Matches the un-exported UPLOAD_FAILED fallback text in upload.adapter.ts. */ const UPLOAD_FAILED = 'Uploaden is niet gelukt. Probeer het opnieuw.'; describe('uploadOutcome', () => { it('resolves a 2xx response with a valid JSON body to the document id', () => { const outcome = uploadOutcome(200, JSON.stringify({ documentId: 'doc-1' })); expect(outcome).toEqual({ ok: true, value: { documentId: 'doc-1' } }); }); it('falls back to the generic error when a 2xx body is not valid JSON', () => { const outcome = uploadOutcome(201, 'not json'); expect(outcome).toEqual({ ok: false, error: UPLOAD_FAILED }); }); it('maps a non-2xx ProblemDetails body to its detail', () => { const outcome = uploadOutcome( 409, JSON.stringify({ detail: 'Document is al aan een aanvraag gekoppeld.', status: 409 }), ); expect(outcome).toEqual({ ok: false, error: 'Document is al aan een aanvraag gekoppeld.' }); }); it('falls back to the generic error for a non-2xx body without a ProblemDetails detail', () => { const outcome = uploadOutcome(500, 'Internal Server Error'); expect(outcome).toEqual({ ok: false, error: UPLOAD_FAILED }); }); it('treats status 200-299 as success and everything else as failure', () => { expect(uploadOutcome(299, JSON.stringify({ documentId: 'd' })).ok).toBe(true); expect(uploadOutcome(300, JSON.stringify({ detail: 'x' })).ok).toBe(false); }); });