import { test } from "node:test"; import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { promisify } from "node:util"; import { verifyMerged, verifyScan } from "./liveVerify-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/liveVerify-server.test.ts // // Real files made by ffmpeg (no network): DASH segments of a picture and a // sound, laid end to end as yt-dlp appends them, merged by a stream copy. const run = promisify(execFile); const ROOT = await mkdtemp(path.join(tmpdir(), "live-verify-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const FFPROBE = process.env.FFPROBE_BIN ?? "ffprobe"; await run("ffmpeg", [ "-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=160x90:rate=30", "-f", "lavfi", "-i", "anoisesrc=color=pink:sample_rate=48000", "-t", "20", "-c:v", "libx264", "-preset", "ultrafast", "-g", "30", "-keyint_min", "30", "-sc_threshold", "0", "-c:a", "aac", "-b:a", "48k", "-map", "0:v", "-map", "1:a", "-f", "dash", "-seg_duration", "1", "-use_template", "1", "-use_timeline", "0", "-init_seg_name", "init-$RepresentationID$.m4s", "-media_seg_name", "c-$RepresentationID$-$Number%05d$.m4s", path.join(ROOT, "s.mpd"), ]); const segs = (rep: number) => readdir(ROOT).then((n) => n.filter((x) => x.startsWith(`c-${rep}-`)).sort()); async function streamFile(rep: number, out: string, skip: number[] = []) { const parts = [await readFile(path.join(ROOT, `init-${rep}.m4s`))]; for (const [i, s] of (await segs(rep)).entries()) if (!skip.includes(i + 1)) parts.push(await readFile(path.join(ROOT, s))); await writeFile(out, Buffer.concat(parts)); return out; } const merge = async (v: string, a: string, out: string) => { await run("ffmpeg", ["-hide_banner", "-loglevel", "error", "-y", "-i", v, "-i", a, "-map", "0:v:0", "-map", "1:a:0", "-c", "copy", out]); return out; }; test("whole: the merged file holds every packet of each stream file, no hole", async () => { const v = await streamFile(0, path.join(ROOT, "v.mp4")); const a = await streamFile(1, path.join(ROOT, "a.m4a")); const m = await merge(v, a, path.join(ROOT, "m.mp4")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, true, JSON.stringify(r)); assert.equal((await verifyScan(FFPROBE, m)).ok, true); }); test("a skipped fragment: the length hides it, the scan does not", async () => { const v = await streamFile(0, path.join(ROOT, "vh.mp4"), [7]); const a = await streamFile(1, path.join(ROOT, "ah.m4a")); const m = await merge(v, a, path.join(ROOT, "mh.mp4")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, false); assert.match((r as { why: string }).why, /step in its video.*a hole/); }); test("a merged file that lost packets of a stream file (a merge cut short) is not whole", async () => { const v = await streamFile(0, path.join(ROOT, "v2.mp4")); const a = await streamFile(1, path.join(ROOT, "a2.m4a")); const vShort = await streamFile(0, path.join(ROOT, "v2s.mp4"), [16, 17, 18, 19, 20]); const m = await merge(vShort, a, path.join(ROOT, "m2.mp4")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, false); assert.match((r as { why: string }).why, /packets merged|stopped short/); }); test("a file that does not read is not whole", async () => { const bad = path.join(ROOT, "bad.mp4"); await writeFile(bad, "not media"); assert.equal((await verifyScan(FFPROBE, bad)).ok, false); }); test("WebM stream files as yt-dlp appends them (Matroska clusters of unknown length: ffprobe warns once per fragment) read as whole — the warnings are not errors", async () => { const dir = path.join(ROOT, "webm"); const { mkdir } = await import("node:fs/promises"); await mkdir(dir); await run("ffmpeg", [ "-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=160x90:rate=30", "-f", "lavfi", "-i", "anoisesrc=color=pink:sample_rate=48000", "-t", "12", "-c:v", "libvpx-vp9", "-deadline", "realtime", "-cpu-used", "8", "-g", "30", "-keyint_min", "30", "-c:a", "libopus", "-b:a", "48k", "-map", "0:v", "-map", "1:a", "-f", "dash", "-dash_segment_type", "webm", "-seg_duration", "1", "-use_template", "1", "-use_timeline", "0", "-init_seg_name", "init-$RepresentationID$.webm", "-media_seg_name", "c-$RepresentationID$-$Number%05d$.webm", path.join(dir, "w.mpd"), ]); const lay = async (rep: number, out: string) => { const names = (await readdir(dir)).filter((x) => x.startsWith(`c-${rep}-`)).sort(); await writeFile(out, Buffer.concat([await readFile(path.join(dir, `init-${rep}.webm`)), ...(await Promise.all(names.map((n) => readFile(path.join(dir, n)))))])); return out; }; const v = await lay(0, path.join(dir, "v.webm")); const a = await lay(1, path.join(dir, "a.webm")); const m = await merge(v, a, path.join(dir, "m.mkv")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, true, JSON.stringify(r)); }); // REVIEW-6 3: the two cases the packet comparison alone passed. test("a TORN raw video track (a fragment cut, later ones appended): raw and merged agree, but the picture stops where the tear is — not whole", async () => { const parts = [await readFile(path.join(ROOT, "init-0.m4s"))]; const names = await segs(0); for (const [i, n] of names.entries()) { const b = await readFile(path.join(ROOT, n)); parts.push(i === 6 ? b.subarray(0, Math.floor(b.length * 0.6)) : b); } const v = path.join(ROOT, "vt.mp4"); await writeFile(v, Buffer.concat(parts)); const a = await streamFile(1, path.join(ROOT, "at.m4a")); const m = await merge(v, a, path.join(ROOT, "mt.mp4")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, false, JSON.stringify(r)); assert.match((r as { why: string }).why, /stopped short|hole/); }); test("a video track that STOPPED EARLY (its thread ended, the sound went on): not whole", async () => { const v = await streamFile(0, path.join(ROOT, "vs.mp4"), [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]); const a = await streamFile(1, path.join(ROOT, "as.m4a")); const m = await merge(v, a, path.join(ROOT, "ms.mp4")); const r = await verifyMerged(FFPROBE, m, [v, a]); assert.equal(r.ok, false, JSON.stringify(r)); assert.match((r as { why: string }).why, /stopped short/); }); test("REVIEW-7 4: the tracks' tolerance is max(2 s, 2 fragments) — 4 s apart fails with no fragment length, passes with 5 s fragments", async () => { const v = await streamFile(0, path.join(ROOT, "vf.mp4"), [17, 18, 19, 20]); const a = await streamFile(1, path.join(ROOT, "af.m4a")); const m = await merge(v, a, path.join(ROOT, "mf.mp4")); assert.equal((await verifyMerged(FFPROBE, m, [v, a])).ok, false, "2 s by default"); assert.equal((await verifyMerged(FFPROBE, m, [v, a], undefined, { fragmentSeconds: 5 })).ok, true, "2 fragments of 5 s"); });