// Run with: // pnpm -C editor exec tsx --test "app/api/ops/_renderMode.test.ts" // // `pnpm ops render-mode` / `get render-mode` against the route handlers, over a // temp corpus and a temp state file (RENDER_PRIORITY_FILE) — never the // machine's render mode, never the live editor's lanes. import { test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { callGet, callPost, setupOpsCorpus } from "./_testCorpus"; const corpus = await setupOpsCorpus(null); const STATE = path.join(corpus.root, "render-priority.json"); process.env.RENDER_PRIORITY_FILE = STATE; const { GET, POST } = await import("./render-mode/route"); const { getSettings } = await import("yt-dlp-transcript-common/lib/settings"); const { getWorkerPool } = await import("yt-dlp-transcript-common/jobs/workerPool"); const { renderModeReapTick } = await import("../../operations/renderMode"); test.after(() => corpus.cleanup()); const held = (lane: "transcription" | "download" | "backfill") => getSettings().autoQueue[lane].held === true; test("on holds the lanes not already held (and says so), off releases only those", async () => { // The Speakers (backfill) lane running: a render takes its cores too. await corpus.writeSettings({ minFreeDiskGB: 0, autoQueue: { ...getSettings().autoQueue, download: { ...getSettings().autoQueue.download, held: true }, backfill: { ...getSettings().autoQueue.backfill, held: false }, }, }); assert.equal(held("download"), true); assert.equal(held("transcription"), false); assert.equal(held("backfill"), false); const on = await callPost(POST!, { on: true, for: "90m", note: "the Ember cut" }); assert.equal(on.status, 200, JSON.stringify(on.body)); const state = on.body.state as { token: string; lanesHeld: string[]; lanesSkipped: string[]; until: string; editor: string }; assert.deepEqual(state.lanesHeld, ["transcription", "backfill"]); assert.deepEqual(state.lanesSkipped, ["download"]); assert.equal(held("backfill"), true); assert.match(String(on.body.join), /^RENDER_PRIORITY_TOKEN=[0-9a-f]{16}$/); assert.match(state.editor, /^http:\/\/localhost:\d+$/); assert.equal(held("transcription"), true); assert.equal(getWorkerPool().isPaused(), true, "the transcription pool's live hold too"); assert.equal(JSON.parse(readFileSync(STATE, "utf8")).token, state.token); const again = await callPost(POST!, { on: true }); assert.equal(again.status, 409); assert.match(String(again.body.error), /already on: render mode on \(by pnpm ops render-mode\) — the Ember cut/); const got = await callGet(GET!, "http://localhost/api/ops/render-mode"); assert.equal(got.status, 200); assert.equal(got.body.held, true); assert.equal(got.body.token, undefined, "the read side carries no token"); assert.deepEqual(got.body.lanesHeld, ["transcription", "backfill"]); const off = await callPost(POST!, { on: false }); assert.equal(off.status, 200, JSON.stringify(off.body)); assert.deepEqual(off.body.released, ["transcription", "backfill"]); assert.equal(held("transcription"), false); assert.equal(held("backfill"), false); assert.equal(getWorkerPool().isPaused(), false); assert.equal(held("download"), true, "someone else's hold stays"); assert.equal(existsSync(STATE), false); }); test("a running take's hold is not ended without force; bad bodies are 400s", async () => { writeFileSync(STATE, JSON.stringify({ version: 1, token: "take", since: new Date().toISOString(), until: null, holder: { pid: process.pid, cmd: "umtool take p --id t" }, lanesHeld: [] })); const off = await callPost(POST!, { on: false }); assert.equal(off.status, 409); assert.match(String(off.body.error), /held by a running render \(umtool take p --id t/); assert.equal((await callPost(POST!, { on: false, force: true })).status, 200); assert.equal(existsSync(STATE), false); assert.equal((await callPost(POST!, {})).status, 400); assert.equal((await callPost(POST!, { on: true, for: "soon" })).status, 400); assert.equal((await callPost(POST!, { on: false, note: "x" })).status, 400); assert.equal((await callPost(POST!, { on: true, lanes: ["x"] })).status, 400); }); test("the heartbeat ends a stale hold this editor made: its lanes released; another editor's is left", async () => { const port = process.env.EDITOR_PORT || process.env.PORT || "3001"; await callPost(POST!, { on: true }); assert.equal(held("transcription"), true); // Its window over (a killed session's): stale. const s = JSON.parse(readFileSync(STATE, "utf8")); writeFileSync(STATE, JSON.stringify({ ...s, until: new Date(Date.now() - 1000).toISOString() })); await renderModeReapTick(); assert.equal(held("transcription"), false); assert.equal(existsSync(STATE), false); // Another worktree's editor's stale hold: not this one's lanes to release. writeFileSync(STATE, JSON.stringify({ ...s, until: new Date(Date.now() - 1000).toISOString(), editor: `http://localhost:${Number(port) + 100}`, lanesHeld: ["download"] })); await renderModeReapTick(); assert.equal(existsSync(STATE), true); });