import { test, expect, type Page } from "@playwright/test"; import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { openPanel } from "./panels"; // --------------------------------------------------------------------------- // Take housekeeping, the rest (UI notes 2026-10-30, item 19): // // - Cancel a take while it renders: a REAL render (the take maker's job, // `umtool take … --priority`), held in the qrencode stub's stall; Cancel // render on the Takes page asks the take maker, which stops its render's // process group, writes takes//cancelled.json, never a take.json, and // lets render mode go (lib/report/takeCancel.mjs) // - Fork as variant: the take as a project beside this one -- its manifest, // variant.json, the media linked, the take in it (variantFork.mjs) // - two takes side by side, of the project and of its variant: two players // in step, the differences listed (takeCompare.mjs, TakesComparePlayers) // // Fixture: variants-fixture (make-fixture.mjs), which this spec WRITES: m01 // vid1 3-6; takes draft-1 (the manifest) and draft-2 (m01 3.5-6, crf 34). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const SONG = path.join(HERE, "..", ".e2e-song"); const PROJECT = "reports/variants-fixture"; const DIR = path.join(SONG, "reports", "variants-fixture"); const VARIANT_DIR = path.join(SONG, "reports", "v-draft-2"); const STALL = path.join(SONG, "bin", "qrencode.stall-ms"); const STALL_PID = path.join(SONG, "bin", "qrencode.stall.pid"); const STATE = path.join(SONG, "render-priority.json"); const SHOTS = process.env.U3_SHOTS ?? null; const shot = async (page: Page, name: string) => { await page.screenshot({ path: test.info().outputPath(name) }); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, name) }); }; const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch { return false; } }; test.afterAll(() => { rmSync(STALL, { force: true }); rmSync(VARIANT_DIR, { recursive: true, force: true }); }); test("two takes side by side: two players in step, and what differs A → B", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(urls.takesCompareHref(PROJECT, `${PROJECT}:draft-1`, `${PROJECT}:draft-2`)); const players = page.locator("[data-compare-players]"); await expect(players.locator("[data-compare-player='a']")).toHaveAttribute("data-compare-take", `${PROJECT}:draft-1`); await expect(players.locator("[data-compare-player='b']")).toHaveAttribute("data-compare-take", `${PROJECT}:draft-2`); await expect(page.locator("[data-compare-cut]")).toContainText("~ m01 window: 3–6 -> 3.5–6"); await expect(page.locator("[data-compare-look-path='render.crf']")).toContainText("render.crf: 30 → 34"); await expect(page.locator("[data-compare-fact='label']")).toHaveAttribute("data-differs", ""); // Both play from one transport, and stay together. await page.locator("[data-compare-play]").click(); await expect(players).toHaveAttribute("data-compare-state", "playing"); await expect.poll(async () => Number(await players.getAttribute("data-compare-time")), { timeout: 10_000 }).toBeGreaterThan(0.6); const times = await page.locator("[data-compare-players] video").evaluateAll((vs) => (vs as HTMLVideoElement[]).map((v) => v.currentTime)); expect(times.length).toBe(2); expect(Math.abs(times[0] - times[1])).toBeLessThan(0.3); // One sound: A's, until B's is asked for. const muted = () => page.locator("[data-compare-players] video").evaluateAll((vs) => (vs as HTMLVideoElement[]).map((v) => v.muted)); expect(await muted()).toEqual([false, true]); await page.locator("[data-compare-sound='b']").click(); expect(await muted()).toEqual([true, false]); await page.locator("[data-compare-play]").click(); await expect(players).toHaveAttribute("data-compare-state", "paused"); }); test("Fork as variant: a project beside this one, with the take in it; compared across the two", async ({ page }) => { rmSync(VARIANT_DIR, { recursive: true, force: true }); await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(urls.takesHref(PROJECT)); const card = page.locator("[data-take='draft-2']"); await openPanel(page, "options", "take-card"); await card.locator("[data-action='fork']").click(); await expect(card.locator("[data-take-fork-name='draft-2']")).toHaveValue("v-draft-2"); await card.locator("[data-action='fork-go']").click(); await expect(card.locator("[data-take-forked='reports/v-draft-2']")).toBeVisible(); const variant = JSON.parse(readFileSync(path.join(VARIANT_DIR, "variant.json"), "utf8")) as { variantOf: string; fromTake: string }; expect(variant).toMatchObject({ variantOf: PROJECT, fromTake: "draft-2" }); const m = JSON.parse(readFileSync(path.join(VARIANT_DIR, "video.manifest.json"), "utf8")) as { slug: string; timeline: { start: number }[] }; expect(m.slug).toBe("v-draft-2"); expect(m.timeline[0].start).toBe(3.5); expect(existsSync(path.join(VARIANT_DIR, "takes", "draft-2", "preview.mp4"))).toBe(true); // The project's Takes page names its variant; the variant's names its parent. await page.reload(); await expect(page.locator("[data-takes-variant='reports/v-draft-2']")).toBeVisible(); await page.goto(urls.takesHref("reports/v-draft-2")); await expect(page.locator("[data-takes-family]")).toContainText(`A variant of ${PROJECT}, forked from its take draft-2`); // Compare with…: the parent's draft-1 against the variant's draft-2. await page.goto(urls.takesHref(PROJECT)); const one = page.locator("[data-take='draft-1']"); await openPanel(page, "options", "take-card"); await one.locator("[data-take-compare-with='draft-1']").selectOption("reports/v-draft-2:draft-2"); await one.locator("[data-action='compare']").click(); await expect(page.locator("[data-compare-player='b']")).toHaveAttribute("data-compare-take", "reports/v-draft-2:draft-2"); await expect(page.locator("[data-compare-look-path='slug']")).toContainText("variants-fixture → v-draft-2"); await page.locator("[data-compare-play]").click(); await expect.poll(async () => Number(await page.locator("[data-compare-players]").getAttribute("data-compare-time")), { timeout: 10_000 }).toBeGreaterThan(0.5); await page.locator("[data-compare-play]").click(); await shot(page, "u3-5-compare-side-by-side.png"); }); test("Cancel a take while it renders: its render stops, it is marked cancelled, render mode lets go", async ({ page, request }) => { test.setTimeout(240_000); rmSync(STATE, { force: true }); rmSync(STALL_PID, { force: true }); writeFileSync(STALL, "120000"); let id = ""; try { // A real take, through the app's take job, with Give renders the machine. const r = await request.post("/api/report/takes/make", { data: { project: PROJECT, take: { label: "Slow render", window: "full" }, priority: true } }); expect(r.status(), await r.text()).toBe(200); const made = (await r.json()) as { takes: string[]; job: { id: string } }; id = made.takes[0]; // It is rendering: its build lock, render mode held for it, the stub stalled inside the build. await expect.poll(() => existsSync(STALL_PID), { timeout: 120_000 }).toBe(true); const stubPid = Number(readFileSync(STALL_PID, "utf8")); expect(alive(stubPid)).toBe(true); expect(existsSync(path.join(DIR, "takes", id, ".building.json"))).toBe(true); expect((JSON.parse(readFileSync(STATE, "utf8")) as { holder?: { take?: string } }).holder?.take).toBe(id); await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(urls.takesHref(PROJECT)); const row = page.locator(`[data-take-rendering='${id}']`); await expect(row).toContainText("Slow render"); await shot(page, "u3-4-cancel-rendering-take.png"); await row.locator("[data-action='cancel-render']").click(); // The answer stays on the page after the refresh takes the row away (it is not rendering any more). await expect(page.locator(`[data-take-cancel-said='${id}']`)).toContainText("its render stopped (the take maker)", { timeout: 60_000 }); // Stopped cleanly: the render's process gone, no take.json, the mark, the lock gone, render mode off. await expect.poll(() => alive(stubPid), { timeout: 15_000 }).toBe(false); expect(existsSync(path.join(DIR, "takes", id, "take.json"))).toBe(false); expect(JSON.parse(readFileSync(path.join(DIR, "takes", id, "cancelled.json"), "utf8"))).toMatchObject({ by: "the Takes page", label: "Slow render", rebuild: false }); expect(existsSync(path.join(DIR, "takes", id, ".building.json"))).toBe(false); await expect.poll(() => existsSync(STATE), { timeout: 15_000 }).toBe(false); await expect .poll(async () => ((await (await request.get(`/api/report/takes/make?job=${made.job.id}`)).json()) as { state: string }).state, { timeout: 30_000 }) .toBe("failed"); // The list: no longer rendering; folded away as cancelled, deletable. await expect(page.locator(`[data-take-rendering='${id}']`)).toHaveCount(0); await shot(page, "u3-4b-cancelled.png"); await page.reload(); await expect(page.locator(`[data-take-rendering='${id}']`)).toHaveCount(0); await expect(page.locator("[data-cancelled-count]")).toContainText("1 cancelled render folded away"); await page.locator("[data-action='show-cancelled']").click(); await expect(page.locator(`[data-take-cancelled='${id}']`)).toContainText("never finished: no take"); } finally { rmSync(STALL, { force: true }); if (id) rmSync(path.join(DIR, "takes", id), { recursive: true, force: true }); rmSync(STATE, { force: true }); } });