import { test, expect } from "@playwright/test"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // THE WORKING DRAFT AND ITS SAVED VERSIONS (umtool/docs/knobs.md, "The working draft and saved versions"; // lib/report/versions.mjs): every edit lands in video.manifest.json (the draft); a saved version is frozen; // opening a version saves the draft first when it has edits, and one Undo puts the draft -- and its parent -- // back. // // draft-versions-fixture (WRITES; make-fixture.mjs): play-cut-fixture's cut on vid1 (c01 c02 k01 c03 c04), // no takes. The tests run in order, each leaving what the next reads. // --------------------------------------------------------------------------- test.describe.configure({ mode: "serial" }); const P = "reports/draft-versions-fixture"; const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "draft-versions-fixture"); const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8"); const order = () => (JSON.parse(manifestText()) as { timeline: { id: string }[] }).timeline.map((e) => e.id).join(" "); const draftFrom = () => (JSON.parse(readFileSync(path.join(DIR, "takes", "draft.json"), "utf8")) as { from: string }).from; const base = (id: string) => readFileSync(path.join(DIR, "takes", id, "base.manifest.json"), "utf8"); const takeDoc = (id: string) => JSON.parse(readFileSync(path.join(DIR, "takes", id, "take.json"), "utf8")) as Record; test("edit → save v1 → edit → open v1: the draft saved as v2 first, v1 back byte for byte; Undo restores the pre-open draft and its parent", async ({ request }) => { expect(order()).toBe("c01 c02 k01 c03 c04"); const original = manifestText(); // An edit, then saved as v1: frozen byte for byte. let r = await request.post("/api/report/timeline", { data: { project: P, op: "move", id: "c04", toIndex: 0 } }); expect(r.ok(), await r.text()).toBe(true); const edited = manifestText(); expect(edited).not.toBe(original); r = await request.post("/api/report/takes/save", { data: { project: P, label: "First pass" } }); expect(r.ok(), await r.text()).toBe(true); expect(await r.json()).toMatchObject({ saved: true, id: "v1", label: "First pass" }); expect(base("v1")).toBe(edited); expect(takeDoc("v1")).toMatchObject({ version: true, draft: true, patch: null, window: "full" }); expect(draftFrom()).toBe("v1"); // Saving again with no edits saves nothing. r = await request.post("/api/report/takes/save", { data: { project: P } }); expect(await r.json()).toMatchObject({ saved: false, id: "v1" }); // Another edit: v1 does not follow it. r = await request.post("/api/report/timeline", { data: { project: P, op: "move", id: "c01", toIndex: 4 } }); expect(r.ok(), await r.text()).toBe(true); const preOpen = manifestText(); expect(order()).toBe("c04 c02 k01 c03 c01"); expect(base("v1")).toBe(edited); // Open v1: the draft (1 edit) is saved as v2 first, then v1 is the draft. r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1" } }); expect(r.ok(), await r.text()).toBe(true); const o = await r.json(); expect(o).toMatchObject({ opened: "v1", saved: "v2", how: "frozen" }); expect(base("v2")).toBe(preOpen); expect(takeDoc("v2")).toMatchObject({ version: true, draftedFrom: "v1" }); expect(manifestText()).toBe(edited); expect(draftFrom()).toBe("v1"); // Opening it again writes nothing. r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1" } }); expect(await r.json()).toMatchObject({ unchanged: true }); // Undo: exactly the open -- the pre-open draft, and its parent (v2, what it was saved as). r = await request.post("/api/report/timeline", { data: { project: P, op: "undo" } }); expect(r.ok(), await r.text()).toBe(true); expect((await r.json()).op).toBe("open-v1"); expect(manifestText()).toBe(preOpen); expect(draftFrom()).toBe("v2"); }); test("the CLI twin's contract: a stale token is refused, the draft untouched", async ({ request }) => { const before = manifestText(); const r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1", token: "1" } }); expect(r.status()).toBe(409); expect((await r.json()).stale).toBe(true); expect(manifestText()).toBe(before); }); test("Play is the draft, named by its parent; a version plays read-only; Open in draft asks, then lands on the draft; Save version on the head", async ({ page, request }) => { await page.setViewportSize({ width: 1366, height: 768 }); // After the undo above: the draft is v2's content, from v2, no edits. await page.goto(urls.playHref(P)); const root = page.locator("[data-play-cut]"); await expect(root).toHaveAttribute("data-play-state", "paused", { timeout: 30_000 }); await expect(root).toHaveAttribute("data-play-cut", ""); await expect(page.locator("[data-play-draft]")).toHaveAttribute("data-play-draft", "v2"); await expect(page.locator("[data-play-draft]")).toContainText("Draft · from v2 · no edits"); await page.screenshot({ path: test.info().outputPath("play-draft.png") }); // v1, read-only, with Open in draft. await page.locator('[data-play-take-link="v1"]').click(); await expect(root).toHaveAttribute("data-play-cut", "v1", { timeout: 20_000 }); await expect(page.locator('[data-play-take="v1"]')).toHaveAttribute("data-play-take-frozen", ""); await expect(page.locator('[data-play-take="v1"]')).toContainText("read-only"); await expect(page.locator("[data-play-watch-render]")).toHaveCount(0); await page.screenshot({ path: test.info().outputPath("play-version.png") }); await page.locator('[data-play-open-draft="v1"]').click(); const ask = page.locator('[data-open-draft="v1"]'); await expect(ask.locator("[data-open-draft-said]")).toContainText("no edits since v2 — nothing to save"); await expect(ask).toHaveAttribute("data-open-draft-saves", ""); await page.screenshot({ path: test.info().outputPath("open-in-draft.png") }); await ask.locator("[data-open-draft-confirm]").click(); // Play of the draft (Play's own page always carries a query since L7b: a bare /play is the workspace's). await expect(page).toHaveURL(/\/projects\/reports\/draft-versions-fixture\/play\?cut=1(&|$)/); await expect(page.locator("[data-play-draft]")).toContainText("Draft · from v1 · no edits", { timeout: 20_000 }); expect(manifestText()).toBe(base("v1")); expect(draftFrom()).toBe("v1"); // The head's Save version: nothing at no edits; after an edit, v3. await page.goto(urls.projectHref(P, { tab: "cut" })); await expect(page.locator('[data-play-link="header"]')).toHaveAttribute("href", urls.playHref(P)); await expect(page.locator("[data-primary-draft]")).toContainText("from v1 · no edits"); await page.locator("[data-save-version]").click(); await expect(page.locator("[data-save-version-said]")).toContainText("no edits since v1"); const r = await request.post("/api/report/timeline", { data: { project: P, op: "move", id: "c02", toIndex: 0 } }); expect(r.ok(), await r.text()).toBe(true); await page.reload(); await expect(page.locator("[data-primary-draft]")).toContainText("from v1 · 1 edit"); await page.locator("[data-save-version]").click(); await expect(page.locator("[data-save-version-said]")).toContainText("saved as v3"); await page.screenshot({ path: test.info().outputPath("head-save-version.png") }); expect(base("v3")).toBe(manifestText()); expect(draftFrom()).toBe("v3"); });