Files
quixos-protocol/test/reference-dependencies.test.ts
T

91 lines
4.9 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { compileCapabilityResourceSource, compileCapabilitySource } from "../src/capability-language/parser.js";
import { compileExecutionPlan, compileKernelProgram } from "../src/execution-world/index.js";
import { generatePortableTypeScriptBindings } from "../src/bindings/portable.js";
import { formatQx } from "../src/capability-language/source.js";
const source = { repository: "https://example.test/reference.git", commit: "a".repeat(40) };
const iface = `interface Data id "data" revision "data@1" {
value content id "content" : string { get id "content:get" execution snapshot targets [server]; }
operation peek id "peek" : interface-ref<Data> -> string { call id "peek:call" execution snapshot targets [server]; }
}`;
const pkg = `external atom Item id "item";
import interface Data;
package Runtime id "runtime" revision "runtime@1" {
operation peek id "runtime:peek" : interface-ref<Data> -> string mode call execution snapshot targets [server]
receiver atom Item requires { interface peers id "peers" : Data using [content.get] from input; };
}`;
const workspace = `workspace Test id "workspace:test" revision "workspace:test@1" commit "${"a".repeat(40)}" {
import interface Data; import package Runtime; atom Item id "item";
conform Item as Data id "item:data" {
private state Content id "content" on Item : string policy register default "hello";
bind content.get to state Content.read;
bind peek.call to package Runtime.peek with { peers to interface Data from input; };
}
}`;
function compile(packageSource = pkg, workspaceSource = workspace, interfaceSource = iface) {
const contract = compileCapabilityResourceSource(interfaceSource, { source });
assert.ok(contract.ok && contract.resource.kind === "interface", JSON.stringify(contract.diagnostics));
const interfaces = new Map([["Data", contract.resource.revision]]);
const runtime = compileCapabilityResourceSource(packageSource, { source, environment: { interfaces } });
if (!runtime.ok) return runtime;
assert.equal(runtime.resource.kind, "package");
if (runtime.resource.kind !== "package") throw Error("Expected package");
return compileCapabilitySource(workspaceSource, "workspace.qx", {
interfaces,
packages: new Map([["Runtime", runtime.resource.revision]]),
});
}
test("reference-selected dependencies round trip and do not require a recursive receiver conformance", () => {
const result = compile(formatQx(pkg), formatQx(workspace));
assert.ok(result.ok, JSON.stringify(result.diagnostics));
const plan = compileExecutionPlan(result.workspace);
const kernel = compileKernelProgram(plan) as {
operations: { operation: string; binding: { ports: { receiver?: string }[] } }[];
};
assert.equal(kernel.operations.find((op) => op.operation === "peek:call")!.binding.ports[0]!.receiver, "input");
const peek = plan.definition.operations.find((op) => op.operationId === "peek:call")!;
assert.ok(
peek.dependencies.some(
(dependency) => dependency.kind === "operation" && dependency.operation.includes("content:get"),
),
);
const generated = generatePortableTypeScriptBindings(plan.definition.bindingSchema, "runtime@1", {
target: "server",
});
assert.ok(generated.includes('ManagedReference<"interface", "data@1">'));
assert.ok(generated.includes('"receiver"'));
});
test("both sides explicitly grant reference selection and only object snapshots are permitted", () => {
assert.equal(compile(pkg, workspace.replace("Data from input", "Data")).ok, false);
assert.equal(compile(pkg.replace(" from input", ""), workspace).ok, false);
assert.equal(compile(pkg.replace("execution snapshot targets [server]", "execution remote"), workspace).ok, false);
assert.equal(compile(pkg.replace("using [content.get]", "")).ok, false);
});
test("closed generic specialization preserves reference-selected interface ports", () => {
const result = compile(
pkg.replace("operation peek id", "operation peek<value T> id"),
workspace.replace("Runtime.peek with", "Runtime.peek<string> with"),
);
assert.ok(result.ok, JSON.stringify(result.diagnostics));
const plan = compileExecutionPlan(result.workspace);
const entry = plan.definition.bindingSchema.packages[0]!.exports[0]!;
assert.ok(
entry.dependencyPorts.some(
(port) => port.requirement.kind === "interface" && port.requirement.receiver === "input",
),
);
assert.ok(
generatePortableTypeScriptBindings(plan.definition.bindingSchema, "runtime@1", { target: "server" }).includes(
'"receiver"',
),
);
});
test("portable input-selected reads require the dependency's declared target support", () => {
const portable = pkg.replaceAll("targets [server]", "targets [browser, server]");
assert.equal(compile(portable).ok, false);
const result = compile(portable, workspace, iface.replaceAll("targets [server]", "targets [browser, server]"));
assert.ok(result.ok, JSON.stringify(result.diagnostics));
});