Files
quixos-protocol/test/scaffold-recipes.test.ts

114 lines
5.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { execFile as callback } from "node:child_process";
import { promisify } from "node:util";
import { scaffoldRecipe } from "../src/capability-language/scaffold-recipes.js";
import { planStructure, applyStructure } from "../src/capability-language/structural-plan.js";
import { reactPlatformTypes } from "../src/bindings/react-platform.js";
const execFile = promisify(callback);
test("execution scaffolds route typed handlers by declared effect and target without legacy lifecycle files", async (context) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "qx-execution-recipe-"));
context.after(() => fs.rm(root, { recursive: true, force: true }));
await execFile("git", ["-C", root, "init"]);
const source = { repository: "https://example.test/execution.git", commit: "a".repeat(40) };
const apply = async (
command: Parameters<typeof scaffoldRecipe>[1],
spec: Partial<Parameters<typeof scaffoldRecipe>[2]>,
) => applyStructure(await planStructure(root, await scaffoldRecipe(root, command, { source, ...spec })));
await apply("package", {
name: "Execution",
id: "package:execution",
revision: "package:execution@1",
template: "typescript-execution",
tools: { quixos: source, protocol: source, helpers: source, sdk: source },
});
for (const file of ["quixos.migrations.json", "src/migrate.ts", "descriptor.quixos-package.txtpb"])
await assert.rejects(fs.access(path.join(root, file)));
await fs.appendFile(path.join(root, "src/server.ts"), "\n// Preserve authored code\n");
const add = (name: string, execution: string) =>
apply("function", {
name,
id: "export:" + name,
declaration: `function ${name} id "export:${name}" : unit -> unit execution ${execution};`,
});
await add("both", "transaction targets [JavaScript, server]");
await add("serverOnly", "snapshot targets [server]");
await add("asyncWork", "remote");
const read = (file: string) => fs.readFile(path.join(root, file), "utf8");
assert.match(await read("src/browser.ts"), /"both":/);
assert.doesNotMatch(await read("src/browser.ts"), /serverOnly|asyncWork/);
assert.match(await read("src/server.ts"), /"serverOnly":/);
assert.match(await read("src/server.ts"), /Preserve authored code/);
assert.match(await read("src/remote.ts"), /"asyncWork":/);
assert.match(await read("src/impl/asyncWork.ts"), /RemoteImplementation/);
assert.match(await read("src/impl/both.ts"), /PortableImplementation/);
assert.match(await read("flake.nix"), /checkedArtifacts/);
assert.match(await read("flake.nix"), /entries.browser/);
await assert.rejects(add("both", "transaction targets [JavaScript, server]"), /unique/);
await assert.rejects(add("invalid", "snapshot targets [remote]"), /TypeScript scaffold supports/);
await assert.rejects(apply("function", { name: "missing", id: "export:missing" }), /explicit execution/);
await assert.rejects(apply("migration", { name: "migration", id: "export:migration" }), /retired/);
});
test("query scaffold preserves placement and inserts its named GraphQL document atomically", async (context) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "qx-query-recipe-"));
context.after(() => fs.rm(root, { recursive: true, force: true }));
await execFile("git", ["-C", root, "init"]);
const source = { repository: "https://example.test/query.git", commit: "a".repeat(40) };
await fs.writeFile(
path.join(root, "package.qx"),
'import interface Task;\npackage Tasks id "tasks" revision "tasks@1" {}\n',
);
await fs.writeFile(path.join(root, "quixos.check.json"), JSON.stringify({ backend: "execution" }));
const request = (name: string, placement: string) => ({
source,
name,
id: `tasks:${name}`,
declaration: `query ${name} id "tasks:${name}" root Task document "queries/${name}.graphql" operation "${name}" { ${placement} max rows 100; }`,
queryDocument: `query ${name} { root { title } }\n`,
});
for (const [name, placement] of [
["Inputs", "replica;"],
["Screen", "local from Inputs;"],
["Confirmed", ""],
]) {
const spec = request(name, placement);
const recipe = await scaffoldRecipe(root, "query", spec);
const planned = await planStructure(root, recipe);
await assert.rejects(fs.access(path.join(root, `queries/${name}.graphql`)), "Preview must not write files");
await applyStructure(planned);
assert.equal(await fs.readFile(path.join(root, `queries/${name}.graphql`), "utf8"), spec.queryDocument);
assert.ok((await fs.readFile(path.join(root, "package.qx"), "utf8")).includes(placement));
await assert.rejects(scaffoldRecipe(root, "query", spec), /unique/);
}
const bad = request("Bad", "replica;");
await assert.rejects(
scaffoldRecipe(root, "query", {
...bad,
declaration: bad.declaration.replace("queries/Bad.graphql", "../Bad.graphql"),
}),
/contained/,
);
await assert.rejects(
scaffoldRecipe(root, "query", { ...bad, queryDocument: "query Other { root { title } }" }),
/declared named query/,
);
await assert.rejects(
scaffoldRecipe(root, "query", { ...bad, queryDocument: "mutation Bad { title }" }),
/declared named query/,
);
await assert.rejects(
scaffoldRecipe(root, "query", { ...bad, declaration: bad.declaration + ' function x id "x" : unit -> unit;' }),
/one QX query/,
);
await fs.mkdir(path.join(root, "queries"), { recursive: true });
await fs.writeFile(path.join(root, "queries/Bad.graphql"), "# authored content\n");
const before = await fs.readFile(path.join(root, "package.qx"), "utf8");
await assert.rejects(planStructure(root, await scaffoldRecipe(root, "query", bad)), /existing file/);
assert.equal(await fs.readFile(path.join(root, "package.qx"), "utf8"), before);
});