import { test, expect as baseExpect } from "@playwright/test";
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
// A write is a request under the app's lock, and this machine runs other suites beside this one: room for it.
const expect = baseExpect.configure({ timeout: 15_000 });
// ---------------------------------------------------------------------------
// INSERT A TEASER OR CLOSING CARD (umtool redesign L6, item 2; components/projects/inspector/TeaserInspector.tsx):
// the Cut tab's "Insert a teaser or closing card" -- where, the lines with a break and a hold, the beat -- writes
// a `teaser` entry into the draft (`umtool timeline
teaser-insert` is the twin), refused in the build's teaser
// words, its row's Edit changes it after; no take touched. inspector-teaser-fixture (WRITES).
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial", timeout: 120_000 });
const P = "reports/inspector-teaser-fixture";
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "inspector-teaser-fixture");
const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8");
const timeline = () => (JSON.parse(manifestText()) as { timeline: Record[] }).timeline;
const takeText = () => readFileSync(path.join(DIR, "takes", "short", "take.json"), "utf8");
test("a closing card at the end: its lines, a break and a hold, its beat -- and a bad break refused", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 768 });
const take = takeText();
await page.goto(urls.projectHref(P, { tab: "cut" }));
await page.locator("[data-cut-insert-card]").click();
const panel = page.locator('[data-inspector="teaser"]');
await expect(panel.locator("[data-teaser-position]")).toHaveValue("end");
await panel.locator('[data-teaser-line="0"] [data-teaser-line-field="text"]').fill("The 291 scale bet");
await panel.locator("[data-teaser-add-line]").click();
const l1 = panel.locator('[data-teaser-line="1"]');
await l1.locator('[data-teaser-line-field="text"]').fill("Scale video: not posted as of Nov 8");
// A break that is not the end of its words: the build's rule, said; nothing written.
await l1.locator('[data-teaser-line-field="break"]').fill("not posted");
const before = manifestText();
await panel.locator('[data-teaser-insert="insert"]').click();
await expect(panel.locator('[data-teaser-said="refused"]')).toContainText("break must be the end of its text");
expect(manifestText()).toBe(before);
await l1.locator('[data-teaser-line-field="break"]').fill("as of Nov 8");
await l1.locator('[data-teaser-line-field="hold"]').fill("1.6");
await panel.locator("[data-teaser-beat]").fill("1.3");
await page.screenshot({ path: test.info().outputPath("teaser-insert.png") });
await panel.locator('[data-teaser-insert="insert"]').click();
await expect(panel.locator('[data-teaser-said="ok"]')).toContainText("Inserted close at 6");
expect(timeline().at(-1)).toEqual({ type: "teaser", id: "close", lines: ["The 291 scale bet", { text: "Scale video: not posted as of Nov 8", break: "as of Nov 8", hold: 1.6 }], beat: 1.3 });
expect(takeText()).toBe(take);
// Its row is in the cut.
await expect(page.locator('[data-entry="close"][data-kind="teaser"]')).toBeVisible();
});
test("a cold open at the start, then its row's Edit changes its beat; Undo takes that edit back", async ({ page, request }) => {
await page.setViewportSize({ width: 1366, height: 768 });
const take = takeText();
await page.goto(urls.projectHref(P, { tab: "cut" }));
await page.locator("[data-cut-insert-card]").click();
const panel = page.locator('[data-inspector="teaser"]');
await panel.locator("[data-teaser-position]").selectOption("start");
await panel.locator('[data-teaser-line="0"] [data-teaser-line-field="text"]').fill("Cold open");
await panel.locator('[data-teaser-insert="insert"]').click();
await expect(panel.locator('[data-teaser-said="ok"]')).toContainText("Inserted cold at 1");
expect(timeline()[0]).toEqual({ type: "teaser", id: "cold", lines: ["Cold open"] });
await page.reload();
const row = page.locator('[data-entry="cold"]');
await row.locator("[data-teaser-edit]").click();
const edit = row.locator('[data-inspector="teaser"]');
await expect(edit.locator('[data-teaser-line="0"] [data-teaser-line-field="text"]')).toHaveValue("Cold open");
await edit.locator("[data-teaser-beat]").fill("0.9");
await edit.locator('[data-teaser-insert="set"]').click();
await expect(edit.locator('[data-teaser-said="ok"]')).toContainText("Saved cold to the draft.");
expect(timeline()[0]).toEqual({ type: "teaser", id: "cold", lines: ["Cold open"], beat: 0.9 });
expect(takeText()).toBe(take);
// The route wants the token it handed out: a write without one is refused.
const bare = await request.post("/api/report/teaser", { data: { project: P, op: "set", id: "cold", patch: { beat: 1.1 } } });
expect(bare.status()).toBe(400);
expect(timeline()[0]).toEqual({ type: "teaser", id: "cold", lines: ["Cold open"], beat: 0.9 });
const 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("teaser");
expect(timeline()[0]).toEqual({ type: "teaser", id: "cold", lines: ["Cold open"] });
});