Files
clean-architecture-frontend…/tests/component/application-query-scope-fence.test.tsx
T
DongHyeonka 68f94f5e93 refactor(server-state): 아무도 읽지 않는 쿼리·뮤테이션 정의 칸을 선택 칸으로 둔다
문제
QueryDefinition의 owner·operationId와 BoundMutation의 owner는 필수 칸이다.
bindQuery가 돌려주는 객체에는 둘 다 없고, src 어디서도 읽지 않는다. 기능
작성자는 효과 없는 값을 매번 채우고, 매뉴얼은 그 오타를 "못 잡는 위험"으로
적어 오히려 신경 쓰게 한다(리뷰 16-06 ①②).

고른 방법
세 칸을 @deprecated 선택 칸으로 바꾼다. 참조 기능과 생성 뼈대는 더는
채우지 않는다. 요청 기록의 operation ID는 지금처럼 descriptor에서 온다.
매뉴얼 6·7단계와 patterns.md에 "적지 않는다"와, 라우트 계약 title·
navigationLabel을 무엇이 읽는지(FE-REG-ROUTE, lab 측정)를 적었다.

버린 대안
- 칸을 바로 지우기: 객체 리터럴의 초과 속성 검사로 테스트 3파일과
  다운스트림 정의가 컴파일 오류가 난다. 선택 칸으로 한 릴리스 두고 지운다.
- 쿼리 operationId를 진단 이름표로 잇기: HTTP 실행 기록이 이미 descriptor의
  operation ID를 싣는다. 같은 값을 두 번 싣는다.

위험·한계
- 라우트 계약의 title·navigationLabel(16-06 ③)은 그대로 둔다. 지우려면
  FE-REG-ROUTE 기준선 행을 catalog에서 만들고, 플랫폼 라우트와
  test-performance.ts를 함께 바꿔야 한다. 다른 작업(예제 화면 이동)과 같은
  파일이라 이번에는 문서로만 적었다.

검증
- RED: application-query-scope-fence 새 테스트가 tsconfig.test에서 owner·
  operationId 누락으로 타입 오류. 선택 칸으로 바꾼 뒤 통과.
- tests/features/reference-feature, tests/unit/create-feature.test.ts,
  scope-fence 129건 통과. check:types(app·node·test), lint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/16-structure-patterns.md 16-06
2026-11-04 23:02:44 +09:00

241 lines
6.8 KiB
TypeScript

