import { test, expect, type Frame, type Page } from "@playwright/test"; import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // The clip editor's live preview (plans/live-preview.md): the cut's on-screen // chrome composited over the playing clip, in step with the playhead -- the // deck's date title, the tally as of the playhead (stepping when the stamp // lands), the stamp, the finale on the last clip -- the deck's words edited // and patched in as typed, then saved; and Render this clip as a job (the // renderer stubbed: e2e/fixtures/clip-render.e2e.mjs), its file shown inline. // Fixture: e2e/fixtures/live-preview-fixture.mjs. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PROJECT = "reports/live-preview-fixture"; const DIR = path.join(FIXTURE, "reports", "live-preview-fixture"); type Entry = { id: string; onscreen?: { title?: string; subtitle?: string } }; const readManifest = (): { render: Record; timeline: Entry[] } => JSON.parse(readFileSync(path.join(DIR, "video.manifest.json"), "utf8")); const entry = (id: string) => readManifest().timeline.find((e) => e.id === id) ?? null; /** Move the editor's player by `d` seconds (file seconds are source seconds, shifted), paused. */ async function nudgePlayer(page: Page, d: number) { await page.locator("[data-editor-player] video[data-player]").evaluate(async (v: HTMLVideoElement, by: number) => { v.pause(); const want = v.currentTime + by; await new Promise((resolve) => { v.addEventListener("seeked", () => resolve(), { once: true }); v.currentTime = want; }); }, d); } /** The editor's player has loaded `clip`'s media (it loads each clip's own, then seeks to its cut). */ async function playerOn(page: Page, clip: string) { await expect .poll(() => page.locator("[data-editor-player] video[data-player]").evaluate((v: HTMLVideoElement) => `${v.currentSrc.includes(`clip=`) ? new URL(v.currentSrc).searchParams.get("clip") : "?"}:${v.readyState >= 2}`), { timeout: 20_000 }) .toBe(`${clip}:true`); } /** The opacity a composition's element has right now (gsap's autoAlpha), inside its frame. */ const opacityIn = (frame: Frame, selector: string) => frame.locator(selector).evaluate((el) => Number(getComputedStyle(el).opacity) * (getComputedStyle(el).visibility === "hidden" ? 0 : 1)); const frameOf = (page: Page, part: RegExp) => { const f = page.frames().find((x) => part.test(x.url())); if (!f) throw new Error(`no frame ${part}`); return f; }; test.beforeEach(() => { test.setTimeout(150_000); test.skip( !readManifest().render.fontRegular, "the deck refuses to draw without render.fontRegular/fontBold, and make-fixture.mjs found no font on this machine", ); }); test("the live preview: the deck over the clip, the tally and the stamp at the playhead, the finale, words, Render this clip", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.clipEditorHref(PROJECT, { clip: "c02", mode: "edit" })); await expect(page.locator("[data-clip-editor]")).toHaveAttribute("data-clip-editor", "c02"); await expect(page.locator("[data-editor-player]")).not.toHaveAttribute("data-editor-player", "looking", { timeout: 15_000 }); const live = page.locator('[data-live-preview="c02"]'); await expect(live).toHaveAttribute("data-live-state", "ready", { timeout: 60_000 }); await expect(page.getByTestId("live-preview")).toHaveAttribute("data-deck-ready", "1", { timeout: 60_000 }); // Never built: the estimate, and the preview says what it does not do. await expect(page.locator("[data-live-approximate]")).toBeVisible(); const deck = page.frameLocator('[data-testid="live-preview-iframe"]'); await expect(deck.locator('[data-seg="c02"] .deck-title')).toHaveText("2 Dec 2024"); await expect(page.locator("[data-live-readout]")).toContainText("clip 2 of 3"); // The clip's two marks, each its own stamp and tally step. The first's button puts the playhead a second // before it lands (source 4.0, the clip's first second). await playerOn(page, "c02"); await expect(page.locator("[data-live-goto-mark]")).toHaveCount(2); await expect(page.locator('[data-live-goto-mark="k1b"]')).toHaveAttribute("data-live-goto-stamp", "6"); const go = page.locator('[data-live-goto-mark="k1"]'); await expect(go).toHaveAttribute("data-live-goto-stamp", "5"); await go.click(); await expect(live).toHaveAttribute("data-live-where", "inside"); const t0 = Number(await live.getAttribute("data-live-t")); await expect(page.locator("[data-live-tally]")).toHaveAttribute("data-live-tally", "CORROBORATED:0,CONTRADICTED:0"); await expect(page.locator("[data-live-stamp]")).toHaveAttribute("data-live-stamp", ""); // 1.4 s on (source 5.4): the stamp has landed on its source second, the tally has stepped. await nudgePlayer(page, 1.4); await expect.poll(async () => Number(await live.getAttribute("data-live-t"))).toBeCloseTo(t0 + 1.4, 1); await expect(page.locator("[data-live-stamp]")).toHaveAttribute("data-live-stamp", "k1:on"); await expect(page.locator("[data-live-tally]")).toHaveAttribute("data-live-tally", "CORROBORATED:0,CONTRADICTED:1"); // And the pages say the same: the stamp's page shows k1, the deck's tally shows Grift 1. await expect(page.getByTestId("live-stamp")).toHaveAttribute("data-feed-ready", "1", { timeout: 15_000 }); await expect.poll(() => opacityIn(frameOf(page, /stamp-preview/), '.stamp[data-claim="k1"]')).toBeGreaterThan(0.9); await expect.poll(() => opacityIn(frameOf(page, /\/sourced\/index\.html/), '[data-k="ty.CONTRADICTED.n1"]')).toBeGreaterThan(0.9); // The outgoing 0 rolls out (power2.in) over STAMP_MOTION.roll, 0.4 s from 5.0: at 5.4 the playhead // may sit on the roll's last frames (it read ~0.2 there), so it is judged a tenth on. await nudgePlayer(page, 0.1); await expect.poll(() => opacityIn(frameOf(page, /\/sourced\/index\.html/), '[data-k="ty.CONTRADICTED.n0"]')).toBeLessThan(0.1); // 0.7 s more (source 6.2): the clip's second mark, its own stamp and step (its stamp is out by 6.3). await nudgePlayer(page, 0.7); await expect(page.locator("[data-live-stamp]")).toHaveAttribute("data-live-stamp", "k1b:on"); await expect(page.locator("[data-live-tally]")).toHaveAttribute("data-live-tally", "CORROBORATED:1,CONTRADICTED:1"); await expect.poll(() => opacityIn(frameOf(page, /stamp-preview/), '.stamp[data-claim="k1b"]')).toBeGreaterThan(0.9); // Its 1 rolls in over 0.4 s from 6.0 (it read 0.88 at 6.2): judged once the roll is over. await nudgePlayer(page, 0.2); await expect.poll(() => opacityIn(frameOf(page, /\/sourced\/index\.html/), '[data-k="ty.CORROBORATED.n1"]')).toBeGreaterThan(0.9); // Back before both (source 4.7): no stamp, nothing counted -- the pages are seeked back too. await nudgePlayer(page, -1.7); await expect(page.locator("[data-live-stamp]")).toHaveAttribute("data-live-stamp", ""); await expect(page.locator("[data-live-tally]")).toHaveAttribute("data-live-tally", "CORROBORATED:0,CONTRADICTED:0"); await expect.poll(() => opacityIn(frameOf(page, /stamp-preview/), '.stamp[data-claim="k1"]')).toBeLessThan(0.1); // The deck's words: patched into the page as typed, saved on Enter, composed again. const title = page.locator("[data-live-title]"); await title.fill("3 Dec 2024"); await expect(deck.locator('[data-seg="c02"] .deck-title')).toHaveText("3 Dec 2024"); expect(entry("c02")?.onscreen?.title).toBe("2 Dec 2024"); await title.press("Enter"); await expect.poll(() => entry("c02")?.onscreen?.title).toBe("3 Dec 2024"); await expect(page.locator("[data-editor-status]")).toHaveText("Saved the on-screen words"); await expect(page.getByTestId("live-preview")).toHaveAttribute("data-deck-ready", "1", { timeout: 60_000 }); await expect(deck.locator('[data-seg="c02"] .deck-title')).toHaveText("3 Dec 2024"); // The last clip is the finale: its page is laid over the deck once it opens; its claim counts there, undrawn. await page.locator('[data-editor-row="c03"]').click(); await playerOn(page, "c03"); const fin = page.locator('[data-live-preview="c03"]'); await expect(fin).toHaveAttribute("data-live-state", "ready"); await expect(page.locator('[data-live-goto-mark="k2"]')).toContainText("counts k2"); await page.locator('[data-live-goto-mark="k2"]').click(); await expect(fin).toHaveAttribute("data-live-where", "inside"); await nudgePlayer(page, 1.0); await expect(page.locator("[data-live-finale-up]")).toBeVisible(); await expect(page.getByTestId("live-finale")).toHaveAttribute("data-feed-ready", "1", { timeout: 30_000 }); await expect(page.locator("[data-live-tally]")).toHaveAttribute("data-live-tally", "CORROBORATED:2,CONTRADICTED:1"); await expect(page.locator("[data-live-stamp]")).toHaveAttribute("data-live-stamp", ""); // Render this clip: a job (stubbed), the file shown inline when done. const render = page.locator('[data-clip-render="c03"]'); await render.locator("[data-clip-render-start]").click(); await expect(render.locator("[data-clip-render-job]")).toHaveAttribute("data-clip-render-job", "done", { timeout: 30_000 }); await expect(render.locator("[data-clip-render-video]")).toBeVisible(); expect(existsSync(path.join(DIR, "out", "sourced", "clip-preview", "c03.mp4"))).toBe(true); await expect(render.locator("[data-clip-render-start]")).toHaveText("Render this clip again"); });