// Deleting an article or an iteration (release 27 follow-up, lane I): a plan // that reads only, then the delete — refused for what is in use, never outside // the known roots. Temp stores only. // // Run with: node_modules/.bin/tsx --test lib/report/delete.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { configureArticle, readArticlesIndex, writeArticle } from "./registry"; import { createIteration, currentIteration, makeCurrent, projectOf, setCurrentVideo, updateIteration, writeIterationFile, } from "./iterations"; import { DeleteRefusedError, deleteArticle, deleteBlocked, deleteIteration, formatDeletePlan, planArticleDelete, planIterationDelete, } from "./delete"; const ROOTS: string[] = []; after(() => { for (const r of ROOTS) rmSync(r, { recursive: true, force: true }); }); function setup() { const root = mkdtempSync(path.join(tmpdir(), "article-delete-")); ROOTS.push(root); const paths = { articlesDir: path.join(root, "transcripts", "reports"), exportIndexDir: path.join(root, ".export-index"), sitesDir: path.join(root, "transcripts", "sites"), }; mkdirSync(paths.articlesDir, { recursive: true }); mkdirSync(paths.sitesDir, { recursive: true }); const reportsRoot = path.join(root, "reports"); const projectsRoot = path.join(reportsRoot, "articles"); const mediaRoot = path.join(root, "media"); mkdirSync(projectsRoot, { recursive: true }); mkdirSync(mediaRoot, { recursive: true }); return { root, paths, projectsRoot, reportsRoot, mediaRoot, opts: { paths, projectsRoot } }; } const doc = (id: string, over: Record = {}) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, sections: [{ id: "s1", title: "One", body: "Plain words." }], ...over, }); const NOW = new Date("2026-10-13T12:00:00.000Z"); const put = (file: string, text = "x") => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, text); }; type Setup = ReturnType; async function iteration(s: Setup, id: string, over: { parent?: number; copyOf?: number; report?: boolean; status?: "done" | "running" | "queued" } = {}) { const it = await createIteration( id, over.parent ? { kind: "revise-report", model: "qwen3:8b", parent: over.parent, feedback: "tighter" } : over.copyOf ? { kind: "sweep", model: "qwen3:8b", copyOf: over.copyOf } : { kind: "sweep", model: "qwen3:8b", question: "What was said?" }, s.paths, NOW, ); if (over.report !== false) await writeIterationFile(id, it.n, "report.json", doc(id, { title: `i${it.n}` }), s.paths); await updateIteration(id, it.n, { status: over.status ?? "done", projectDir: projectOf(s.projectsRoot, id, it.n) }, s.paths, NOW); put(path.join(projectOf(s.projectsRoot, id, it.n), "video.manifest.json"), "{}"); put(path.join(projectOf(s.projectsRoot, id, it.n), "out", "final.mp4"), "y".repeat(1000)); return it.n; } test("a draft with iterations, projects and staged exports: planned with sizes, then all of it goes", async () => { const s = setup(); await writeArticle(doc("draft-a"), { paths: s.paths }); put(path.join(s.paths.articlesDir, "draft-a", "video.mp4"), "v".repeat(5000)); put(path.join(s.paths.articlesDir, "draft-a", "notes.json"), "{}"); await iteration(s, "draft-a"); await iteration(s, "draft-a", { parent: 1 }); put(path.join(s.paths.exportIndexDir, "articles", "report-exports", "draft-a", "report.html"), "

