import { describe, it, expect } from 'vitest'; import { OrgTemplateAdminViewDto, OrgTemplateDto } from '@shared/infrastructure/api-client'; import { parseOrgTemplateAdminView, proefbriefErrorMessage, PROEFBRIEF_FAILED, } from './org-template.adapter'; const draft: OrgTemplateDto = { subOrgId: 'cibg-registers', orgName: 'CIBG', returnAddress: 'Postbus 1', footerContact: 'info@cibg.nl', footerLegal: 'onderdeel van VWS', signatureName: 'A. de Vries', signatureRole: 'Hoofd Registratie', signatureClosing: 'Met vriendelijke groet,', margins: { topMm: 25, rightMm: 20, bottomMm: 25, leftMm: 20 }, version: 3, }; const view: OrgTemplateAdminViewDto = { draft, publishedVersion: 3, unsentBriefs: 2, history: [{ version: 2, publishedAt: '2026-06-01', template: draft }], }; describe('parseOrgTemplateAdminView', () => { it('parses a well-formed admin view', () => { const r = parseOrgTemplateAdminView(view); expect(r.ok).toBe(true); if (!r.ok) return; expect(r.value.draft.orgName).toBe('CIBG'); expect(r.value.publishedVersion).toBe(3); expect(r.value.unsentBriefs).toBe(2); expect(r.value.history).toHaveLength(1); expect(r.value.history[0].version).toBe(2); }); it('rejects a missing draft', () => { const r = parseOrgTemplateAdminView({ ...view, draft: undefined }); expect(r.ok).toBe(false); }); it('rejects a missing count field', () => { const r = parseOrgTemplateAdminView({ ...view, unsentBriefs: undefined }); expect(r.ok).toBe(false); }); it('rejects a malformed history entry', () => { const r = parseOrgTemplateAdminView({ ...view, history: [ { version: 2, publishedAt: '2026-06-01', template: { ...draft, orgName: undefined } }, ], }); expect(r.ok).toBe(false); }); }); // Minimal Response stand-in — proefbriefErrorMessage only calls `.json()`. Avoids // stubbing globalThis.fetch to reach this trust boundary (TE-002). const fakeResponse = (body: unknown): Response => ({ json: () => Promise.resolve(body) }) as unknown as Response; describe('proefbriefErrorMessage (TE-002 trust boundary)', () => { it('surfaces the ProblemDetails detail when present', async () => { expect( await proefbriefErrorMessage(fakeResponse({ detail: 'Niet gevonden.', status: 404 })), ).toBe('Niet gevonden.'); }); it('falls back to PROEFBRIEF_FAILED when the body has no detail', async () => { expect(await proefbriefErrorMessage(fakeResponse({ status: 500 }))).toBe(PROEFBRIEF_FAILED); }); it('falls back to PROEFBRIEF_FAILED when the body is not JSON', async () => { const res = { json: () => Promise.reject(new Error('not json')) } as unknown as Response; expect(await proefbriefErrorMessage(res)).toBe(PROEFBRIEF_FAILED); }); });