import test from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test 'app/channels/*/videos/fetchWindowsTier.test.ts' // // The fetch-windows PLAN and THE HEIGHT TIER (lib/clipWindow.ts): a tall ask // over a held window lower than the saved copy plans a cut beside it on the // saved-video queue; one over a window as tall as the saved copy answers it // as cached and lists it in `lower` with why; no ask plans as before. A temp // corpus with a real (tiny) saved container; nothing is started. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-windows-tier-")); const SLUG = "demo-channel"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); delete process.env.FFPROBE_BIN; delete process.env.FFMPEG_BIN; await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 })); await mkdir(path.join(ROOT, "channels", SLUG), { recursive: true }); await writeFile( path.join(ROOT, "channels", SLUG, "config.json"), JSON.stringify({ name: "Demo", handling: "youtube", url: "https://www.youtube.com/@demo" }), ); // A video whose saved copy is a 160×90 picture, with a held window [0, 8] // whose sidecar records `height`. async function video(id: string, heldHeight: number): Promise { const videoDir = path.join(ROOT, "channels", SLUG, "data", id); const store = path.join(ROOT, "saved-videos", SLUG, id); const clips = path.join(videoDir, "clips"); await mkdir(clips, { recursive: true }); await mkdir(store, { recursive: true }); await writeFile(path.join(videoDir, "metadata.info.json"), JSON.stringify({ id, webpage_url: `https://www.youtube.com/watch?v=${id}` })); execFileSync("ffmpeg", [ "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=8", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", path.join(store, "source-media.mp4"), ]); await writeFile( path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "", dir: store, file: "source-media.mp4", bytes: 1, keepReason: "pin" }), ); await writeFile(path.join(clips, "0.00-8.00.mp4"), "held"); await writeFile(path.join(clips, "0.00-8.00.json"), JSON.stringify({ requestedBy: "reports", height: heldHeight })); } await video("short1", 72); await video("equal1", 90); const { fetchWindowsAction } = await import("./fetchWindowsAction"); test.after(() => rm(ROOT, { recursive: true, force: true })); const items = [ { slug: SLUG, id: "short1", from: 1, to: 3, clipId: "c#1" }, { slug: SLUG, id: "equal1", from: 1, to: 3, clipId: "c#2" }, ]; test("a tall ask plans a taller cut beside a window lower than the saved copy, and lists the one that cannot be taller", async () => { const r = await fetchWindowsAction({ items, requestedBy: "umtool", maxHeight: 1080, dryRun: true }); assert.ok(r.ok); if (!r.ok) return; assert.deepEqual( r.groups.map((g) => [g.platform, g.items.map((i) => i.clipId)]), [["saved-video", ["c#1"]]], ); // A 90p copy is itself short of the 1080 ask: "lower", with why (not "best"). assert.deepEqual(r.cached.map((i) => i.clipId), ["c#2"]); assert.equal(r.lower?.length, 1); assert.equal(r.lower![0].item.clipId, "c#2"); assert.equal(r.lower![0].height, 90); assert.equal(r.lower![0].asked, 1080); assert.match(r.lower![0].why, /saved copy is 90p/); }); test("no ask plans as before: both held windows are cached, nothing listed lower", async () => { const r = await fetchWindowsAction({ items, requestedBy: "umtool", dryRun: true }); assert.ok(r.ok); if (!r.ok) return; assert.deepEqual(r.cached.map((i) => i.clipId), ["c#1", "c#2"]); assert.equal(r.groups.length, 0); assert.equal(r.lower, undefined); }); test("an ask above clips.windowMaxHeight is planned at the ceiling, and says so", async () => { await writeFile(process.env.SETTINGS_FILE!, JSON.stringify({ minFreeDiskGB: 0, clips: { windowMaxHeight: 1080 } })); const r = await fetchWindowsAction({ items: [items[1]], requestedBy: "umtool", maxHeight: 2160, dryRun: true }); assert.ok(r.ok); if (!r.ok) return; assert.equal(r.clampedFrom, 2160); assert.equal(r.lower?.[0]?.asked, 1080); }); test("a tall refetch is not answered by a held window: planned onto the saved copy's cut", async () => { await writeFile(process.env.SETTINGS_FILE!, JSON.stringify({ minFreeDiskGB: 0 })); const r = await fetchWindowsAction({ items: [{ ...items[1], refetch: true }], requestedBy: "umtool", maxHeight: 1080, dryRun: true }); assert.ok(r.ok); if (!r.ok) return; assert.deepEqual(r.cached, []); assert.deepEqual(r.groups.map((g) => [g.platform, g.items.map((i) => i.clipId)]), [["saved-video", ["c#2"]]]); assert.equal(r.lower, undefined); });