Scaffold explicit query placement and authored GraphQL documents

This commit is contained in:
Timothy J. Aveni
2026-09-21 01:30:57 -07:00
parent 20ef63007c
commit 01a3ec5ae2
4 changed files with 157 additions and 5 deletions
+57
View File
@@ -166,3 +166,60 @@ test("execution scaffolds route typed handlers by declared effect and target wit
await assert.rejects(apply("function", { name: "missing", id: "export:missing" }), /explicit execution/);
await assert.rejects(apply("migration", { name: "migration", id: "export:migration" }), /not supported/);
});
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',
);
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);
});