import { describe, expect, it } from "vitest"; import { readSuccessEnvelope } from "../../src/adapters/http/backend-envelope.ts"; import { composeContractContributions } from "../../src/contracts/external-contract-runtime.ts"; import { COMPOSED_CONTRACT_CONTRIBUTIONS } from "../../src/features/loaded-feature-runtimes.ts"; import { answerInstalledApiRequest } from "../e2e/support/installed-api-answers.ts"; import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts"; /** * 1103/11-01, 16-03. The accessibility e2e answers a protected page's API * requests from the installed descriptors: an empty list in the frame the * operation declares. A global bare `[]` put every Envelope feature on the * mismatch error screen and still passed. */ const [LIST, GET, CREATE] = TEST_CONTRACT_CONTRIBUTION.http; function composed(responseEnvelope?: "ENVELOPE_V1") { return composeContractContributions([ Object.freeze({ ...TEST_CONTRACT_CONTRIBUTION, http: Object.freeze( [LIST!, GET!, CREATE!].map((installed) => responseEnvelope === undefined ? installed : Object.freeze({ ...installed, contract: Object.freeze({ ...installed.contract, responseEnvelope }), }), ), ), }), ]); } describe("accessibility e2e API answers", () => { it("answers a DTO list read with a bare empty list", () => { expect(answerInstalledApiRequest(composed(), "GET", "/api/test-entities")).toEqual({ kind: "empty-list", operationId: "TEST_LIST_ENTITIES", body: [], }); }); it("answers an Envelope list read with an empty list inside the Envelope", () => { const answer = answerInstalledApiRequest( composed("ENVELOPE_V1"), "GET", "/api/test-entities", ); expect(answer.kind).toBe("empty-list"); if (answer.kind !== "empty-list") return; // What the executor reads out of the frame is what the operation accepts. // 1106/01-01. The reader also gives the scenario meta's `traceId`. expect(readSuccessEnvelope(answer.body)).toEqual({ ok: true, value: [], traceId: "0af7651916cd43dd8448eb211c80319c", }); expect(LIST!.contract.outputValidator.safeParse([]).success).toBe(true); }); it("names a single-resource read, which no empty list can answer", () => { expect( answerInstalledApiRequest(composed("ENVELOPE_V1"), "GET", "/api/test-entities/a11y-1"), ).toMatchObject({ kind: "single-resource", operationId: "TEST_GET_ENTITY" }); }); it("refuses a request no installed read declares", () => { expect( answerInstalledApiRequest(composed(), "POST", "/api/test-entities"), ).toEqual({ kind: "unanswerable", request: "POST /api/test-entities" }); expect( answerInstalledApiRequest(composed(), "GET", "/api/elsewhere"), ).toEqual({ kind: "unanswerable", request: "GET /api/elsewhere" }); }); it("refuses a parameterless read whose payload is not a list", () => { const pagedOnly = composeContractContributions([ Object.freeze({ ...TEST_CONTRACT_CONTRIBUTION, http: Object.freeze([ Object.freeze({ ...GET!, contract: Object.freeze({ ...GET!.contract, operationId: "TEST_PAGED_ENTITIES", pathTemplate: "/api/paged-entities", }), }), ]), }), ]); expect(answerInstalledApiRequest(pagedOnly, "GET", "/api/paged-entities")).toEqual({ kind: "unanswerable", request: "GET /api/paged-entities", reason: "TEST_PAGED_ENTITIES reads no single resource and rejects an empty list", }); }); it("can answer every parameterless read the installed features declare", () => { for (const { contract } of COMPOSED_CONTRACT_CONTRIBUTIONS.httpByOperationId.values()) { if (contract.method !== "GET" || contract.pathTemplate.includes("{")) continue; expect( answerInstalledApiRequest(COMPOSED_CONTRACT_CONTRIBUTIONS, "GET", contract.pathTemplate), ).toMatchObject({ kind: "empty-list", operationId: contract.operationId }); } }); });