import { test, expect, type Page } from "@playwright/test"; import { 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"; // --------------------------------------------------------------------------- // Export one clip (umtool/docs/knobs.md, "Export one clip"): Play the cut's // Export clip writes the clip at the playhead -- clean, the cut's edges, no // deck -- into the project's exports/ under the clip's name, then offers // Download (the file as an attachment) and Open folder (recorded by the // fixture's E2E_UMTOOL_OPEN_FOLDER_LOG, never a real file manager); raw is // the whole fetched window. On a phone, the bench's reader has the same // Export and Download. // // Fixtures: play-cut-fixture (c01 playvid 1.0-3.0, "24 Oct 2020") and // phone-walk-fixture (p01 walk1 3.0-6.0). Their exports/ are removed after. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PLAY = "reports/play-cut-fixture"; const WALK = "reports/phone-walk-fixture"; const EXPORT = "reports/export-fixture"; const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); const SHOTS = process.env.U3_SHOTS ?? null; const exportsOf = (project: string) => path.join(FIXTURE, ...project.split("/"), "exports"); const OPEN_LOG = path.join(FIXTURE, "open-folder.jsonl"); test.beforeEach(() => { test.setTimeout(120_000); }); test.afterAll(() => { for (const p of [PLAY, WALK, EXPORT]) rmSync(exportsOf(p), { recursive: true, force: true }); }); const ready = async (page: Page) => { const root = page.locator("[data-play-cut]"); await expect(root).toHaveAttribute("data-play-state", "paused"); await expect(root).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); }; test("Play the cut: Export clip writes the clip at the playhead to exports/; Download and Open folder; raw", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PLAY)); await ready(page); await expect(page.locator("[data-play-cut]")).toHaveAttribute("data-play-item", "c01"); await openPanel(page, "options", "play"); const go = page.locator('[data-clip-export-go="c01"]'); await expect(go).toBeEnabled(); await go.click(); const state = page.locator("[data-clip-export-state]"); await expect(state).toHaveAttribute("data-clip-export-state", "ready", { timeout: 60_000 }); const name = "c01-2020-10-24-this-is-a-complete-sentence.mp4"; await expect(state).toHaveAttribute("data-clip-export-name", name); await expect(state).toContainText(`exports/${name}`); expect(existsSync(path.join(exportsOf(PLAY), name))).toBe(true); // Download: the file, as an attachment under its own name. const href = await page.locator("[data-clip-export-download]").getAttribute("href"); expect(href).toBeTruthy(); const res = await page.request.get(href!); expect(res.status()).toBe(200); expect(res.headers()["content-type"]).toBe("video/mp4"); expect(res.headers()["content-disposition"]).toContain(`attachment; filename="${name}"`); expect((await res.body()).length).toBeGreaterThan(1000); // The listing has it. const listed = (await (await page.request.get(`/api/report/exports?project=${encodeURIComponent(PLAY)}`)).json()) as { exports: { name: string }[]; canOpen: boolean }; expect(listed.exports.map((e) => e.name)).toContain(name); expect(listed.canOpen).toBe(true); // A name that is not one plain mp4 under exports/ is nothing. expect((await page.request.get(`/api/report/exports?project=${encodeURIComponent(PLAY)}&name=..%2Fvideo.manifest.json`)).status()).toBe(404); // Open folder: asked of this machine's file manager (here, the log), the file selected. await page.locator("[data-clip-export-open]").click(); await expect(page.locator("[data-clip-export-opened]")).toBeVisible(); const asked = readFileSync(OPEN_LOG, "utf8").trim().split("\n").map((l) => JSON.parse(l) as { dir: string; file: string | null }); expect(asked.at(-1)?.dir).toBe(exportsOf(PLAY)); expect(asked.at(-1)?.file).toBe(path.join(exportsOf(PLAY), name)); // From another device it is refused, with the reason (and the page's own Host is not that device's). const far = await page.request.post("/api/report/exports", { data: { project: PLAY, name }, headers: { "x-forwarded-for": "192.168.1.50" } }); expect(far.status()).toBe(403); expect(((await far.json()) as { error: string }).error).toContain("Download"); // raw: the whole fetched window, its own name. await page.locator('[data-clip-export-raw="c01"]').click(); await expect(state).toHaveAttribute("data-clip-export-name", "c01-raw-2020-10-24-this-is-a-complete-sentence.mp4", { timeout: 60_000 }); await expect(state).toContainText("the whole window"); await page.screenshot({ path: test.info().outputPath("play-export-1366.png") }); // A clip with no window on disk (c03): refused by the server with "fetch it first". const refused = await page.request.post("/api/report/clip-export", { data: { project: PLAY, clip: "c03" } }); expect(refused.status()).toBe(409); expect(((await refused.json()) as { error: string }).error).toContain("fetch it first"); }); test("the bench on a phone: Export clip in the reader, then Download", async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await page.goto(urls.clipBenchHref(WALK, "p01")); await expect(page.locator('[data-bench="p01"]')).toHaveCount(1); const go = page.locator('[data-clip-export-go="p01"]'); await go.scrollIntoViewIfNeeded(); await expect(go).toBeEnabled(); await go.click(); const state = page.locator("[data-clip-export-state]"); await expect(state).toHaveAttribute("data-clip-export-state", "ready", { timeout: 60_000 }); await expect(state).toHaveAttribute("data-clip-export-name", /^p01-\d{4}-\d{2}-\d{2}-and-because\.mp4$/); await expect(page.locator("[data-clip-export-download]")).toBeVisible(); expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(0); await page.screenshot({ path: test.info().outputPath("bench-export-390.png") }); }); test("Export ▾ on the bench: with N s of context, and the full exchange fetched through the editor first", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(urls.clipBenchHref(EXPORT, "x01")); await expect(page.locator('[data-bench="x01"]')).toHaveCount(1); const menu = page.locator('[data-clip-export-menu="x01"]'); await menu.scrollIntoViewIfNeeded(); await expect(menu).toBeEnabled(); await menu.click(); const options = page.locator("[data-clip-export-options]"); await expect(options).toBeVisible(); await expect(options.locator('[data-clip-export-kind="cut"]')).toHaveText("As cut (no chrome)"); // The full exchange starts from the clip's extent ± 30 s (the recording starts at 0). await expect(options.locator("[data-clip-export-from]")).toHaveValue("0"); await expect(options.locator("[data-clip-export-to]")).toHaveValue("36"); await page.screenshot({ path: test.info().outputPath("bench-export-menu.png") }); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "u3-3-export-menu.png") }); // With 2 s of context: the cached 0–9 window holds it, nothing is fetched. await options.locator("[data-clip-export-context]").fill("2"); await options.locator('[data-clip-export-kind="context"]').click(); const state = page.locator("[data-clip-export-state]"); await expect(state).toHaveAttribute("data-clip-export-state", "ready", { timeout: 60_000 }); await expect(state).toHaveAttribute("data-clip-export-name", "x01-ctx2-2026-07-22-the-full-exchange.mp4"); await expect(state).toContainText("the cut with 2 s either side, as recorded"); expect(existsSync(path.join(exportsOf(EXPORT), "x01-ctx2-2026-07-22-the-full-exchange.mp4"))).toBe(true); // A context the cache does not hold, asked without `fetch`: refused, with what it needs. const no = await page.request.post("/api/report/clip-export", { data: { project: EXPORT, clip: "x01", kind: "context", context: 20 } }); expect(no.status()).toBe(409); expect(((await no.json()) as { error: string }).error).toContain("not on disk"); // The full exchange, 0–20 s: past the cached 9 s, so the job asks the editor first (fetch-window), then cuts it. await menu.click(); await options.locator("[data-clip-export-from]").fill("0"); await options.locator("[data-clip-export-to]").fill("20"); await options.locator('[data-clip-export-kind="raw"]').click(); await expect(state).toHaveAttribute("data-clip-export-name", "x01-raw-0-20-2026-07-22-the-full-exchange.mp4", { timeout: 90_000 }); await expect(state).toContainText("the span asked for, as recorded"); const asked = JSON.parse(readFileSync(STUB_LOG, "utf8")) as { videoId?: string; clipId?: string; from?: number; to?: number }[]; const fetchAsk = asked.filter((r) => r.videoId === "exportvid").at(-1); expect(fetchAsk?.clipId).toBe("x01"); expect(fetchAsk?.from).toBe(0); expect(fetchAsk?.to).toBe(20); expect(existsSync(path.join(exportsOf(EXPORT), "x01-raw-0-20-2026-07-22-the-full-exchange.mp4"))).toBe(true); });