import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { classifyCommand, readHeavyQueue } from "./heavyQueue.mjs"; // The render queue panel's reader (UI notes 2026-10-30, #18): the heavy slot // as its own files say, never guessed. A live ticket is this process's pid; a // dead one a pid nothing runs as. function slot() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "heavy-queue-")); const lock = path.join(dir, "heavy-queue.lock"); const put = (file: string, doc: unknown) => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, JSON.stringify(doc)); }; return { dir, lock, put, done: () => fs.rmSync(dir, { recursive: true, force: true }) }; } const LIVE = process.pid; const DEAD = 2 ** 22 + 12345; const isAlive = (h: { pid: number }) => h.pid === LIVE || h.pid === LIVE + 1 || h.pid === LIVE + 2; test("classifyCommand: a take's render names its take and project; a suite, a build", () => { assert.deepEqual( classifyCommand("node /x/umtool/report-to-video/build-video.mjs /r/reports/ember/cut/.take-draft-3.render.json --out /r/reports/ember/cut/takes/draft-3/out --progress ndjson"), { kind: "render", label: "render of draft-3 (ember/cut)", projectDir: "/r/reports/ember/cut", take: "draft-3" }, ); assert.equal(classifyCommand("node build-video.mjs /r/p/video.manifest.json").label, "render of r/p"); assert.equal(classifyCommand("playwright test a.spec.ts").kind, "e2e"); assert.equal(classifyCommand("systemd-run --user --scope -p MemoryMax=5G pnpm --filter export exec next build").label, "next build (export)"); }); test("readHeavyQueue: running and waiting tickets (the dead ones not), the head of the line, the suites, render mode, memory", () => { const s = slot(); try { const at = (min: number) => new Date(Date.parse("2026-11-02T10:00:00Z") + min * 60_000).toISOString(); s.put(`${s.lock}.jobs/${LIVE}.json`, { pid: LIVE, estimateMb: 1500, build: false, exclusive: false, worktree: "/w/lane-a", branch: "lane/a", cmd: "playwright test", startedAt: at(0) }); s.put(`${s.lock}.jobs/${DEAD}.json`, { pid: DEAD, estimateMb: 5000, cmd: "next build", startedAt: at(0) }); s.put(`${s.lock}.waiting/${LIVE + 1}.json`, { pid: LIVE + 1, estimateMb: 6020, cmd: "node build-video.mjs /r/reports/ember/cut/.take-draft-9.render.json --out /r/reports/ember/cut/takes/draft-9/out", since: at(2), stage: "memory" }); s.put(`${s.lock}.waiting/${LIVE + 2}.json`, { pid: LIVE + 2, estimateMb: 5120, build: true, cmd: "pnpm --filter editor exec next build", since: at(1), stage: "line" }); s.put(`${s.lock}.admit.holder.json`, { pid: LIVE + 1, cmd: "node build-video.mjs …", startedAt: at(2) }); s.put(path.join(s.dir, "e2e-umtool-queue.lock.holder.json"), { pid: LIVE, cmd: "playwright test", startedAt: at(-1) }); s.put(path.join(s.dir, "e2e-editor-queue.lock.holder.json"), { pid: DEAD, cmd: "playwright test", startedAt: at(-1) }); const mem = path.join(s.dir, "meminfo"); fs.writeFileSync(mem, "MemTotal: 16282624 kB\nMemAvailable: 8388608 kB\n"); const q = readHeavyQueue({ heavyLock: s.lock, renderFile: path.join(s.dir, "none.json"), env: {}, now: Date.parse(at(3)), isAlive, meminfo: mem }); assert.deepEqual(q.running.map((r) => [r.pid, r.what, r.estimateMb, r.worktree, r.forSeconds]), [[LIVE, "e2e suite e2e-umtool", 1500, "lane-a", 180]]); // The head of the line first (it waits for memory), then the one behind it. assert.deepEqual(q.waiting.map((w) => [w.pid, w.stage, w.what]), [ [LIVE + 1, "memory", "render of draft-9 (ember/cut)"], [LIVE + 2, "line", "next build (editor)"], ]); assert.equal(q.waiting[0].stageWords, "at the head of the line, for memory"); assert.equal(q.waiting[0].take, "draft-9"); assert.equal(q.head?.pid, LIVE + 1); assert.deepEqual(q.suites.map((x) => x.suite), ["e2e-umtool"]); assert.deepEqual(q.render, { held: false }); assert.deepEqual(q.memory, { availableMb: 8192, totalMb: 15901, floorMb: 6000, promisedMb: 1500 }); assert.equal(q.heldBy, "e2e suite e2e-umtool"); // Nothing was removed: queue-lock owns the files. assert.ok(fs.existsSync(`${s.lock}.jobs/${DEAD}.json`)); } finally { s.done(); } }); test("readHeavyQueue: an empty slot, and render mode holding the machine", () => { const s = slot(); try { const renderFile = path.join(s.dir, "heavy-queue.lock.render.json"); s.put(renderFile, { token: "t", since: "2026-11-02T10:00:00.000Z", until: "2099-01-01T00:00:00.000Z", by: "umtool take", holder: { pid: LIVE, cmd: "umtool take", project: "ember/cut", take: "draft-9" }, lanesHeld: [] }); const q = readHeavyQueue({ heavyLock: s.lock, renderFile, env: {}, isAlive, meminfo: path.join(s.dir, "no-meminfo") }); assert.deepEqual([q.running, q.waiting, q.head, q.build, q.suites, q.memory, q.heldBy], [[], [], null, null, [], null, null]); assert.equal(q.render.held, true); assert.match(String((q.render as { what?: string }).what), /^umtool take, since /); } finally { s.done(); } });