Implement checked workspace integration contracts (WI-01)
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { compileCapabilityResourceSource, compileCapabilitySource } from "../src/capability-language/index.js";
|
||||
import {
|
||||
compileExecutionPlan,
|
||||
generateInterfaceClientBindings,
|
||||
publicOperationContract,
|
||||
linkInterfaceOperation,
|
||||
publicQueryContract,
|
||||
linkPackageQuery,
|
||||
} from "../src/execution-world/index.js";
|
||||
import { compileQuery } from "../src/query/compile.js";
|
||||
import { parseControllerCatalog, validateControllers } from "../src/capability-model/controllers.js";
|
||||
const source = { repository: "https://example.test/contracts.git", commit: "a".repeat(40) };
|
||||
const parsed = compileCapabilityResourceSource(
|
||||
`interface Data id "data" revision "data@1" {
|
||||
queryable portable value label id "label" : string { get id "label:get" execution snapshot targets [browser, server]; }
|
||||
operation publish id "publish" : string -> unit { call id "publish:call"; }
|
||||
}`,
|
||||
{ source },
|
||||
);
|
||||
assert.ok(parsed.ok && parsed.resource.kind === "interface", JSON.stringify(parsed.diagnostics));
|
||||
const iface = parsed.resource.revision;
|
||||
const environment = { interfaces: new Map([["Data", iface]]), interfaceClosure: [iface] };
|
||||
const compile = async () => {
|
||||
const pkg = compileCapabilityResourceSource(
|
||||
`import interface Data; package Views id "views" revision "views@1" {
|
||||
query Labels id "labels" root workspace Data document "labels.graphql" operation "Labels" { replica; }
|
||||
operation publish id "export:publish" : string -> unit mode call receiver any;
|
||||
}`,
|
||||
{ source, environment },
|
||||
);
|
||||
assert.ok(pkg.ok && pkg.resource.kind === "package", JSON.stringify(pkg.diagnostics));
|
||||
const revision = pkg.resource.revision;
|
||||
revision.checkedQueries = [
|
||||
await compileQuery(
|
||||
revision.queries![0]!,
|
||||
[iface],
|
||||
async () => "query Labels { root { objects(first: 10) { entries { node { label } } } } }",
|
||||
),
|
||||
];
|
||||
const result = compileCapabilitySource(
|
||||
`workspace Test id "test" revision "test@1" commit "${source.commit}" {
|
||||
import interface Data; import package Views;
|
||||
atom One id "one"; atom Two id "two";
|
||||
${["One", "Two"]
|
||||
.map(
|
||||
(atom) => `conform ${atom} as Data id "${atom}:data" {
|
||||
private state ${atom}Label id "${atom}:label" on ${atom} : string policy register default "";
|
||||
bind label.get to state ${atom}Label.read;
|
||||
bind publish.call to package Views.publish;
|
||||
}`,
|
||||
)
|
||||
.join("\n")}
|
||||
}`,
|
||||
"workspace.qx",
|
||||
{ ...environment, packages: new Map([["Views", revision]]) },
|
||||
);
|
||||
assert.ok(result.ok, JSON.stringify(result.diagnostics));
|
||||
return compileExecutionPlan(result.workspace);
|
||||
};
|
||||
|
||||
test("one imported operation contract links to two atom implementations without containing either", async () => {
|
||||
const plan = await compile();
|
||||
const contract = publicOperationContract(iface, iface.members[0]!.operations[0]!);
|
||||
assert.equal(linkInterfaceOperation(plan, "one", contract).atomId, "one");
|
||||
assert.equal(linkInterfaceOperation(plan, "two", contract).atomId, "two");
|
||||
const generated = generateInterfaceClientBindings({ Data: iface });
|
||||
assert.ok(generated.includes("defineInterfaceOperation<null, string>"));
|
||||
for (const privateValue of [plan.digest, "One:label", '"atomId"', '"binding"'])
|
||||
assert.equal(generated.includes(privateValue), false);
|
||||
assert.throws(() => linkInterfaceOperation(plan, "absent", contract), /UNINSTALLED_OPERATION/);
|
||||
for (const change of [{ scope: "class" }, { input: { kind: "string" } }, { execution: { kind: "remote" } }])
|
||||
assert.throws(
|
||||
() => linkInterfaceOperation(plan, "one", { ...contract, ...change } as typeof contract),
|
||||
/CLIENT_CONTRACT_MISMATCH/,
|
||||
);
|
||||
assert.throws(() => linkInterfaceOperation({ ...plan, digest: "bad" }, "one", contract), /INVALID_PLAN/);
|
||||
});
|
||||
|
||||
test("package query handles bind the checked definition, placement and codecs", async () => {
|
||||
const plan = await compile();
|
||||
const contract = publicQueryContract(plan.definition.workspace.packageImports[0]!, "labels");
|
||||
assert.equal(linkPackageQuery(plan, contract).query.checked.declaration.id, "labels");
|
||||
for (const change of [{ placement: "local" }, { definitionDigest: "bad" }, { output: null }])
|
||||
assert.throws(() => linkPackageQuery(plan, { ...contract, ...change } as typeof contract), /CLIENT_QUERY_MISMATCH/);
|
||||
});
|
||||
|
||||
test("controller templates check object capability effects and exact input mapping", () => {
|
||||
const catalog = {
|
||||
schemaVersion: 1,
|
||||
controllers: [
|
||||
{ id: "publish-label", subject: "data@1", snapshot: "label:get", remote: "publish:call", input: "value" },
|
||||
],
|
||||
};
|
||||
assert.equal(parseControllerCatalog(JSON.stringify(catalog), [iface]).length, 1);
|
||||
for (const change of [
|
||||
{ input: "unit" },
|
||||
{ input: "callback" },
|
||||
{ subject: "missing" },
|
||||
{ snapshot: "publish:call" },
|
||||
{ remote: "label:get" },
|
||||
{ url: "https://example.test" },
|
||||
]) {
|
||||
const invalid = { ...catalog, controllers: [{ ...catalog.controllers[0], ...change }] };
|
||||
assert.throws(() => parseControllerCatalog(JSON.stringify(invalid), [iface]), /Controller|controller/);
|
||||
}
|
||||
assert.throws(
|
||||
() =>
|
||||
parseControllerCatalog(
|
||||
JSON.stringify({ ...catalog, controllers: [...catalog.controllers, ...catalog.controllers] }),
|
||||
[iface],
|
||||
),
|
||||
/unique/,
|
||||
);
|
||||
const classIface = structuredClone(iface);
|
||||
classIface.members[1]!.operations[0]!.scope = "class";
|
||||
assert.ok(
|
||||
validateControllers(parseControllerCatalog(JSON.stringify(catalog), [iface]), [classIface], "controllers").length,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user