// The sync check's filesystem half: a real ffprobe over tiny lavfi windows // (one in sync, one whose picture starts 3 s in — the Rumble HLS shape), the // verdict recorded in the sidecar, and the set-aside every reader then ignores. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/clipWindowSync-server.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { copyFile, mkdir, mkdtemp, readFile, readdir, rm, symlink, writeFile, lstat } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { findInSyncContainingClipWindow, judgeClipWindow, probeWindowStreams, setAsideDesyncedWindow, type WindowStreamProbe, } from "./clipWindowSync-server"; import { findContainingClipWindow, listClipWindows } from "./clipWindow-server"; import { windowSyncOf } from "./clipWindowSync"; import { windowsFromBareNames } from "./evidenceClip.mjs"; let ROOT = ""; let GOOD = ""; let BAD = ""; function ff(args: string[]): void { execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", ...args]); } before(async () => { ROOT = await mkdtemp(path.join(tmpdir(), "window-sync-")); const fx = path.join(ROOT, "fixtures"); await mkdir(fx, { recursive: true }); GOOD = path.join(fx, "good.mp4"); BAD = path.join(fx, "bad.mp4"); ff([ "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=4", "-f", "lavfi", "-i", "sine=frequency=440:duration=4", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", GOOD, ]); // The picture 3 s into the container and 3 s longer than the sound. ff([ "-itsoffset", "3", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=4", "-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", BAD, ]); }); after(() => rm(ROOT, { recursive: true, force: true })); let n = 0; async function videoDir(): Promise { const dir = path.join(ROOT, String(++n), "channels", "c", "data", "v1"); await mkdir(path.join(dir, "clips"), { recursive: true }); return dir; } const clips = (dir: string) => path.join(dir, "clips"); // A probe that counts its calls and asks the real ffprobe. function counting(): { probe: WindowStreamProbe; calls: string[] } { const calls: string[] = []; return { calls, probe: async (file) => { calls.push(path.basename(file)); return probeWindowStreams(file); }, }; } test("ffprobe reads the two shapes the rule separates", async () => { const good = await probeWindowStreams(GOOD); assert.ok(good); assert.equal(windowSyncOf(good).ok, true); const bad = await probeWindowStreams(BAD); assert.ok(bad); const s = windowSyncOf(bad, 4); assert.equal(s.ok, false); assert.ok((s.videoStart ?? 0) > 2.9, `video start ${s.videoStart}`); assert.ok(s.lagS >= 2.9, `lag ${s.lagS}`); // Not a media file at all: not judged. const junk = path.join(ROOT, "junk.mp4"); await writeFile(junk, "mp4"); assert.equal(await probeWindowStreams(junk), null); assert.equal(await probeWindowStreams(GOOD, "/nonexistent/ffprobe"), null); }); test("a good verdict is recorded in the sidecar, and not probed again for the same size", async () => { const dir = await videoDir(); await copyFile(GOOD, path.join(clips(dir), "10.00-14.00.mp4")); await writeFile(path.join(clips(dir), "10.00-14.00.json"), JSON.stringify({ requestedBy: "umtool", clipId: "r#1" })); const { probe, calls } = counting(); const [w] = await listClipWindows(dir); const first = await judgeClipWindow(w, { probe }); assert.equal(first.sync?.ok, true); assert.equal(first.fromRecord, false); const side = JSON.parse(await readFile(path.join(clips(dir), "10.00-14.00.json"), "utf8")); assert.equal(side.clipId, "r#1", "the sidecar's other fields are kept"); assert.equal(side.sync.ok, true); assert.equal(side.sync.bytes, w.bytes); const [again] = await listClipWindows(dir); assert.equal(again.provenance?.sync?.ok, true); const second = await judgeClipWindow(again, { probe }); assert.equal(second.fromRecord, true); assert.deepEqual(calls, ["10.00-14.00.mp4"], "one probe for two judgements"); // The bytes changed: probed again. await judgeClipWindow({ ...again, bytes: again.bytes + 1 }, { probe }); assert.equal(calls.length, 2); // `record: false` writes nothing — a window with no sidecar stays without one. await copyFile(GOOD, path.join(clips(dir), "20.00-24.00.mp4")); const bare = (await listClipWindows(dir)).find((x) => x.from === 20)!; await judgeClipWindow(bare, { probe, record: false }); assert.ok(!(await readdir(clips(dir))).includes("20.00-24.00.json")); // ...and with recording on, gets one holding only the verdict — still no provenance. await judgeClipWindow(bare, { probe }); const after = (await listClipWindows(dir)).find((x) => x.from === 20)!; assert.equal(after.provenance, null); }); test("set aside: renamed with its sidecar (verdict + setAsideAt), and no reader sees it", async () => { const dir = await videoDir(); await copyFile(BAD, path.join(clips(dir), "10.00-14.00.mp4")); await writeFile(path.join(clips(dir), "10.00-14.00.json"), JSON.stringify({ requestedBy: "umtool", reason: "why" })); // An older set-aside of the same span, in another container: replaced. await writeFile(path.join(clips(dir), "10.00-14.00.desynced.mkv"), "old"); const [w] = await listClipWindows(dir); const { sync } = await judgeClipWindow(w, { probe: (f) => probeWindowStreams(f) }); assert.equal(sync?.ok, false); const at = new Date("2026-10-23T12:00:00.000Z"); const aside = await setAsideDesyncedWindow(w, sync!, at); assert.equal(aside.file, "10.00-14.00.desynced.mp4"); assert.deepEqual((await readdir(clips(dir))).sort(), ["10.00-14.00.desynced.json", "10.00-14.00.desynced.mp4"]); const side = JSON.parse(await readFile(path.join(clips(dir), "10.00-14.00.desynced.json"), "utf8")); assert.equal(side.requestedBy, "umtool"); assert.equal(side.reason, "why"); assert.equal(side.sync.ok, false); assert.equal(side.sync.bytes, w.bytes); assert.equal(side.setAsideAt, at.toISOString()); // Every reader: the editor's lister and its dedup, and umtool's bare-name tier. assert.deepEqual(await listClipWindows(dir), []); assert.equal(await findContainingClipWindow(dir, 11, 13), null); assert.deepEqual(windowsFromBareNames(await readdir(clips(dir)), clips(dir)), []); }); test("a window that is a link is renamed as the link, never through it", async () => { const dir = await videoDir(); const target = path.join(ROOT, `target-${n}.mp4`); await copyFile(BAD, target); await symlink(target, path.join(clips(dir), "10.00-14.00.mp4")); const [w] = await listClipWindows(dir); const { sync } = await judgeClipWindow(w, { probe: (f) => probeWindowStreams(f) }); await setAsideDesyncedWindow(w, sync!); const st = await lstat(path.join(clips(dir), "10.00-14.00.desynced.mp4")); assert.ok(st.isSymbolicLink()); assert.ok((await lstat(target)).isFile(), "the target is untouched, under its own name"); }); test("the in-sync finder skips a desynced tightest window for a good wider one, probing only what it walks", async () => { const dir = await videoDir(); await copyFile(BAD, path.join(clips(dir), "10.00-14.00.mp4")); await copyFile(GOOD, path.join(clips(dir), "0.00-60.00.mp4")); await copyFile(GOOD, path.join(clips(dir), "100.00-104.00.mp4")); const { probe, calls } = counting(); // A planner's look: judged, nothing renamed. const look = await findInSyncContainingClipWindow(dir, 11, 13, { probe, setAside: false }); assert.equal(look.hit?.file, "0.00-60.00.mp4"); assert.deepEqual(look.desynced.map((d) => [d.window.file, d.setAsideAs]), [["10.00-14.00.mp4", null]]); assert.ok((await readdir(clips(dir))).includes("10.00-14.00.mp4")); // The run's: set aside. const r = await findInSyncContainingClipWindow(dir, 11, 13, { probe }); assert.equal(r.hit?.file, "0.00-60.00.mp4"); assert.deepEqual(r.desynced.map((d) => d.setAsideAs), ["10.00-14.00.desynced.mp4"]); assert.ok(!calls.includes("100.00-104.00.mp4"), "a window that does not cover the span is never probed"); // Nothing else covers: the span is missing. await rm(path.join(clips(dir), "0.00-60.00.mp4")); assert.equal((await findInSyncContainingClipWindow(dir, 11, 13, { probe })).hit, null); // A window ffprobe cannot read is kept and answers, as before. await writeFile(path.join(clips(dir), "5.00-20.00.mp4"), "mp4"); assert.equal((await findInSyncContainingClipWindow(dir, 11, 13, { probe })).hit?.file, "5.00-20.00.mp4"); });