import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { findContainingClipWindow, listClipWindows } from "./clipWindow-server"; // THE LISTER AND THE EVICTOR MUST AGREE ABOUT WHAT A WINDOW IS. // // They did not: `listClipWindows` pre-filtered on `.mp4` while // `parseClipWindowName` had been widened, so a `.mkv` window was invisible to // the video page and to `findContainingClipWindow` — the fetch dedup, which // would then have paid a source for those seconds again — while // `evictClipWindows` could see and delete it. A file one half of the code owns // and the other cannot see is the worst of both. async function withClips( files: Record, fn: (videoDir: string) => Promise, ): Promise { const dir = await mkdtemp(path.join(tmpdir(), "ttb-clipwin-")); try { const clips = path.join(dir, "clips"); await mkdir(clips, { recursive: true }); for (const [name, body] of Object.entries(files)) { await writeFile(path.join(clips, name), body); } await fn(dir); } finally { await rm(dir, { recursive: true, force: true }); } } test("a .webm window lists, and the fetch dedup finds it", async () => { await withClips( { "10.00-40.00.webm": "not really webm", "10.00-40.00.json": JSON.stringify({ requestedBy: "umtool" }), }, async (videoDir) => { const windows = await listClipWindows(videoDir); assert.deepEqual( windows.map((w) => w.file), ["10.00-40.00.webm"], ); assert.equal(windows[0].from, 10); assert.equal(windows[0].to, 40); // The sidecar was read BY the window, never listed AS one. assert.equal(windows[0].provenance?.requestedBy, "umtool"); const found = await findContainingClipWindow(videoDir, 12, 30); assert.equal(found?.file, "10.00-40.00.webm"); // And a span it does not hold is still a miss. assert.equal(await findContainingClipWindow(videoDir, 12, 90), null); }, ); }); test("all three extensions list, and nothing else does", async () => { await withClips( { "1.00-2.00.mp4": "a", "3.00-4.00.mkv": "b", "5.00-6.00.webm": "c", // A sidecar with no window: it has no media extension, so it is not a // window — and listing it would put a file with no bytes of media in // front of the operator as one. "7.00-8.00.json": "{}", // Not a `-` name at all. "notes.txt": "d", "audio.mp4": "e", }, async (videoDir) => { const windows = await listClipWindows(videoDir); assert.deepEqual( windows.map((w) => w.file).sort(), ["1.00-2.00.mp4", "3.00-4.00.mkv", "5.00-6.00.webm"], ); }, ); }); test("a video dir with no clips/ lists nothing rather than throwing", async () => { const dir = await mkdtemp(path.join(tmpdir(), "ttb-clipwin-")); try { assert.deepEqual(await listClipWindows(dir), []); assert.equal(await findContainingClipWindow(dir, 1, 2), null); } finally { await rm(dir, { recursive: true, force: true }); } });