49 lines
2.5 KiB
TypeScript
49 lines
2.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|||
|
|
import test from "node:test";
|
||
|
|
import { compileCapabilityResourceSource, compileCapabilitySource } from "../src/capability-language/parser.js";
|
||
|
|
import { generateTypeScriptBindings } from "../src/bindings/index.js";
|
||
|
|
const source = { repository: "https://example.test/meta.git", commit: "a".repeat(40) };
|
||
|
|
|
||
|
|
test("reflection authority is an explicit checked workspace binding, including generic exports", () => {
|
||
|
|
const iface = compileCapabilityResourceSource(
|
||
|
|
'interface Inspect id "inspect" revision "inspect@1" {operation run id "run" : unit -> unit {call id "call";}}',
|
||
|
|
{ source },
|
||
|
|
);
|
||
|
|
assert.ok(iface.ok && iface.resource.kind === "interface");
|
||
|
|
if (!iface.ok || iface.resource.kind !== "interface") throw Error("interface");
|
||
|
|
const interfaceRevision = iface.resource.revision;
|
||
|
|
for (const generic of [false, true]) {
|
||
|
|
const pkg = compileCapabilityResourceSource(
|
||
|
|
`package Inspector id "inspector" revision "inspector@1" {operation run${generic ? "<value T>" : ""} id "run" : unit -> unit mode call receiver any requires {reflection meta id "meta" [atoms, interfaces];};}`,
|
||
|
|
{ source },
|
||
|
|
);
|
||
|
|
assert.ok(pkg.ok && pkg.resource.kind === "package", JSON.stringify(pkg.diagnostics));
|
||
|
|
if (!pkg.ok || pkg.resource.kind !== "package") throw Error("package");
|
||
|
|
const packageRevision = pkg.resource.revision;
|
||
|
|
const compile = (selectors: string) =>
|
||
|
|
compileCapabilitySource(
|
||
|
|
`workspace W id "ws" revision "ws@1" commit "${source.commit}" {
|
||
|
|
import interface Inspect;import package Inspector;atom Thing id "thing";
|
||
|
|
conform Thing as Inspect id "inspection" {bind run.call to package Inspector.run${generic ? "<string>" : ""} with {meta to workspace reflection [${selectors}];};}
|
||
|
|
}`,
|
||
|
|
"workspace.qx",
|
||
|
|
{ interfaces: new Map([["Inspect", interfaceRevision]]), packages: new Map([["Inspector", packageRevision]]) },
|
||
|
|
);
|
||
|
|
const result = compile("atoms, interfaces");
|
||
|
|
assert.ok(result.ok, JSON.stringify(result.diagnostics));
|
||
|
|
assert.equal(compile("atoms").ok, false, "binding cannot narrow or expand the declared contract silently");
|
||
|
|
assert.equal(compile("atoms, interfaces, queries").ok, false);
|
||
|
|
if (!result.ok) throw Error("workspace");
|
||
|
|
const generated = generateTypeScriptBindings(
|
||
|
|
{
|
||
|
|
format: "quixos-bindings",
|
||
|
|
version: 1,
|
||
|
|
interfaces: result.workspace.interfaceImports,
|
||
|
|
packages: result.workspace.packageImports,
|
||
|
|
},
|
||
|
|
"inspector@1",
|
||
|
|
);
|
||
|
|
assert.match(generated, /QxReflectionPort/);
|
||
|
|
}
|
||
|
|
});
|