// @vitest-environment jsdom
import { act, renderHook, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import {
type ApplicationResult,
useApplicationQuery,
} from "../../src/presentation/adapters/query/application-query.ts";
import {
createQueryInvalidationPrefix,
defineQueryNamespaceIdentity,
} from "../../src/contracts/query-keys.ts";
import {
bindQuery,
type QueryResultMeasure,
} from "../../src/contracts/server-state.ts";
import {
measureOne,
queryClient,
scopeSnapshot,
wrapper,
} from "./application-query-fixture.tsx";
describe("scope-bound query commit fence", () => {
it("binds the namespace-first V2 query key", () => {
const scope = scopeSnapshot();
const definition = {
definitionId: "resource-detail-v1",
definitionVersion: 1,
owner: "platform-test",
namespace: "resource",
namespaceVersion: 1,
operationId: "GET_RESOURCE",
profileId: "DETAIL_STANDARD" as const,
measureResult: measureOne,
execute: async () => ({ ok: true as const, value: "value" }),
};
const bound = bindQuery(definition, "resource-1", scope);
expect(bound.queryKey).toEqual([
"query",
2,
"resource",
1,
"scope-fingerprint-0001",
1,
"scope-identity-token-0001",
]);
expect(bound.queryKey.slice(0, 4)).toEqual(
createQueryInvalidationPrefix(
defineQueryNamespaceIdentity("resource", 1),
),
);
});
// 1103/16-06. `owner` and `operationId` were required and nothing read
// them: the bound query never carried them. A definition leaves them out.
it("binds the same query without the owner and operation id it never read", () => {
const scope = scopeSnapshot();
const bound = bindQuery(
{
definitionId: "resource-detail-v1",
definitionVersion: 1,
namespace: "resource",
namespaceVersion: 1,
profileId: "DETAIL_STANDARD" as const,
measureResult: measureOne,
execute: async () => ({ ok: true as const, value: "value" }),
},
"resource-1",
scope,
);
expect(bound.queryKey).toEqual([
"query",
2,
"resource",
1,
"scope-fingerprint-0001",
1,
"scope-identity-token-0001",
]);
expect(Object.keys(bound)).not.toContain("owner");
expect(Object.keys(bound)).not.toContain("operationId");
});
it("discards a successful result whose scope was fenced during execution", async () => {
const client = queryClient();
const scope = scopeSnapshot();
let complete: (value: ApplicationResult<string>) => void = () => {};
const hook = renderHook(
() =>
useApplicationQuery(
bindQuery(
{
definitionId: "fenced-detail-v1",
definitionVersion: 1,
owner: "platform-test",
namespace: "fenced",
namespaceVersion: 1,
operationId: "GET_FENCED",
profileId: "DETAIL_STANDARD",
measureResult: measureOne,
execute: () =>
new Promise<ApplicationResult<string>>((resolve) => {
complete = resolve;
}),
},
"input",
scope,
),
),
{ wrapper: wrapper(client) },
);
// §10.8: the scope goes stale after dispatch but before commit.
scope.fence();
await act(async () => {
complete({ ok: true, value: "late" });
});
await waitFor(() =>
expect(hook.result.current.state.failure?.kind).toBe(
"SCOPE_GENERATION_CHANGED",
),
);
expect(hook.result.current.data).toBeUndefined();
});
it("refuses to start when the captured scope is already stale", async () => {
const client = queryClient();
const scope = scopeSnapshot();
const execute = vi.fn(async () => ({ ok: true as const, value: "v" }));
scope.fence();
const hook = renderHook(
() =>
useApplicationQuery(
bindQuery(
{
definitionId: "stale-detail-v1",
definitionVersion: 1,
owner: "platform-test",
namespace: "stale",
namespaceVersion: 1,
operationId: "GET_STALE",
profileId: "DETAIL_STANDARD",
measureResult: measureOne,
execute,
},
"input",
scope,
),
),
{ wrapper: wrapper(client) },
);
await waitFor(() =>
expect(hook.result.current.state.failure?.kind).toBe(
"SCOPE_GENERATION_CHANGED",
),
);
expect(execute).not.toHaveBeenCalled();
});
it("rejects a result that exceeds the profile budget instead of caching it", async () => {
const client = queryClient();
const scope = scopeSnapshot();
const hook = renderHook(
() =>
useApplicationQuery(
bindQuery(
{
definitionId: "oversized-list-v1",
definitionVersion: 1,
owner: "platform-test",
namespace: "oversized",
namespaceVersion: 1,
operationId: "LIST_OVERSIZED",
profileId: "VOLATILE_STATUS",
// §10.4: VOLATILE_STATUS admits 1 item and 64KiB.
measureResult: (): QueryResultMeasure => ({
itemCount: 2,
estimatedBytes: 8,
}),
execute: async () => ({ ok: true as const, value: "value" }),
},
"input",
scope,
),
),
{ wrapper: wrapper(client) },
);
await waitFor(() =>
expect(hook.result.current.state.failure?.kind).toBe(
"RESULT_LIMIT_EXCEEDED",
),
);
expect(hook.result.current.data).toBeUndefined();
});
it("treats a throwing measurement as a measurement failure, not a cache commit", async () => {
const client = queryClient();
const scope = scopeSnapshot();
const hook = renderHook(
() =>
useApplicationQuery(
bindQuery(
{
definitionId: "unmeasurable-v1",
definitionVersion: 1,
owner: "platform-test",
namespace: "unmeasurable",
namespaceVersion: 1,
operationId: "GET_UNMEASURABLE",
profileId: "DETAIL_STANDARD",
measureResult: (): QueryResultMeasure => {
throw new Error("estimator defect");
},
execute: async () => ({ ok: true as const, value: "value" }),
},
"input",
scope,
),
),
{ wrapper: wrapper(client) },
);
await waitFor(() =>
expect(hook.result.current.state.failure?.kind).toBe(
"RESULT_LIMIT_EXCEEDED",
),
);
expect(hook.result.current.data).toBeUndefined();
});
});