import assert from "node:assert/strict"; import { test } from "node:test"; import { compileQuery } from "../src/query/compile.js"; import { linkQueries } from "../src/query/link.js"; import { compileExecutionPlan } from "../src/execution-world/plan.js"; import { loadReaderWorkspace as load, readReaderFixture as read } from "./reader-fixture.js"; test("complete reader contracts compose factories, portable selectors, CRDT edits, and bounded query scopes", async () => { const workspace = await load(); assert.equal(workspace.constructors.length, 0); assert.equal(workspace.atoms.length, 3); const linked = linkQueries(workspace); assert.equal(linked.length, 9); assert.equal(linked.filter((q) => q.checked.declaration.placement?.kind === "local").length, 4); const screen = linked.find((q) => q.checked.declaration.displayName === "ReaderScreen")!; assert.ok(screen.fields.some((f) => f.memberId === "highlight:excerpt" && f.binding.kind === "package")); assert.ok(screen.fields.some((f) => f.memberId === "comment:content" && f.binding.kind === "state")); assert.equal( linked.find((q) => q.checked.declaration.displayName === "HighlightEditorInputs")!.checked.declaration.root.kind, "object", ); assert.equal( linked.find((q) => q.checked.declaration.displayName === "HighlightScope")!.checked.declaration.root.kind, "workspace-interface", ); }); test("ordinary operations allow server fallback; explicitly local projections require local code", async () => { const serverExcerpt = await load((name, text) => name === "ReaderRuntime.qx" ? text.replace(/(operation excerpt[\s\S]*?targets )\[JavaScript, server\]/, "$1[server]") : text, ); assert.throws(() => compileExecutionPlan(serverExcerpt), /NONPORTABLE_QUERY_FIELD/); for (const pkg of serverExcerpt.packageImports) pkg.queries = pkg.queries?.filter((q) => q.placement?.kind !== "local"); const fallback = compileExecutionPlan(serverExcerpt).definition.operations.find( (op) => op.operationId === "highlight:excerpt:get", )!; assert.deepEqual(fallback.availableTargets, ["server"]); await assert.rejects( load((name, text) => name === "ReaderRuntime.qx" ? text.replace( "using [sourceRevision.get, content.get];", "using [sourceRevision.get, content.get, highlights.connect];", ) : text, ), /Snapshot execution cannot perform a transaction/, ); }); test("a field can remain server-queryable while failing local query placement", async () => { const workspace = await load(); const pkg = workspace.packageImports[0]!; const highlight = workspace.interfaceImports.find((i) => i.displayName === "HighlightData")!; const field = highlight.members.find((m) => m.displayName === "color")!; delete field.operations[0]!.execution; const admission = pkg.queries!.find((q) => q.displayName === "HighlightScope")!; const local = pkg.queries!.find((q) => q.displayName === "HighlightList")!; await compileQuery(admission, workspace.interfaceImports, read); await assert.rejects( compileQuery(local, workspace.interfaceImports, read), /graph snapshot getter for HighlightData.color/, ); });