import { test } from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/storedText.unreadable.test.ts // // REVIEW-1 F3 (captions lane): A STORED FILE THAT CANNOT BE READ IS AN ERROR // THAT SURFACES, NEVER "NO TEXT". On a Node without zstd every process refuses // to start, naming the Node it needs; and a stored caption whose frame does // not decode is thrown past the readers that skip a missing track — the copies // lane does not read it as "not done". (The index's side is buildIndex.test.ts // (w).) Temp dirs, synthetic names. const ROOT = await mkdtemp(path.join(tmpdir(), "stored-unreadable-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const COMMON = path.dirname(path.dirname(fileURLToPath(import.meta.url))); const { zstdSupportProblem, assertZstdSupport } = await import("../lib/zstdSupport.mjs"); const { readEnglishVttCues } = await import("../lib/videoStatus"); const { readVideoTracks } = await import("../lib/captionTracks-server"); const { isStoredTextReadError } = await import("../lib/storedText-server"); const { __computeDoneForTest, unreadableStoredCaptionCount } = await import("./copiesLane"); const VTT = "WEBVTT\n\n00:00:00.000 --> 00:00:02.000\none two three four\n"; test("no zstd in this Node: the problem names the Node it needs; asked to assert, it refuses", () => { assert.equal(zstdSupportProblem(), null, "this Node has it"); const old = {}; const p = zstdSupportProblem(old, "v22.11.0"); assert.match(String(p), /needs node:zlib zstd — Node >= 22\.15 \(this is v22\.11\.0\)/); assert.throws(() => assertZstdSupport("the editor", old, "v22.11.0"), /the editor refuses to start: .*Node >= 22\.15/); }); test("a bare-node reader of stored text, and a common/bin CLI, refuse to start on a Node without zstd", async (t) => { // A preload that takes zstd away, as an older Node would not have it (its // own temp dir: made and removed by this test). const own = await mkdtemp(path.join(tmpdir(), "no-zstd-")); t.after(() => rm(own, { recursive: true, force: true })); const NO_ZSTD = path.join(own, "no-zstd.mjs"); await writeFile( NO_ZSTD, 'import zlib from "node:zlib";\nfor (const k of ["zstdDecompress", "createZstdCompress", "createZstdDecompress"]) delete zlib[k];\n', ); const reader = spawnSync(process.execPath, ["--import", NO_ZSTD, "-e", `await import(${JSON.stringify(path.join(COMMON, "lib", "storedText-read.mjs"))})`], { encoding: "utf8", input: "", }); assert.notEqual(reader.status, 0); assert.match(reader.stderr, /refuses to start: stored text .*needs node:zlib zstd — Node >= 22\.15/); // A CLI entry (runIfEntryPoint): a tiny entry point in the temp dir. const entry = path.join(own, "cli-entry.ts"); const cli = path.join(COMMON, "bin", "_cli.ts"); await writeFile(entry, `import { runIfEntryPoint } from ${JSON.stringify(cli)};\nrunIfEntryPoint(import.meta.url, async () => { console.log("ran"); });\n`); const tsx = path.join(COMMON, "node_modules", ".bin", "tsx"); const run = spawnSync(tsx, [entry], { encoding: "utf8", cwd: own, env: { ...process.env, NODE_OPTIONS: `--import=${NO_ZSTD}` } }); assert.notEqual(run.status, 0, run.stdout + run.stderr); assert.doesNotMatch(run.stdout, /ran/); assert.match(run.stderr, /cli-entry\.ts refuses to start: .*Node >= 22\.15/); const ok = spawnSync(tsx, [entry], { encoding: "utf8", cwd: own, env: { ...process.env, NODE_OPTIONS: "" } }); assert.equal(ok.status, 0, ok.stderr); assert.match(ok.stdout, /ran/); }); async function videoWithCorruptStored(id: string): Promise { const dir = path.join(ROOT, "channels", "synth", "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id })); // A frame header claiming 64 bytes, then garbage: it does not decode. await writeFile(path.join(dir, "transcript.en.vtt.zst"), Buffer.from([0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x40, 1, 2, 3, 4, 5, 6])); return dir; } test("a stored caption that does not decode is THROWN by the caption readers, never skipped as no text", async () => { const dir = await videoWithCorruptStored("bad1"); await assert.rejects(readEnglishVttCues(dir), (e: unknown) => isStoredTextReadError(e)); await assert.rejects(readVideoTracks(dir), (e: unknown) => isStoredTextReadError(e)); // A readable fallback track does not hide it either: the corrupt one is // the preferred one, and its words are unknown. await writeFile(path.join(dir, "transcript.en-US.vtt"), VTT); await assert.rejects(readEnglishVttCues(dir), (e: unknown) => isStoredTextReadError(e)); // A MISSING track is still just missing. const none = path.join(ROOT, "channels", "synth", "data", "none1"); await mkdir(none, { recursive: true }); assert.equal(await readEnglishVttCues(none), null); }); test("the copies lane: an unreadable stored caption is not \"not done\" (it would send the lane to transcribe it)", async () => { await videoWithCorruptStored("bad2"); const paths = { channelsDir: path.join(ROOT, "channels") } as never; const before = unreadableStoredCaptionCount(); const warned: string[] = []; const warn = console.warn; console.warn = (m: string) => void warned.push(m); try { const done = await __computeDoneForTest( "transcription", paths, {} as never, "synth/bad2", { facts: { duration: 10, captionsCountAsDone: true }, replaceAutoSubs: false }, ["metadata.info.json", "transcript.en.vtt"], ); assert.equal(done, true); } finally { console.warn = warn; } assert.equal(unreadableStoredCaptionCount(), before + 1, "counted"); assert.match(warned.join("\n"), /synth\/bad2: a stored caption cannot be read — .*transcript\.en\.vtt\.zst/); });