// `archilyzer reports move-to-articles` (release 26 A7's script), over a temp // corpus only: a dry run writes nothing; --apply moves each listed report // (history-git and notes.json with it) to reports//, private, published in // the sites' order (one with problems as a draft), off its site; a kept page // stays; refusals come before anything moves. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test bin/move-reports-to-articles.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "move-to-articles-")); Object.assign(process.env, { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SITES_DIR: path.join(ROOT, "transcripts", "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), }); delete process.env.ARTICLES_DIR; after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { main } = await import("./move-reports-to-articles"); const paths = getPaths(); const put = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, typeof value === "string" ? value : JSON.stringify(value, null, 2)); }; const readJson = >(file: string): T => JSON.parse(readFileSync(file, "utf8")) as T; const doc = (id: string, over: Record = {}) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Fixture ${id}`, sections: [{ id: "s", title: "S", body: "Words." }], ...over, }); // `key`: site-a is written with the pre-release-26 key, which reads as `pages`. function site(id: string, pages: string[], extra: Record = {}, key: "pages" | "reports" = "pages") { put(path.join(paths.sitesDir, id, "site.json"), { siteId: id, siteTitle: id, groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [], [key]: pages, ...extra, }); } const reportDir = (s: string, id: string) => path.join(paths.sitesDir, s, "reports", id); function capture() { const lines: string[] = []; return { text: () => lines.join("\n"), out: { log: (s: string) => lines.push(s), error: (s: string) => lines.push(s) } } as const; } // Two sites: a report-only private one with two listed reports (one with a // problem) and a draft; a full one with a page it keeps and one to move. site("site-a", ["alpha", "beta"], { search: false, audience: "private" }, "reports"); put(path.join(reportDir("site-a", "alpha"), "report.json"), doc("alpha")); put(path.join(reportDir("site-a", "alpha"), "notes.json"), { format: "umtool-notes", version: 1, notes: [] }); put(path.join(reportDir("site-a", "alpha"), "history-git", "HEAD"), "ref: refs/heads/main\n"); put(path.join(reportDir("site-a", "alpha"), "stills", "a.png"), "png"); put(path.join(reportDir("site-a", "beta"), "report.json"), doc("beta", { summary: "see [x](cite:nobody)" })); put(path.join(reportDir("site-a", "draft-one"), "report.json"), doc("draft-one")); site("site-b", ["page-kept", "gamma"]); put(path.join(reportDir("site-b", "page-kept"), "report.json"), doc("page-kept")); put(path.join(reportDir("site-b", "gamma"), "report.json"), doc("gamma", { audience: "public" })); const snapshot = () => { const out: string[] = []; const walk = (d: string, rel: string) => { for (const e of readdirSync(d, { withFileTypes: true })) { const r = `${rel}/${e.name}`; if (e.isDirectory()) walk(path.join(d, e.name), r); else out.push(`${r}:${readFileSync(path.join(d, e.name), "utf8")}`); } }; walk(paths.transcriptsDir, ""); return out.sort().join("\n"); }; test("refusals before anything moves: a --keep that names nothing, an id reports/ already holds", async () => { const before = snapshot(); const c = capture(); assert.equal(await main(["--keep", "site-b/nope", "--apply"], paths, c.out), 1); assert.match(c.text(), /REFUSED --keep site-b\/nope: no site lists that report/); put(path.join(paths.articlesDir, "gamma", "report.json"), doc("gamma")); const c2 = capture(); assert.equal(await main(["--keep", "site-b/page-kept", "--apply"], paths, c2.out), 1); assert.match(c2.text(), /REFUSED site-b\/gamma: reports\/gamma\/ already exists/); rmSync(path.join(paths.articlesDir, "gamma"), { recursive: true }); assert.equal(snapshot(), before, "nothing written"); }); test("a dry run prints the plan and writes nothing", async () => { const before = snapshot(); const c = capture(); assert.equal(await main(["--keep", "site-b/page-kept"], paths, c.out), 0); const text = c.text(); assert.match(text, /move {3}site-a\/alpha → reports\/alpha\/ {2}published, audience private/); assert.match(text, /move {3}site-a\/beta → reports\/beta\/ {2}draft \(1 problem\(s\) in report\.json\)/); assert.match(text, /keep {3}site-b\/page-kept/); assert.match(text, /retire site-a: search off and no pages left — saved with search on, as an empty site; delete it on \/sites/); assert.match(text, /3 to move \(2 published\), 1 kept, 0 refusal/); assert.match(text, /dry run: nothing moved/); assert.equal(snapshot(), before); }); test("--apply: moved whole, private, published in the sites' order, off the site; the kept page stays", async () => { const c = capture(); assert.equal(await main(["--keep", "site-b/page-kept", "--apply"], paths, c.out), 0, c.text()); const art = (id: string, ...p: string[]) => path.join(paths.articlesDir, id, ...p); for (const f of ["report.json", "notes.json", "history-git/HEAD", "stills/a.png"]) assert.ok(existsSync(art("alpha", f)), f); assert.equal(existsSync(reportDir("site-a", "alpha")), false); assert.deepEqual(readJson(path.join(paths.articlesDir, "index.json")), { published: ["alpha", "gamma"] }); for (const id of ["alpha", "beta", "gamma"]) assert.equal(readJson(art(id, "report.json")).audience, "private", id); // The emptied search-off site is retired: no pages, search on (a save // refuses search off with none); the legacy key is gone from both files. const a = readJson(path.join(paths.sitesDir, "site-a", "site.json")); assert.equal("pages" in a || "reports" in a || "search" in a, false, JSON.stringify(a)); assert.equal(a.audience, "private"); const b = readJson(path.join(paths.sitesDir, "site-b", "site.json")); assert.deepEqual(b.pages, ["page-kept"]); assert.equal("reports" in b, false); assert.ok(existsSync(path.join(reportDir("site-b", "page-kept"), "report.json"))); // The unlisted draft stays without --drafts. assert.ok(existsSync(path.join(reportDir("site-a", "draft-one"), "report.json"))); // Again: nothing left to move but with --drafts the draft. const again = capture(); assert.equal(await main(["--keep", "site-b/page-kept", "--drafts", "--apply"], paths, again.out), 0, again.text()); assert.match(again.text(), /moved site-a\/draft-one → reports\/draft-one\/ \(draft\)/); assert.deepEqual(readJson(path.join(paths.articlesDir, "index.json")), { published: ["alpha", "gamma"] }); });