Generate portable capability contexts and checked handler registries

This commit is contained in:
Timothy J. Aveni
2026-09-20 00:25:53 -07:00
parent 0aaa80a4ba
commit 60b1bdb39f
7 changed files with 338 additions and 2 deletions
+38
View File
@@ -0,0 +1,38 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { generatePortableTypeScriptBindings } from "../src/bindings/portable.js";
import { compileExecutionPlan } from "../src/execution-world/plan.js";
import { loadReaderWorkspace } from "./reader-fixture.js";
test("reader portable bindings have selected facets, creation inputs and stable keyed calls", async () => {
const schema = compileExecutionPlan(await loadReaderWorkspace()).definition.bindingSchema;
const source = generatePortableTypeScriptBindings(schema, "package:reader-runtime@1", { target: "browser" });
assert.match(source, /readonly "sourceRevision.get": \(\) => Promise<string>/);
assert.match(
source,
/readonly "highlights.connect": \(input: ManagedReference<"atom", "atom:reader:highlight">, options: MutationKey\)/,
);
assert.match(source, /readonly "createDocument"/);
assert.match(source, /readonly "create":/);
assert.match(source, /readonly "comments": ReadonlyArray/);
assert.doesNotMatch(source, /"highlights.resolve"|context\.conform|objectId|"live"/);
assert.equal(source, generatePortableTypeScriptBindings(schema, "package:reader-runtime@1", { target: "browser" }));
const server = generatePortableTypeScriptBindings(schema, "package:reader-runtime@1", { target: "server" });
assert.equal(source.replace('target = "browser"', 'target = "server"'), server);
});
test("generation rejects remote dependencies, wrong effects and unsupported shapes explicitly", async () => {
const schema = compileExecutionPlan(await loadReaderWorkspace()).definition.bindingSchema;
const generate = () => generatePortableTypeScriptBindings(schema, "package:reader-runtime@1", { target: "browser" });
const entry = schema.packages[0]!.exports.find((e) => e.displayName === "addHighlight")!;
entry.execution = { kind: "snapshot", targets: ["browser"] };
assert.throws(generate, /Snapshot context cannot expose mutation ports/);
entry.execution = { kind: "transaction", targets: ["browser"] };
const iface = schema.interfaces.find((i) => i.revisionId === "interface:reader:document-data@1")!;
const getter = iface.members[0]!.operations[0]!;
getter.execution = { kind: "remote" };
assert.throws(generate, /not an object call available in portable target browser/);
getter.execution = { kind: "snapshot", targets: ["browser", "server"] };
entry.dependencyPorts[0]!.requirement = { kind: "reflection", selectors: ["active"] };
assert.throws(generate, /Unsupported reflection port/);
});