import { test, expect, type Page } from "@playwright/test"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // The Play view (umtool/docs/knobs.md, "Play the cut"): the whole cut played // in the browser on one cut clock, no render -- clip 1 plays, the clock // carries into clip 2, prev / next / the keys move between entries (a card // and an unfetched clip play as placeholders), the reel strip scrubs and // carries the counter's staircase, a note taken at the playhead (with its // kind) lists, pins and seeks, and a take plays its own timeline. // // The clock is the RENDER'S: each clip at its snapped edges (the cut-points // route), in the encode's whole frames (15 fps here). On the fixture's window (playvid) // (silences 2.90-3.11 and 5.92-6.11) c01 plays 1.000-3.082 (2.133 s), c02 // 3.011-6.101 (3.133 s), c04 6.007-8.000 (2 s), so with the 0.2 s // crossfades: c01 0-2.133 (shown to 2.033), c02 1.933-5.067 (shown 2.033- // 4.967), k01 4.867-6.367 (shown to 6.267), c03 6.167-8.167 (shown to // 8.067), c04 7.967-9.967. play-engine.spec.ts covers the engine itself. // Screenshots at 1280 and 390 px land in test-results. Fixture: // e2e/fixtures/play-cut-fixture.mjs. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PROJECT = "reports/play-cut-fixture"; const DIR = path.join(FIXTURE, "reports", "play-cut-fixture"); const root = (page: Page) => page.locator("[data-play-cut]"); const tNow = async (page: Page) => Number(await root(page).getAttribute("data-play-t")); const rows = (page: Page) => page.locator("[data-play-row]"); /** Arrived, with the render's edges measured (until then the plan is the manifest's). */ const ready = async (page: Page) => { await expect(root(page)).toHaveAttribute("data-play-state", "paused"); await expect(root(page)).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); }; test.beforeEach(() => { test.setTimeout(120_000); }); test("the cut plays clip after clip on one clock; prev / next / keys; the strip scrubs; placeholders never stall", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT)); await ready(page); await expect(rows(page)).toHaveCount(5); expect(await rows(page).evaluateAll((els) => els.map((e) => e.getAttribute("data-play-row")))).toEqual(["c01", "c02", "k01", "c03", "c04"]); await expect(page.locator('[data-play-row="c03"][data-play-row-missing]')).toHaveCount(1); await expect(page.locator('[data-play-row="c01"]')).toHaveAttribute("aria-current", "true"); await expect(page.locator("[data-play-approximate]")).toContainText("edges are the render's, snapped to silence"); // The strip: the section seam before c03 dated by its first dated clip, the counter's staircase and its total. await expect(page.locator('[data-play-seam-date="c03"]')).toHaveText("27 Feb 2025"); await expect(page.locator("[data-play-staircase] path")).toHaveCount(1); await expect(page.locator("[data-play-counter-end]")).toHaveText("Seizures 3"); await expect(page.locator("[data-play-strip-at]")).toContainText("Seizures 0"); await expect(page.locator("[data-play-clock]")).toContainText("00:00 / 00:09"); // The deck composes: "Seizures" is wider than the tally's cell in the fixture's face, and is drawn smaller // (factcheck.mjs fitTallyLabel) -- never "the chrome did not compose". await expect(root(page)).toHaveAttribute("data-play-compose", "ready", { timeout: 60_000 }); await expect(page.locator("[data-play-compose-error]")).toHaveCount(0); // Hovering a cell names it: the clip, its recording's date, how long it plays. await page.locator('[data-play-row="c02"]').hover(); await expect(page.locator('[data-play-strip-hover="c02"]')).toHaveText("c02 · 2 Dec 2024 · 2.9 s"); await page.locator('[data-play-row="c03"]').hover(); await expect(page.locator('[data-play-strip-hover="c03"]')).toContainText("no media — fetch it"); // Play: clip 1's media (the bench's raw route) plays, the clock moves. await page.locator("[data-play-toggle]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "c01"); await expect.poll(() => tNow(page), { timeout: 20_000 }).toBeGreaterThan(0.5); // ...and carries on into clip 2 (c01 is shown to 2.033 s on the render's clock). await expect(root(page)).toHaveAttribute("data-play-item", "c02", { timeout: 20_000 }); await expect(page.locator('[data-play-row="c02"]')).toHaveAttribute("aria-current", "true"); // c02's picture is drawn (whichever path the browser plays it on). await expect(page.locator("[data-play-footage]")).toHaveAttribute("data-play-layers", /^[12]$/, { timeout: 15_000 }); await expect.poll(() => tNow(page)).toBeGreaterThan(2.033); // NOW: c02 on the reader, the next entry named. await expect(page.locator('[data-play-now="c02"]')).toBeVisible(); await expect(page.locator("[data-play-next-up]")).toHaveAttribute("data-play-next-up", "k01"); await page.keyboard.press("k"); await expect(root(page)).toHaveAttribute("data-play-state", "paused"); // Next: the card, shown as a placeholder from its first second. await page.locator("[data-play-next]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "k01"); await expect(page.locator('[data-play-placeholder="card"]')).toContainText("Between the clips"); expect(await tNow(page)).toBeCloseTo(4.967, 2); // The stamp on c02 (k1 at source 4.5) has landed by now: the counter at the playhead says so. await expect(page.locator("[data-play-strip-at]")).toContainText("Seizures 1"); // n: the clip whose source was never fetched -- named, not waited on. await page.keyboard.press("n"); await expect(root(page)).toHaveAttribute("data-play-item", "c03"); await expect(page.locator('[data-play-missing="c03"]')).toContainText("No media for c03 — fetch it"); // Playing through it (L): the wall clock carries on to c04. await page.keyboard.press("l"); await expect(root(page)).toHaveAttribute("data-play-item", "c04", { timeout: 15_000 }); await page.keyboard.press("Space"); await expect(root(page)).toHaveAttribute("data-play-state", "paused"); // p, prev: back an entry at a time. await page.keyboard.press("p"); await expect(root(page)).toHaveAttribute("data-play-item", "c03"); await page.locator("[data-play-prev]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "k01"); // ← is 5 s back: from 4.967, the cut's first second. await page.keyboard.press("ArrowLeft"); await expect(root(page)).toHaveAttribute("data-play-item", "c01"); expect(await tNow(page)).toBe(0); // → is 5 s on: the card's second (it is shown 4.967-6.267). await page.keyboard.press("ArrowRight"); await expect(root(page)).toHaveAttribute("data-play-item", "k01"); expect(await tNow(page)).toBeCloseTo(5, 1); // A cell's Enter jumps to its start; a click on the strip scrubs to that second. await page.locator('[data-play-row="c02"]').press("Enter"); await expect(root(page)).toHaveAttribute("data-play-item", "c02"); expect(await tNow(page)).toBeCloseTo(2.033, 2); const cells = page.locator("[data-play-cells]"); const box = (await cells.boundingBox())!; await page.mouse.click(box.x + box.width * 0.9, box.y + box.height / 2); await expect(root(page)).toHaveAttribute("data-play-item", "c04"); expect(Math.abs((await tNow(page)) - 0.9 * 9.967)).toBeLessThan(0.15); // ? lists the keys; Esc closes it. await page.keyboard.press("?"); await expect(page.locator("[data-play-keys]")).toContainText("previous / next note"); await page.keyboard.press("Escape"); await expect(page.locator("[data-play-keys]")).toHaveCount(0); }); test("a note at the playhead: its kind, the cut second, the clip and its source second; listed, pinned, [ ] and a click seek", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT)); await ready(page); await expect(page.locator("[data-play-note-list]")).toContainText("Press M while it plays to note this moment."); await page.locator('[data-play-row="c02"]').press("Enter"); await page.keyboard.press("ArrowRight"); // 2.033 + 5 = 7.033: c03 (shown to 8.067) await page.keyboard.press("n"); // 8.067: c04's first shown second await expect(root(page)).toHaveAttribute("data-play-item", "c04"); await expect(page.locator("[data-play-note-add]")).toHaveText("Note at 00:08.0"); // Paused, the clip's frame is in the picture (the fixture's window is dark green), not a black box -- // half of it: c04's first shown second is the middle of its crossfade from c03 (no media: the ground). // The engine says when c04's frame is painted (data-play-drawn, set after the draw): under load its // decode can take a while, so the wait is on that, and the pixels are read once it is there. const footage = page.locator("[data-play-footage]"); // (A short poll after it only covers a canvas resized between the paint and the read, which clears it // until the next frame redraws.) await expect(footage).toHaveAttribute("data-play-drawn", /^c04:/, { timeout: 30_000 }); await expect .poll( () => footage.evaluate((c: HTMLCanvasElement) => { const [r, g, b] = c.getContext("2d")!.getImageData(Math.floor(c.width / 2), Math.floor(c.height / 2), 1, 1).data; return g > 35 && g > r + 20 && g > b + 20; }), { timeout: 2_000 }, ) .toBe(true); await page.keyboard.press("m"); const input = page.getByTestId("play-note-input"); await expect(input).toBeFocused(); await expect(page.locator("[data-play-note-at]")).toHaveAttribute("data-play-note-at", "8.07"); await input.fill("start this one later"); await input.press("Alt+t"); await expect(page.locator('[data-play-ask="trim"]')).toHaveAttribute("aria-pressed", "true"); await input.press("Enter"); const note = page.locator("[data-play-note]"); await expect(note).toHaveCount(1); await expect(note).toContainText("start this one later"); await expect(note.locator("[data-play-note-seek]")).toHaveText("00:08.0"); await expect(note.locator("[data-play-note-ask]")).toHaveText("trim"); await expect(page.locator("[data-play-pin]")).toHaveCount(1); await expect(page.locator("[data-play-send-all]")).toHaveText("Send 1 open note to the assistant"); // Written through the notes store: the cut anchor, the clip, its source second (c04's segment starts at 7.967 at // source 6.007, so 8.067 is 6.107, stored to the hundredth), the kind. const doc = JSON.parse(readFileSync(path.join(DIR, "notes.json"), "utf8")); const n = doc.notes.find((x: { text: string }) => x.text === "start this one later"); expect(n.anchor).toEqual({ kind: "cut", t: 8.07, entry: "c04", sourceT: 6.11, ask: "trim" }); expect(n.author).toBe("operator"); // Elsewhere, then ] (the next note) and the note's timecode: back where it was taken. await page.locator('[data-play-row="c01"]').press("Enter"); await expect(root(page)).toHaveAttribute("data-play-item", "c01"); // (A note seeks by its clip and source second: 6.11 in c04 is 8.070 on this clock.) await page.keyboard.press("]"); await expect(root(page)).toHaveAttribute("data-play-item", "c04"); expect(Math.abs((await tNow(page)) - 8.07)).toBeLessThan(0.011); await page.locator('[data-play-row="c01"]').press("Enter"); await note.locator("[data-play-note-seek]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "c04"); expect(Math.abs((await tNow(page)) - 8.07)).toBeLessThan(0.011); // How it looks: the desk at 1280, the stack at a phone's 390 (no sideways scroll). await page.setViewportSize({ width: 1280, height: 860 }); await page.screenshot({ path: test.info().outputPath("play-1280.png") }); await page.setViewportSize({ width: 390, height: 844 }); expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); await page.screenshot({ path: test.info().outputPath("play-390.png") }); await page.locator("[data-play-now]").scrollIntoViewIfNeeded(); await page.screenshot({ path: test.info().outputPath("play-390-now.png") }); await page.locator("[data-play-send-all]").scrollIntoViewIfNeeded(); await page.screenshot({ path: test.info().outputPath("play-390-notes.png") }); // And the dark base, at the desk's width. await page.emulateMedia({ colorScheme: "dark" }); await page.setViewportSize({ width: 1280, height: 860 }); await page.screenshot({ path: test.info().outputPath("play-1280-dark.png") }); }); test("a take plays its own timeline (its patch over the project's manifest); a recipe's take plays unrendered", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); // The takes page links each take's Play view. await page.goto(urls.takesHref(PROJECT)); await expect(page.locator('a[data-play-take="short"]')).toHaveAttribute("href", urls.playHref(PROJECT, { take: "short" })); // The take's clip comes through the raw route with the take named (whichever path decodes it), // and the version of the window's bytes: a refetch under the same name is a new URL. const takeMedia = page.waitForRequest((r) => { const u = new URL(r.url()); return u.pathname === "/api/report/raw" && u.searchParams.get("clip") === "c01" && u.searchParams.get("take") === "short" && /^[0-9a-f]{16}$/.test(u.searchParams.get("v") ?? ""); }); await page.goto(urls.playHref(PROJECT, { take: "short" })); await expect(root(page)).toHaveAttribute("data-play-cut", "short"); await expect(root(page)).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); await expect(page.locator('[data-play-take="short"]')).toContainText("take “Short”"); await expect(page.locator('[data-play-take-link="short"]')).toHaveAttribute("aria-current", "page"); await expect(rows(page)).toHaveCount(2); expect(await rows(page).evaluateAll((els) => els.map((e) => e.getAttribute("data-play-row")))).toEqual(["c04", "c01"]); await page.locator("[data-play-toggle]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "c01", { timeout: 20_000 }); await takeMedia; await page.keyboard.press("Space"); // The recipe's take: listed, not made, playing the project's timeline with its look. await page.locator('[data-play-take-link="quick-joins"]').click(); await expect(root(page)).toHaveAttribute("data-play-cut", "quick-joins"); await expect(page.locator('[data-play-take="quick-joins"]')).toContainText("not rendered yet"); await expect(root(page)).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); await expect(rows(page)).toHaveCount(5); // A hard-cut look: each entry is its segment, so c02 starts where c01's render segment ends (2.133 s), not mid-fade. await page.locator('[data-play-row="c02"]').press("Enter"); expect(await tNow(page)).toBeCloseTo(2.133, 2); }); test("a saved version plays its frozen manifest, read-only, with Open in draft -- never the manifest as it is now", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT, { take: "v1" })); await expect(root(page)).toHaveAttribute("data-play-cut", "v1"); await expect(page.locator('[data-play-take="v1"]')).toHaveAttribute("data-play-take-frozen", ""); await expect(page.locator('[data-play-take="v1"]')).toContainText("frozen as it was saved"); expect(await rows(page).evaluateAll((els) => els.map((e) => e.getAttribute("data-play-row")))).toEqual(["c01", "c02", "c04"]); await expect(page.locator('[data-play-open-draft="v1"]')).toHaveAttribute("href", `/projects/${PROJECT}/takes/v1/open`); // The draft (the manifest) still has all five. await page.goto(urls.playHref(PROJECT)); await expect(rows(page)).toHaveCount(5); }); test("opened at ?clip=&at=, the clock never jumps back there when the render's edges and the deck arrive while it plays", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); // The late data, held until the clock has run on from the place: the render's edges // (cut-points, 12-17 s on a long real cut) and the composed deck (the chrome preview). let release!: () => void; const gate = new Promise((r) => (release = r)); const held: string[] = []; for (const late of ["/api/report/cut-points", "/api/report/chrome/preview"]) { await page.route((u) => u.pathname === late, async (route) => { held.push(new URL(route.request().url()).pathname); await gate; await route.continue(); }); } await page.goto(urls.playHref(PROJECT, { clip: "c01", at: 0.3 })); await expect(root(page)).toHaveAttribute("data-play-state", "paused"); await expect(root(page)).toHaveAttribute("data-play-edges", "measuring"); await expect.poll(() => tNow(page)).toBeCloseTo(0.3, 2); // Every frame's clock, read off the engine itself (not the attribute React renders). await page.evaluate(() => { const w = window as Window & { __umtoolPlayEngine?: { now(): number; playing: boolean }; __clock?: number[] }; w.__clock = []; const loop = () => { const e = w.__umtoolPlayEngine; if (e?.playing) w.__clock!.push(e.now()); requestAnimationFrame(loop); }; requestAnimationFrame(loop); }); await page.locator("[data-play-toggle]").click(); await expect.poll(() => tNow(page), { timeout: 20_000 }).toBeGreaterThan(1.3); expect(held).toEqual(expect.arrayContaining(["/api/report/cut-points", "/api/report/chrome/preview"])); // The frames from here on are the ones the late data could pull back (the clock is past 1.3 s now). const released = await page.evaluate(() => ((window as Window & { __clock?: number[] }).__clock ?? []).length); release(); await expect(root(page)).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); // On past the re-plan: the clock goes on from where it was. const at = await tNow(page); await expect.poll(() => tNow(page), { timeout: 20_000 }).toBeGreaterThan(at + 0.8); await page.screenshot({ path: test.info().outputPath("play-at-no-jump.png") }); await page.keyboard.press("k"); const clock = await page.evaluate(() => (window as Window & { __clock?: number[] }).__clock ?? []); expect(clock.length).toBeGreaterThan(30); // The render's edges re-time the plan under the playhead (the same source second, a few // frames either way); a jump back to the place it opened at is a second and more. const back = clock.slice(1).map((t, i) => clock[i] - t); expect(Math.max(...back), `the clock went back: ${JSON.stringify(clock.map((t) => Math.round(t * 100) / 100))}`).toBeLessThan(0.25); // Never back to the place it opened at, once the late data arrived: every frame after the release. (Not "the second // half of the frames": since the deck composes (A1, fitTallyLabel), the frames after it come slower, and half the // count fell before the release.) expect(Math.min(...clock.slice(released))).toBeGreaterThan(1.3); });