import { http, HttpResponse } from "msw"; export type ReferenceResourceFixture = Readonly<{ id: string; name: string; createdAt?: string; }>; const DEFAULT_RESOURCE = Object.freeze({ id: "reference-1", name: "Reference", createdAt: "2026-07-26T00:00:00.000Z", }); /** * A working reference resource API. Failure scenarios are the platform's HTTP * scenario catalog (`tests/mocks/scenarios/catalog.ts`), which runs on the * test contracts; a sample test that needs a failure overrides one route. */ export function createReferenceResourceHandlers( options: Readonly<{ baseUrl?: string; resources?: ReferenceResourceFixture[]; onList?(search: string): void; onCreate?(body: Readonly>): void; }> = {}, ) { const baseUrl = options.baseUrl ?? "https://api.test"; const resources = options.resources ?? [{ ...DEFAULT_RESOURCE }]; return [ http.get(`${baseUrl}/api/reference-resources`, ({ request }) => { options.onList?.(new URL(request.url).search); return HttpResponse.json(resources); }), http.post(`${baseUrl}/api/reference-resources`, async ({ request }) => { const rawBody = await request.json(); const body = rawBody && typeof rawBody === "object" && !Array.isArray(rawBody) ? (rawBody as Readonly>) : {}; options.onCreate?.(body); const created = { id: "reference-created", name: String(body.name ?? "Created"), createdAt: "2026-07-26T00:00:00.000Z", }; resources.push(created); return HttpResponse.json(created); }), http.get(`${baseUrl}/api/reference-resources/:resourceId`, ({ params }) => HttpResponse.json( resources.find((entry) => entry.id === params.resourceId) ?? DEFAULT_RESOURCE, ), ), ] as const; }