"); // Another article's files are never touched. await writeArticle(doc("other"), { paths: s.paths }); put(path.join(s.projectsRoot, "other", "i1", "video.manifest.json"), "{}"); const plan = await planArticleDelete("draft-a", s.opts); assert.deepEqual(plan.refusals, []); assert.equal(deleteBlocked(plan), false); assert.deepEqual( plan.remove.map((e) => e.label), ["reports/draft-a/", "articles/draft-a/i1/", "articles/draft-a/i2/", ".export-index/articles/report-exports/draft-a/"], ); const dir = plan.remove[0]; assert.equal(dir.parts?.[0].name, "video.mp4", "the biggest part first"); assert.ok(dir.parts?.some((p) => p.name === "iterations/")); assert.ok(plan.bytes >= 5000 + 2000); assert.ok(plan.kept.some((k) => /usage ledger/.test(k.what))); assert.ok(formatDeletePlan(plan).some((l) => l.includes("remove reports/draft-a/"))); // A plan writes nothing. assert.ok(existsSync(path.join(s.paths.articlesDir, "draft-a", "report.json"))); const res = await deleteArticle("draft-a", s.opts, NOW); assert.deepEqual(res.failed, []); assert.equal(existsSync(path.join(s.paths.articlesDir, "draft-a")), false); assert.equal(existsSync(path.join(s.projectsRoot, "draft-a")), false, "the emptied projects folder goes too"); assert.equal(existsSync(path.join(s.paths.exportIndexDir, "articles", "report-exports", "draft-a")), false); assert.deepEqual(readdirSync(s.paths.articlesDir).filter((n) => !n.startsWith(".registry")).sort(), ["other"], "no .deleting- leftover"); assert.ok(existsSync(path.join(s.projectsRoot, "other", "i1", "video.manifest.json"))); await assert.rejects(planArticleDelete("draft-a", s.opts), /no article "draft-a"/); }); test("an article in the making (no report.json) is deleted like any other", async () => { const s = setup(); await iteration(s, "pending-b", { report: false }); const plan = await planArticleDelete("pending-b", s.opts); assert.equal(plan.hasReport, false); assert.match(plan.remove[0].what, /in the making/); await deleteArticle("pending-b", s.opts, NOW); assert.equal(existsSync(path.join(s.paths.articlesDir, "pending-b")), false); }); test("a published article is refused unless forced; force unpublishes it through the registry", async () => { const s = setup(); await writeArticle(doc("pub"), { paths: s.paths }); await writeArticle(doc("stays"), { paths: s.paths }); await configureArticle("pub", { published: true }, s.paths); await configureArticle("stays", { published: true }, s.paths); const plan = await planArticleDelete("pub", s.opts); assert.equal(plan.refusals.length, 1); assert.equal(plan.refusals[0].forceable, true); assert.match(plan.refusals[0].reason, /is published/); await assert.rejects(deleteArticle("pub", s.opts, NOW), (e: unknown) => e instanceof DeleteRefusedError && /published/.test(e.message)); assert.ok(existsSync(path.join(s.paths.articlesDir, "pub", "report.json")), "refused: nothing written"); assert.deepEqual((await readArticlesIndex(s.paths)).published, ["pub", "stays"]); const forced = await planArticleDelete("pub", { ...s.opts, force: true }); assert.ok(forced.steps.some((l) => /unpublish "pub"/.test(l))); assert.equal(deleteBlocked(forced, true), false); await deleteArticle("pub", { ...s.opts, force: true }, NOW); assert.deepEqual((await readArticlesIndex(s.paths)).published, ["stays"]); assert.equal(existsSync(path.join(s.paths.articlesDir, "pub")), false); }); test("a queued or running iteration refuses the article's delete, force or not", async () => { const s = setup(); await writeArticle(doc("busy"), { paths: s.paths }); await iteration(s, "busy", { status: "running" }); const plan = await planArticleDelete("busy", { ...s.opts, force: true }); assert.equal(deleteBlocked(plan, true), true); assert.match(plan.refusals[0].reason, /i1 is running/); await assert.rejects(deleteArticle("busy", { ...s.opts, force: true }, NOW), DeleteRefusedError); // A narration step running counts too. await updateIteration("busy", 1, { status: "done", steps: [{ task: "narrate", status: "running", jobId: "j9" }] }, s.paths, NOW); assert.match((await planArticleDelete("busy", s.opts)).refusals[0].reason, /narrate step is running \(job j9\)/); }); test("site pages: the site's own report of the same id is kept; a page with no report of its own refuses unless forced", async () => { const s = setup(); await writeArticle(doc("dup"), { paths: s.paths }); put(path.join(s.paths.sitesDir, "owner", "site.json"), JSON.stringify({ siteTitle: "Owner", pages: ["dup"] })); put(path.join(s.paths.sitesDir, "owner", "reports", "dup", "report.json"), JSON.stringify(doc("dup"))); put(path.join(s.paths.sitesDir, "stale", "site.json"), JSON.stringify({ siteTitle: "Stale", pages: ["dup", "keep-me"] })); const plan = await planArticleDelete("dup", s.opts); assert.equal(plan.refusals.length, 1); assert.match(plan.refusals[0].reason, /site stale lists a page "dup"/); assert.ok(plan.kept.some((k) => /site owner's page "dup"/.test(k.what))); await deleteArticle("dup", { ...s.opts, force: true }, NOW); const stale = JSON.parse(readFileSync(path.join(s.paths.sitesDir, "stale", "site.json"), "utf8")) as { pages: string[] }; assert.deepEqual(stale.pages, ["keep-me"]); const owner = JSON.parse(readFileSync(path.join(s.paths.sitesDir, "owner", "site.json"), "utf8")) as { pages: string[] }; assert.deepEqual(owner.pages, ["dup"], "the owner's page is its own report: untouched"); assert.ok(existsSync(path.join(s.paths.sitesDir, "owner", "reports", "dup", "report.json"))); }); test("links: a project's out/ linked to its media mirror goes with its target; any other link's target is kept", async () => { const s = setup(); const media = { reportsRoot: s.reportsRoot, mediaRoot: s.mediaRoot }; await writeArticle(doc("tiered"), { paths: s.paths }); const n = await iteration(s, "tiered"); const proj = projectOf(s.projectsRoot, "tiered", n); rmSync(path.join(proj, "out"), { recursive: true }); const mirror = path.join(s.mediaRoot, "articles", "tiered", "i1", "out"); put(path.join(mirror, "final.mp4"), "m".repeat(3000)); symlinkSync(mirror, path.join(proj, "out")); // A link to somewhere else: removed as a link, its target untouched. const elsewhere = path.join(s.root, "elsewhere"); put(path.join(elsewhere, "keep.mp4"), "k"); symlinkSync(elsewhere, path.join(proj, "clips")); // Without the media root named, the mirror is not known: kept. const blind = await planArticleDelete("tiered", s.opts); assert.ok(blind.kept.some((k) => k.path === mirror)); const plan = await planArticleDelete("tiered", { ...s.opts, media }); const m = plan.remove.find((e) => e.path === mirror); assert.ok(m, "the mirror is listed"); assert.equal(m.bytes, 3000); assert.ok(plan.kept.some((k) => k.path === elsewhere)); await deleteArticle("tiered", { ...s.opts, media }, NOW); assert.equal(existsSync(mirror), false); assert.ok(existsSync(path.join(elsewhere, "keep.mp4"))); assert.ok(existsSync(s.mediaRoot), "the media root itself stays"); }); test("an article directory that is a link is refused; a project recorded outside the root is kept", async () => { const s = setup(); const real = path.join(s.root, "real-dir"); put(path.join(real, "report.json"), JSON.stringify(doc("linked"))); symlinkSync(real, path.join(s.paths.articlesDir, "linked")); const plan = await planArticleDelete("linked", s.opts); assert.equal(deleteBlocked(plan, true), true); assert.match(plan.refusals[0].reason, /is a link/); await writeArticle(doc("far"), { paths: s.paths }); const it = await createIteration("far", { kind: "sweep", model: "qwen3:8b", question: "Q?" }, s.paths, NOW); const outside = path.join(s.root, "outside-project"); put(path.join(outside, "video.manifest.json"), "{}"); await updateIteration("far", it.n, { status: "done", projectDir: outside }, s.paths, NOW); const far = await planArticleDelete("far", s.opts); assert.ok(far.kept.some((k) => k.path === outside && /outside the projects root/.test(k.why))); await deleteArticle("far", s.opts, NOW); assert.ok(existsSync(path.join(outside, "video.manifest.json"))); }); test("a cut delete's parked directory is listed and removed by the next delete", async () => { const s = setup(); await writeArticle(doc("cut"), { paths: s.paths }); put(path.join(s.paths.articlesDir, ".deleting-cut-20261013T000000Z-1", "video.mp4"), "v"); const plan = await planArticleDelete("cut", s.opts); assert.ok(plan.remove.some((e) => e.label === "reports/.deleting-cut-20261013T000000Z-1/")); await deleteArticle("cut", s.opts, NOW); assert.deepEqual(readdirSync(s.paths.articlesDir).filter((n) => n !== ".registry.lock"), []); }); test("an iteration: refused when current, the report's source, the video, busy or a parent; a leaf goes with its project", async () => { const s = setup(); await writeArticle(doc("its"), { paths: s.paths }); await iteration(s, "its"); // i1 await iteration(s, "its", { parent: 1 }); // i2 revises i1 await iteration(s, "its", { copyOf: 1 }); // i3 copy of i1 await iteration(s, "its", { parent: 1 }); // i4 await iteration(s, "its", { status: "running" }); // i5 await makeCurrent("its", 2, s.paths, NOW); await setCurrentVideo("its", 4, s.paths, NOW); const reasons = async (n: number) => (await planIterationDelete("its", n, s.opts)).refusals.map((r) => r.reason).join(" | "); assert.match(await reasons(1), /i2 revises it; i3 is a copy of it; i4 revises it: delete those first/); assert.match(await reasons(2), /i2 is the current article/); assert.match(await reasons(4), /i4 is the article's current video/); assert.match(await reasons(5), /i5 is running/); for (const n of [1, 2, 4, 5]) await assert.rejects(deleteIteration("its", n, s.opts, NOW), DeleteRefusedError); const plan = await planIterationDelete("its", 3, s.opts); assert.deepEqual(plan.refusals, []); assert.deepEqual(plan.remove.map((e) => e.label), ["reports/its/iterations/3/", "articles/its/i3/"]); await deleteIteration("its", 3, s.opts, NOW); assert.equal(existsSync(path.join(s.paths.articlesDir, "its", "iterations", "3")), false); assert.equal(existsSync(projectOf(s.projectsRoot, "its", 3)), false); assert.ok(existsSync(projectOf(s.projectsRoot, "its", 1))); assert.deepEqual(readdirSync(path.join(s.paths.articlesDir, "its", "iterations")).filter((n) => n.startsWith(".")), []); // The current is kept, and the number is retired. const cur = await currentIteration("its", s.paths); assert.deepEqual([cur.report, cur.video, cur.highest], [2, 4, 5]); await assert.rejects(planIterationDelete("its", 3, s.opts), /has no iteration i3/); // The report's source: i6 a video revision of i2 made current reads i2's report. const i6 = await createIteration("its", { kind: "revise-video", model: "qwen3:8b", parent: 2, feedback: "shorter" }, s.paths, NOW); assert.equal(i6.n, 6, "never 3 again"); await updateIteration("its", 6, { status: "done" }, s.paths, NOW); await makeCurrent("its", 6, s.paths, NOW); assert.match(await reasons(2), /report\.json is i2's \(current: i6\)/); }); test("the highest number survives deleting the newest iteration: the next is not reused", async () => { const s = setup(); await iteration(s, "nums", { report: false }); // i1 await iteration(s, "nums", { report: false }); // i2 await deleteIteration("nums", 2, s.opts, NOW); const next = await createIteration("nums", { kind: "sweep", model: "qwen3:8b", question: "Q?" }, s.paths, NOW); assert.equal(next.n, 3); await updateIteration("nums", 3, { status: "cancelled" }, s.paths, NOW); // An article in the making with its last iteration gone: the project folder goes once empty. await deleteIteration("nums", 3, s.opts, NOW); await deleteIteration("nums", 1, s.opts, NOW); assert.equal(existsSync(path.join(s.projectsRoot, "nums")), false); assert.ok(lstatSync(path.join(s.paths.articlesDir, "nums", "iterations")).isDirectory()); }); test("ids and numbers are checked before anything is read", async () => { const s = setup(); await assert.rejects(planArticleDelete("../etc", s.opts), /not an article id/); await assert.rejects(planIterationDelete("ok-id", 0, s.opts), /not an iteration number/); await assert.rejects(planIterationDelete("ok-id", 1, s.opts), /has no iteration i1/); });