Files
tech-log-frontend/tests/component/application-query-scope-fence.test.tsx
DongHyeonkaandClaude Opus 5 afde2b1da1 chore: sync the frontend template from 5434760 to ebea300
Carries 21 template commits: the shared IndexedDB connection/transaction
kernel and the four runtimes moved onto it, the adapter barrel boundary and
the architecture rule that enforces it, the feature adapter contribution
model, `feature-http-binding`, the split of several oversized test files,
and the release identity now derived from the package name.

Product identity is unchanged. The three hard-coded `tech-log-frontend/…`
verifier ids are gone because the template derives them from the package
name and this package is `tech-log-frontend`.

Two decisions worth naming. The platform HTTP executor is the template's
verbatim — it now takes the installed contract so a feature's types flow
through the binding — and TechLog's four gateways, which address operations
by id, reach it through one adapter at the composition root instead of being
rewritten here. And the three derived CI baselines were recomputed from the
merged `config/ci/gates.json` rather than taken from either side: 94 command
definitions, 106 references, 141 artifacts, gate shape `629e5898…`. The
method was checked by first reproducing the pre-merge values from the
pre-merge file.

Verified here: types, lint, architecture, adapter inventory, CI contract,
design system, diagnostics, remediation ledger, browser security and build
all pass; 1789 unit, 152 component, 415 TechLog, 102 integration, 19
reference-feature, 17 recipe and 7 contract tests pass. Five gates stay
red and every one was reproduced at b297d65 first: `check:i18n`,
`check:registries`, `test:runtime-schema`, `verify:documentation` and
`test:sample-removal`.

Full decision record, including what was deliberately not carried and the
five open follow-ups, in `docs/operations/template-sync-2026-09-21.md`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 16:31:05 +09:00

210 lines
5.9 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),
),
);
});
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();
});
});