// `archilyzer reports respell`: a dry run that writes nothing, then --apply through the // registry and the iterations store, with backups; a temp store. import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { writeArticle } from "../lib/report/registry"; import { createIteration, makeCurrent, updateIteration, writeIterationFile } from "../lib/report/iterations"; import { respellMain } from "./reports-respell"; const ROOTS: string[] = []; after(() => { for (const r of ROOTS) rmSync(r, { recursive: true, force: true }); }); function setup() { const root = mkdtempSync(path.join(tmpdir(), "reports-respell-")); ROOTS.push(root); const paths = { articlesDir: path.join(root, "reports"), exportIndexDir: path.join(root, ".export-index") }; return { paths, projectsRoot: path.join(root, "projects") }; } const doc = (id: string) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, sections: [{ id: "s1", title: "Coffee", body: 'He said [“my own cakeup machine”](cite:a).' }], citations: { a: { kind: "video", channel: "ch", id: "v1", start: 1, end: 2, quote: "my own cakeup machine" } }, }); function capture() { const lines: string[] = []; const errors: string[] = []; return { out: { log: (s: string) => lines.push(s), error: (s: string) => errors.push(s) }, lines, errors }; } const FLAGS = { heard: "cake cup, cakeup", reads: "K-Cup" }; test("dry by default: every change listed, nothing written; --apply writes the iteration, the article and the manifest, backed up", async () => { const s = setup(); await writeArticle(doc("cb"), { paths: s.paths }); await createIteration("cb", { kind: "sweep", model: "claude-code", question: "Q?" }, s.paths); await writeIterationFile("cb", 1, "report.json", doc("cb"), s.paths); await updateIteration("cb", 1, { status: "done" }, s.paths); await makeCurrent("cb", 1, s.paths); const project = path.join(s.projectsRoot, "cb", "i1"); mkdirSync(project, { recursive: true }); const manifestFile = path.join(project, "video.manifest.json"); writeFileSync(manifestFile, JSON.stringify({ slug: "cb", timeline: [{ id: "c", quote: "my own cakeup machine" }] })); const projectReport = path.join(project, "report.json"); writeFileSync(projectReport, JSON.stringify(doc("cb"))); const articleFile = path.join(s.paths.articlesDir, "cb", "report.json"); const iterFile = path.join(s.paths.articlesDir, "cb", "iterations", "1", "report.json"); const before = readFileSync(articleFile, "utf8"); const dry = capture(); assert.equal(await respellMain("cb", FLAGS, s, dry.out), 0); assert.equal(readFileSync(articleFile, "utf8"), before); const text = dry.lines.join("\n"); assert.match(text, /«cakeup» → «K-Cup»/); assert.match(text, /iteration-report .*: 2 change\(s\)/); assert.match(text, /article-report .*: 2 change\(s\)/); assert.match(text, /video-manifest .*: 1 change\(s\)/); assert.match(text, /project-report .*: 2 change\(s\)/); assert.match(text, /dry run: nothing written/); const run = capture(); assert.equal(await respellMain("cb", { ...FLAGS, apply: true }, s, run.out), 0); for (const f of [articleFile, iterFile]) { const r = JSON.parse(readFileSync(f, "utf8")); assert.equal(r.citations.a.quote, "my own K-Cup machine"); assert.match(r.sections[0].body, /my own K-Cup machine/); assert.deepEqual(r.respellings, [{ heard: ["cake cup", "cakeup"], reads: "K-Cup" }]); } assert.equal(JSON.parse(readFileSync(manifestFile, "utf8")).timeline[0].quote, "my own K-Cup machine"); assert.equal(JSON.parse(readFileSync(projectReport, "utf8")).citations.a.quote, "my own K-Cup machine"); const backups = readdirSync(path.join(project, "revisions")); assert.equal(backups.length, 4); assert.ok(backups.some((b) => b.endsWith("-before-respell.article-report.json"))); // The backup is the file as it was. const ab = backups.find((b) => b.endsWith("article-report.json"))!; assert.equal(readFileSync(path.join(project, "revisions", ab), "utf8"), before); // Again: nothing left to change. const again = capture(); assert.equal(await respellMain("cb", { ...FLAGS, apply: true }, s, again.out), 1); assert.equal(readdirSync(path.join(project, "revisions")).length, 4); }); test("an article with no iterations: its report only, backed up outside the store", async () => { const s = setup(); await writeArticle(doc("solo"), { paths: s.paths }); const run = capture(); assert.equal(await respellMain("solo", { ...FLAGS, bracket: true, apply: true }, s, run.out), 0); const r = JSON.parse(readFileSync(path.join(s.paths.articlesDir, "solo", "report.json"), "utf8")); assert.equal(r.citations.a.quote, "my own [K-Cup] machine"); assert.deepEqual(readdirSync(path.join(s.paths.articlesDir, "solo")).sort(), ["report.json"]); assert.ok(existsSync(path.join(s.projectsRoot, "solo", "revisions"))); }); test("usage and unknowns: no --heard or --reads, a bad --iteration, no such article", async () => { const s = setup(); await writeArticle(doc("x"), { paths: s.paths }); assert.equal(await respellMain("x", { reads: "K-Cup" }, s, capture().out), 2); assert.equal(await respellMain("x", { heard: "cake cup" }, s, capture().out), 2); assert.equal(await respellMain("x", { ...FLAGS, iteration: "zero" }, s, capture().out), 2); assert.equal(await respellMain("x", { ...FLAGS, iteration: "3" }, s, capture().out), 2); assert.equal(await respellMain("nope", FLAGS, s, capture().out), 2); });