import assert from "node:assert/strict"; import { test } from "vitest"; import { TECH_LOG_MANAGEMENT_CONTRIBUTION } from "../../../src/features/tech-log/contracts/tech-log-management-contract-contribution.ts"; import type { DeletableDocumentKind } from "../../../src/features/tech-log/application/ports/management-gateway.ts"; /** * 종류마다 지우는 주소가 다르다. 이 대응이 어긋나면 지우려던 것과 다른 종류의 API 를 부른다. * *

실제로 그렇게 났다: 개념을 더할 때 관리 계약에 삭제 경로를 만들지 않았고, 게이트웨이가 * 종류를 삼항으로 이어 붙여 마지막 else 가 남은 것을 다 받아 갔다. 부르는 화면은 종류를 * `as "CASE" | "REFERENCE" | "QUESTION"` 으로 좁혀 넘겼으므로 타입도 잡지 못했다. 개념을 * 지우려 하면 질문 삭제가 불렸고 「질문을 찾을 수 없습니다」가 났다. */ const EXPECTED: Record = { CASE: { operationId: "deleteCaseDraft", path: "/api/v1/studio/cases/{id}" }, REFERENCE: { operationId: "deleteReferenceDraft", path: "/api/v1/studio/references/{id}" }, QUESTION: { operationId: "deleteQuestion", path: "/api/v1/studio/questions/{id}" }, CONCEPT: { operationId: "deleteConceptDraft", path: "/api/v1/studio/concepts/{id}" }, SETUP: { operationId: "deleteSetupDraft", path: "/api/v1/studio/setups/{id}" }, }; test("지울 수 있는 종류마다 자기 주소가 있고 서로 겹치지 않는다", () => { const byId = new Map( TECH_LOG_MANAGEMENT_CONTRIBUTION.http.map((entry) => [entry.contract.operationId, entry]), ); const paths = new Set(); for (const [kind, expected] of Object.entries(EXPECTED)) { const installed = byId.get(expected.operationId); assert.ok(installed, `${kind} 를 지우는 ${expected.operationId} 가 계약에 있어야 한다`); assert.equal(installed.contract.method, "DELETE"); assert.equal( installed.contract.pathTemplate, expected.path, `${kind} 는 자기 주소로 나가야 한다`, ); assert.ok(!paths.has(expected.path), `${kind} 가 다른 종류와 같은 주소를 쓴다`); paths.add(expected.path); } }); /** * 앞의 검사는 계약에 주소가 있는지만 본다. 그것만으로는 부족했다 — 계약에 개념 삭제를 넣고도 * 게이트웨이는 여전히 삼항의 마지막 else 로 질문 삭제를 불렀고, 배포된 뒤에야 서버 로그의 * `DELETE /api/v1/studio/questions/{id} 404` 로 드러났다. * *

타입이 잡아 주지 못한 이유: 메서드 매개변수는 TypeScript 에서 bivariant 라, 구현이 종류를 * 좁게 적어도 넓은 포트 시그니처를 만족한 것으로 통과한다. * *

그래서 여기서는 게이트웨이를 실제로 불러 어떤 연산이 실행되는지 본다. */ test("게이트웨이가 종류마다 그 종류의 연산을 부른다", async () => { const { createHttpManagementGateway } = await import( "../../../src/features/tech-log/adapters/http/http-management-gateway.ts" ); const called: Array<{ operationId: string; idempotencyKey: string | undefined; }> = []; const gateway = createHttpManagementGateway({ http: { execute: ( operationId: string, _input: unknown, context: Readonly<{ intent?: Readonly<{ idempotencyKey?: string }>; }>, ) => { called.push({ operationId, idempotencyKey: context.intent?.idempotencyKey, }); return Promise.resolve({ ok: true as const, value: undefined }); }, }, } as never); for (const kind of ["CASE", "REFERENCE", "QUESTION", "CONCEPT"] as const) { await gateway.deleteDocument( kind, "11111111-1111-4111-8111-111111111111", 1, { idempotencyKey: `delete-${kind.toLowerCase()}-test` }, ); } assert.deepEqual(called.map((entry) => entry.operationId), [ "deleteCaseDraft", "deleteReferenceDraft", "deleteQuestion", "deleteConceptDraft", ]); assert.deepEqual( called.map((entry) => entry.idempotencyKey), [ "delete-case-test", "delete-reference-test", "delete-question-test", "delete-concept-test", ], ); });