// `archilyzer reports import-sweep`: a report made outside the editor's // report-draft jobs becomes the next iteration of its article, a dry run by // default. A temp corpus only — never the real store. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test bin/reports-import-sweep.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; const ROOT = mkdtempSync(path.join(tmpdir(), "import-sweep-")); 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_INDEX_DIR: path.join(ROOT, ".export-index"), }); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { writeArticle } = await import("../lib/report/registry"); const { currentIteration, readIteration, readIterationFile } = await import("../lib/report/iterations"); const imp = await import("./reports-import-sweep"); const paths = getPaths(); const KIT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "testing", "draft"); const BASIC = JSON.parse(readFileSync(path.join(KIT, "reports", "basic.report.json"), "utf8")) as Record; const SWEEP = readFileSync(path.join(KIT, "sweeps", "basic.md"), "utf8"); const IN = path.join(ROOT, "in"); mkdirSync(IN, { recursive: true }); const reportFile = path.join(IN, "report.json"); writeFileSync(reportFile, JSON.stringify({ ...BASIC, id: "some-other-id", audience: "public" })); const sweepFile = path.join(IN, "sweep.md"); writeFileSync(sweepFile, SWEEP); const silent = () => { const lines: string[] = []; return { lines, out: { log: (s: string) => lines.push(s), error: (s: string) => lines.push(s) } }; }; const base = { reportFile, question: "What does the baker say about the bakery?", paths } as const; test("a dry run plans the next iteration and writes nothing", async () => { const { lines, out } = silent(); const code = await imp.importSweepMain({ ...base, article: "baker-sweep", sweepFile, channels: ["synth-kitchen"], apply: false }, out); assert.equal(code, 0); assert.match(lines.join("\n"), /DRY RUN/); assert.match(lines.join("\n"), /i1 of "baker-sweep", a NEW article in the making/); assert.match(lines.join("\n"), /its id "some-other-id" becomes "baker-sweep"/); assert.equal(existsSync(path.join(paths.articlesDir, "baker-sweep")), false); }); test("apply writes a done sweep iteration, id set, audience dropped, provenance named, never current", async () => { const { out } = silent(); const code = await imp.importSweepMain( { ...base, article: "baker-sweep", sweepFile, channels: ["synth-kitchen"], minutes: 42, model: "claude-opus (Claude Code)", apply: true, now: () => new Date("2026-10-15T12:00:00Z") }, out, ); assert.equal(code, 0); const it = await readIteration("baker-sweep", 1, paths); assert.ok(it); assert.equal(it.kind, "sweep"); assert.equal(it.status, "done"); assert.equal(it.model, "claude-opus (Claude Code)"); assert.equal(it.question, "What does the baker say about the bakery?"); assert.deepEqual(it.scope?.channels, ["synth-kitchen"]); assert.equal(it.minutes, 42); assert.deepEqual((it as Record).imported, { from: "claude-code", at: "2026-10-15T12:00:00.000Z", report: "report.json" }); const report = JSON.parse((await readIterationFile("baker-sweep", 1, "report.json", paths)) ?? "{}"); assert.equal(report.id, "baker-sweep"); assert.equal(report.audience, undefined); assert.equal(await readIterationFile("baker-sweep", 1, "sweep.md", paths), SWEEP); // Never made current: an article in the making stays one. assert.equal((await currentIteration("baker-sweep", paths)).report, null); assert.equal(existsSync(path.join(paths.articlesDir, "baker-sweep", "report.json")), false); }); test("a second import is the next iteration", async () => { const { lines, out } = silent(); assert.equal(await imp.importSweepMain({ ...base, article: "baker-sweep", apply: true }, out), 0); assert.match(lines.join("\n"), /i2 of "baker-sweep"/); const it = await readIteration("baker-sweep", 2, paths); assert.equal(it?.model, imp.DEFAULT_IMPORT_MODEL); assert.equal(await readIterationFile("baker-sweep", 2, "sweep.md", paths), null); }); test("an article the registry has gets an iteration; its report is untouched", async () => { await writeArticle({ ...BASIC, id: "baker-live" }, { paths }); const before = readFileSync(path.join(paths.articlesDir, "baker-live", "report.json"), "utf8"); const { lines, out } = silent(); assert.equal(await imp.importSweepMain({ ...base, article: "baker-live", apply: true }, out), 0); assert.match(lines.join("\n"), /an article the registry has \(it is NOT made current\)/); assert.ok(await readIteration("baker-live", 1, paths)); assert.equal(readFileSync(path.join(paths.articlesDir, "baker-live", "report.json"), "utf8"), before); }); test("refusals: a bad id, no question, a bad channel, a missing file, a report that does not parse", async () => { const cases: [Partial[0]>, RegExp][] = [ [{ article: "Not An Id" }, /is not an article id/], [{ article: "new" }, /is not an article id/], [{ article: "baker-x", question: " " }, /names its question/], [{ article: "baker-x", channels: ["bad slug!"] }, /is not a channel slug/], [{ article: "baker-x", reportFile: path.join(IN, "nope.json") }, /no such file/], [{ article: "baker-x", sweepFile: path.join(IN, "nope.md") }, /no such file/], ]; const notReport = path.join(IN, "not-a-report.json"); writeFileSync(notReport, JSON.stringify({ format: "archilyzer-report", version: 1, title: "x" })); cases.push([{ article: "baker-x", reportFile: notReport }, /does not parse/]); for (const [o, why] of cases) { const { lines, out } = silent(); const code = await imp.importSweepMain({ ...base, apply: true, ...o } as Parameters[0], out); assert.equal(code, 1, JSON.stringify(o)); assert.match(lines.join("\n"), why); } assert.equal(existsSync(path.join(paths.articlesDir, "baker-x")), false); }); test("the CLI row: usage errors exit 2", async () => { const err = console.error; console.error = () => {}; try { assert.equal(await imp.importSweepRow(undefined, { article: "a", question: "q" }), 2); assert.equal(await imp.importSweepRow(reportFile, { question: "q" }), 2); assert.equal(await imp.importSweepRow(reportFile, { article: "a", question: "q", minutes: "soon" }), 2); } finally { console.error = err; } }); test("local only (demo epic D5): import-sweep and import-local-runs are refused before they read or write anything", async () => { const { importSweepRow } = await import("./reports-import-sweep"); const { importRow } = await import("./reports-import-local-runs"); const was = process.env.ARCHILYZER_LOCAL_ONLY; const errors: string[] = []; const err = console.error; console.error = (m: string) => errors.push(m); process.env.ARCHILYZER_LOCAL_ONLY = "1"; try { assert.equal(await importSweepRow("/nonexistent/report.json", { article: "lo-import", question: "q", apply: true }), 1); assert.equal(await importRow({ apply: true }), 1); } finally { console.error = err; if (was === undefined) delete process.env.ARCHILYZER_LOCAL_ONLY; else process.env.ARCHILYZER_LOCAL_ONLY = was; } assert.deepEqual(errors, [ "reports import-sweep: refused — this instance is local only (ARCHILYZER_LOCAL_ONLY=1), and a report made elsewhere is not imported into it", "reports import-local-runs: refused — this instance is local only (ARCHILYZER_LOCAL_ONLY=1), and a report made elsewhere is not imported into it", ]); });