79 lines
3.8 KiB
TypeScript
79 lines
3.8 KiB
TypeScript
import assert from "node:assert/strict";
|
|||
|
|
import { test } from "node:test";
|
||
|
|
import {
|
||
|
|
compileCapabilityResourceSource,
|
||
|
|
compileCapabilitySource,
|
||
|
|
formatQx,
|
||
|
|
} from "../src/capability-language/index.js";
|
||
|
|
import { compileExecutionPlan } from "../src/execution-world/plan.js";
|
||
|
|
import { compileKernelProgram } from "../src/execution-world/kernel.js";
|
||
|
|
import { generatePortableTypeScriptBindings } from "../src/bindings/portable.js";
|
||
|
|
const source = { repository: "https://example.test/keyed.git", commit: "a".repeat(40) };
|
||
|
|
const iface = compileCapabilityResourceSource(
|
||
|
|
`interface Index<object Item> id "index" revision "index@1" {
|
||
|
|
queryable relation entries id "entries" : many object Item keyed "string" {
|
||
|
|
resolve id "resolve" execution snapshot targets [browser,server];
|
||
|
|
connect id "connect" execution transaction targets [browser,server];
|
||
|
|
}
|
||
|
|
operation add id "add" : record {mapKey:string;target:ref<Item>;} -> unit {
|
||
|
|
call id "add-call" execution transaction targets [browser,server];
|
||
|
|
}
|
||
|
|
}`,
|
||
|
|
{ source },
|
||
|
|
);
|
||
|
|
assert.ok(iface.ok && iface.resource.kind === "interface", JSON.stringify(iface.diagnostics));
|
||
|
|
const pkg = compileCapabilityResourceSource(
|
||
|
|
`package Runtime id "runtime" revision "runtime@1" {
|
||
|
|
operation add<object Item> id "add" : record {mapKey:string;target:ref<Item>;} -> unit mode call execution transaction targets [browser,server] receiver any requires {
|
||
|
|
edge entries id "entries" : many object Item keyed "string" [resolve,connect];
|
||
|
|
};
|
||
|
|
}`,
|
||
|
|
{ source },
|
||
|
|
);
|
||
|
|
assert.ok(pkg.ok && pkg.resource.kind === "package", JSON.stringify(pkg.diagnostics));
|
||
|
|
const indexContract = iface.resource.revision,
|
||
|
|
runtimeContract = pkg.resource.revision;
|
||
|
|
const declarations = `workspace W id "w" revision "w@1" commit "${source.commit}" {
|
||
|
|
import interface Index; import package Runtime; atom Node id "node";
|
||
|
|
conform Node as Index<atom Node> id "node:index" {
|
||
|
|
private edge Entries id "entries" {
|
||
|
|
atom Node projection entries id "entries" many keyed "string";
|
||
|
|
atom Node projection parent id "parent" optional-one;
|
||
|
|
}
|
||
|
|
bind entries.resolve to edge Entries.entries.resolve;
|
||
|
|
bind entries.connect to edge Entries.entries.connect;
|
||
|
|
bind add.call to package Runtime.add<atom Node> with {entries to edge Entries.entries;};
|
||
|
|
}
|
||
|
|
}`;
|
||
|
|
const compile = (text = declarations) =>
|
||
|
|
compileCapabilitySource(formatQx(text), "workspace.qx", {
|
||
|
|
interfaces: new Map([["Index", indexContract]]),
|
||
|
|
packages: new Map([["Runtime", runtimeContract]]),
|
||
|
|
});
|
||
|
|
test("generic keyed relationships and portable ports have matching checked shapes", () => {
|
||
|
|
const result = compile();
|
||
|
|
assert.ok(result.ok, JSON.stringify(result.diagnostics));
|
||
|
|
const closed = result.workspace.interfaceImports.find((i) => i.application)!;
|
||
|
|
assert.equal(closed.members[0]!.kind, "relationship");
|
||
|
|
assert.equal("keyType" in closed.members[0]! && closed.members[0]!.keyType, "string");
|
||
|
|
const operation = closed.members[0]!.operations.find((o) => o.displayName === "connect")!;
|
||
|
|
assert.equal(operation.inputType.kind, "record");
|
||
|
|
const plan = compileExecutionPlan(result.workspace);
|
||
|
|
assert.doesNotThrow(() => compileKernelProgram(plan));
|
||
|
|
const bindings = generatePortableTypeScriptBindings(plan.definition.bindingSchema, "runtime@1", {
|
||
|
|
target: "browser",
|
||
|
|
});
|
||
|
|
assert.match(bindings, /readonly "mapKey": string/);
|
||
|
|
assert.match(bindings, /readonly "connect": \(input: \{ readonly "mapKey": string;/);
|
||
|
|
});
|
||
|
|
test("key contracts cannot silently bind to unkeyed storage or the inverse projection", () => {
|
||
|
|
for (const text of [
|
||
|
|
declarations.replace('many keyed "string";', "many;"),
|
||
|
|
declarations.replace("entries to edge Entries.entries", "entries to edge Entries.parent"),
|
||
|
|
]) {
|
||
|
|
const result = compile(text);
|
||
|
|
assert.equal(result.ok, false);
|
||
|
|
assert.match(JSON.stringify(result.diagnostics), /key type|keyed projection|signature/i);
|
||
|
|
}
|
||
|
|
});
|