문제: 0929/03-03의 두 가드(재전송 버튼 canResend, 같은 입력 재제출 거절)가 intent의 키 유무로만 가른다. 뮤테이션 정책은 키 여부만 실어서 훅이 NEVER와 IDEMPOTENT를 가를 수 없다. 그래서 IDEMPOTENT 쓰기(손으로 쓴 PUT·DELETE)도 결과 불명 뒤 재전송 버튼이 없고 같은 입력 저장이 UNKNOWN_EFFECT_PENDING으로 거절된다. 출구는 사실을 모르는 "변경됨으로 확인"이나 새로고침뿐이다. 매뉴얼은 IDEMPOTENT를 "여러 번 적용해도 결과가 같다"로 적고, 실행기도 보낸 뒤에 다시 보낸다. 고른 방법(리뷰 03-03 대안 (a)): commandMutationIdentity가 resendSafe (KEYED 또는 IDEMPOTENT)를 함께 준다. BoundMutation에 선택 칸 resendSafe를 더했다(추가만). 훅은 admission마다 resendable(키가 있거나 정책이 resendSafe)을 적고, 두 가드가 키 대신 그 값을 본다. 칸이 없는 손으로 쓴 정책은 지금처럼 키 유무로 간다. NEVER의 재전송 금지와 같은 입력 거절은 그대로다. 매뉴얼 01·02와 런북 6.3-4에 IDEMPOTENT 행을 적었다. 버린 대안: 문서만 고치기(회복 경로가 그대로 불편하다), IDEMPOTENT에도 키를 붙이기(백엔드가 키를 읽지 않으면 의미가 없고 실행기가 UNEXPECTED_IDEMPOTENCY_KEY로 거절한다). 위험·한계: commandMutationIdentity의 반환 객체에 칸이 하나 늘었다. 그 값을 toEqual로 비교하던 테스트는 칸을 더해야 한다(이 repo는 1곳을 고쳤다). IDEMPOTENT로 잘못 선언한 쓰기(백엔드가 실제로는 중복을 막지 못함)는 이제 결과 불명 뒤 사용자가 다시 보낼 수 있다. 선언의 정확성은 기능 작성자 몫이다(02 결정표 3절). 템플릿 안에는 IDEMPOTENT 쓰기 소비자가 없다. CHANGELOG 행은 문서 담당이 적는다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/component/application-mutation-admission.test.tsx tests/unit/command-mutation-identity.test.ts Refs: 1106/03-03
73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
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,
|
|
resendSafe: true,
|
|
});
|
|
});
|
|
|
|
// 1106/03-03. An IDEMPOTENT command carries no key, but sending it again
|
|
// gives the same result, so the coordinator may resend it after an
|
|
// unknown result. A NEVER command may not.
|
|
it.each([
|
|
["NEVER", false],
|
|
["IDEMPOTENT", true],
|
|
] as const)(
|
|
"sends no key for a %s command, resend safe: %s",
|
|
(retrySemantics, resendSafe) => {
|
|
expect(
|
|
commandMutationIdentity({
|
|
contract: { ...TEST_CREATE_HTTP_CONTRACT.contract, retrySemantics },
|
|
}),
|
|
).toEqual({
|
|
operationId: "TEST_CREATE_ENTITY",
|
|
requiresIdempotencyKey: false,
|
|
resendSafe,
|
|
});
|
|
},
|
|
);
|
|
|
|
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<BoundMutation<string, string>, "operationId"> = {
|
|
// @ts-expect-error a hand-written id is not the descriptor's MutationOperationId.
|
|
operationId: "TEST_CREAT_ENTITY",
|
|
};
|
|
const fromDescriptor: Pick<BoundMutation<string, string>, "operationId"> = {
|
|
operationId: commandMutationIdentity(TEST_CREATE_HTTP_CONTRACT).operationId,
|
|
};
|
|
|
|
expect(fromDescriptor.operationId).toBe("TEST_CREATE_ENTITY");
|
|
expect(handWritten.operationId).not.toBe(fromDescriptor.operationId);
|
|
});
|
|
});
|