// A download that just landed gets its transcript.cues.json (2026-10-26): the // caption download never reached the normalize, so umtool refused the video // ("clip-no-cues") until `pnpm ops build-cues` ran by hand. // // Run with: node_modules/.bin/tsx --test common/controller/writeCuesAfterDownload.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 { isCuesJsonFresh, writeCuesAfterDownload } from "./normalizeTranscript"; const ROOT = mkdtempSync(path.join(tmpdir(), "cues-after-download-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const VTT = readFileSync(path.join(import.meta.dirname, "..", "lib", "__fixtures__", "vtt-cue-blocks.vtt"), "utf8"); const META = { id: "vid", title: "A video", upload_date: "20261024", duration: 30, webpage_url: "https://www.youtube.com/watch?v=vid", extractor_key: "Youtube", }; let n = 0; function videoDir(files: Record): string { const dir = path.join(ROOT, `c${n}`, "data", `vid${n++}`); mkdirSync(dir, { recursive: true }); for (const [name, body] of Object.entries(files)) writeFileSync(path.join(dir, name), body); return dir; } test("a caption download's VTT becomes a fresh transcript.cues.json, said in the log", async () => { const dir = videoDir({ "metadata.info.json": JSON.stringify(META), "transcript.en-orig.vtt": VTT }); const lines: string[] = []; const out = await writeCuesAfterDownload({ videoDir: dir, channelSlug: "c", onLog: (l) => lines.push(l) }); assert.equal(out?.status, "wrote"); assert.ok(existsSync(path.join(dir, "transcript.cues.json"))); assert.equal((await isCuesJsonFresh(dir)).fresh, true); assert.match(lines.join(""), /Normalized c\/vid\d+ \(transcript\.en-orig\.vtt, \d+ cues\)/); // Again: fresh, nothing rewritten. assert.equal((await writeCuesAfterDownload({ videoDir: dir, channelSlug: "c", onLog: () => {} }))?.status, "fresh"); }); test("a download with no transcript (audio for transcription) is skipped, not an error", async () => { const dir = videoDir({ "metadata.info.json": JSON.stringify(META), "audio.mp3": "x" }); const out = await writeCuesAfterDownload({ videoDir: dir, channelSlug: "c", onLog: () => {} }); assert.deepEqual(out, { status: "skipped", reason: "no-raw-transcript" }); assert.equal(existsSync(path.join(dir, "transcript.cues.json")), false); }); test("a metadata file it cannot read is a log line and null — the download is not failed", async () => { const dir = videoDir({ "metadata.info.json": "{not json", "transcript.en.vtt": VTT }); const lines: string[] = []; const out = await writeCuesAfterDownload({ videoDir: dir, channelSlug: "c", onLog: (l) => lines.push(l) }); assert.equal(out, null); assert.match(lines.join(""), /Could not write transcript\.cues\.json for vid\d+: .*pnpm ops build-cues/); });