import { test, expect } from "@playwright/test"; import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // ADD A CLIP, AS ONE FLOW (lane/ui-simplify, 5 Nov; acceptance test A): find // the words in a recording the cut draws on, the in and out from its cues, // placed after a row, added -- and its window fetched through the editor's // paced fetch (the stub editor here). Fixture: add-clip-fixture, a copy of the // clip editor's cut on testchan/vid1 (its cues: e2e/fixtures/make-fixture.mjs // CUES.vid1). The old spec form stays (clip-blur.spec.ts, "Add by spec"). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const P = "reports/add-clip-fixture"; const DIR = path.join(HERE, "..", ".e2e-song", "reports", "add-clip-fixture"); type Entry = Record & { id: string; type: string }; const timeline = (): Entry[] => JSON.parse(readFileSync(path.join(DIR, "video.manifest.json"), "utf8")).timeline; // Where the stub editor writes the fetched window: the fixture corpus other specs read (the walk fixture's // clips are on the same recording), so the window this spec fetched is taken away again when it ends. const CLIPS = path.join(HERE, "..", ".e2e-song", "channels", "testchan", "data", "vid1", "clips"); const clipFiles = () => (existsSync(CLIPS) ? readdirSync(CLIPS) : []); let had: Set | null = null; test.afterEach(() => { if (had) for (const f of clipFiles()) if (!had.has(f)) rmSync(path.join(CLIPS, f), { force: true }); had = null; }); // And whatever this file's fetches left that landed after a test ended (the stub editor writes the window when its // job runs, which can be after the page saw the job end): every spec after this one reads vid1's windows, and a // wider one changes their numbers (clip-bench.spec: peaks, `ready N of M`). let before: Set | null = null; test.beforeAll(() => { before = new Set(clipFiles()); }); test.afterAll(async () => { for (let i = 0; i < 20; i += 1) { const extra = clipFiles().filter((f) => before && !before.has(f)); for (const f of extra) rmSync(path.join(CLIPS, f), { force: true }); if (!extra.length && i >= 4) break; await new Promise((r) => setTimeout(r, 250)); } }); test("Add a clip: find the words, in and out on the cues, after a row, added and fetched", async ({ page }) => { test.setTimeout(120_000); // The head's + Add a clip lands on the Cut tab with the panel open. await page.goto(urls.projectHref(P, { tab: "overview" })); await page.locator("[data-primary-add-clip]").click(); await expect(page.locator('[data-add-clip="cut"]')).toBeVisible(); await page.locator("[data-add-clip-q]").fill("fourth one"); await page.locator("[data-add-clip-find]").click(); await expect(page.locator("[data-add-clip-hits]")).toHaveAttribute("data-add-clip-hits", "1"); await page.locator('[data-add-clip-hit="vid1@12"]').click(); // The moment's cue, lit: in 12, out at the end of its sentence (the same cue closes one). await expect(page.locator('[data-add-clip-edge="in"]')).toHaveValue("12"); await expect(page.locator('[data-add-clip-edge="out"]')).toHaveValue("15"); await expect(page.locator("[data-add-clip-quote]")).toHaveValue("A fourth one, done."); // Shift-click a later line: the out moves to its end, the quote follows. await page.locator('[data-add-clip-cue="18"]').click({ modifiers: ["Shift"] }); await expect(page.locator('[data-add-clip-edge="out"]')).toHaveValue("21"); await expect(page.locator("[data-add-clip-quote]")).toHaveValue(/A fourth one, done\..*it lands at last\./); // Nudge the in half a second later. await page.getByRole("button", { name: "in 0.5 s later" }).click(); await expect(page.locator('[data-add-clip-edge="in"]')).toHaveValue("12.5"); had = new Set(clipFiles()); const first = timeline()[0].id; await page.locator("[data-add-clip-after]").selectOption(first); await page.locator("[data-add-clip-submit]").click(); const added = page.locator("[data-add-clip-added]"); await expect(added).toBeVisible(); const id = await added.getAttribute("data-add-clip-added"); await expect.poll(() => timeline().map((e) => e.id).indexOf(id!)).toBe(1); const e = timeline()[1]; expect(e).toMatchObject({ type: "clip", video: "vid1", start: 12.5, end: 21 }); // Its window, through the editor's paced fetch: the job runs and ENDS (done, or the stub's own answer) before the // test does -- "not running" was true while it was still idle, so the window (vid1 0.00-41.00, a 20 s pad each // side) landed after the afterEach looked, and every later spec read a wider vid1 window (clip-bench, projects). await expect(added).toHaveAttribute("data-add-clip-fetch", /^(done|failed)$/, { timeout: 60_000 }); }); test("Add a clip from a pasted link: the recording the cut already carries, at its time", async ({ page }) => { await page.goto(urls.projectHref(P, { tab: "cut" })); await page.locator('[data-add-clip-open="cut"]').click(); await page.locator('[data-add-clip-mode="link"]').click(); await page.locator("[data-add-clip-link]").fill("testchan/vid1@0:09"); await page.locator("[data-add-clip-open-link]").click(); await expect(page.locator("[data-add-clip-moment]")).toHaveAttribute("data-add-clip-moment", "testchan/vid1@9"); await expect(page.locator('[data-add-clip-edge="in"]')).toHaveValue("9"); await expect(page.locator("[data-add-clip-quote]")).toHaveValue("Another whole sentence entirely."); });