// The heavy slot for a render (heavy-gate.mjs) and which build-video runs // take it (takesHeavySlot). // // The real machine-wide lock is never taken here: every run that reaches the // gate names its own HEAVY_LOCK_FILE in a temp dir, with the floor at 0. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { existsSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { checkoutRoot, heavyLockFile, withRenderSlot } from "./heavy-gate.mjs"; import { takesHeavySlot } from "./build-video.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); /** Run `fn` with process.env patched, restoring every key after. */ async function withEnv(patch, fn) { const saved = Object.fromEntries(Object.keys(patch).map((k) => [k, process.env[k]])); for (const [k, v] of Object.entries(patch)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } try { return await fn(); } finally { for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } } } test("which runs take the slot: every one that renders the cut, never a fetch, --only, the chapters or the thumbnail", () => { assert.equal(takesHeavySlot({ opts: {} }), true, "a full build"); assert.equal(takesHeavySlot({ opts: { chromeOnly: true } }), true); assert.equal(takesHeavySlot({ opts: { chromePreview: { at: 0, dur: 2 } } }), true); assert.equal(takesHeavySlot({ opts: { railOnly: true } }), true); assert.equal(takesHeavySlot({ opts: { noChrome: true } }), true); assert.equal(takesHeavySlot({ opts: {}, fetchOnly: "c1" }), false); assert.equal(takesHeavySlot({ opts: {}, only: "c1" }), false); assert.equal(takesHeavySlot({ opts: { chaptersOnly: true } }), false); assert.equal(takesHeavySlot({ opts: { thumbnailOnly: true } }), false); }); // A container image ships the tree without its .git: no common dir, so no // lock file to find (the gate then renders without the slot, with a note -- // the next test). Asked of git itself, not of the code under test. const inGitCheckout = (() => { const r = spawnSync("git", ["-C", checkoutRoot(), "rev-parse", "--git-common-dir"], { stdio: ["ignore", "pipe", "ignore"] }); return r.status === 0 && String(r.stdout).trim() !== ""; })(); test("the checkout's own lock file: beside the e2e queue's, in its git common dir", { skip: !inGitCheckout && "this checkout has no git common dir (a container image ships the tree without .git)" }, () => { assert.equal(checkoutRoot(), path.resolve(HERE, "..", "..")); const lock = heavyLockFile(); assert.ok(lock, "this checkout is a git checkout"); assert.equal(path.basename(lock), "heavy-queue.lock"); assert.ok(existsSync(path.dirname(lock)), lock); }); test("outside a git checkout there is no lock file", () => { assert.equal(checkoutRoot(), path.resolve(HERE, "..", "..")); assert.equal(heavyLockFile(path.join(tmpdir(), "not-a-checkout-" + process.pid)), null); }); test("HEAVY=0 and HEAVY_HELD pass straight through; no gate in the checkout renders with a note", async () => { const ran = []; await withEnv({ HEAVY: "0", HEAVY_HELD: undefined }, () => withRenderSlot(["x"], async () => ran.push("bypass"))); await withEnv({ HEAVY: undefined, HEAVY_HELD: "1" }, () => withRenderSlot(["x"], async () => ran.push("held"))); const notes = []; const root = mkdtempSync(path.join(tmpdir(), "rtv-nogate-")); try { await withEnv({ HEAVY: undefined, HEAVY_HELD: undefined }, () => withRenderSlot(["x"], async () => ran.push("nogate"), { root, say: (l) => notes.push(l) }), ); } finally { rmSync(root, { recursive: true, force: true }); } assert.deepEqual(ran, ["bypass", "held", "nogate"]); assert.match(notes[0], /^heavy: no heavy-slot gate in this checkout .* — rendering without it$/); }); test("a render runs inside the slot: the lock is taken, HEAVY_HELD is set for what it starts, and both are let go", async () => { const dir = mkdtempSync(path.join(tmpdir(), "rtv-heavy-")); const lock = path.join(dir, "heavy-queue.lock"); try { const seen = await withEnv( { HEAVY: undefined, HEAVY_HELD: undefined, HEAVY_LOCK_FILE: lock, HEAVY_MIN_FREE_MB: "0" }, async () => { const inside = await withRenderSlot(["build-video", "m.json"], async () => ({ held: process.env.HEAVY_HELD, lockExists: existsSync(lock), holder: existsSync(`${lock}.holder.json`), })); return { inside, after: process.env.HEAVY_HELD, lockFile: process.env.HEAVY_LOCK_FILE }; }, ); assert.deepEqual(seen.inside, { held: "1", lockExists: true, holder: true }); assert.equal(seen.after, undefined); assert.equal(seen.lockFile, lock, "a HEAVY_LOCK_FILE already set is kept"); assert.equal(existsSync(`${lock}.holder.json`), false, "the holder file goes with the slot"); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("with no HEAVY_LOCK_FILE the lock is pinned to the checkout's git common dir, not the cwd's", async () => { const { execFileSync } = await import("node:child_process"); const { copyFileSync, mkdirSync } = await import("node:fs"); // A stand-in checkout: a git repo carrying the real gate. const repo = mkdtempSync(path.join(tmpdir(), "rtv-heavy-repo-")); try { execFileSync("git", ["init", "-q", repo]); mkdirSync(path.join(repo, "scripts")); copyFileSync(path.join(checkoutRoot(), "scripts", "queue-lock.mjs"), path.join(repo, "scripts", "queue-lock.mjs")); // ...and render mode's state module, which the gate imports. mkdirSync(path.join(repo, "common", "lib"), { recursive: true }); copyFileSync(path.join(checkoutRoot(), "common", "lib", "renderPriority.mjs"), path.join(repo, "common", "lib", "renderPriority.mjs")); // ...and the temp-name helper it writes its state through. copyFileSync(path.join(checkoutRoot(), "common", "lib", "atomicFile.mjs"), path.join(repo, "common", "lib", "atomicFile.mjs")); const lock = path.join(repo, ".git", "heavy-queue.lock"); const seen = await withEnv( { HEAVY: undefined, HEAVY_HELD: undefined, HEAVY_LOCK_FILE: undefined, HEAVY_MIN_FREE_MB: "0" }, () => withRenderSlot(["build-video"], async () => ({ file: process.env.HEAVY_LOCK_FILE, exists: existsSync(lock) }), { root: repo }), ); assert.deepEqual(seen, { file: lock, exists: true }); } finally { rmSync(repo, { recursive: true, force: true }); } });