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", () => { // 백엔드 1101/02-01. The resend waits on this value. it("carries the Retry-After of an in-flight 409 onto the unknown failure", async () => { const executor = executorReturning( Object.freeze({ kind: "PROBLEM", problem: Object.freeze({ type: "https://api.test/problems/idempotent_in_flight", title: "IDEMPOTENT_IN_FLIGHT", status: 409, code: "IDEMPOTENT_IN_FLIGHT", }), metadata: Object.freeze({ status: 409, retryAfterMs: 2_000 }), effect: "MAYBE_APPLIED", }), ); const gateway = createReferenceHttpGateway( createFeatureHttpBinding(executor, REFERENCE_HTTP_OPERATIONS), ); await expect(gateway.create({ name: "n" })).resolves.toMatchObject({ ok: false, error: { code: "IDEMPOTENT_IN_FLIGHT", effect: "MAYBE_APPLIED", retryAfterMs: 2_000, }, }); }); 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, }, }); }, ); // 백엔드 1101/02-01. The paired backend refuses both before it runs the // command (its docs/web/http-contract.md §2). The failure keeps the cause // code and does not invite a resend. it.each([ { status: 422, code: "IDEMPOTENT_REQUEST_MISMATCH", expectedKind: "VALIDATION_REJECTED", }, { // The executor checked the key before sending: the two rules disagree. status: 400, code: "IDEMPOTENCY_KEY_INVALID", expectedKind: "UNKNOWN_CLIENT_FAILURE", }, ] as const)( "keeps the paired backend's $status $code as a not-applied failure", async ({ status, code, expectedKind }) => { const executor = executorReturning( Object.freeze({ kind: "PROBLEM", problem: Object.freeze({ type: "https://api.test/problems/" + code.toLowerCase(), title: code, status, code, }), metadata: Object.freeze({ status }), effect: "NOT_APPLIED", }), ); const gateway = createReferenceHttpGateway( createFeatureHttpBinding(executor, REFERENCE_HTTP_OPERATIONS), ); await expect(gateway.create({ name: "n" })).resolves.toMatchObject({ ok: false, error: { kind: expectedKind, code, httpStatus: status, effect: "NOT_APPLIED", retryable: false, }, }); }, ); 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" }, // 백엔드 1101/02-01. Refused before the command ran: this request was // not applied, so the next save may mint a new key. { status: 422, code: "IDEMPOTENT_REQUEST_MISMATCH", effect: "NOT_APPLIED" }, { status: 400, code: "IDEMPOTENCY_KEY_INVALID", 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); }, ); });