// The check-windows sweep (lib/clipWindowSync.ts) over a temp corpus, with an // injected probe: a window is desynced when its name says "bad". // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/checkClipWindows.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { checkClipWindows, checkClipWindowsSummary } from "./checkClipWindows"; import type { WindowStreamProbe } from "../lib/clipWindowSync-server"; import type { Paths } from "../lib/paths"; const DESYNCED = [ { codec_type: "video", start_time: "3.200000", duration: "93.733000" }, { codec_type: "audio", start_time: "3.140000", duration: "90.580000" }, ]; const IN_SYNC = [ { codec_type: "video", start_time: "0.000000", duration: "30.000000" }, { codec_type: "audio", start_time: "0.000000", duration: "30.000000" }, ]; async function withCorpus(fn: (paths: Paths, clipsOf: (slug: string, id: string) => string) => Promise) { const dir = await mkdtemp(path.join(tmpdir(), "check-windows-")); const transcriptsDir = path.join(dir, "corpus"); const paths = { transcriptsDir, channelsDir: path.join(transcriptsDir, "channels") } as Paths; const clipsOf = (slug: string, id: string) => path.join(paths.channelsDir, slug, "data", id, "clips"); const seed = async (slug: string, id: string, names: string[]) => { await mkdir(clipsOf(slug, id), { recursive: true }); for (const n of names) await writeFile(path.join(clipsOf(slug, id), n), "mp4"); }; await seed("alpha", "v1", ["10.00-40.00.mp4", "bad-placeholder"]); await seed("alpha", "v2", ["0.00-30.00.mp4"]); await seed("beta", "v3", ["5.00-35.00.mp4"]); try { await fn(paths, clipsOf); } finally { await rm(dir, { recursive: true, force: true }); } } // alpha/v1's 10-40 and beta/v3's window are the desynced ones. const BAD = new Set(["alpha/v1/10.00-40.00.mp4", "beta/v3/5.00-35.00.mp4"]); function probeOf(calls: string[]): WindowStreamProbe { return async (file) => { const parts = file.split(path.sep); const key = `${parts.at(-5)}/${parts.at(-3)}/${parts.at(-1)}`; calls.push(key); return BAD.has(key) ? DESYNCED : IN_SYNC; }; } test("a dry run lists the desynced windows and touches nothing", async () => { await withCorpus(async (paths, clipsOf) => { const logs: string[] = []; const calls: string[] = []; const r = await checkClipWindows({ paths, dryRun: true, probe: probeOf(calls), onLog: (l) => logs.push(l) }); assert.equal(r.checked, 3); assert.equal(r.videos, 3); assert.equal(r.channels, 2); assert.deepEqual(r.desynced.map((d) => [d.slug, d.id, d.file, d.lagS, d.setAsideAs]), [ ["alpha", "v1", "10.00-40.00.mp4", 3.2, null], ["beta", "v3", "5.00-35.00.mp4", 3.2, null], ]); assert.equal(r.setAside, 0); assert.deepEqual((await readdir(clipsOf("alpha", "v1"))).sort(), ["10.00-40.00.mp4", "bad-placeholder"]); assert.deepEqual(await readdir(clipsOf("alpha", "v2")), ["0.00-30.00.mp4"], "a dry run records no verdict either"); assert.match(logs[0], /^desynced alpha\/v1\/clips\/10\.00-40\.00\.mp4: the picture runs 3\.2 s off the sound \(.*\) — would be set aside$/); assert.equal( checkClipWindowsSummary(r), "Checked 3 window(s) in 3 video(s) across 2 channel(s): 2 desynced, 0 set aside (dry run: nothing renamed).", ); }); }); test("a real run sets the desynced ones aside, records the good verdicts, and a second run probes nothing", async () => { await withCorpus(async (paths, clipsOf) => { const logs: string[] = []; const calls: string[] = []; const r = await checkClipWindows({ paths, probe: probeOf(calls), onLog: (l) => logs.push(l) }); assert.equal(r.setAside, 2); assert.deepEqual(r.desynced.map((d) => d.setAsideAs), ["10.00-40.00.desynced.mp4", "5.00-35.00.desynced.mp4"]); assert.deepEqual((await readdir(clipsOf("alpha", "v1"))).sort(), [ "10.00-40.00.desynced.json", "10.00-40.00.desynced.mp4", "bad-placeholder", ]); assert.deepEqual((await readdir(clipsOf("alpha", "v2"))).sort(), ["0.00-30.00.json", "0.00-30.00.mp4"]); assert.ok(logs.some((l) => l.endsWith("— set aside as 5.00-35.00.desynced.mp4")), logs.join("\n")); assert.equal( checkClipWindowsSummary(r), "Checked 3 window(s) in 3 video(s) across 2 channel(s): 2 desynced, 2 set aside.", ); calls.length = 0; const again = await checkClipWindows({ paths, probe: probeOf(calls) }); assert.equal(again.checked, 1); assert.equal(again.fromRecord, 1); assert.deepEqual(calls, []); assert.equal(again.desynced.length, 0); }); }); test("one channel, one video; a video without its channel is refused", async () => { await withCorpus(async (paths) => { const calls: string[] = []; const one = await checkClipWindows({ paths, slug: "alpha", video: "v2", dryRun: true, probe: probeOf(calls) }); assert.equal(one.checked, 1); assert.deepEqual(calls, ["alpha/v2/0.00-30.00.mp4"]); const ch = await checkClipWindows({ paths, slug: "beta", dryRun: true, probe: probeOf([]) }); assert.equal(ch.checked, 1); assert.equal(ch.desynced.length, 1); await assert.rejects(() => checkClipWindows({ paths, video: "v2", probe: probeOf([]) }), /give its slug too/); }); });