import { describe, expect, it } from "vitest"; import { createFeatureHttpBinding, type InstalledHttpOperationExecutor, } from "../../../src/adapters/http/index.ts"; import { createReferenceHttpGateway, REFERENCE_HTTP_OPERATIONS, } from "../../../src/features/reference-feature/adapters/reference-http-gateway.ts"; import { CREATE_REFERENCE_RESOURCE_CONTRACT } from "../../../src/features/reference-feature/contracts/reference-feature-contract-contribution.ts"; function executorReturning( outcome: Readonly>, ): InstalledHttpOperationExecutor { const execute = (async () => outcome) as unknown as InstalledHttpOperationExecutor["execute"]; return Object.freeze({ execute }); } describe("reference HTTP business problem mapping", () => { it("maps a typed 409 reference problem to the feature-owned business code", async () => { const executor = executorReturning( Object.freeze({ kind: "PROBLEM", problem: Object.freeze({ type: "https://example.test/problems/reference-conflict", title: "Reference conflict", status: 409, code: "REFERENCE_NAME_ALREADY_EXISTS", }), metadata: Object.freeze({ status: 409 }), effect: "NOT_APPLIED", }), ); const gateway = createReferenceHttpGateway( createFeatureHttpBinding(executor, REFERENCE_HTTP_OPERATIONS), ); await expect( gateway.create({ name: "duplicate" }), ).resolves.toMatchObject({ ok: false, error: { kind: "CONFLICT", code: "REFERENCE_NAME_ALREADY_EXISTS", httpStatus: 409, }, }); }); it.each([ { status: 409, code: undefined, expectedKind: "CONFLICT", expectedCode: "REFERENCE_RESOURCE_CONFLICT", }, { status: 422, code: "REFERENCE_NAME_REJECTED", expectedKind: "VALIDATION_REJECTED", expectedCode: "REFERENCE_NAME_REJECTED", }, { status: 422, code: undefined, expectedKind: "VALIDATION_REJECTED", expectedCode: "REFERENCE_RESOURCE_REJECTED", }, { status: 400, code: "REFERENCE_BAD_REQUEST", expectedKind: "UNKNOWN_CLIENT_FAILURE", expectedCode: "CONTRACT_PROBLEM", }, ] as const)( "projects create problem status $status through the feature-owned mapper/fallback", async ({ status, code, expectedKind, expectedCode }) => { const executor = executorReturning( Object.freeze({ kind: "PROBLEM", problem: Object.freeze({ type: "https://example.test/problems/reference-" + status, title: "Reference request rejected", status, ...(code === undefined ? {} : { code }), }), metadata: Object.freeze({ status }), effect: "NOT_APPLIED", }), ); const gateway = createReferenceHttpGateway( createFeatureHttpBinding(executor, REFERENCE_HTTP_OPERATIONS), ); await expect( gateway.create({ name: "rejected" }), ).resolves.toMatchObject({ ok: false, error: { kind: expectedKind, code: expectedCode, httpStatus: status, }, }); }, ); it("keeps the platform status fallback when the operation has no problem mapper", async () => { const executor = executorReturning( Object.freeze({ kind: "PROBLEM", problem: Object.freeze({ type: "https://example.test/problems/not-found", title: "Not found", status: 404, }), metadata: Object.freeze({ status: 404 }), effect: "NOT_APPLIED", }), ); const gateway = createReferenceHttpGateway( createFeatureHttpBinding(executor, REFERENCE_HTTP_OPERATIONS), ); await expect(gateway.get("missing")).resolves.toMatchObject({ ok: false, error: { kind: "NOT_FOUND", code: "CONTRACT_PROBLEM", httpStatus: 404, }, }); }); }); describe("reference create problem effect classification (0929/03-01)", () => { const classify = (status: number, code?: string) => CREATE_REFERENCE_RESOURCE_CONTRACT.contract.commandEffect.classifyProblem({ status, problem: Object.freeze({ type: "https://api.test/problems/reference", title: "Reference problem", status, ...(code === undefined ? {} : { code }), }), }); it.each([ // The first attempt with this key still runs: the key must stay bound. { status: 409, code: "IDEMPOTENT_IN_FLIGHT", effect: "MAYBE_APPLIED" }, // An undeclared 409 may hide a running attempt: safe side. { status: 409, code: "SOMETHING_NEW", effect: "MAYBE_APPLIED" }, { status: 409, code: undefined, effect: "MAYBE_APPLIED" }, // The fixture's declared semantic conflict is the only refused 409. { status: 409, code: "CONFLICT", effect: "NOT_APPLIED" }, { status: 400, code: undefined, effect: "NOT_APPLIED" }, { status: 422, code: "VALIDATION_REJECTED", effect: "NOT_APPLIED" }, { status: 404, code: "NOT_FOUND", effect: "MAYBE_APPLIED" }, { status: 503, code: "SERVER_FAILURE", effect: "MAYBE_APPLIED" }, ] as const)( "classifies status $status code $code as $effect", ({ status, code, effect }) => { expect(classify(status, code)).toBe(effect); }, ); });