// THE HEIGHT TIER (lib/clipWindow.ts): a window asked above 720 is its own // cache entry beside the plain one. The names, the listing, the one-probe // height record, the tall lookup's order and the taller-from-saved decision — // temp dirs, stubbed probes, no media. (Eviction: controller/fetchWindowsTier.test.ts.) // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/clipWindowTier.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { clampWindowCap, clipWindowDesyncedFile, clipWindowFile, clipWindowSidecar, parseAnyClipWindowName, parseClipWindowName, parseDesyncedClipWindowName, parseTieredClipWindowName, windowTierOf, } from "./clipWindow"; import { listClipWindows, tallClipWindowCandidates, windowHeightOf, } from "./clipWindow-server"; import { findInSyncContainingClipWindow } from "./clipWindowSync-server"; import { tallerFromSaved, NO_SAVED_COPY_WHY } from "./clipWindowTier-server"; import { evidenceCutArgs, fittedHeight } from "./evidenceClip-server"; import { resolveCorpusSource, windowsFromBareNames } from "./evidenceClip.mjs"; import { DEFAULT_CLIPS_SETTINGS, sanitizeClips, windowMaxHeightOf } from "./clipSettings"; import type { Paths } from "./paths"; import type { SavedWindowSource } from "./savedVideoWindow-server"; async function withVideo(fn: (o: { root: string; videoDir: string; clips: string; paths: Paths }) => Promise) { const root = await mkdtemp(path.join(tmpdir(), "clip-tier-")); const channelsDir = path.join(root, "channels"); const videoDir = path.join(channelsDir, "demo", "data", "v1"); const clips = path.join(videoDir, "clips"); await mkdir(clips, { recursive: true }); await writeFile(path.join(channelsDir, "demo", "config.json"), JSON.stringify({ handling: "transcribe", platform: "youtube" })); const paths = { transcriptsDir: root, channelsDir, savedVideosDir: path.join(root, "saved"), ffprobeBin: "/nonexistent/ffprobe", } as Paths; try { await fn({ root, videoDir, clips, paths }); } finally { await rm(root, { recursive: true, force: true }); } } const put = (dir: string, name: string, body = "media") => writeFile(path.join(dir, name), body); test("names: a tiered window is -.h.; the plain parser never takes one", () => { assert.equal(clipWindowFile(10, 20), "10.00-20.00.mp4"); assert.equal(clipWindowFile(10, 20, 1080), "10.00-20.00.h1080.mp4"); assert.equal(clipWindowSidecar(10, 20, 1080), "10.00-20.00.h1080.json"); assert.equal(clipWindowDesyncedFile(10, 20, ".mp4", 1080), "10.00-20.00.h1080.desynced.mp4"); assert.equal(parseClipWindowName("10.00-20.00.h1080.mp4"), null); assert.deepEqual(parseTieredClipWindowName("10.00-20.00.h1080.mp4"), { from: 10, to: 20, tier: 1080 }); assert.deepEqual(parseAnyClipWindowName("10.00-20.00.mp4"), { from: 10, to: 20 }); assert.deepEqual(parseAnyClipWindowName("10.00-20.00.h1440.webm"), { from: 10, to: 20, tier: 1440 }); assert.deepEqual(parseDesyncedClipWindowName("10.00-20.00.h1080.desynced.mp4"), { from: 10, to: 20, tier: 1080 }); // Not windows: a part, a sidecar, a tier at or under 720, a tier above 4K. for (const n of ["10.00-20.00.h1080.part.mp4", "10.00-20.00.h1080.json", "10.00-20.00.h720.mp4", "10.00-20.00.h4320.mp4"]) { assert.equal(parseAnyClipWindowName(n), null, n); } // A site's evidence cut and umtool's raw parser read bare names only. assert.deepEqual(windowsFromBareNames(["10.00-20.00.mp4", "10.00-20.00.h1080.mp4"], "/c").map((w) => w.name), ["10.00-20.00.mp4"]); }); test("the tier and the ceiling: no ask is the plain path; above the ceiling is clamped and said", () => { assert.equal(windowTierOf(undefined), undefined); assert.equal(windowTierOf(720), undefined); assert.equal(windowTierOf(480), undefined); assert.equal(windowTierOf(1080), 1080); // THE LADDER: 900, 1000, 1080 share one file; held to the ceiling. assert.equal(windowTierOf(900, 1080), 1080); assert.equal(windowTierOf(1000, 1080), 1080); assert.equal(windowTierOf(1200, 2160), 1440); assert.equal(windowTierOf(1600, 2160), 2160); assert.equal(windowTierOf(1200, 1200), 1200); assert.equal(windowTierOf(721, 900), 900); assert.deepEqual(clampWindowCap(undefined, 1080), { cap: undefined }); assert.deepEqual(clampWindowCap(1080, 1080), { cap: 1080 }); assert.deepEqual(clampWindowCap(2160, 1080), { cap: 1080, clampedFrom: 2160 }); assert.equal(DEFAULT_CLIPS_SETTINGS.windowMaxHeight, 1080); assert.equal(sanitizeClips({}).windowMaxHeight, 1080); assert.equal(sanitizeClips({ windowMaxHeight: 1440 }).windowMaxHeight, 1440); assert.equal(sanitizeClips({ windowMaxHeight: 99 }).windowMaxHeight, 1080); assert.equal(sanitizeClips({ windowMaxHeight: 1080.5 }).windowMaxHeight, 1080); assert.equal(windowMaxHeightOf(undefined), 1080); assert.deepEqual(sanitizeClips({ wholeRecordingPlatforms: ["Rumble"] }), { wholeRecordingPlatforms: ["rumble"], windowMaxHeight: 1080 }); }); test("the cut box: evidence stays 1280x720; a tier's box is its 16:9, never upscaled", () => { const evidence = evidenceCutArgs("/in.mp4", 1, 2, "video", "/out"); assert.ok(evidence.includes("scale='min(1280,iw)':'min(720,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2,format=yuv420p")); const tall = evidenceCutArgs("/in.mp4", 1, 2, "video", "/out", 1080); assert.ok(tall.includes("scale='min(1920,iw)':'min(1080,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2,format=yuv420p")); assert.equal(fittedHeight(1920, 1080), 720); assert.equal(fittedHeight(1920, 1080, 1080), 1080); assert.equal(fittedHeight(640, 360, 1080), 360); assert.equal(fittedHeight(3840, 2160, 1080), 1080); assert.equal(fittedHeight(1080, 1920, 1080), 1080); assert.equal(fittedHeight(null, null, 1080), null); }); test("listing: plain only by default; with tiers, each window with its tier and recorded height", async () => { await withVideo(async ({ videoDir, clips }) => { await put(clips, "10.00-20.00.mp4"); await put(clips, "10.00-20.00.json", JSON.stringify({ requestedBy: "umtool", height: 720 })); await put(clips, "10.00-20.00.h1080.mp4", "taller"); await put(clips, "10.00-20.00.h1080.json", JSON.stringify({ requestedBy: "umtool", height: 1080 })); await put(clips, "10.00-20.00.h1080.part.mp4", "in flight"); assert.deepEqual((await listClipWindows(videoDir)).map((w) => w.file), ["10.00-20.00.mp4"]); const all = await listClipWindows(videoDir, { tiers: true }); assert.deepEqual( all.map((w) => [w.file, w.tier ?? null, w.height ?? null, w.provenance?.height ?? null]), [ ["10.00-20.00.mp4", null, 720, 720], ["10.00-20.00.h1080.mp4", 1080, 1080, 1080], ], ); }); }); test("a window with no recorded height is probed ONCE; the answer goes into its sidecar (made when missing)", async () => { await withVideo(async ({ videoDir, clips }) => { await put(clips, "10.00-20.00.mp4"); await put(clips, "10.00-20.00.json", JSON.stringify({ requestedBy: "umtool", reason: "kept" })); await put(clips, "30.00-40.00.mp4"); const before = await stat(path.join(clips, "10.00-20.00.mp4")); const calls: string[] = []; const probe = async (f: string) => { calls.push(path.basename(f)); return 360; }; for (const w of await listClipWindows(videoDir)) assert.equal(await windowHeightOf(w, probe), 360); assert.deepEqual(calls.sort(), ["10.00-20.00.mp4", "30.00-40.00.mp4"]); // Merged: the provenance is kept beside the height. assert.deepEqual(JSON.parse(await readFile(path.join(clips, "10.00-20.00.json"), "utf8")), { requestedBy: "umtool", reason: "kept", height: 360, }); // Made: a height-only sidecar, which parses as no provenance. assert.deepEqual(JSON.parse(await readFile(path.join(clips, "30.00-40.00.json"), "utf8")), { height: 360 }); // The next ask reads the record: no probe. for (const w of await listClipWindows(videoDir)) await windowHeightOf(w, probe); assert.equal(calls.length, 2); // The window itself is not touched. assert.equal((await stat(path.join(clips, "10.00-20.00.mp4"))).mtimeMs, before.mtimeMs); // Unreadable: nothing recorded. await put(clips, "50.00-60.00.mp4"); const w = (await listClipWindows(videoDir)).find((x) => x.from === 50)!; assert.equal(await windowHeightOf(w, async () => null), null); assert.ok(!(await readdir(clips)).includes("50.00-60.00.json")); }); }); test("the tall lookup: the tallest at or under the ask first, then a taller one; unknown is lowest", async () => { await withVideo(async ({ videoDir, clips }) => { await put(clips, "0.00-60.00.mp4"); await put(clips, "0.00-60.00.json", JSON.stringify({ height: 720 })); await put(clips, "5.00-25.00.mp4"); await put(clips, "5.00-25.00.json", JSON.stringify({ height: 720 })); await put(clips, "0.00-60.00.h1080.mp4"); await put(clips, "0.00-60.00.h1080.json", JSON.stringify({ height: 1080 })); await put(clips, "0.00-60.00.h1440.mp4"); await put(clips, "0.00-60.00.h1440.json", JSON.stringify({ height: 1440 })); await put(clips, "8.00-22.00.mp4"); const order = await tallClipWindowCandidates(videoDir, 10, 20, 1080, async () => null); assert.deepEqual(order.map((w) => w.file), [ "0.00-60.00.h1080.mp4", // Two at 720: the tighter first. "5.00-25.00.mp4", "0.00-60.00.mp4", // Unknown height counts as the lowest. "8.00-22.00.mp4", // Taller than asked: still a hit before any fetch. "0.00-60.00.h1440.mp4", ]); // The sync-checked lookup: a tall ask gets the 1080; any other ask the plain tightest, as before. const tall = await findInSyncContainingClipWindow(videoDir, 10, 20, { probe: async () => null, maxHeight: 1080, heightProbe: async () => null }); assert.equal(tall.hit?.file, "0.00-60.00.h1080.mp4"); assert.equal(tall.hit?.height, 1080); const plain = await findInSyncContainingClipWindow(videoDir, 10, 20, { probe: async () => null }); assert.equal(plain.hit?.file, "8.00-22.00.mp4"); const at720 = await findInSyncContainingClipWindow(videoDir, 10, 20, { probe: async () => null, maxHeight: 720, heightProbe: async () => null }); assert.equal(at720.hit?.file, "8.00-22.00.mp4"); }); }); const covers = (height: number | null, hasVideo = true): SavedWindowSource => ({ kind: "covers", pointer: { storedAt: "", dir: "/s", file: "source-media.mp4", bytes: 1 }, container: "/s/source-media.mp4", durationSec: 100, probe: { durationSec: 100, width: height ? Math.round((height * 16) / 9) : null, height, hasVideo, hasAudio: true }, }) as SavedWindowSource; async function pointer(videoDir: string, format?: { height: number }) { await writeFile( path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "2026-11-13T00:00:00.000Z", dir: "/s", file: "source-media.mp4", bytes: 1, ...(format ? { format: { preset: "original", ...format } } : {}) }), ); } test("taller from the saved copy: only when the copy is taller than the window; no copy is the window as it is", async () => { await withVideo(async ({ videoDir, paths }) => { const base = { paths, slug: "demo", videoDir, from: 10, to: 20, cap: 1080 }; // No saved copy: lower, and never a fetch. assert.deepEqual(await tallerFromSaved({ ...base, hitHeight: 720 }), { kind: "lower", why: NO_SAVED_COPY_WHY }); // A recorded 720 copy rules it out without a probe. await pointer(videoDir, { height: 720 }); let asked = 0; const src = (s: SavedWindowSource) => async () => { asked += 1; return s; }; const r1 = await tallerFromSaved({ ...base, hitHeight: 720, source: src(covers(720)) }); assert.equal(r1.kind, "lower"); assert.match((r1 as { why: string }).why, /saved copy is 720p/); assert.equal(asked, 0); // A 1080 copy: cut at 1080. await pointer(videoDir, { height: 1080 }); const r2 = await tallerFromSaved({ ...base, hitHeight: 720, source: src(covers(1080)) }); assert.equal(r2.kind, "cut"); assert.equal((r2 as { height: number }).height, 1080); // A 1440 copy with no recorded height, asked 1080: cut at the ask, and the // probed height kept on the pointer so the next ask needs no probe (I). await pointer(videoDir); const r3 = await tallerFromSaved({ ...base, hitHeight: null, source: src(covers(1440)) }); assert.equal((r3 as { height: number }).height, 1080); assert.equal(JSON.parse(await readFile(path.join(videoDir, "saved-video.json"), "utf8")).probed.height, 1440); // A copy short of the ask, probed (no recorded height): "lower" on the first // ask as on the second (the pointer exit), never "best". await pointer(videoDir); const short1 = await tallerFromSaved({ ...base, hitHeight: 720, source: src(covers(720)) }); assert.equal(short1.kind, "lower"); assert.match((short1 as { why: string }).why, /saved copy is 720p/); const short2 = await tallerFromSaved({ ...base, hitHeight: 720, source: src(covers(720)) }); assert.equal(short2.kind, "lower"); await pointer(videoDir); // A copy that does not hold the span, or has no picture: lower, with why. const r4 = await tallerFromSaved({ ...base, hitHeight: 720, source: async () => ({ kind: "not-covering", container: "/s", durationSec: 5 }) }); assert.match((r4 as { why: string }).why, /does not hold this span/); const r5 = await tallerFromSaved({ ...base, hitHeight: 720, source: src(covers(null, false)) }); assert.match((r5 as { why: string }).why, /cannot be read/); }); }); test("a site's evidence lookup reads the plain window even beside a taller one; a tall ask reads the taller", async () => { await withVideo(async ({ clips, root }) => { await put(clips, "10.00-20.00.mp4"); await put(clips, "10.00-20.00.json", JSON.stringify({ requestedBy: "reports", height: 720 })); await put(clips, "0.00-30.00.h1080.mp4"); await put(clips, "0.00-30.00.h1080.json", JSON.stringify({ requestedBy: "umtool", height: 1080 })); const channelsDir = path.join(root, "channels"); const want = { video: "v1", slug: "demo", from: 12, to: 18 }; const site = await resolveCorpusSource(want, { channelsDir, platform: "youtube" }); assert.equal(site?.name, "10.00-20.00.mp4"); const tall = await resolveCorpusSource(want, { channelsDir, platform: "youtube", maxHeight: 1080, probe: async () => null }); assert.equal(tall?.name, "0.00-30.00.h1080.mp4"); assert.equal(tall?.height, 1080); // At or under 720 the ask is the plain path. const at720 = await resolveCorpusSource(want, { channelsDir, platform: "youtube", maxHeight: 720 }); assert.equal(at720?.name, "10.00-20.00.mp4"); }); });