// The sync verdict on the numbers measured on the live corpus (2026-10-23). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/clipWindowSync.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { desyncedWindowMessage, parseRecordedClipWindowSync, windowSyncOf, WINDOW_SYNC_MAX_DURATION_GAP_S, WINDOW_SYNC_MAX_START_S, type ProbedWindowStream, } from "./clipWindowSync"; import { clipWindowDesyncedFile, clipWindowDesyncedSidecar, parseClipProvenance, parseClipWindowName, parseDesyncedClipWindowName, } from "./clipWindow"; // ffprobe prints its numbers as strings. const streams = (v: [string, string] | null, a: [string, string] | null): ProbedWindowStream[] => [ ...(v ? [{ codec_type: "video", start_time: v[0], duration: v[1] }] : []), ...(a ? [{ codec_type: "audio", start_time: a[0], duration: a[1] }] : []), ]; test("the costco window — video 3.200/93.73, audio 3.140/90.58 — is desynced", () => { const s = windowSyncOf(streams(["3.200000", "93.733000"], ["3.140000", "90.580000"]), 93.72); assert.equal(s.ok, false); assert.equal(s.videoStart, 3.2); assert.equal(s.audioStart, 3.14); assert.equal(s.videoDuration, 93.733); assert.equal(s.audioDuration, 90.58); assert.equal(s.requestedLength, 93.72); assert.equal(s.lagS, 3.2); assert.match(s.reason ?? "", /the video track starts 3\.20 s in/); assert.match(s.reason ?? "", /the audio track starts 3\.14 s in/); assert.match(s.reason ?? "", /the video runs 3\.15 s longer than the audio/); }); test("the good shape — starts ≤ 0.07 s, durations within 0.03 s — is in sync", () => { const s = windowSyncOf(streams(["0.067000", "30.030000"], ["0.000000", "30.000000"]), 30); assert.equal(s.ok, true); assert.equal(s.reason, undefined); assert.equal(s.lagS, 0.07); // An encoder's negative priming start is not "in". assert.equal(windowSyncOf(streams(["0.000000", "10.0"], ["-0.021333", "10.01"])).ok, true); }); test("the borderline 0.50/0.43 window is desynced", () => { const s = windowSyncOf(streams(["0.500000", "40.0"], ["0.430000", "40.0"])); assert.equal(s.ok, false); assert.equal(s.lagS, 0.5); }); test("each half of the signature is enough on its own", () => { assert.equal(WINDOW_SYNC_MAX_START_S, 0.25); assert.equal(WINDOW_SYNC_MAX_DURATION_GAP_S, 0.25); // Starts clean, durations apart. const gap = windowSyncOf(streams(["0.0", "20.0"], ["0.0", "19.5"])); assert.equal(gap.ok, false); assert.match(gap.reason ?? "", /the video runs 0\.50 s longer than the audio/); // Only the audio starts late. assert.equal(windowSyncOf(streams(["0.0", "20.0"], ["0.3", "20.0"])).ok, false); // A duration ffprobe has no number for (Matroska) is not a gap. assert.equal(windowSyncOf(streams(["0.0", "N/A"], ["0.0", "N/A"])).ok, true); }); test("an audio-only (or video-only) window is not judged", () => { const audio = windowSyncOf(streams(null, ["2.0", "30.0"])); assert.equal(audio.ok, true); assert.match(audio.reason ?? "", /no video track: not judged/); const video = windowSyncOf(streams(["2.0", "30.0"], null)); assert.equal(video.ok, true); assert.match(video.reason ?? "", /no audio track: not judged/); // Cover art is not a picture track. const art = windowSyncOf([ { codec_type: "video", start_time: "5.0", duration: "1", disposition: { attached_pic: 1 } }, { codec_type: "audio", start_time: "0.0", duration: "30" }, ]); assert.equal(art.ok, true); assert.match(art.reason ?? "", /no video track/); }); test("the recorded verdict round-trips through a sidecar; a malformed one is no verdict", () => { const s = windowSyncOf(streams(["0.0", "20.0"], ["0.0", "20.0"]), 20); const rec = { ...s, bytes: 1234, checkedAt: "2026-10-23T00:00:00.000Z" }; assert.deepEqual(parseRecordedClipWindowSync(JSON.parse(JSON.stringify(rec))), rec); assert.equal(parseRecordedClipWindowSync({ ok: true }), undefined); assert.equal(parseRecordedClipWindowSync("yes"), undefined); const prov = parseClipProvenance({ requestedBy: "umtool", sync: rec, setAsideAt: "2026-10-23T01:00:00.000Z" }); assert.deepEqual(prov?.sync, rec); assert.equal(prov?.setAsideAt, "2026-10-23T01:00:00.000Z"); }); test("the message names the lag, the cause, the new name and the remedy", () => { assert.equal( desyncedWindowMessage({ lagS: 3.14 }, "10.00-20.00.desynced.mp4", "rumble"), 'the picture runs 3.1 s off the sound (a Rumble HLS seek); set aside as 10.00-20.00.desynced.mp4 — ' + 'save the whole recording ("Persist source video") and fetch again to cut it from the saved video', ); assert.match(desyncedWindowMessage({ lagS: 2 }, "x", "youtube"), /\(a seek into the source's stream\)/); }); test("a set-aside name is no window to the window parser, and parses as a set-aside one", () => { assert.equal(clipWindowDesyncedFile(12, 42), "12.00-42.00.desynced.mp4"); assert.equal(clipWindowDesyncedFile(12, 42, ".mkv"), "12.00-42.00.desynced.mkv"); assert.equal(clipWindowDesyncedSidecar(12, 42), "12.00-42.00.desynced.json"); for (const name of ["12.00-42.00.desynced.mp4", "12.00-42.00.desynced.mkv", "12.00-42.00.desynced.json"]) { assert.equal(parseClipWindowName(name), null, name); } assert.deepEqual(parseDesyncedClipWindowName("12.00-42.00.desynced.mp4"), { from: 12, to: 42 }); assert.deepEqual(parseDesyncedClipWindowName("12.00-42.00.desynced.webm"), { from: 12, to: 42 }); for (const name of ["12.00-42.00.mp4", "12.00-42.00.desynced.json", "12.00-42.00.desynced", "x.desynced.mp4"]) { assert.equal(parseDesyncedClipWindowName(name), null, name); } });