import { test, expect, type Page } from "@playwright/test"; import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { openPanel } from "./panels"; // --------------------------------------------------------------------------- // Tighten to the sentence, snap an edge to its pause, and Propose tight // windows (UI notes 2026-10-30, item 4; lib/report/tightWindow.mjs, // tightPropose.mjs, components/projects/bench/TightenTools.tsx, // report/TightWindowsPanel.tsx): // // - the bench's zoom draws a box per WORD (the corpus's words.json for the // recording) and the pauses (the silences and the gaps between words) // - Tighten to this sentence writes both edges through the window writer, // Undo puts them back; Snap edge (start / end) moves one edge to the // middle of its nearest pause // - the Cut tab's Propose tight windows lists before/after per clip and // writes NOTHING until a row's Apply; an applied row's Undo restores it // // Fixture: tight-fixture (make-fixture.mjs), which this spec WRITES and puts // back before each test: t01 tightvid 1.2-7.4, t02 3.0-9.0 (already the // sentence), t03 0.5-8.6 with a cut 3.5-5.0; vid1's cues (sentences 0-3, 3-9, // 9-12), the tone window cached as 0-9, timed words in the corpus. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const PROJECT = "reports/tight-fixture"; const MANIFEST = path.join(HERE, "..", ".e2e-song", "reports", "tight-fixture", "video.manifest.json"); const SHOTS = process.env.U3_SHOTS ?? null; type Clip = { id: string; start: number; end: number; cutStart?: number; cutEnd?: number; cite?: number }; const clipOf = (id: string) => (JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Clip[] }).timeline.find((e) => e.id === id)!; let PRISTINE = ""; test.beforeAll(() => { PRISTINE = readFileSync(MANIFEST, "utf8"); }); test.beforeEach(() => { test.setTimeout(120_000); writeFileSync(MANIFEST, PRISTINE); }); const shot = async (page: Page, name: string) => { await page.screenshot({ path: test.info().outputPath(name) }); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, name) }); }; const openBench = async (page: Page, clip = "t01") => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(urls.clipBenchHref(PROJECT, clip)); await page.locator("[data-wave-box]").scrollIntoViewIfNeeded(); await expect(page.locator("[data-wave-box]")).toBeInViewport(); }; test("the zoom draws the recording's words as boxes and its pauses", async ({ page }) => { await openBench(page); // A 9 s window is close enough at fit: the words and the pauses are drawn. const word = page.locator("[data-wave-word='because']"); await expect(word).toHaveCount(1, { timeout: 20_000 }); await expect(word).toHaveAttribute("data-wave-word-at", "5.10-5.85"); expect(await page.locator("[data-wave-word]").count()).toBeGreaterThanOrEqual(10); // The pause between "sentence." and "And" (the words' gap, 2.80-3.20, merged with the measured silence). await expect(page.locator("[data-wave-pause]").first()).toBeAttached(); const pauses = await page.locator("[data-wave-pause]").evaluateAll((els) => els.map((e) => e.getAttribute("data-wave-pause"))); expect(pauses.some((p) => { const [a, b] = String(p).split("-").map(Number); return a <= 2.81 && b >= 3.19; })).toBe(true); await page.locator("[data-zoom-in]").click(); await page.locator("[data-wave-box]").scrollIntoViewIfNeeded(); await expect(word).toBeVisible(); await shot(page, "u3-1-word-boxes-and-pauses.png"); }); test("Tighten to this sentence writes both edges through the window writer; Undo puts them back", async ({ page }) => { await openBench(page); await page.locator("[data-tighten]").click(); // No playhead: the window 1.2-7.4 holds no whole sentence, so the one it is inside (3-9), the end at the cache's 9. await expect.poll(() => [clipOf("t01").start, clipOf("t01").end]).toEqual([3, 9]); await expect(page.locator("body")).toContainText("tightened to the sentence the window is inside"); await page.locator("[data-edge-undo]").click(); await expect.poll(() => [clipOf("t01").start, clipOf("t01").end]).toEqual([1.2, 7.4]); await expect(page.locator("body")).toContainText("undone"); }); test("Snap edge: the start, then the end, each to the middle of its nearest pause", async ({ page }) => { await openBench(page); await page.locator("[data-snap-edge='start']").click(); // 1.2 sits between "a" (…0.8) / "complete" (0.9…) and "complete" (…1.6) / "sentence." (1.7…): the nearer pause is 0.8-0.9. await expect.poll(() => clipOf("t01").start).toBe(0.85); await expect(page.locator("[data-edge-undo]")).toBeVisible(); await page.locator("[data-snap-edge='end']").click(); // 7.4: "it" (…7.0) / "finishes" (7.1…) is nearer than "finishes" (…7.9) / "here." (8.0…). await expect.poll(() => clipOf("t01").end).toBe(7.05); expect(clipOf("t01").start).toBe(0.85); }); test("Propose tight windows: a review list, nothing written until Apply; Apply and Undo per row", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1100 }); await page.goto(urls.section(PROJECT, "cut")); await openPanel(page, "options", "cut"); await openPanel(page, "advanced", "cut"); const panel = page.locator("[data-tight-windows]"); await panel.scrollIntoViewIfNeeded(); await panel.locator("[data-action='propose-tight']").click(); const t01 = panel.locator("[data-tight-row='t01']"); await expect(t01).toBeVisible({ timeout: 60_000 }); await expect(t01.locator("[data-tight-before]")).toHaveAttribute("data-tight-before", "1.2-7.4"); await expect(t01.locator("[data-tight-after]")).toHaveAttribute("data-tight-after", "3-9"); await expect(t01.locator("[data-tight-delta]")).toHaveAttribute("data-tight-delta", "-0.20"); // t03's cut (its quote, 3.5-5.0) anchors it: the sentence 3-9. await expect(panel.locator("[data-tight-row='t03'] [data-tight-after]")).toHaveAttribute("data-tight-after", "3-9"); // t02 is already the sentence: not listed until every clip is shown. await expect(panel.locator("[data-tight-row='t02']")).toHaveCount(0); await expect(panel.locator("[data-tight-counts]")).toContainText("2 of 3 clips would change · 1 already tight"); // Proposing wrote nothing. expect(readFileSync(MANIFEST, "utf8")).toBe(PRISTINE); await shot(page, "u3-2-propose-tight-review.png"); await t01.locator("[data-action='tight-apply']").click(); await expect(t01).toHaveAttribute("data-tight-state", "applied"); await expect.poll(() => [clipOf("t01").start, clipOf("t01").end]).toEqual([3, 9]); // Only that row: t03 is as it was. expect([clipOf("t03").start, clipOf("t03").end]).toEqual([0.5, 8.6]); await t01.locator("[data-action='tight-undo']").click(); await expect(t01).toHaveAttribute("data-tight-state", "undone"); await expect.poll(() => [clipOf("t01").start, clipOf("t01").end]).toEqual([1.2, 7.4]); await panel.locator("[data-action='tight-show-all']").click(); await expect(panel.locator("[data-tight-row='t02']")).toHaveAttribute("data-tight-state", "same"); });