// A recording is not a window: the bench is given an excerpt of a long file. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import path from "node:path"; import os from "node:os"; import { execFileSync } from "node:child_process"; import { mkdtemp, readFile, rename, rm, stat, utimes, writeFile } from "node:fs/promises"; const root = await mkdtemp(path.join(os.tmpdir(), "bench-excerpt-")); // The roots are read when paths.mjs loads: this file's temp dir is one. process.env.MIX_ROOTS = root; const { excerptOf, excerptSourceOf, ensureWindowFile, windowEtag, windowFileFresh, windowVersion, EXCERPT_OVER_S } = await import("./serve.mjs"); test.after(() => rm(root, { recursive: true, force: true })); const probe = (f) => JSON.parse(execFileSync("ffprobe", ["-v", "error", "-show_entries", "stream=codec_type,start_time,duration:format=duration", "-of", "json", f]).toString()); test("a window is left alone; a recording gets the clip and a minute either side, edges rounded out", () => { const win = { name: "100.00-160.00.mp4", path: "/x/100.00-160.00.mp4", from: 100, to: 160 }; assert.equal(excerptOf(win, { video: "v", start: 110, end: 120 }, "/p"), null); const rec = { name: "source-media.mp4", path: "/s/source-media.mp4", from: 0, to: 13554 }; const e = excerptOf(rec, { video: "v7gkv5w", start: 4129.16, end: 4147.13 }, "/p"); assert.equal(e.from, 4080); assert.equal(e.to, 4200); assert.equal(e.name, "4080.00-4200.00.excerpt.mp4"); assert.equal(e.path, path.join("/p", "out", "bench-excerpts", "v7gkv5w", e.name)); assert.deepEqual(e.excerptOf, { name: "source-media.mp4", path: "/s/source-media.mp4", from: 0 }); // Clamped to the recording, and a small trim inside the rounding reuses the same file. assert.equal(excerptOf(rec, { video: "v", start: 20, end: 30 }, "/p").from, 0); assert.equal(excerptOf(rec, { video: "v", start: 4129.5, end: 4146 }, "/p").name, e.name); assert.ok(EXCERPT_OVER_S >= 300); assert.equal(excerptOf(rec, { video: "v" }, "/p"), null, "no span, no excerpt"); }); test("the excerpt is cut once, frame-exact, both tracks from zero", async () => { const src = path.join(root, "source-media.mp4"); execFileSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25:duration=40", "-f", "lavfi", "-i", "sine=frequency=440:duration=40", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", src, ]); const rec = { name: "source-media.mp4", path: src, from: 0, to: 40 }; const e = excerptOf(rec, { video: "vid", start: 18, end: 22 }, root, { over: 10, pad: 3, step: 5 }); assert.deepEqual([e.from, e.to], [15, 25]); await Promise.all([ensureWindowFile(e), ensureWindowFile(e)]); const st = await stat(e.path); const p = probe(e.path); const v = p.streams.find((s) => s.codec_type === "video"); const a = p.streams.find((s) => s.codec_type === "audio"); assert.ok(Number(v.start_time) <= 0.05 && Number(a.start_time) <= 0.05, JSON.stringify(p.streams)); assert.ok(Math.abs(Number(p.format.duration) - 10) < 0.2, p.format.duration); // A second ask finds it and does not cut again. await ensureWindowFile(e); assert.equal((await stat(e.path)).mtimeMs, st.mtimeMs); // A real window is never touched. await ensureWindowFile({ name: "w.mp4", path: path.join(root, "nope.mp4"), from: 0, to: 1 }); }); test("a replaced recording -- even one with an OLDER mtime -- makes its excerpt stale, and it is cut again", async () => { const src = path.join(root, "replaced-media.mp4"); const make = (pattern, out) => execFileSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", `${pattern}=size=320x240:rate=25:duration=40`, "-f", "lavfi", "-i", "sine=frequency=440:duration=40", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", out, ]); make("testsrc2", src); const rec = { name: "replaced-media.mp4", path: src, from: 0, to: 40 }; const e = excerptOf(rec, { video: "rep", start: 18, end: 22 }, root, { over: 10, pad: 3, step: 5 }); await ensureWindowFile(e); assert.equal(await windowFileFresh(e), true); const side = JSON.parse(await readFile(excerptSourceOf(e.path), "utf8")); assert.equal(side.path, src); assert.equal(side.size, (await stat(src)).size, "the sidecar records the source it was cut from"); const first = await stat(e.path); const v1 = await windowVersion(e); assert.equal(v1, await windowVersion(rec), "an excerpt's version is its source's"); // The real case: the excerpt was cut AFTER its source's mtime, and a good // copy lands under the same name dated earlier still (yt-dlp --mtime; the // saved-video replace). "Newer than its source" said fresh, and Play showed // the corrupt cut for good. const next = path.join(root, "replaced-media.next.mp4"); make("smptebars", next); const old = new Date(first.mtimeMs - 3_600_000); await utimes(next, old, old); await rename(next, src); assert.ok((await stat(src)).mtimeMs < first.mtimeMs, "the replacement is older than the excerpt"); assert.equal(await windowFileFresh(e), false, "a replaced source makes the excerpt stale"); assert.notEqual(await windowVersion(e), v1, "and its version changes, so the URL does"); await ensureWindowFile(e); assert.equal(await windowFileFresh(e), true); assert.notEqual((await stat(e.path)).ino, first.ino, "cut again"); // An excerpt with no sidecar (cut before there were any) is stale too. await rm(excerptSourceOf(e.path)); assert.equal(await windowFileFresh(e), false); await ensureWindowFile(e); assert.equal(await windowFileFresh(e), true); }); test("a window's version changes with its size, its mtime, its inode -- and a rewrite in place that puts the mtime back", async () => { const f = path.join(root, "w_0.00-10.00.mp4"); await writeFile(f, Buffer.alloc(1000, 1)); const t = new Date("2026-01-01T00:00:00Z"); await utimes(f, t, t); const win = { name: "w_0.00-10.00.mp4", path: f, from: 0, to: 10 }; const v0 = await windowVersion(win); assert.match(String(v0), /^[0-9a-f]{16}$/); assert.equal(await windowVersion(win), v0, "unchanged, the same"); const seen = new Set([v0]); const isNew = async (why) => { const v = await windowVersion(win); assert.ok(!seen.has(v), why); seen.add(v); }; // The same bytes under the same name, by rename, with the old mtime: a new inode. await writeFile(`${f}.tmp`, Buffer.alloc(1000, 1)); await utimes(`${f}.tmp`, t, t); await rename(`${f}.tmp`, f); await isNew("a replace by rename is a new file"); // Rewritten in place, the same size, the mtime put back: only ctime says so. await new Promise((r) => setTimeout(r, 20)); await writeFile(f, Buffer.alloc(1000, 2)); await utimes(f, t, t); await isNew("a rewrite in place with its mtime restored is a new version"); await writeFile(f, Buffer.alloc(1001, 2)); await utimes(f, t, t); await isNew("another size"); const later = new Date("2026-02-01T00:00:00Z"); await utimes(f, later, later); await isNew("another mtime"); assert.equal(await windowVersion({ name: "gone.mp4", path: path.join(root, "gone.mp4"), from: 0, to: 1 }), null); assert.equal(await windowVersion({ name: "x.mp4", path: "/etc/passwd", from: 0, to: 1 }), null, "outside the roots"); // The ETag of what is sent: the version, and for an excerpt its own file too. const st = await stat(f); assert.equal(windowEtag("abc", win, st), '"abc"'); assert.match(windowEtag("abc", { excerptOf: {} }, st), /^"abc\.[0-9a-f]{16}"$/); });