// `archilyzer reports prepare|export|check|verify-quotes|attach-video // --article ` (release 26 A6): the same work as a site's, over the // ARTICLES — transcripts/reports//, staged under .export-index/articles/, // any channel of the corpus citable. A temp corpus only. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test bin/reports-article.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "reports-article-")); 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"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), }); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { articleFile, configureArticle, readArticlesIndex } = await import("../lib/report/registry"); const { checkMain, verifyQuotesMain } = await import("./reports-check"); const { main: prepareMain } = await import("./reports-prepare"); const { main: exportMain } = await import("./reports-export"); const { attachVideoMain } = await import("./reports-attach-video"); const { readRevisionHead } = await import("../publish/reportHistory"); const { createIteration, makeCurrent, updateIteration, writeIterationFile } = await import("../lib/report/iterations"); const paths = getPaths(); const CHAN = "lone"; const STAGING = path.join(paths.exportIndexDir, "articles"); const writeJson = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value, null, 2)); }; const readJson = >(file: string): T => JSON.parse(readFileSync(file, "utf8")) as T; const ts = (s: number) => new Date(s * 1000).toISOString().slice(11, 23); const vtt = (cues: [number, number, string][]) => "WEBVTT\nKind: captions\nLanguage: en\n\n" + cues.map(([a, b, text]) => `${ts(a)} --> ${ts(b)} align:start position:0%\n${text}<${ts(a)}>\n`).join("\n"); function capture() { const lines: string[] = []; return { text: () => lines.join("\n"), out: { log: (s: string) => lines.push(s), error: (s: string) => lines.push(s) } }; } const QUOTE = "The bridge opened in the spring, I was there for it."; function article(id: string, quote = QUOTE) { return { format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, citations: { c1: { kind: "video", channel: CHAN, id: "abc1", start: 10, end: 20, quote } }, sections: [{ id: "s", title: "S", body: "Said [this](cite:c1)." }], }; } writeJson(paths.settingsFile, {}); // A channel no site has. writeJson(path.join(paths.channelsDir, CHAN, "config.json"), { handling: "youtube", name: "Lone", url: "https://www.youtube.com/@lone/videos" }); const rec = path.join(paths.channelsDir, CHAN, "data", "abc1"); writeJson(path.join(rec, "metadata.info.json"), { id: "abc1", title: "Stream abc1", channel: CHAN, upload_date: "20260110", duration: 600, webpage_url: "https://www.youtube.com/watch?v=abc1", extractor_key: "Youtube", }); writeFileSync(path.join(rec, "transcript.en.vtt"), vtt([[10, 20, QUOTE]])); writeJson(articleFile(paths, "pub"), article("pub")); writeJson(articleFile(paths, "draft"), article("draft", "Words the record never had at all.")); await configureArticle("pub", { published: true }, paths); test("check --article: as the homepage's compose would — a channel no site has is citable; a draft is checked by name", async () => { const ok = capture(); assert.equal(await checkMain({ article: "pub", paths, allowMissingMedia: true }, ok.out), 0, ok.text()); assert.match(ok.text(), /reports check --article pub: 1 report\(s\), 1 moment\(s\) — compose would pass/); const media = capture(); assert.equal(await checkMain({ article: "pub", paths }, media.out), 1); assert.match(media.text(), /missing-media: lone\/abc1\/10\.00-20\.00: no prepared media/); const draft = capture(); assert.equal(await checkMain({ article: "draft", paths, allowMissingMedia: true }, draft.out), 1); assert.match(draft.text(), /quote-drift: draft#c1/); const none = capture(); assert.equal(await checkMain({ article: "nope", paths }, none.out), 2); assert.match(none.text(), /no article "nope"/); }); test("prepare --article: the articles' cache, every published article plus the one named; a missing clip is listed", async () => { const c = capture(); assert.equal(await prepareMain({ article: "draft", exportOptions: { paths } }, c.out), 1, c.text()); const index = readJson<{ siteId: string; problems: { kind: string; citations: string[] }[] }>( path.join(STAGING, "report-media", "index.json"), ); assert.equal(index.siteId, "articles"); assert.deepEqual( index.problems.map((p) => [p.kind, p.citations]), [["missing-media", ["pub#c1", "draft#c1"]]], ); assert.match(c.text(), /reports prepare --article draft: 1 problem\(s\)/); assert.equal(existsSync(path.join(paths.exportIndexDir, "sites")), false, "no site's staging touched"); }); test("prepare --article --only: that article answers for the run — its gaps fail it, another's are warnings, it alone is exported (release 27 S7)", async () => { // An article whose own moments are all prepared: another's gap is a warning. writeJson(articleFile(paths, "solo"), { format: "archilyzer-report", version: 1, id: "solo", kind: "sweep", title: "Article solo", sections: [{ id: "s", title: "S", body: "Nothing cited here." }], }); const exportOptions = { paths, openPdfPrinter: async () => ({ missing: "no browser in the test" }) }; const ok = capture(); assert.equal(await prepareMain({ article: "solo", only: true, exportOptions }, ok.out), 0, ok.text()); assert.match(ok.text(), /warning: 1 problem\(s\) in OTHER articles — not this one's/); assert.match(ok.text(), /missing-media: lone\/abc1\/10\.00-20\.00 \(cited by pub#c1\)/); assert.ok(existsSync(path.join(STAGING, "report-exports", "solo", "report.html")), "this article is exported"); // The cache still holds every published article's moments: nothing pruned, the index whole. const index = readJson<{ problems: { citations: string[] }[] }>(path.join(STAGING, "report-media", "index.json")); assert.deepEqual(index.problems.map((p) => p.citations), [["pub#c1"]]); // Its own gap fails the run, named. const own = capture(); assert.equal(await prepareMain({ article: "draft", only: true, exportOptions }, own.out), 1); assert.match(own.text(), /reports prepare --article draft --only: 1 problem\(s\):/); assert.match(own.text(), /the article was not exported/); // --only needs an article. const bad = capture(); assert.equal(await prepareMain({ siteId: "x", only: true }, bad.out), 2); }); test("export --article: one article's files under .export-index/articles/, its history in reports//history-git", async () => { const c = capture(); assert.equal( await exportMain({ article: "pub", paths, formats: ["html", "md"], allowMissingMedia: true }, c.out), 0, c.text(), ); const dir = path.join(STAGING, "report-exports", "pub"); assert.ok(existsSync(path.join(dir, "report.html"))); assert.ok(existsSync(path.join(dir, "report.md"))); const manifest = readJson<{ siteId: string; revision?: { revision: number } }>(path.join(dir, "export.json")); assert.equal(manifest.siteId, "articles"); assert.equal(manifest.revision?.revision, 1); const head = await readRevisionHead(path.join(path.dirname(articleFile(paths, "pub")), "history-git")); assert.equal(head?.revision, 1); const author = execFileSync("git", ["--git-dir", path.join(path.dirname(articleFile(paths, "pub")), "history-git"), "log", "-1", "--format=%an <%ae>"], { encoding: "utf8" }).trim(); assert.equal(author, "Archilyzer "); // An article that is not there. const none = capture(); assert.equal(await exportMain({ article: "nope", paths }, none.out), 2); }); test("an article's iterations stay out of its history; making one current is the next revision (release 27 S4)", async () => { const gitDir = path.join(path.dirname(articleFile(paths, "pub")), "history-git"); await createIteration("pub", { kind: "sweep", model: "qwen3:8b", question: "What was said?" }, paths); await writeIterationFile("pub", 1, "report.json", { ...article("pub"), title: "Article pub, iterated" }, paths); await writeIterationFile("pub", 1, "sweep.md", "# iterated\n", paths); await updateIteration("pub", 1, { status: "done" }, paths); const tree = () => execFileSync("git", ["--git-dir", gitDir, "ls-tree", "-r", "--name-only", "main"], { encoding: "utf8" }).trim().split("\n"); // An export with iterations on disk and the article unchanged: nothing new. const same = capture(); assert.equal(await exportMain({ article: "pub", paths, formats: ["md"], allowMissingMedia: true }, same.out), 0, same.text()); assert.equal((await readRevisionHead(gitDir))?.revision, 1); await makeCurrent("pub", 1, paths); const c = capture(); assert.equal(await exportMain({ article: "pub", paths, formats: ["md"], allowMissingMedia: true }, c.out), 0, c.text()); const head = await readRevisionHead(gitDir); assert.equal(head?.revision, 2); assert.deepEqual(head?.summary, ["Title changed: “Article pub” → “Article pub, iterated”"]); assert.deepEqual(tree(), ["exports.json", "report.json", "report.md"]); assert.ok(!readdirSync(path.join(STAGING, "report-exports", "pub")).some((f) => /iteration|sweep|turns/.test(f))); }); test("an article in the making is no article to check, prepare or export (release 27 S4)", async () => { await createIteration("making", { kind: "sweep", model: "qwen3:8b", question: "What was said?" }, paths); await writeIterationFile("making", 1, "report.json", article("making"), paths); const check = capture(); assert.equal(await checkMain({ article: "making", paths, allowMissingMedia: true }, check.out), 2); assert.match(check.text(), /no article "making"/); const ex = capture(); assert.equal(await exportMain({ article: "making", paths }, ex.out), 2); }); test("verify-quotes takes an article's file", async () => { const c = capture(); assert.equal(await verifyQuotesMain({ file: articleFile(paths, "pub"), paths }, c.out), 0, c.text()); }); const hasFfmpeg = (() => { try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); return true; } catch { return false; } })(); test("attach-video --article: `video` written through the registry, the audience kept", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { await configureArticle("pub", { audience: "public" }, paths); const src = path.join(ROOT, "clip.mp4"); execFileSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=640x360:rate=25", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=44100", "-t", "2", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", src, ]); const c = capture(); assert.equal(await attachVideoMain({ article: "pub", video: src, caption: "Cut." }, c.out), 0, c.text()); const doc = readJson<{ video: unknown; audience: string }>(articleFile(paths, "pub")); assert.deepEqual(doc.video, { src: "video.mp4", poster: "poster.jpg", caption: "Cut." }); assert.equal(doc.audience, "public"); assert.match(c.text(), /reports attach-video: article pub video =/); assert.deepEqual((await readArticlesIndex(paths)).published, ["pub"]); const none = capture(); assert.equal(await attachVideoMain({ article: "nope", video: src }, none.out), 1); }); test("reports iterations: the list with lineage, the current marked; JSON; an article in the making; refusals (release 27 S4)", async () => { const { iterationsMain } = await import("./reports-iterations"); await createIteration("pub", { kind: "revise-report", model: "claude-sonnet-5-5", parent: 1, feedback: "Name the bridge." }, paths); const c = capture(); assert.equal(await iterationsMain({ article: "pub", paths }, c.out), 0, c.text()); const lines = c.text().split("\n"); assert.match(lines[0], /reports iterations pub: 2 iteration\(s\)$/); assert.match(lines[1], /^\* i1 +sweep +qwen3:8b +done .* i1 “What was said\?” ← current article$/); assert.match(lines[2], /^ {2}i2 +revise-report +claude-sonnet-5-5 +queued .* i1 › i2 \(revises i1\) “Name the bridge\.”$/); const j = capture(); assert.equal(await iterationsMain({ article: "pub", json: true, paths }, j.out), 0); const parsed = JSON.parse(j.text()) as { inTheMaking: boolean; current: { report: number }; iterations: { n: number; lineage: number[]; currentReport: boolean }[] }; assert.equal(parsed.inTheMaking, false); assert.equal(parsed.current.report, 1); assert.deepEqual(parsed.iterations.map((i) => [i.n, i.lineage, i.currentReport]), [[1, [1], true], [2, [1, 2], false]]); const making = capture(); assert.equal(await iterationsMain({ article: "making", paths }, making.out), 0); assert.match(making.text(), /an article in the making/); const none = capture(); assert.equal(await iterationsMain({ article: "nope", paths }, none.out), 2); assert.match(none.text(), /no article "nope"/); const bad = capture(); assert.equal(await iterationsMain({ article: "Bad", paths }, bad.out), 2); });