import { test, expect } from "@playwright/test"; import { execFileSync } from "node:child_process"; import { mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; // --------------------------------------------------------------------------- // THE BUILT SERVER UNDER A LARGE REPORTS ROOT (the regression S4 shipped with, 17 Nov 2026: the live server pegged a // core walking the reports root over and over and never answered `/`). This suite's own fixture gains 300 project // directories (30 folders of 10, plus loose non-project folders) and 124 OLD queue steps (done and failed take // renders, no reads batch anywhere). Then: the server, left alone, walks nothing; `/` and a project page answer // within seconds; and afterwards it is idle again -- the walk count flat, its CPU near nothing. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const LOAD = path.join(FIXTURE, "walk-load"); const QUEUE = path.join(FIXTURE, ".umtool-queue", "steps"); const OUR_STEPS = Array.from({ length: 124 }, (_, i) => `q${9001 + i}`); const KIND = "report-video"; const walks = async (base: string) => ((await (await fetch(`${base}/api/queue?walks=1`)).json()) as { walks: { calls: number; inFlight: number } }).walks; /** The server's CPU seconds so far (utime + stime), from /proc: the pid that listens on the port. */ function cpuSeconds(port: number): number { const out = execFileSync("ss", ["-ltnpH", `sport = :${port}`], { encoding: "utf8" }); const pid = Number(/pid=(\d+)/.exec(out)?.[1]); const f = readFileSync(`/proc/${pid}/stat`, "utf8").split(") ")[1].split(" "); return (Number(f[11]) + Number(f[12])) / 100; } const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); test.beforeAll(() => { for (let g = 0; g < 30; g++) { for (let i = 0; i < 10; i++) { const d = path.join(LOAD, `group-${g}`, `proj-${i}`); mkdirSync(path.join(d, "out"), { recursive: true }); writeFileSync(path.join(d, "project.json"), JSON.stringify({ kind: KIND, title: `Load ${g}-${i}` })); writeFileSync(path.join(d, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: `proj-${i}`, title: `Load ${g}-${i}`, render: { width: 1920, height: 1080, fps: 30 }, timeline: [] })); } // Folders with no project beneath: walked, never listed. mkdirSync(path.join(LOAD, `group-${g}`, "scratch", "a", "b"), { recursive: true }); } mkdirSync(QUEUE, { recursive: true }); OUR_STEPS.forEach((qid, n) => { const p = `walk-load/group-${n % 30}/proj-${n % 10}`; writeFileSync(path.join(QUEUE, `${qid}.json`), JSON.stringify({ qid, seq: 9001 + n, label: `take ${p} --id rough-1 --preview`, argv: ["take", p, "--id", "rough-1", "--preview"], cwd: null, after: null, target: { project: p, take: "rough-1" }, state: n % 3 ? "done" : "failed", createdAt: "2026-11-01T00:00:00.000Z", by: "umtool queue add", startedAt: "2026-11-01T00:00:01.000Z", endedAt: "2026-11-01T00:10:00.000Z", exitCode: n % 3 ? 0 : 1, signal: null, pid: 1, pidStart: null, host: os.hostname(), runnerPid: 1, runnerStart: null, jobId: null, attempts: 1, error: n % 3 ? null : "the render failed", blockedBy: null, cancelRequested: null, timeoutMin: 720, })); }); }); test.afterAll(() => { rmSync(LOAD, { recursive: true, force: true }); for (const qid of OUR_STEPS) rmSync(path.join(QUEUE, `${qid}.json`), { force: true }); void readdirSync; }); test("a large reports root and 124 old queue steps: idle, then `/` and a project page within seconds, then idle again", async ({ page, baseURL }) => { test.setTimeout(180_000); const base = String(baseURL); const port = Number(new URL(base).port); // Left alone a while (the queue runner ticks every 2 s): no walk. const w0 = await walks(base); const c0 = cpuSeconds(port); await sleep(6000); const w1 = await walks(base); const c1 = cpuSeconds(port); expect(w1.calls - w0.calls, "walks while idle").toBe(0); expect(c1 - c0, "CPU seconds while idle (6 s)").toBeLessThan(1.5); // `/` and a project page. let t = Date.now(); const home = await page.goto("/", { timeout: 30_000 }); expect(home?.status()).toBe(200); const homeMs = Date.now() - t; t = Date.now(); const proj = await page.goto("/projects/walk-load/group-7/proj-3", { timeout: 30_000 }); expect(proj?.status()).toBe(200); const projMs = Date.now() - t; expect(homeMs, "`/` answers within seconds").toBeLessThan(15_000); expect(projMs, "a project page answers within seconds").toBeLessThan(15_000); // Then idle again: no page polling, the walk count flat and the CPU near nothing. await page.goto("about:blank"); await sleep(3000); const w2 = await walks(base); const c2 = cpuSeconds(port); await sleep(6000); const w3 = await walks(base); const c3 = cpuSeconds(port); console.log(`walk-load: home ${homeMs} ms, project ${projMs} ms; walks idle ${w1.calls - w0.calls}, during pages ${w2.calls - w1.calls}, after ${w3.calls - w2.calls} (in flight ${w3.inFlight}); CPU idle ${(c1 - c0).toFixed(2)} s, after ${(c3 - c2).toFixed(2)} s`); expect(w3.calls - w2.calls, "walks after the pages").toBe(0); expect(w3.inFlight).toBe(0); expect(c3 - c2, "CPU seconds after (6 s)").toBeLessThan(1.5); });