import test from "node:test"; import assert from "node:assert/strict"; import { clipWindowContains, clipWindowFile, clipWindowName, clipWindowSidecar, isFetchMaxHeight, parseClipProvenance, parseClipWindowName, tightestClipWindow, WIN_EPS, } from "./clipWindow"; import { parseSavedVideoPointer } from "./savedVideo"; test("a window name round-trips through two decimals", () => { assert.equal(clipWindowName(12, 42), "12.00-42.00"); assert.equal(clipWindowFile(12, 42), "12.00-42.00.mp4"); assert.equal(clipWindowSidecar(12, 42), "12.00-42.00.json"); // The rounding is the name's, so a request carrying more precision than the // name can hold still addresses ONE file. assert.equal(clipWindowName(12.004, 41.999), "12.00-42.00"); assert.deepEqual(parseClipWindowName("12.00-42.00.mp4"), { from: 12, to: 42 }); // A window an older build (or a hand-run yt-dlp that ignored the format pin) // left behind. Unreadable names were bytes nothing counted and nothing could // evict, which is the worse half of being invisible. assert.deepEqual(parseClipWindowName("12.00-42.00.mkv"), { from: 12, to: 42 }); assert.deepEqual(parseClipWindowName("12.00-42.00.webm"), { from: 12, to: 42 }); // NOT a sidecar: it is removed with the window it describes and must never // parse as one. assert.equal(parseClipWindowName("12.00-42.00.json"), null); assert.deepEqual(parseClipWindowName("12.00-42.00"), { from: 12, to: 42 }); assert.deepEqual(parseClipWindowName("0.00-1234.50.mp4"), { from: 0, to: 1234.5, }); }); test("only -.mp4 parses as a window", () => { for (const name of [ "12.00-42.00.json", ".12.00-42.00.mp4.part", "audio.mp3", "source-media.mp4", "12.00.mp4", "-42.00.mp4", "42.00-12.00.mp4", // to <= from is not a window "12.00-12.00.mp4", "abc-def.mp4", ]) { assert.equal(parseClipWindowName(name), null, name); } }); test("containment is to the naming tolerance, not exact", () => { const w = { from: 10, to: 20 }; assert.ok(clipWindowContains(w, 10, 20)); assert.ok(clipWindowContains(w, 12, 18)); // A 2 dp manifest can ask for a hair outside the file that produced it. assert.ok(clipWindowContains(w, 10 - WIN_EPS / 2, 20 + WIN_EPS / 2)); assert.ok(!clipWindowContains(w, 9, 20)); assert.ok(!clipWindowContains(w, 10, 21)); }); test("the tightest container wins, and overlap alone does not count", () => { const windows = [ { from: 0, to: 100, name: "wide" }, { from: 9, to: 21, name: "tight" }, { from: 5, to: 50, name: "middle" }, { from: 15, to: 30, name: "overlap-only" }, ]; assert.equal(tightestClipWindow(windows, 10, 20)?.name, "tight"); assert.equal(tightestClipWindow(windows, 60, 70)?.name, "wide"); assert.equal(tightestClipWindow(windows, 99, 101), null); assert.equal(tightestClipWindow([], 1, 2), null); }); test("provenance parses tolerantly and refuses an anonymous sidecar", () => { const full = parseClipProvenance({ requestedBy: "umtool", manifest: "elfpire-eva", clipId: "c03", reason: "the clip ends mid-sentence", requestedAt: "2026-09-20T00:00:00.000Z", pad: 20, bytes: 1234, fetchedAt: "2026-09-20T00:00:05.000Z", ytdlp: { args: ["--ignore-config", "-f", "best"] }, }); assert.equal(full?.requestedBy, "umtool"); assert.equal(full?.clipId, "c03"); assert.deepEqual(full?.ytdlp?.args, ["--ignore-config", "-f", "best"]); // The requester is the one required field: a sidecar that cannot say who // asked is not provenance. assert.equal(parseClipProvenance({ manifest: "x" }), null); assert.equal(parseClipProvenance(null), null); assert.equal(parseClipProvenance("umtool"), null); assert.equal(parseClipProvenance([]), null); // Junk in the optional fields degrades to absent, never to a throw. const partial = parseClipProvenance({ requestedBy: "umtool", manifest: 7, pad: "twenty", ytdlp: { args: ["ok", 3, null] }, }); assert.equal(partial?.manifest, undefined); assert.equal(partial?.pad, undefined); assert.deepEqual(partial?.ytdlp?.args, ["ok"]); }); test("a saved-video pointer written before `origin` parses unchanged", () => { const legacy = parseSavedVideoPointer({ storedAt: "2026-01-01T00:00:00.000Z", dir: "/store/chan/vid", file: "source-media.mp4", bytes: 42, keepReason: "override", }); assert.equal(legacy?.origin, undefined); assert.equal(legacy?.keepReason, "override"); const sourced = parseSavedVideoPointer({ storedAt: "2026-01-01T00:00:00.000Z", dir: "/store/chan/vid", file: "source-media.mp4", bytes: 42, keepReason: "override", origin: { requestedBy: "umtool", manifest: "m", clipId: "c01", reason: "" }, }); assert.equal(sourced?.origin?.requestedBy, "umtool"); assert.equal(sourced?.origin?.clipId, "c01"); // An empty reason is absent, not "". assert.equal(sourced?.origin?.reason, undefined); // A requester-less origin is dropped without taking the pointer with it. const bad = parseSavedVideoPointer({ dir: "/d", file: "f.mp4", bytes: 1, origin: { manifest: "m" }, }); assert.ok(bad); assert.equal(bad?.origin, undefined); }); test("isFetchMaxHeight: a whole number of pixels from 144 to 2160, nothing else", () => { for (const ok of [144, 360, 720, 1080, 2160]) assert.ok(isFetchMaxHeight(ok), String(ok)); for (const bad of [143, 2161, 720.5, 0, -720, Number.NaN, Infinity, "720", null, undefined]) { assert.ok(!isFetchMaxHeight(bad), String(bad)); } });