import { test, expect, type Page } from "@playwright/test"; import { copyFileSync, existsSync, readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { openPanel } from "./panels"; // --------------------------------------------------------------------------- // The Trailer page (UI notes 2026-10-30, item 11): // // trailer-fixture six clips c01–c06 (1.5 s each), c04 and c06 starred on // the walk; promo/trailer-beats.json (1-space, an array): // c01 inside its clip, c03 starting 0.5 s before its // window, c02 after it -- out of the cut's order. // WRITES promo/; every test starts from the fixture's. // // The running length against the limit, the beat outside its clip and "fit // to clip", Order by the cut, the platform limit, the ★ candidates, add, // move, remove -- each through POST /api/report/trailer-beats. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const DIR = path.join(HERE, "..", ".e2e-song", "reports", "trailer-fixture"); const BEATS = path.join(DIR, "promo", "trailer-beats.json"); const PROJECT = "reports/trailer-fixture"; const doc = () => JSON.parse(readFileSync(BEATS, "utf8")); const beats = () => { const d = doc(); return (Array.isArray(d) ? d : d.beats) as { id?: string; start?: number; end?: number }[]; }; test.beforeAll(() => { if (!existsSync(`${BEATS}.pristine`)) copyFileSync(BEATS, `${BEATS}.pristine`); }); test.beforeEach(() => { copyFileSync(`${BEATS}.pristine`, BEATS); rmSync(`${BEATS}.bak`, { force: true }); }); async function open(page: Page) { await page.goto(urls.trailerHref(PROJECT)); await expect(page.getByTestId("trailer-editor")).toBeVisible(); } test("the Cut tab links the Trailer page; it says the running length, the limit and the beat to fix", async ({ page }) => { await page.goto(urls.section(PROJECT, "timeline")); await openPanel(page, "options", "cut"); await openPanel(page, "advanced", "cut"); await expect(page.locator("[data-trailer-link]")).toHaveText("Trailer — 3 beats"); await page.locator("[data-trailer-link]").click(); await expect(page.getByTestId("trailer-editor")).toBeVisible(); // 3 × 1.5 s, two 0.3 s crossfades. await expect(page.getByTestId("trailer-total")).toHaveText("0:03.9"); await expect(page.getByTestId("trailer-editor")).toContainText("of 2:20.0"); await expect(page.getByTestId("trailer-outside")).toHaveText("1 beat to fix"); await expect(page.locator("[data-beat-problems='2']")).toHaveText("starts 0.50 s before c03's window (5–6.5)"); await expect(page.getByTestId("trailer-order")).toHaveText("Order by the cut"); }); test("fit to clip pulls the beat inside; Order by the cut; the file keeps its shape", async ({ page }) => { await open(page); await page.locator("[data-beat-fit='2']").click(); await expect.poll(() => beats()[1]).toEqual({ id: "c03", start: 5, end: 6.5 }); await expect(page.getByTestId("trailer-outside")).toHaveCount(0); await page.getByTestId("trailer-order").click(); await expect.poll(() => beats().map((b) => b.id)).toEqual(["c01", "c02", "c03"]); await expect(page.getByTestId("trailer-order")).toHaveText("in the cut's order"); expect(readFileSync(BEATS, "utf8")).toMatch(/^\[\n \{/); }); test("the platform's limit, and the ★ clips added in the cut's order until it is over", async ({ page }) => { await open(page); await expect(page.getByTestId("trailer-candidates")).toContainText("2 not yet beats"); await expect(page.locator("[data-candidate='c04']")).toContainText("this is gold"); await page.getByTestId("trailer-limit").selectOption("instagram"); await expect.poll(() => doc().limit).toBe(90); await expect(page.getByTestId("trailer-editor")).toContainText("of 1:30.0"); await page.getByTestId("trailer-limit-seconds").fill("6"); await page.getByTestId("trailer-limit-seconds").press("Enter"); await expect.poll(() => doc().limit).toBe(6); // c04 plays after every clip beat so far: it goes last (the cut's order). await page.locator("[data-candidate='c04'] button").click(); await expect.poll(() => beats().map((b) => b.id)).toEqual(["c01", "c03", "c02", "c04"]); await expect(page.getByTestId("trailer-total")).toHaveAttribute("data-over", "0"); await page.locator("[data-candidate='c06'] button").click(); await expect.poll(() => beats().length).toBe(5); await expect(page.getByTestId("trailer-total")).toHaveAttribute("data-over", "1"); await expect(page.locator("[data-beat='5']")).toHaveAttribute("data-beat-over", "1"); await expect(page.getByTestId("trailer-candidates")).toContainText("0 not yet beats"); }); test("add a beat from a clip, edit its seconds, move it and remove it", async ({ page }) => { await open(page); await page.getByTestId("trailer-add-clip").selectOption("c05"); await expect(page.getByTestId("trailer-add-from")).toHaveValue("9"); await page.getByTestId("trailer-add-to").fill("10.25"); await page.getByTestId("trailer-add").click(); await expect.poll(() => beats().map((b) => b.id)).toEqual(["c01", "c03", "c02", "c05"]); await page.locator("[data-beat-to='4']").fill("10.5"); await page.locator("[data-beat-to='4']").press("Enter"); await expect.poll(() => beats()[3].end).toBe(10.5); await page.locator("[data-beat-up='4']").click(); await expect.poll(() => beats().map((b) => b.id)).toEqual(["c01", "c03", "c05", "c02"]); await page.locator("[data-beat-remove='1']").click(); await expect.poll(() => beats().map((b) => b.id)).toEqual(["c03", "c05", "c02"]); }); test("the outro card: on adds its 3 s to the running length; its title written through the writer; off keeps the words", async ({ page }) => { await open(page); await expect(page.getByTestId("trailer-total")).toHaveText("0:03.9"); await expect(page.getByTestId("trailer-outro-title")).toHaveCount(0); await page.getByTestId("trailer-outro").click(); await expect.poll(() => doc().outro).toEqual({ on: true }); // 3.9 s of beats, then the card's 3.5 s less its 0.5 s fade in. await expect(page.getByTestId("trailer-total")).toHaveText("0:06.9"); await expect(page.getByTestId("trailer-editor")).toContainText("outro 3.5 s"); await page.getByTestId("trailer-outro-title").fill("The Whole Story"); await page.getByTestId("trailer-outro-title").press("Enter"); await expect.poll(() => doc().outro).toEqual({ on: true, title: "The Whole Story" }); await page.getByTestId("trailer-outro").click(); await expect.poll(() => doc().outro).toEqual({ on: false, title: "The Whole Story" }); await expect(page.getByTestId("trailer-total")).toHaveText("0:03.9"); await expect(page.getByTestId("trailer-outro-title")).toHaveValue("The Whole Story"); });