import { test, expect, type APIRequestContext, 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 MEASURE'S INSTRUMENTS (umtool/docs/knobs.md, Modules: "Scale dial", // "Lost odometer", "Weight trail"): three deck modules that read the // schedule's measure slice and declare `play` -- each page composed for the // chrome preview, posting `:ready`, seeked by `deck:seek` in the cut's // seconds; the same page the build captures. // // Fixture: e2e/fixtures/instruments-fixture.mjs -- the Play cut with // deck.dial/odometer/trail on and a built schedule carrying six rows of the // measure (w07 stated >240 0.6 s into c01, w21 remembered ≈390 at c01 +1.2, // w27 goal 250 at c02 +0.4, w64 lost 31 at c02 +1.0, w115 stated <300 at c04 // +0.2, w118 stated 291.5 at c04 +0.6; the spec reads the seconds back from // the fixture's schedule). The chrome preview without `estimate` reads that // schedule. (Play the cut composes on the estimate's clock: the instruments // join it there once the build's measure producer puts the slice in the // estimate.) // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const PROJECT = "reports/instruments-fixture"; const DIR = path.join(HERE, "..", ".e2e-song", "reports", "instruments-fixture"); const manifest = () => JSON.parse(readFileSync(path.join(DIR, "video.manifest.json"), "utf8")); /** The fixture's schedule (its seconds are the estimate's on this machine's fixture render block). */ const schedule = () => JSON.parse(readFileSync(path.join(DIR, "out", "sourced", "schedule.json"), "utf8")) as { total: number; measure: { events: { row: string; t: number }[] } }; /** A cut second: a row's, plus `after`. */ const tOf = (row: string, after = 0) => schedule().measure.events.find((e) => e.row === row)!.t + after; /** The cut's last moment. */ const tEnd = () => schedule().total - 0.05; type Layer = { id: string; z?: number; region?: string; ready?: string; src?: string; error?: string; off?: string; geometry?: { x: number; y: number; width: number; height: number } }; async function preview(request: APIRequestContext, data: Record) { const res = await request.post("/api/report/chrome/preview", { data: { project: PROJECT, ...data } }); expect(res.ok(), await res.text()).toBeTruthy(); return (await res.json()) as { layers: Layer[]; geometry: { footage: { x: number; width: number } } }; } /** * A harness page holding one layer's page in an iframe, as Play holds it: the * messages it posts are kept in `window.__msgs`; `seek(t)` posts `deck:seek`. */ async function harness(page: Page, src: string, baseURL: string) { const url = new URL(`${src}${src.includes("?") ? "&" : "?"}preview=1`, baseURL).toString(); await page.setContent(` `); return { frame: page.frameLocator("#f"), messages: () => page.evaluate(() => (window as unknown as { __msgs: { type: string }[] }).__msgs.map((m) => m.type)), seek: (t: number) => page.evaluate((v) => (document.getElementById("f") as HTMLIFrameElement).contentWindow!.postMessage({ type: "deck:seek", t: v }, "*"), t), }; } /** The readout block the dial shows: the one `cv` block that is visible. */ const visibleReadout = (frame: ReturnType) => frame.locator(".read").evaluateAll((els) => els.filter((e) => getComputedStyle(e).visibility !== "hidden" && Number(getComputedStyle(e).opacity) > 0.5).map((e) => (e.querySelector(".big")?.textContent ?? "").trim()), ); /** The needle's rotation in degrees, read off its computed transform. */ const needleDeg = (frame: ReturnType) => frame.locator('[data-k="nd"]').evaluate((el) => { const m = new DOMMatrix(getComputedStyle(el).transform); return Math.round((Math.atan2(m.b, m.a) * 180) / Math.PI); }); test.beforeEach(() => { test.setTimeout(120_000); }); test("the dial, the odometer and the trail are play layers of a cut with a measure, at the instruments' column", async ({ request }) => { test.skip(!manifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); const full = await preview(request, {}); const by = Object.fromEntries(full.layers.map((l) => [l.id, l])); for (const id of ["dial", "odometer", "trail"]) { expect(by[id], `${id} is a play layer`).toBeTruthy(); expect(by[id].error ?? null).toBeNull(); expect(by[id].ready).toBe(`${id}:ready`); expect(by[id].src).toContain(`${id}-preview/index.html`); } // The column at the frame's right, the footage moved to the left for it (1920x1080). expect(by.dial.geometry).toEqual({ x: 1296, y: 24, width: 600, height: 540 }); expect(by.odometer.geometry).toEqual({ x: 1296, y: 576, width: 600, height: 140 }); expect(by.trail.geometry).toEqual({ x: 1296, y: 728, width: 600, height: 138 }); expect(full.geometry.footage.x).toBe(24); expect(full.geometry.footage.x + full.geometry.footage.width).toBeLessThanOrEqual(1296 - 24); // No finale in this cut: no dial finale. expect(by["dial-finale"]).toBeUndefined(); // The page it serves is the instrument's own, drawn from the measure. const dial = await (await request.get(by.dial.src!)).text(); expect(dial).toContain('id="dial-data"'); expect(dial).toContain(">240LB"); expect(dial).toContain("291.5LB"); const odo = await (await request.get(by.odometer.src!)).text(); expect(odo).toContain('id="odometer-data"'); expect(odo).toContain("POUNDS HE SAYS HE"); }); test("the dial's page posts dial:ready and is seeked in the cut's seconds: the needle and the readout land on each stated weight", async ({ page, request, baseURL }) => { test.skip(!manifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); const { layers } = await preview(request, { layers: ["dial"] }); const dial = layers.find((l) => l.id === "dial")!; expect(dial.src, dial.off ?? dial.error).toBeTruthy(); const h = await harness(page, dial.src!, baseURL!); await expect.poll(h.messages, { timeout: 30_000 }).toContain("dial:ready"); // Before any stated weight: the pre-state in words (rev 9), the needle at rest on the scale's foot (220 lb: -135°). await h.seek(0.2); await expect.poll(() => visibleReadout(h.frame)).toEqual(["NO WEIGHTSTATED YET"]); await expect.poll(() => needleDeg(h.frame)).toBe(-135); // After "over 240" has settled (and the ≈390 is said): its weight, the needle at 240 (-105°), its wedge up. const settled = tOf("w21", 0.9); await h.seek(settled); await expect.poll(() => visibleReadout(h.frame)).toEqual([">240LB"]); await expect.poll(() => needleDeg(h.frame)).toBe(-105); await expect(h.frame.locator('[data-k="q1"]')).toBeVisible(); // The remembered ≈390 is a ghost with its as-of date. await expect(h.frame.locator('[data-k="gn"]')).toBeVisible(); await expect(h.frame.locator('[data-k="ga0"]')).toContainText("AS OF"); // The last stated weight: 291.5 (-27.75°), and back again -- a seek lands the same from either side. await h.seek(tEnd()); await expect.poll(() => visibleReadout(h.frame)).toEqual(["291.5LB"]); await expect.poll(() => needleDeg(h.frame)).toBe(-28); await h.seek(settled); await expect.poll(() => visibleReadout(h.frame)).toEqual([">240LB"]); await page.screenshot({ path: test.info().outputPath("instruments-dial.png") }); }); test("the odometer and the trail post ready and seek: the wheels only roll up, the line draws by date", async ({ page, request, baseURL }) => { test.skip(!manifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); const { layers } = await preview(request, { layers: ["odometer", "trail"] }); const by = Object.fromEntries(layers.map((l) => [l.id, l])); const odo = await harness(page, by.odometer.src!, baseURL!); await expect.poll(odo.messages, { timeout: 30_000 }).toContain("odometer:ready"); // A wheel's digit: which of its strip's cells stands in its window. const digits = () => odo.frame.locator(".wheel").evaluateAll((ws) => ws.map((w) => { const strip = w.querySelector(".strip") as HTMLElement; const y = new DOMMatrix(getComputedStyle(strip).transform).f; return Math.round(-y / w.getBoundingClientRect().height) % 10; }).join("")); await odo.seek(tEnd()); const end = await digits(); await odo.seek(0.1); const start = await digits(); expect(Number(end)).toBeGreaterThan(Number(start)); await page.screenshot({ path: test.info().outputPath("instruments-odometer.png") }); const trail = await harness(page, by.trail.src!, baseURL!); await expect.poll(trail.messages, { timeout: 30_000 }).toContain("trail:ready"); const shown = () => trail.frame.locator(".pt").evaluateAll((ps) => ps.filter((p) => Number(getComputedStyle(p).opacity) > 0.5).length); await trail.seek(0.3); await expect.poll(shown).toBe(0); await trail.seek(tEnd()); // Every weight said so far: the three stated (>240, <300, 291.5) and the remembered ≈390, hollow, at its as-of date. await expect.poll(shown).toBe(4); const remembered = () => trail.frame.locator(".pt.rem").evaluateAll((ps) => ps.filter((p) => Number(getComputedStyle(p).opacity) > 0.5).length); await expect.poll(remembered).toBe(1); await expect(trail.frame.locator('[data-k="uc1"]')).toContainText("UNDER 300 · SAID 2×"); await expect(trail.frame.locator('[data-k="uc1"]')).toBeVisible(); }); test("Play the cut draws the dial, the odometer and the trail from the estimate's measure: laid at the column, ready, seeked to the playhead", async ({ page }) => { test.skip(!manifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); // play-instruments-fixture: the extras' Play ledger (one stated weight, 291.5, on c03) with the instruments on. const PLAY = "reports/play-instruments-fixture"; const root = page.locator("[data-play-cut]"); await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PLAY, { clip: "c04", at: 1 })); await expect(root).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); await expect(root).toHaveAttribute("data-play-item", "c04"); for (const id of ["dial", "odometer", "trail"]) { await expect(page.locator(`[data-play-layer="${id}"]`)).toHaveAttribute("data-play-layer-up", "1", { timeout: 30_000 }); await expect(page.getByTestId(`play-layer-${id}`)).toHaveAttribute("data-feed-ready", "1", { timeout: 30_000 }); } const dial = page.frameLocator('[data-testid="play-layer-dial-iframe"]'); await expect.poll(() => visibleReadout(dial), { timeout: 15_000 }).toEqual(["291.5LB"]); await page.screenshot({ path: test.info().outputPath("instruments-play-c04.png") }); // Back to c01, before the weight is said: the pre-state. await page.locator('[data-play-row="c01"]').press("Enter"); await expect(root).toHaveAttribute("data-play-item", "c01"); await expect.poll(() => visibleReadout(dial), { timeout: 15_000 }).toEqual(["NO WEIGHTSTATED YET"]); }); test("the module panel lists the instruments and previews the dial alone, seeked by its scrubber", async ({ page }) => { test.skip(!manifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); await page.goto(urls.section(PROJECT, "onscreen")); const panel = page.getByTestId("modules-panel"); await expect(panel.getByTestId("module-dial")).toBeAttached({ timeout: 60_000 }); await expect(panel.getByTestId("module-odometer")).toBeAttached(); await expect(panel.getByTestId("module-trail")).toBeAttached(); const card = page.getByTestId("module-dial"); await card.locator("summary").click(); await card.getByTestId("module-preview-dial").click(); const iframe = card.getByTestId("module-preview-iframe"); await expect(iframe).toHaveAttribute("src", /dial-preview\/index\.html/); // The scrubber to the cut's end, as a person drags it (a key, so React hears the input). await card.getByTestId("module-preview-scrub").focus(); await page.keyboard.press("End"); const frame = card.frameLocator('[data-testid="module-preview-iframe"]'); await expect.poll(() => visibleReadout(frame), { timeout: 30_000 }).toEqual(["291.5LB"]); await page.screenshot({ path: test.info().outputPath("instruments-panel.png") }); });