refactor: 리펙토링
This commit is contained in:
@@ -20,10 +20,44 @@ import {
|
||||
type QueryInvalidationCoordinator,
|
||||
} from "../../src/contracts/query-invalidation.ts";
|
||||
import { createFailure } from "../../src/contracts/errors.ts";
|
||||
import { createRuntimeIdentityRegistry } from "../../src/contracts/query-keys.ts";
|
||||
import {
|
||||
bindQuery,
|
||||
type QueryResultMeasure,
|
||||
} from "../../src/contracts/server-state.ts";
|
||||
import type { CacheScopeSnapshot } from "../../src/contracts/server-state-scope.ts";
|
||||
|
||||
const RESOURCE_INVALIDATION_TOPIC =
|
||||
defineQueryInvalidationTopic("resource");
|
||||
|
||||
/**
|
||||
* §24.12: the scope-bound commit fence is common runtime, so it is verified
|
||||
* here with a local scope fixture rather than through the removable sample
|
||||
* feature.
|
||||
*/
|
||||
function scopeSnapshot(): CacheScopeSnapshot & { fence(): void } {
|
||||
let current = true;
|
||||
const lifetime = new AbortController();
|
||||
const identities = createRuntimeIdentityRegistry({
|
||||
tokenFactory: () => "scope-identity-token-0001",
|
||||
});
|
||||
return {
|
||||
generation: 1,
|
||||
fingerprint: "scope-fingerprint-0001",
|
||||
identities,
|
||||
signal: lifetime.signal,
|
||||
isCurrent: () => current,
|
||||
fence() {
|
||||
current = false;
|
||||
lifetime.abort();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function measureOne(): QueryResultMeasure {
|
||||
return { itemCount: 1, estimatedBytes: 8 };
|
||||
}
|
||||
|
||||
function queryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
@@ -181,7 +215,303 @@ describe("application query inbound bridge", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("scope-bound query commit fence", () => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
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: "CREATE_FENCED",
|
||||
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" },
|
||||
});
|
||||
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: "CREATE_LATE",
|
||||
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: "CREATE_HUNG",
|
||||
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",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("application mutation inbound bridge", () => {
|
||||
it("rejects a duplicate submit by default while one is active", async () => {
|
||||
const client = queryClient();
|
||||
let complete: (value: ApplicationResult<string>) => void = () => {};
|
||||
const execute = vi.fn(
|
||||
() =>
|
||||
new Promise<ApplicationResult<string>>((resolve) => {
|
||||
complete = resolve;
|
||||
}),
|
||||
);
|
||||
const hook = renderHook(
|
||||
() => useApplicationMutation<string, string>({ execute }),
|
||||
{ wrapper: wrapper(client) },
|
||||
);
|
||||
|
||||
let first: Promise<ApplicationResult<string>> | null = null;
|
||||
let duplicate: Promise<ApplicationResult<string>> | null = null;
|
||||
act(() => {
|
||||
first = hook.result.current.submit("created");
|
||||
duplicate = hook.result.current.submit("created");
|
||||
});
|
||||
|
||||
if (!first || !duplicate) throw new Error("expected two submissions");
|
||||
await expect(duplicate).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "DUPLICATE_IN_FLIGHT" },
|
||||
});
|
||||
expect(execute).toHaveBeenCalledOnce();
|
||||
|
||||
complete({ ok: true, value: "created" });
|
||||
await act(() => first as Promise<ApplicationResult<string>>);
|
||||
});
|
||||
|
||||
it("deduplicates submit and commits one optimistic mutation", async () => {
|
||||
const client = queryClient();
|
||||
const key = ["resource", "list"];
|
||||
@@ -196,6 +526,8 @@ describe("application mutation inbound bridge", () => {
|
||||
const hook = renderHook(
|
||||
() =>
|
||||
useApplicationMutation<string, string>({
|
||||
// §11.2: joining is opt-in. The default is REJECT_WHILE_ACTIVE.
|
||||
duplicatePolicy: "JOIN_IDENTICAL",
|
||||
execute,
|
||||
invalidate: [RESOURCE_INVALIDATION_TOPIC],
|
||||
currentData: client.getQueryData(key),
|
||||
|
||||
Reference in New Issue
Block a user