문제: 1103/16-05 수정으로 commandMutationIdentity가 생겼지만 BoundMutation.operationId는 여전히 string이다. 정책에 operation ID를 문자열로 직접 적으면 오타가 check:types와 check:registries를 통과하고, 저장할 때마다 실행기가 보내기 전에 MISSING_IDEMPOTENCY_KEY 위반으로 거부한다. 매뉴얼 01은 이것을 "못 잡음"으로 적고 있었다. 고른 방법: BoundMutation.operationId를 brand 타입 MutationOperationId로 바꾼다. 이 값을 돌려주는 것은 commandMutationIdentity뿐이라, 문자열을 직접 적으면 check:types가 실패한다. 런타임 값은 descriptor의 문자열 그대로이고 실행기·intent factory는 바뀌지 않는다. 참조 기능과 쓰기 slice 생성기는 이미 commandMutationIdentity를 spread하므로 고칠 것이 없다. 템플릿 테스트의 손글씨 ID 20곳은 tests/helpers/mutation-operation-id.ts (commandMutationIdentity를 거치는 test helper)나 그 파일의 descriptor로 바꿨다. 매뉴얼 01·patterns 표·state 설계 예시를 맞췄다. 버린 대안: (1) 위반 이름을 INTENT_OPERATION_MISMATCH로 나누는 것. 실행 때만 드러나는 것은 같고, 공개 위반 어휘에 값이 하나 늘어 다운스트림 분기가 바뀐다. (2) 설치된 operation ID의 union 타입. 기능마다 늦게 적재되는 계약 전체를 타입으로 모을 곳이 없다. 위험·한계: 공개 타입(src/contracts/server-state.ts)이 좁아진다. 정책에 operation ID를 문자열로 적은 다운스트림 코드는 동기화 뒤 check:types가 실패한다. commandMutationIdentity에 descriptor 상수 대신 손으로 만든 객체를 넘기면 여전히 오타가 통과한다(매뉴얼 "못 잡음"). HTTP가 아닌 뮤테이션은 descriptor 모양 객체를 넘겨야 한다. 이 repo에는 그런 소비자가 없다. 검증: desktop batch에서 확인. corepack pnpm check:types:test tests/unit/command-mutation-identity.test.ts의 @ts-expect-error가 부모 커밋에서는 쓰이지 않아(TS2578) 실패하고 이 커밋에서 통과한다. node scripts/run-vitest.ts run tests/unit/command-mutation-identity.test.ts tests/component/application-mutation-admission.test.tsx tests/component/application-mutation-scope-fence.test.tsx tests/component/application-query.test.tsx tests/component/form-foundation.test.tsx BREAKING CHANGE: BoundMutation.operationId는 string이 아니라 MutationOperationId다. 뮤테이션 정책의 `operationId: "<ID>"`와 `requiresIdempotencyKey`를 `...commandMutationIdentity(<OPERATION>_CONTRACT)` 로 바꾼다(src/contracts/server-state.ts). commandMutationIdentity의 반환 타입도 operationId가 MutationOperationId다. Refs: 1103/16-05
135 lines
4.0 KiB
TypeScript
135 lines
4.0 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { renderHook, waitFor } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
type ApplicationResult,
|
|
useApplicationMutation,
|
|
} from "../../src/presentation/adapters/query/application-query.ts";
|
|
import { createFailure } from "../../src/contracts/errors.ts";
|
|
import {
|
|
queryClient,
|
|
scopeSnapshot,
|
|
wrapper,
|
|
} from "./application-query-fixture.tsx";
|
|
import { testMutationOperationId } from "../helpers/mutation-operation-id.ts";
|
|
|
|
describe("scope-bound mutation fence", () => {
|
|
it("rejects a submit whose scope is already fenced", async () => {
|
|
const client = queryClient();
|
|
const scope = scopeSnapshot();
|
|
const execute = vi.fn(async () => ({ ok: true as const, value: "v" }));
|
|
const hook = renderHook(
|
|
() =>
|
|
useApplicationMutation<string, string>({
|
|
definitionId: "fenced-mutation-v1",
|
|
definitionVersion: 1,
|
|
operationId: testMutationOperationId("CREATE_FENCED"),
|
|
requiresIdempotencyKey: false,
|
|
owner: "platform-test",
|
|
duplicatePolicy: "REJECT_WHILE_ACTIVE",
|
|
scope,
|
|
execute,
|
|
invalidate: [],
|
|
}),
|
|
{ wrapper: wrapper(client) },
|
|
);
|
|
|
|
scope.fence();
|
|
const outcome = await hook.result.current.submit("value");
|
|
expect(outcome).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
kind: "SCOPE_GENERATION_CHANGED",
|
|
effect: "NOT_STARTED",
|
|
},
|
|
});
|
|
expect(execute).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("discards a mutation result whose scope was fenced after dispatch", async () => {
|
|
const client = queryClient();
|
|
const scope = scopeSnapshot();
|
|
const execute = vi.fn(async () => {
|
|
scope.fence();
|
|
return { ok: true as const, value: "committed" };
|
|
});
|
|
const hook = renderHook(
|
|
() =>
|
|
useApplicationMutation<string, string>({
|
|
definitionId: "late-mutation-v1",
|
|
definitionVersion: 1,
|
|
operationId: testMutationOperationId("CREATE_LATE"),
|
|
requiresIdempotencyKey: false,
|
|
owner: "platform-test",
|
|
duplicatePolicy: "ALLOW_PARALLEL",
|
|
scope,
|
|
execute,
|
|
invalidate: [],
|
|
}),
|
|
{ wrapper: wrapper(client) },
|
|
);
|
|
|
|
const outcome = await hook.result.current.submit("value");
|
|
expect(outcome).toMatchObject({
|
|
ok: false,
|
|
error: { kind: "SCOPE_GENERATION_CHANGED" },
|
|
});
|
|
expect(execute).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("aborts a hung mutation when its captured scope is fenced", async () => {
|
|
const client = queryClient();
|
|
const scope = scopeSnapshot();
|
|
let observedSignal: AbortSignal | undefined;
|
|
const execute = vi.fn(
|
|
(_input: string, context: Readonly<{ signal: AbortSignal }>) =>
|
|
new Promise<ApplicationResult<string>>((resolve) => {
|
|
observedSignal = context.signal;
|
|
context.signal.addEventListener(
|
|
"abort",
|
|
() =>
|
|
resolve({
|
|
ok: false,
|
|
error: createFailure("REQUEST_ABORTED", "CREATE_HUNG", 0),
|
|
}),
|
|
{ once: true },
|
|
);
|
|
}),
|
|
);
|
|
const hook = renderHook(
|
|
() =>
|
|
useApplicationMutation<string, string>({
|
|
definitionId: "hung-mutation-v1",
|
|
definitionVersion: 1,
|
|
operationId: testMutationOperationId("CREATE_HUNG"),
|
|
requiresIdempotencyKey: false,
|
|
owner: "platform-test",
|
|
duplicatePolicy: "REJECT_WHILE_ACTIVE",
|
|
scope,
|
|
execute,
|
|
invalidate: [],
|
|
}),
|
|
{ wrapper: wrapper(client) },
|
|
);
|
|
|
|
const outcome = hook.result.current.submit("value");
|
|
await waitFor(() => expect(observedSignal).toBe(scope.signal));
|
|
|
|
scope.fence();
|
|
|
|
expect(observedSignal?.aborted).toBe(true);
|
|
await expect(outcome).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
kind: "SCOPE_GENERATION_CHANGED",
|
|
effect: "MAYBE_APPLIED",
|
|
retryable: false,
|
|
action: "contact-support",
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|