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).clear(); } catch { // A read facade has no `clear`; that is the expected outcome. } try { Map.prototype.clear.call(map as Map); } 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); }); });