// render.tallWindows (the height tier's opt-in, report-to-video/sources.mjs // tallWindowCap): the build and every tool that vouches for its sources // resolve ONE file; without the knob a project resolves exactly as before — // no probe, no height index, no tiered window read — even beside a taller // saved copy and a tiered window; and every call site states its render. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { planQuoteCheck } from "./quoteCheck.mjs"; import { localSources } from "umtool-report-to-video/build-video"; import { rawWindowName, resolveLocalSource, tallWindowCap } from "umtool-report-to-video/sources"; import { fetchWindowsBody, markRefetched, refetchEvictions, refetchTiers } from "./fetchWindows.mjs"; const SLUG = "demo"; const VIDEO = "vid1"; const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../.."); async function project() { const dir = await mkdtemp(path.join(os.tmpdir(), "tall-windows-")); const rawDir = path.join(dir, "out", "clips-raw"); const channelsDir = path.join(dir, "channels"); const videoDir = path.join(channelsDir, SLUG, "data", VIDEO); const clips = path.join(videoDir, "clips"); const store = path.join(dir, "store"); for (const d of [rawDir, clips, store]) await mkdir(d, { recursive: true }); // A 720 raw file, a 720 corpus window, a 1080 tiered window elsewhere, and a 1080 saved copy. await writeFile(path.join(rawDir, rawWindowName(VIDEO, 0, 60)), "raw"); await writeFile(path.join(clips, "0.00-60.00.mp4"), "plain"); await writeFile(path.join(clips, "0.00-60.00.json"), JSON.stringify({ requestedBy: "reports", height: 720 })); await writeFile(path.join(clips, "5.00-55.00.h1080.mp4"), "tiered"); await writeFile(path.join(clips, "5.00-55.00.h1080.json"), JSON.stringify({ requestedBy: "umtool", height: 1080 })); await writeFile(path.join(store, "source-media.mp4"), "whole"); await writeFile( path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "", dir: store, file: "source-media.mp4", bytes: 5, format: { preset: "original", height: 1080 } }), ); return { dir, rawDir, channelsDir }; } const manifestOf = (render) => ({ render, provenance: { channelSlug: SLUG }, timeline: [{ type: "clip", id: "c1", video: VIDEO, start: 10, end: 20, quote: "hello there" }], }); function counting() { const calls = []; return { calls, probe: async (f) => (calls.push(f), { duration: 600, height: 720 }) }; } test("tallWindowCap: only render.tallWindows turns it on; maxHeightSource alone is no switch", () => { assert.equal(tallWindowCap({ maxHeightSource: 1080 }), undefined); assert.equal(tallWindowCap({ tallWindows: true, maxHeightSource: 1080 }), 1080); assert.equal(tallWindowCap({ tallWindows: true }), 1080); assert.equal(tallWindowCap({ tallWindows: true, maxHeightSource: 720 }), undefined); assert.equal(tallWindowCap(null), undefined); }); test("with the knob on, quote-check vouches for the very file the build cuts (the taller saved copy)", async () => { const t = await project(); try { const manifest = manifestOf({ tallWindows: true, maxHeightSource: 1080 }); const { probe } = counting(); const [row] = await planQuoteCheck(t.dir, manifest, { channelsDirs: [t.channelsDir], rawDir: t.rawDir, probe, pad: 0 }); const local = localSources({ rawDir: t.rawDir, channelsDir: t.channelsDir, channelSlug: SLUG, probe, render: manifest.render }); const built = await local.resolve(manifest.timeline[0], row.cut); assert.equal(row.source.path, built.path); assert.notEqual(built.kind, "raw-cache", "the 720 raw file gave way"); assert.equal(built.height, 1080); } finally { await rm(t.dir, { recursive: true, force: true }); } }); test("without the knob a project resolves as before: the raw file, no probe, no height index, no tier read", async () => { const t = await project(); try { const manifest = manifestOf({ maxHeightSource: 1080 }); const { calls, probe } = counting(); const want = { video: VIDEO, slug: SLUG, from: 10, to: 20 }; const plain = await resolveLocalSource(want, { rawDir: t.rawDir, channelsDir: t.channelsDir, probe, render: manifest.render }); // What main resolves: the raw file, exactly this shape, nothing else. assert.deepEqual(plain, { kind: "raw-cache", path: path.join(t.rawDir, rawWindowName(VIDEO, 0, 60)), name: rawWindowName(VIDEO, 0, 60), windowStart: 0, windowEnd: 60, }); const [row] = await planQuoteCheck(t.dir, manifest, { channelsDirs: [t.channelsDir], rawDir: t.rawDir, probe, pad: 0 }); assert.equal(row.source.path, plain.path); // The corpus alone: the plain window, never the tiered one. const corpus = await resolveLocalSource(want, { channelsDir: t.channelsDir, probe, render: manifest.render }); assert.equal(corpus.name, "0.00-60.00.mp4"); assert.equal(corpus.height, undefined); assert.deepEqual(calls, [], "nothing probed"); assert.ok(!(await readdir(path.join(t.dir, "out"))).includes("clips-raw.heights.json")); } finally { await rm(t.dir, { recursive: true, force: true }); } }); test("umtool fetch sends a window height only with the knob; a refetch evicts its own tier only", () => { const off = fetchWindowsBody(manifestOf({ maxHeightSource: 1080 }), { manifestPath: "/x/m.json", project: "p" }).body; assert.equal("maxHeight" in off, false); assert.equal(refetchTiers(off), "plain"); const on = fetchWindowsBody(manifestOf({ tallWindows: true, maxHeightSource: 1080 }), { manifestPath: "/x/m.json", project: "p" }).body; assert.equal(on.maxHeight, 1080); assert.equal(refetchTiers(on), "tiered"); }); // SO CALLERS CANNOT DRIFT: every lookup outside the tests states the render it // resolves for (sources.mjs tallWindowCap reads it) -- `render: null` for one // that is not a cut's (a reference recording's sound). test("every resolveLocalSource / localSources / corpusWindowsOf call site states a render", async () => { const { execFileSync } = await import("node:child_process"); const files = execFileSync("git", ["ls-files", "umtool/*.mjs", "umtool/*.ts", "umtool/**/*.mjs", "umtool/**/*.ts"], { cwd: REPO, encoding: "utf8" }) .split("\n") .filter((f) => f && !/\.test\.|\/e2e\/|\.d\.ts$/.test(f)); const missing = []; for (const f of files) { const text = await readFile(path.join(REPO, f), "utf8"); const re = /(?:resolveLocalSource|localSources|corpusWindowsOf)\s*\)?\s*\(/g; for (let m = re.exec(text); m; m = re.exec(text)) { const before = text.slice(Math.max(0, m.index - 30), m.index); if (/function\s+$|export\s+$|\.\s*$/.test(before) || /import|typeof/.test(before)) continue; // The call's text: to its closing parenthesis (depth-counted). let depth = 0; let i = m.index + m[0].length - 1; for (; i < text.length; i++) { if (text[i] === "(") depth++; else if (text[i] === ")" && --depth === 0) break; } const call = text.slice(m.index, i + 1); if (!/\brender\b/.test(call)) missing.push(`${f}:${text.slice(0, m.index).split("\n").length}`); } } assert.deepEqual(missing, []); }); test("a refetch marks the items it evicts for (refetch: true), and only those", () => { const manifest = { ...manifestOf({ tallWindows: true, maxHeightSource: 1080 }), timeline: [ { type: "clip", id: "c1", video: VIDEO, start: 10, end: 20 }, { type: "clip", id: "c2", video: VIDEO, start: 300, end: 310 }, ], }; const { body, skipped } = fetchWindowsBody(manifest, { manifestPath: "/x/m.json", project: "p" }); const plan = refetchEvictions(body, skipped, { clipIds: ["c1"] }); assert.ok(plan.ok); markRefetched(body, plan.requests); assert.deepEqual(body.items.map((i) => [i.clipId, i.refetch === true]), [["c1", true], ["c2", false]]); });