Compile complete local-first reader and scope fixtures
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import {
|
||||
compileCapabilityResourceSource,
|
||||
compileCapabilitySource,
|
||||
formatQx,
|
||||
} from "../src/capability-language/index.js";
|
||||
import type { InterfaceRevision } from "../src/capability-model/types.js";
|
||||
import { compileQuery } from "../src/query/compile.js";
|
||||
import { linkQueries } from "../src/query/link.js";
|
||||
const directory = resolve("test/fixtures/local-first-reader");
|
||||
const read = (name: string) => readFile(resolve(directory, name), "utf8");
|
||||
const source = { repository: "https://example.test/reader.git", commit: "a".repeat(40) };
|
||||
const load = async (transform = (name: string, text: string) => text) => {
|
||||
const interfaces = new Map<string, InterfaceRevision>();
|
||||
for (const name of ["DocumentData", "HighlightData", "CommentData", "DocumentActions", "DocumentFactory"]) {
|
||||
const text = transform(`${name}.qx`, await read(`${name}.qx`));
|
||||
const result = compileCapabilityResourceSource(formatQx(text), { source });
|
||||
assert.ok(result.ok && result.resource.kind === "interface", `${name}: ${JSON.stringify(result.diagnostics)}`);
|
||||
interfaces.set(name, result.resource.revision);
|
||||
}
|
||||
const environment = { interfaces, interfaceClosure: [...interfaces.values()] };
|
||||
const pkg = compileCapabilityResourceSource(formatQx(transform("ReaderRuntime.qx", await read("ReaderRuntime.qx"))), {
|
||||
source,
|
||||
environment,
|
||||
});
|
||||
assert.ok(pkg.ok && pkg.resource.kind === "package", JSON.stringify(pkg.diagnostics));
|
||||
const revision = pkg.resource.revision;
|
||||
revision.checkedQueries = await Promise.all(
|
||||
revision.queries!.map((query) => compileQuery(query, [...interfaces.values()], read)),
|
||||
);
|
||||
const result = compileCapabilitySource(formatQx(await read("workspace.qx")), "workspace.qx", {
|
||||
...environment,
|
||||
packages: new Map([["ReaderRuntime", revision]]),
|
||||
packageClosure: [revision],
|
||||
});
|
||||
assert.ok(result.ok, JSON.stringify(result.diagnostics));
|
||||
return result.workspace;
|
||||
};
|
||||
test("complete reader contracts compose factories, portable selectors, CRDT edits, and six 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, 6);
|
||||
assert.equal(linked.filter((q) => q.checked.declaration.placement?.kind === "local").length, 3);
|
||||
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("reader composition reports the dependency that weakens a portable promise", async () => {
|
||||
await assert.rejects(
|
||||
load((name, text) =>
|
||||
name === "ReaderRuntime.qx"
|
||||
? text.replace(/(operation excerpt[\s\S]*?targets )\[browser, server\]/, "$1[server]")
|
||||
: text,
|
||||
),
|
||||
/does not provide execution target browser/,
|
||||
);
|
||||
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),
|
||||
/browser snapshot getter for HighlightData.color/,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user