// Deleting a project or a take (release 27 follow-up): the plan reads only, // the delete refuses what is in use, never leaves the roots, and follows a link // only to its own media mirror. Temp trees only. import { test, after } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { DeleteRefused, deleteBlocked, deleteProject, deleteTake, planProjectDelete, planTakeDelete } from "./delete.mjs"; const ROOTS = []; after(() => { for (const r of ROOTS) rmSync(r, { recursive: true, force: true }); }); const put = (file, text = "x") => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, text); }; function tree() { const root = mkdtempSync(path.join(tmpdir(), "umtool-delete-")); ROOTS.push(root); const reportsRoot = path.join(root, "reports"); const mediaRoot = path.join(root, "media"); const articlesDir = path.join(root, "transcripts", "reports"); mkdirSync(reportsRoot, { recursive: true }); mkdirSync(mediaRoot, { recursive: true }); return { root, reportsRoot, mediaRoot, articlesDir, projectsRoot: path.join(reportsRoot, "articles") }; } const project = (t, id, manifest = { slug: path.basename(id) }) => { const dir = path.join(t.reportsRoot, id); put(path.join(dir, "video.manifest.json"), JSON.stringify(manifest)); put(path.join(dir, "out", "final.mp4"), "f".repeat(2000)); return { id, dir }; }; test("a project: planned with sizes, then removed; its out/ media mirror goes too, another link's target stays", async () => { const t = tree(); const p = project(t, "folder/cut"); rmSync(path.join(p.dir, "out"), { recursive: true }); const mirror = path.join(t.mediaRoot, "folder", "cut", "out"); put(path.join(mirror, "final.mp4"), "m".repeat(4000)); symlinkSync(mirror, path.join(p.dir, "out")); const elsewhere = path.join(t.root, "elsewhere"); put(path.join(elsewhere, "keep.mp4")); symlinkSync(elsewhere, path.join(p.dir, "clips")); const sibling = project(t, "folder/other"); const opts = { reportsRoot: t.reportsRoot, mediaRoot: t.mediaRoot }; const plan = await planProjectDelete(p, opts); assert.deepEqual(plan.refusals, []); assert.deepEqual(plan.remove.map((e) => e.path), [p.dir, mirror]); assert.equal(plan.bytes, plan.remove[0].bytes + 4000); assert.ok(plan.kept.some((k) => k.path === elsewhere)); assert.ok(existsSync(p.dir), "a plan removes nothing"); const res = await deleteProject(p, opts); assert.deepEqual(res.failed, []); assert.equal(existsSync(p.dir), false); assert.equal(existsSync(mirror), false); assert.ok(existsSync(path.join(elsewhere, "keep.mp4"))); assert.ok(existsSync(sibling.dir)); assert.ok(existsSync(path.join(t.reportsRoot, "folder")), "a plain folder is not tidied away"); }); test("a project is refused while a job works in it, outside the root, or as a link", async () => { const t = tree(); const opts = { reportsRoot: t.reportsRoot, mediaRoot: t.mediaRoot }; const p = project(t, "busy"); const busy = await planProjectDelete(p, { ...opts, busyJob: "build busy" }); assert.equal(deleteBlocked(busy, true), true); await assert.rejects(deleteProject(p, { ...opts, busyJob: "build busy", force: true }), DeleteRefused); assert.ok(existsSync(p.dir)); const out = { id: "x", dir: path.join(t.root, "x") }; mkdirSync(out.dir); assert.match((await planProjectDelete(out, opts)).refusals[0].reason, /not inside the reports root/); assert.match((await planProjectDelete({ id: "root", dir: t.reportsRoot }, opts)).refusals[0].reason, /not inside the reports root/); const real = path.join(t.root, "real-project"); put(path.join(real, "video.manifest.json"), "{}"); symlinkSync(real, path.join(t.reportsRoot, "linked")); const linked = await planProjectDelete({ id: "linked", dir: path.join(t.reportsRoot, "linked") }, opts); assert.match(linked.refusals[0].reason, /is a link/); assert.deepEqual(linked.remove, []); }); test("an iteration project: refused as the current video or while it runs; else its record is named and the emptied folder goes", async () => { const t = tree(); const opts = { reportsRoot: t.reportsRoot, mediaRoot: t.mediaRoot, projectsRoot: t.projectsRoot, articlesDir: t.articlesDir }; const its = path.join(t.articlesDir, "art", "iterations"); const iteration = (n, status) => put(path.join(its, String(n), "iteration.json"), JSON.stringify({ article: "art", n, status, steps: [] })); iteration(1, "done"); iteration(2, "running"); iteration(3, "done"); put(path.join(its, "current.json"), JSON.stringify({ report: 1, video: 1 })); const p1 = project(t, "articles/art/i1"); const p2 = project(t, "articles/art/i2"); const p3 = project(t, "articles/art/i3"); assert.match((await planProjectDelete(p1, opts)).refusals[0].reason, /current video/); assert.match((await planProjectDelete(p2, opts)).refusals[0].reason, /i2 of "art" is running/); const plan3 = await planProjectDelete(p3, opts); assert.deepEqual(plan3.refusals, []); assert.ok(plan3.kept.some((k) => /i3's iteration record/.test(k.what))); await deleteProject(p3, opts); assert.equal(existsSync(p3.dir), false); assert.ok(existsSync(path.join(t.projectsRoot, "art")), "the folder still holds i1 and i2"); }); test("a project linked to an article with a video is refused unless forced", async () => { const t = tree(); const opts = { reportsRoot: t.reportsRoot, mediaRoot: t.mediaRoot, articleVideo: async (ref) => ref === "articles/withvideo" }; const p = project(t, "linked-video", { slug: "x", article: "articles/withvideo" }); const plan = await planProjectDelete(p, opts); assert.equal(plan.refusals.length, 1); assert.equal(plan.refusals[0].forceable, true); await assert.rejects(deleteProject(p, opts), DeleteRefused); await deleteProject(p, { ...opts, force: true }); assert.equal(existsSync(p.dir), false); const q = project(t, "linked-plain", { slug: "y", article: "articles/novideo" }); assert.deepEqual((await planProjectDelete(q, opts)).refusals, []); }); test("a take: removed with its renders; the one the project's manifest is refused unless forced; its verdict is kept", async () => { const t = tree(); const opts = { reportsRoot: t.reportsRoot, mediaRoot: t.mediaRoot }; const p = project(t, "takes-proj", { slug: "p", v: 1 }); const take = (id, manifest) => { const dir = path.join(p.dir, "takes", id); put(path.join(dir, "take.json"), JSON.stringify({ id, group: "g", order: 1, label: id, kind: "similar", preview: "preview.mp4" })); put(path.join(dir, "preview.mp4"), "p".repeat(500)); put(path.join(dir, "video.manifest.json"), manifest); return dir; }; const chosen = take("chosen", readFileSync(path.join(p.dir, "video.manifest.json"), "utf8")); const other = take("other", JSON.stringify({ slug: "p", v: 2 })); put(path.join(p.dir, "takes", "verdicts.json"), JSON.stringify({ other: { verdict: "no", note: "", at: "" } })); const refused = await planTakeDelete(p, "chosen", opts); assert.equal(refused.refusals[0].forceable, true); assert.match(refused.refusals[0].reason, /the deliverable is built from it/); await assert.rejects(deleteTake(p, "chosen", opts), DeleteRefused); const plan = await planTakeDelete(p, "other", opts); assert.deepEqual(plan.refusals, []); assert.deepEqual(plan.remove.map((e) => e.label), ["takes-proj/takes/other/"]); await deleteTake(p, "other", opts); assert.equal(existsSync(other), false); assert.ok(existsSync(chosen)); assert.match(readFileSync(path.join(p.dir, "takes", "verdicts.json"), "utf8"), /"other"/, "the verdict row is kept"); await deleteTake(p, "chosen", { ...opts, force: true }); assert.equal(existsSync(chosen), false); assert.ok(existsSync(path.join(p.dir, "video.manifest.json"))); await assert.rejects(planTakeDelete(p, "chosen", opts), /has no take chosen/); await assert.rejects(planTakeDelete(p, "../x", opts), /not a take id/); });