import { describe, expect, it } from "vitest"; import { commandMutationIdentity, type BoundMutation, } from "../../src/contracts/server-state.ts"; import { TEST_CREATE_HTTP_CONTRACT, TEST_LIST_HTTP_CONTRACT, } from "../helpers/external-contract-fixture.ts"; /** * 1103/16-05, 1101/03-03. A mutation policy used to restate its command's * operation id and key requirement as free values. A misspelt id passed the * type check and the registries, and the executor refused every save before * sending, as a missing idempotency key. The policy now takes both from the * HTTP descriptor. */ describe("commandMutationIdentity", () => { it("takes the operation id and the key requirement from a KEYED descriptor", () => { expect(commandMutationIdentity(TEST_CREATE_HTTP_CONTRACT)).toEqual({ operationId: "TEST_CREATE_ENTITY", requiresIdempotencyKey: true, }); }); it.each(["NEVER", "IDEMPOTENT"] as const)( "sends no key for a %s command", (retrySemantics) => { expect( commandMutationIdentity({ contract: { ...TEST_CREATE_HTTP_CONTRACT.contract, retrySemantics }, }), ).toEqual({ operationId: "TEST_CREATE_ENTITY", requiresIdempotencyKey: false }); }, ); it("refuses a read descriptor", () => { expect(() => commandMutationIdentity(TEST_LIST_HTTP_CONTRACT)).toThrow( "commandMutationIdentity: TEST_LIST_ENTITIES is a GET; a mutation binds a write operation", ); }); // 1103/16-05. The rest of the finding: a policy that wrote its operation // id by hand still passed the type check. `BoundMutation.operationId` is a // `MutationOperationId`, which only commandMutationIdentity returns, so the // misspelt id below no longer compiles (check:types:test fails if the // directive is unused). At run time the value is the descriptor's string. it("takes no hand-written operation id", () => { const handWritten: Pick, "operationId"> = { // @ts-expect-error a hand-written id is not the descriptor's MutationOperationId. operationId: "TEST_CREAT_ENTITY", }; const fromDescriptor: Pick, "operationId"> = { operationId: commandMutationIdentity(TEST_CREATE_HTTP_CONTRACT).operationId, }; expect(fromDescriptor.operationId).toBe("TEST_CREATE_ENTITY"); expect(handWritten.operationId).not.toBe(fromDescriptor.operationId); }); });