import { test, expect } from "@playwright/test";
import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
// ---------------------------------------------------------------------------
// The apps do the work (operator, 4 Nov 2026): every take render records
// itself, and a durable queue the umtool app runs puts steps in order.
//
// umtool queue add -- take
--id good … q1, renders (the fixture's stubs)
// umtool queue add --after q1 -- take
--id broken --full --patch-json …
// q2, a clip nothing on disk covers: fails
// umtool queue add --after q2 -- takes ls
q3, blocked by q2's failure
//
// run by THIS suite's app (lib/queue/runner.ts, on under `next start`) over the
// fixture's own queue dir. Then the record (`umtool take status`), the Takes
// panel (the failed first render with its error and its log; the project's
// queue) and /jobs (the queue, the renders), and Cancel on the blocked step.
//
// Fixture: queue-fixture (make-fixture.mjs), which this spec WRITES (takes/).
// ---------------------------------------------------------------------------
const HERE = path.dirname(fileURLToPath(import.meta.url));
const FIXTURE = path.join(HERE, "..", ".e2e-song");
const PROJECT = "reports/queue-fixture";
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
function cli(args: string[], base: string) {
const r = spawnSync("node", ["bin/umtool.mjs", ...args], {
cwd: path.join(HERE, ".."),
encoding: "utf8",
env: { ...process.env, REPORTS_DIR: FIXTURE, CHANNELS_DIR: path.join(FIXTURE, "channels"), UMTOOL_CACHE_DIR: path.join(FIXTURE, "cache"), UMTOOL_MEDIA_DIR: "", UMTOOL_URL: base },
});
return { code: r.status, out: r.stdout, err: r.stderr };
}
/** Poll `umtool queue status ` until it is not 3 (still going). */
async function settle(qid: string, base: string, ms = 180_000) {
const until = Date.now() + ms;
for (;;) {
const r = cli(["queue", "status", qid, "--json"], base);
if (r.code !== 3) return { code: r.code, step: JSON.parse(r.out) as { state: string; error: string | null; exitCode: number | null } };
if (Date.now() > until) throw new Error(`${qid} still going after ${ms} ms: ${r.out}`);
await sleep(1000);
}
}
test("a queued chain runs in order in the app; the failed render records itself; the Takes panel and /jobs show it", async ({ page, baseURL }) => {
test.setTimeout(300_000);
const base = String(baseURL);
// The app runs the queue.
const ls = cli(["queue", "ls", "--json"], base);
expect(ls.code).toBe(0);
expect((JSON.parse(ls.out) as { runner: { live: boolean } | null }).runner?.live).toBe(true);
const add = (args: string[]) => {
const r = cli(["queue", "add", ...args], base);
expect(r.code, r.err).toBe(0);
return r.out.trim();
};
const q1 = add(["--label", "the good take", "--", "take", PROJECT, "--id", "good", "--label", "Good"]);
const patch = JSON.stringify({ timeline: [{ type: "clip", id: "m09", video: "vid9", start: 1, end: 2, cite: 1, section: 0, lock: true, quote: "nothing here" }] });
const q2 = add(["--after", q1, "--label", "the broken take", "--", "take", PROJECT, "--id", "broken", "--label", "Broken", "--full", "--patch-json", patch]);
const q3 = add(["--after", "last", "--", "takes", "ls", PROJECT]);
expect([q1, q2, q3]).toEqual([expect.stringMatching(/^q\d+$/), expect.stringMatching(/^q\d+$/), expect.stringMatching(/^q\d+$/)]);
// While q2 waits, its take reads queued.
const early = cli(["take", "status", PROJECT, "--id", "broken", "--json"], base);
expect([3, 1]).toContain(early.code); // queued (3) -- or, on a fast machine, already failed (1)
const s1 = await settle(q1, base);
expect(s1.code).toBe(0);
expect(s1.step.state).toBe("done");
const s2 = await settle(q2, base);
expect(s2.code).toBe(1);
expect(s2.step.state).toBe("failed");
expect(s2.step.error).toMatch(/take broken: /);
const s3 = await settle(q3, base, 20_000);
expect(s3.code).toBe(1);
expect(s3.step.state).toBe("blocked");
expect(s3.step.error).toContain(`${q2}, which failed`);
// The records, as a cheaper model polls them.
const good = cli(["take", "status", PROJECT, "--id", "good", "--json"], base);
expect(good.code).toBe(0);
const goodRec = JSON.parse(good.out) as { state: string; mp4: string; mp4Bytes: number };
expect(goodRec.state).toBe("done");
expect(goodRec.mp4).toBe("preview.mp4");
expect(goodRec.mp4Bytes).toBeGreaterThan(0);
const broken = cli(["take", "status", PROJECT, "--id", "broken", "--json"], base);
expect(broken.code).toBe(1);
const rec = JSON.parse(broken.out) as { state: string; error: string; tail: string[]; exitCode: number };
expect(rec.state).toBe("failed");
expect(rec.exitCode).toBe(1);
expect(rec.error).toMatch(/m09/);
expect(rec.tail.length).toBeGreaterThan(0);
// The Takes panel: the failed first render, in the open, with its error and its log; the project's queue.
await page.goto(urls.takesHref(PROJECT));
const failed = page.locator("[data-take-failed='broken']");
await expect(failed).toBeVisible();
await expect(failed.locator("[data-render-state='failed'] [data-render-error]")).toContainText("m09");
const logHref = await failed.locator("[data-render-log]").getAttribute("href");
const log = await page.request.get(String(logHref));
expect(log.status()).toBe(200);
expect(await log.text()).toMatch(/# failed — .*m09/);
await expect(page.locator("[data-take='good']")).toBeVisible();
const queue = page.locator("[data-takes-queue]");
await expect(queue.locator(`[data-queue-step='${q1}']`)).toHaveAttribute("data-queue-state", "done");
await expect(queue.locator(`[data-queue-step='${q2}']`)).toHaveAttribute("data-queue-state", "failed");
await expect(queue.locator(`[data-queue-step='${q2}'] [data-queue-error]`)).toContainText("take broken");
await expect(queue.locator(`[data-queue-step='${q3}']`)).toHaveAttribute("data-queue-state", "blocked");
const stepLog = await page.request.get(String(await queue.locator(`[data-queue-step='${q2}'] [data-queue-log]`).getAttribute("href")));
expect(stepLog.status()).toBe(200);
expect(await stepLog.text()).toContain(`# queue ${q2} attempt 1: the broken take`);
// /jobs: the queue and the renders, across projects; the rail's Running links it (the Takes page is a bench: no rail).
await page.goto("/");
await page.locator("[data-rail-jobs]").click();
await expect(page).toHaveURL(/\/jobs$/);
await expect(page.locator("[data-queue-runner='live']")).toBeVisible();
await expect(page.locator(`[data-panel='queue'] [data-queue-step='${q2}']`)).toHaveAttribute("data-queue-state", "failed");
await expect(page.locator(`[data-panel='renders'] [data-render-record='broken']`)).toHaveAttribute("data-render-state", "failed");
await expect(page.locator(`[data-panel='renders'] [data-render-record='broken'] [data-render-error]`)).toContainText("m09");
await expect(page.locator(`[data-panel='renders'] [data-render-record='good']`)).toHaveAttribute("data-render-state", "done");
// Cancel the blocked step from the page: cancelled, and the CLI agrees.
await page.locator(`[data-panel='queue'] [data-queue-step='${q3}'] [data-action='queue-cancel']`).click();
await expect(page.locator(`[data-panel='queue'] [data-queue-step='${q3}']`)).toHaveAttribute("data-queue-state", "cancelled");
const after = cli(["queue", "status", q3, "--json"], base);
expect(after.code).toBe(1);
expect((JSON.parse(after.out) as { state: string }).state).toBe("cancelled");
// `umtool jobs` lists them all.
const jobs = cli(["jobs", "--json"], base);
expect(jobs.code).toBe(0);
const j = JSON.parse(jobs.out) as { queue: { steps: { qid: string }[] }; renders: { take: string; state: string }[] };
expect(j.queue.steps.map((s) => s.qid)).toEqual(expect.arrayContaining([q1, q2, q3]));
expect(j.renders.find((r) => r.take === "broken")?.state).toBe("failed");
});