Files

59 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

import assert from "node:assert/strict";
import test from "node:test";
import { create } from "@bufbuild/protobuf";
import { InjectedDependencySchema } from "../dist/quixos/refs_pb.js";
import { DefinitionPageSchema } from "../dist/quixos/orch_pb.js";
import { createRuntimeContext, bindQxHandler } from "../dist/index.js";
test("checked reflection bindings preserve injected scope, selector authority and cancellation", async () => {
const seen = [];
const page = create(DefinitionPageSchema, { identity: { workspaceId: "ws", workspaceEpoch: "7" } });
const signal = new AbortController().signal;
const raw = createRuntimeContext(
{},
{
readDefinitions: async (request) => {
seen.push(request);
return page;
},
async *watchDefinitions(request, options) {
assert.equal(options.signal, signal);
seen.push(request);
yield page;
},
},
{
objectId: "",
input: {},
dependencies: [
create(InjectedDependencySchema, {
portId: "meta",
binding: { case: "reflection", value: { workspaceId: "ws", workspaceEpoch: "7", selectors: ["atoms"] } },
}),
],
},
);
const handler = bindQxHandler(
{
receiver: "none",
inputType: { kind: "builtin", name: "unit" },
outputType: { kind: "builtin", name: "unit" },
ports: { meta: { kind: "reflection", id: "meta", selectors: ["atoms"] } },
},
async (context) => {
await context.ports.meta.read({ selector: { kind: "atoms" }, workspaceId: "forged", workspaceEpoch: "9" });
for await (const result of context.ports.meta.watch({ selector: { kind: "atoms" } }, signal))
assert.equal(result, page);
assert.throws(() => context.ports.meta.read({ selector: { kind: "queries" } }), /Undeclared reflection/);
},
{},
);
await handler(raw);
assert.equal(seen.length, 2);
for (const request of seen) {
assert.equal(request.workspaceId, "ws");
assert.equal(request.workspaceEpoch, "7");
}
assert.throws(() => raw.reflection("missing"), /Missing/);
});