Files
clean-architecture-frontend…/tests/unit/contract-index-immutability.test.ts
DongHyeonkaandClaude Opus 5.5 abaaa9c773 fix(contracts): return read-only indexes and frozen row snapshots (Q-6.7-7)
indexInvalidationRegistry and indexInvalidationTopicVersions froze only
the wrapper object, so `.clear()` on the returned Map emptied a
validated fan-out. validateWebWorkerContributions,
validateOfflineCommandContribution and validateRealtimeContributions
froze only the outer array and returned the caller's rows, so a later
write could raise a deadline, request byte bound or polling interval
that had already been checked.

read-only-registry.ts gains createReadOnlyMap (same private-store
facade, typed ReadonlyMap so the public index types do not change;
forEach now also passes the map argument). The three contribution
validators snapshot every row (exact keys, arrays by index) with
exact-snapshot.ts, validate the snapshot and return it frozen.

Behavior: contribution rows with extra keys, accessors or non-plain
prototypes are now rejected (template registries are empty today).

Verified: new tests/unit/contract-index-immutability.test.ts (5 cases,
all RED before); gates green except the known removal-fixture test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:45:37 +09:00

125 lines
4.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { validateOfflineCommandContribution } from "../../src/contracts/offline-command.ts";
import {
defineQueryInvalidationTopic,
indexInvalidationRegistry,
indexInvalidationTopicVersions,
} from "../../src/contracts/query-invalidation.ts";
import { defineQueryNamespaceIdentity } from "../../src/contracts/query-keys.ts";
import { validateRealtimeContributions } from "../../src/contracts/runtime-capabilities.ts";
import { validateWebWorkerContributions } from "../../src/contracts/web-worker.ts";
/**
* Q-6.7-7. A validated index or registry is authority. `Object.freeze` on the
* outer wrapper leaves a returned `Map` clearable and leaves every row the
* caller's still-mutable object, so a later write changes what was validated.
*/
function tryClear(map: unknown): void {
try {
(map as Map<unknown, unknown>).clear();
} catch {
// A read facade has no `clear`; that is the expected outcome.
}
try {
Map.prototype.clear.call(map as Map<unknown, unknown>);
} catch {
// Borrowing the mutator must fail on the missing internal slot.
}
}
describe("query invalidation index", () => {
const topic = defineQueryInvalidationTopic("qinv.orders.changed");
const orders = defineQueryNamespaceIdentity("orders", 1);
const registry = {
topics: [topic],
namespaces: [orders],
edges: [{ topicId: topic, namespace: orders }],
};
it("cannot be cleared after indexing", () => {
const index = indexInvalidationRegistry(registry);
tryClear(index.namespacesForTopic);
tryClear(index.topicsForNamespace);
expect(index.namespacesForTopic.get(topic)).toEqual([orders]);
expect(index.topicsForNamespace.get('["orders",1]')).toEqual([topic]);
});
it("returns topic versions that cannot be cleared", () => {
const versions = indexInvalidationTopicVersions(registry, [
{ topicId: topic, topicVersion: 1 },
]);
tryClear(versions);
expect(versions.get(topic)).toBe(1);
});
});
describe("runtime capability contributions", () => {
it("snapshots web worker task bounds", () => {
const task = {
taskId: "hash",
taskVersion: 1 as const,
maximumInputBytes: 1_024,
maximumOutputBytes: 1_024,
deadlineMs: 1_000,
};
const [installed] = validateWebWorkerContributions([
{ taskGroupId: "crypto", tasks: [task], fallback: "UNSUPPORTED" },
]);
task.deadlineMs = 999_999;
expect(installed?.tasks[0]?.deadlineMs).toBe(1_000);
expect(Object.isFrozen(installed?.tasks[0])).toBe(true);
});
it("snapshots offline command operation bounds", () => {
const operation = {
operationId: "CREATE_ORDER",
contractPackageId: "@test/contracts",
maximumRequestBytes: 1_024,
retentionClass: "STANDARD_7D" as const,
};
const installed = validateOfflineCommandContribution({
datasetId: "OFFLINE_COMMANDS_V1",
operations: [operation],
});
operation.maximumRequestBytes = 999_999_999;
expect(installed.operations[0]?.maximumRequestBytes).toBe(1_024);
expect(Object.isFrozen(installed.operations[0])).toBe(true);
});
it("snapshots realtime transport and effect rows", () => {
const transport = {
kind: "POLLING" as const,
operationId: "LIST_ORDERS",
intervalMs: 5_000,
};
const topics = ["qinv.orders.changed"];
const [installed] = validateRealtimeContributions([
{
contributionId: "orders-realtime",
featureId: "orders",
contractSourcePackageId: "@test/contracts",
streamId: "ORDERS",
recoveryMode: "SESSION_REBUILD",
eventEffects: [
{
eventType: "orders.changed",
mapperId: "OrdersChanged",
effect: "INVALIDATE_TOPICS",
invalidationTopics: topics,
},
],
transport,
},
]);
transport.intervalMs = 1;
topics.length = 0;
const effect = installed?.eventEffects[0];
expect(installed?.transport).toMatchObject({ intervalMs: 5_000 });
expect(effect?.invalidationTopics).toEqual(["qinv.orders.changed"]);
expect(Object.isFrozen(installed?.transport)).toBe(true);
expect(Object.isFrozen(effect?.invalidationTopics)).toBe(true);
});
});