// `umtool new --seed phrase --phrase … --channels … --prefer …` (release // 33): every time a phrase was said, from a synthetic corpus in a temp tree. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { scaffoldReportVideo } from "./projects/scaffold.mjs"; async function video(channelsDir, slug, id, doc) { const dir = path.join(channelsDir, slug, "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "transcript.cues.json"), JSON.stringify({ id, title: `t ${id}`, ...doc })); } test("--seed phrase: chronological clips cut from the preferred copy, the scan beside the manifest, a checklist", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-phrase-seed-")); try { const channelsDir = path.join(tmp, "channels"); await video(channelsDir, "synth-rumble", "r1", { uploadDate: "20230102", duration: 1000, platform: "rumble", cues: [{ start: 100, end: 103, text: "kind of a slow news day today" }] }); await video(channelsDir, "synth-yt", "y1", { uploadDate: "20230101", duration: 1005, platform: "youtube", cues: [{ start: 110, end: 113, text: "kind of a slow news day today" }] }); await video(channelsDir, "synth-yt", "y0", { uploadDate: "20190502", duration: 600, platform: "youtube", cues: [{ start: 4, end: 6, text: "a slow" }, { start: 6, end: 8, text: "news week indeed" }] }); const r = await scaffoldReportVideo({ root: path.join(tmp, "reports"), slug: "slow-news", seed: "phrase", phrase: String.raw`slow[\s,]+news`, channels: ["synth-yt"], prefer: "synth-rumble", channelsDir, }); assert.equal(r.seeded, 2); const doc = JSON.parse(await readFile(path.join(r.dir, "video.manifest.json"), "utf8")); assert.deepEqual(doc.timeline.map((c) => [c.id, c.channel, c.video, c.cutStart, c.cutEnd, c.onscreen.subtitle]), [ ["s01", "synth-yt", "y0", 4, 8, "2 May 2019"], ["s02", "synth-rumble", "r1", 100, 103, "2 Jan 2023"], ]); assert.equal(doc.provenance.channelSlug, "synth-rumble"); assert.equal(doc.render.transition, 0); assert.equal(doc.render.fetchPad, 0, "the window is the fetched extent"); const scan = JSON.parse(await readFile(path.join(r.dir, "phrase-scan.json"), "utf8")); assert.equal(scan.instances.length, 2); assert.equal(scan.hits, 3); const readme = await readFile(path.join(r.dir, "README.md"), "utf8"); assert.match(readme, /Clips seeded from a phrase scan/); assert.match(readme, /- \[ \] s02 {2}2 Jan 2023 {2}synth-rumble\/r1 100–103 {2}"slow news"/); } finally { await rm(tmp, { recursive: true, force: true }); } }); test("--seed phrase refuses what it cannot scan", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-phrase-seed-")); const base = { root: path.join(tmp, "reports"), slug: "x", seed: "phrase", channelsDir: tmp }; try { await assert.rejects(scaffoldReportVideo({ ...base, channels: ["a"] }), /needs --phrase/); await assert.rejects(scaffoldReportVideo({ ...base, phrase: "slow news" }), /needs --channels/); await assert.rejects(scaffoldReportVideo({ ...base, phrase: "slow (news", channels: ["a"] }), /not a regular expression/); await assert.rejects(scaffoldReportVideo({ ...base, phrase: "x", channels: ["A B"] }), /not a channel slug/); await assert.rejects(scaffoldReportVideo({ ...base, phrase: "x", channels: ["a"], from: "a/b" }), /without --from/); await assert.rejects(scaffoldReportVideo({ ...base, seed: null, phrase: "x" }), /go with --seed phrase/); } finally { await rm(tmp, { recursive: true, force: true }); } }); test("--seed phrase with a finished scan (release 34, the phrase finder): the same project, the corpus not read again", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-phrase-seed-")); try { const channelsDir = path.join(tmp, "channels"); await video(channelsDir, "synth-rumble", "r1", { uploadDate: "20230102", duration: 1000, platform: "rumble", cues: [{ start: 100, end: 103, text: "kind of a slow news day today" }] }); await video(channelsDir, "Synth-YT", "y0", { uploadDate: "20190502", duration: 600, platform: "youtube", cues: [{ start: 4, end: 6, text: "a slow" }, { start: 6, end: 8, text: "news week indeed" }] }); const { phraseScanMany } = await import("yt-dlp-transcript-common/lib/montage/phraseScan.mjs"); const phrase = String.raw`slow[\s,]+news`; const [{ id: _id, phrase: _p, ...scan }] = await phraseScanMany({ channelsDir, channels: ["Synth-YT"], prefer: "synth-rumble", phrases: [{ id: "a", phrase }] }); const args = { root: path.join(tmp, "reports"), seed: "phrase", phrase, channels: ["Synth-YT"], prefer: "synth-rumble" }; const fresh = await scaffoldReportVideo({ ...args, slug: "fresh", channelsDir }); // An empty channels dir: only the given scan can seed it. const given = await scaffoldReportVideo({ ...args, slug: "given", channelsDir: path.join(tmp, "nothing"), scan }); assert.equal(given.seeded, 2); const read = async (dir, f) => (await readFile(path.join(dir, f), "utf8")).replaceAll("given", "fresh"); for (const f of ["video.manifest.json", "phrase-scan.json", "README.md"]) assert.equal(await read(given.dir, f), await read(fresh.dir, f), f); await assert.rejects(scaffoldReportVideo({ ...args, slug: "bad", channelsDir, scan: { instances: "no" } }), /a phrase scan is/); await assert.rejects(scaffoldReportVideo({ root: args.root, slug: "bad2", channelsDir, scan }), /go with --seed phrase/); } finally { await rm(tmp, { recursive: true, force: true }); } }); test("--seed phrase --max N (release 35): N instances spread across the years, the rest as skippedByCap, the cap in the README", async () => { const tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-phrase-seed-")); try { const channelsDir = path.join(tmp, "channels"); // 2019: 1, 2020: 6, 2021: 3 — ten instances, each its own video. const plan = [["2019", 1], ["2020", 6], ["2021", 3]]; for (const [year, n] of plan) { for (let k = 0; k < n; k++) { await video(channelsDir, "synth-yt", `y${year}${k}`, { uploadDate: `${year}${String(k + 1).padStart(2, "0")}10`, duration: 600, platform: "youtube", cues: [{ start: 10, end: 13, text: k === 0 && year === "2020" ? ">> a slow news day" : "a slow news day" }] }); } } const base = { root: path.join(tmp, "reports"), seed: "phrase", phrase: String.raw`slow[\s,]+news`, channels: ["synth-yt"], channelsDir }; const capped = await scaffoldReportVideo({ ...base, slug: "capped", max: 5 }); assert.equal(capped.seeded, 5); const doc = JSON.parse(await readFile(path.join(capped.dir, "video.manifest.json"), "utf8")); // 5 of 10: quotas 0.5, 3, 1.5 -> 1, 3, 1 = 5. The 2020 speaker-checked instance is passed over. assert.deepEqual(doc.timeline.map((c) => c.video), ["y20190", "y20201", "y20203", "y20205", "y20211"]); assert.deepEqual(doc.timeline.map((c) => [c.id, c.montage.n, c.montage.of]), [["s01", 1, 5], ["s02", 2, 5], ["s03", 3, 5], ["s04", 4, 5], ["s05", 5, 5]]); const scan = JSON.parse(await readFile(path.join(capped.dir, "phrase-scan.json"), "utf8")); assert.equal(scan.max, 5); assert.equal(scan.instances.length, 5); assert.deepEqual(scan.skippedByCap.map((i) => i.videoId), ["y20200", "y20202", "y20204", "y20210", "y20212"]); assert.ok(scan.instances.every((i) => Array.isArray(i.alternates)), "the instance shape, alternates and all"); const readme = await readFile(path.join(capped.dir, "README.md"), "utf8"); assert.match(readme, /Capped at 5 \(--max\): 5 of 10 instance\(s\) kept, spread across the years in proportion; the other 5 are in phrase-scan.json \("skippedByCap"\)\./); assert.ok(capped.notes.some((n) => /Capped at 5/.test(n))); // No cap and a cap above the count: the same manifest; only a cap writes max / skippedByCap. const plain = await scaffoldReportVideo({ ...base, slug: "plain" }); const roomy = await scaffoldReportVideo({ ...base, slug: "roomy", max: 50 }); const text = async (r, f) => (await readFile(path.join(r.dir, f), "utf8")).replaceAll(path.basename(r.dir), "X"); assert.equal(await text(roomy, "video.manifest.json"), await text(plain, "video.manifest.json")); const plainScan = JSON.parse(await readFile(path.join(plain.dir, "phrase-scan.json"), "utf8")); assert.equal("max" in plainScan || "skippedByCap" in plainScan, false); assert.doesNotMatch(await text(plain, "README.md"), /Capped/); assert.match(await text(roomy, "README.md"), /Capped at 50 \(--max\): all 10 instance\(s\) kept/); await assert.rejects(scaffoldReportVideo({ ...base, slug: "bad", max: 0 }), /--max must be a whole number/); await assert.rejects(scaffoldReportVideo({ ...base, slug: "bad", max: 2.5 }), /--max must be a whole number/); await assert.rejects(scaffoldReportVideo({ root: base.root, slug: "bad", channelsDir, max: 3 }), /go with --seed phrase/); } finally { await rm(tmp, { recursive: true, force: true }); } });