import { test, expect, type Page } from "@playwright/test";
import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
import { WARM_TIMEOUT, warm } from "./warm";
import { CUT_INTEGRITY, SITE_PLACEHOLDER } from "./fixtures/cut-integrity-fixture.mjs";
// ---------------------------------------------------------------------------
// CUT INTEGRITY (UI notes 2026-10-30): every surface through the window
// writer, each with its terminal twin.
//
// #3 a window-overlap row's "It's deliberate: c02 repeats c01" writes
// repeatOf; the Cut tab and the bench show "repeats c01"
// #5 a clip-cite-off row's "Move to 0:06" puts the clock AND the QR back
// on the clip
// #6 a take patch whose markers name a clip that is gone is a row; removing
// a clip re-anchors the patch's markers
// #7 ★ (s) and the walk note on the bench -- neither a verdict nor a
// correction; the Cut tab's ★ trailer candidates
// #8 Check links resolves every clip QR against the site's corpus.json
// contract (this spec's own server): one dead link blocks; a site that
// does not answer leaves them "not verified"
//
// Fixture (fixtures/cut-integrity-fixture.mjs), written by this file alone.
// The tests run in order, each leaving what the next reads.
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial" });
const PROJECT = `reports/${CUT_INTEGRITY}`;
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", CUT_INTEGRITY);
const MANIFEST = path.join(DIR, "video.manifest.json");
type Entry = { id: string; cite?: number; citeUrl?: string; repeatOf?: string; star?: boolean; walkNote?: string; correction?: string; verdict?: string };
const entries = () => (JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Entry[] }).timeline;
const entry = (id: string) => entries().find((e) => e.id === id);
// Screenshots for the morning review, when asked for (CUT_INTEGRITY_SHOTS=
).
const SHOTS = process.env.CUT_INTEGRITY_SHOTS ?? "";
const shot = async (page: Page, name: string) => {
if (!SHOTS) return;
mkdirSync(SHOTS, { recursive: true });
await page.screenshot({ path: path.join(SHOTS, `${name}.png`), fullPage: false });
};
// The site the QRs link: corpus.json -> testchan's manifest -> page-0000.json,
// holding vid1 and vid2 (not "nope").
let server: Server;
let origin = "";
const asked: string[] = [];
const startSite = () =>
new Promise((resolve) => {
server = createServer((req, res) => {
const p = new URL(req.url ?? "/", "http://x").pathname;
asked.push(p);
const json = (doc: unknown) => {
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify(doc));
};
if (p === "/corpus.json") {
return json({
spec: 5,
generatedAt: "2026-10-09T10:00:00.000Z",
site: { title: "Fixture archive" },
channels: [{ slug: "testchan", manifests: { transcripts: `${origin}/transcripts/testchan/manifest.json` } }],
});
}
if (p === "/transcripts/testchan/manifest.json") return json({ pageCount: 1, slugToPage: { vid1: 0, vid2: 0 } });
if (p === "/transcripts/testchan/page-0000.json") return json([{ id: "vid1" }, { id: "vid2" }]);
res.writeHead(404);
res.end();
});
server.listen(0, "127.0.0.1", () => {
origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
resolve();
});
});
test.beforeAll(async ({ playwright }) => {
test.setTimeout(WARM_TIMEOUT);
await startSite();
// The QRs on this run's server: the placeholder origin, replaced.
writeFileSync(MANIFEST, readFileSync(MANIFEST, "utf8").split(SITE_PLACEHOLDER).join(origin));
await warm(playwright, [urls.projectHref(PROJECT), urls.projectHref(PROJECT, { tab: "cut" }), urls.clipBenchHref(PROJECT, "c01")]);
});
test.afterAll(() => {
server?.close();
});
const row = (page: Page, kind: string, target: string) => page.locator(`[data-decision="${kind}"][data-target="${target}"]`);
test("the check rows: an overlap, a clock and QR off the clip, a stale take patch, links never checked — each with its settle and its fix", async ({ page }) => {
await page.goto(urls.projectHref(PROJECT, { tab: "overview" }));
const overlap = row(page, "window-overlap", "c01");
await expect(overlap).toContainText("overlaps c02 by 2.0s");
await expect(overlap.locator('[data-settle-button="c02"]')).toHaveText("It's deliberate: c02 repeats c01");
await expect(overlap.locator("[data-fix]")).toHaveAttribute("data-fix", /^umtool window reports\/cut-integrity-fixture c02 --repeat-of c01/);
const cite = row(page, "clip-cite-off", "c03");
await expect(cite).toContainText("the clock and the QR point 6 s before the clip (cite 0:00, it plays 0:06–0:08)");
await expect(cite.locator('[data-settle-button="c03"]')).toHaveText("Move to 0:06");
await expect(cite.locator("[data-fix]")).toHaveAttribute("data-fix", "umtool window reports/cut-integrity-fixture c03 --cite 6");
const stale = row(page, "patch-markers-stale", "draft-1.patch.json");
await expect(stale).toContainText('a deck marker names a clip not in the cut: "Hand-made" after gone');
await expect(row(page, "links-unchecked", "QR links")).toContainText("4 archive links never checked against the sites they open");
await expect(row(page, "links-unchecked", "QR links").locator('[data-settle-button="links"]')).toHaveText("Check links");
await shot(page, "3-5-6-8-check-rows");
});
test("#3 It's deliberate: repeatOf through the writer, the row gone, the chip on the Cut tab and the bench", async ({ page }) => {
await page.goto(urls.projectHref(PROJECT, { tab: "overview" }));
await row(page, "window-overlap", "c01").locator('[data-settle-button="c02"]').click();
await expect(row(page, "window-overlap", "c01")).toHaveCount(0);
expect(entry("c02")?.repeatOf).toBe("c01");
await page.goto(urls.projectHref(PROJECT, { tab: "cut" }));
await expect(page.locator('[data-entry="c02"] [data-repeat-of="c01"]')).toHaveText("repeats c01");
await shot(page, "3-repeat-of-chip-cut-tab");
await page.goto(urls.clipBenchHref(PROJECT, "c02"));
await expect(page.locator('[data-bench-repeat-of="c01"]')).toHaveText("repeats c01");
// #7: an editorial note written as a correction on a confirmed clip moves to the walk note in one press.
await expect(async () => {
await page.locator("[data-move-to-walk-note]").click({ timeout: 2000 });
await expect(page.locator("[data-move-to-walk-note]")).toHaveCount(0, { timeout: 2000 });
}).toPass({ timeout: 15_000 });
await expect.poll(() => entry("c02")?.walkNote).toBe("might want a bit of silence");
expect(entry("c02")?.correction).toBeUndefined();
expect(entry("c02")?.verdict).toBe("confirmed");
await expect(page.locator('[data-attrib-field="walkNote"]')).toHaveValue("might want a bit of silence");
});
test("#5 Move to 0:06: the clock and the QR back on the clip, and the row gone", async ({ page }) => {
await page.goto(urls.projectHref(PROJECT, { tab: "overview" }));
await row(page, "clip-cite-off", "c03").locator('[data-settle-button="c03"]').click();
await expect(row(page, "clip-cite-off", "c03")).toHaveCount(0);
expect(entry("c03")?.cite).toBe(6);
expect(entry("c03")?.citeUrl).toBe(`${origin}/?v=testchan%2Fvid1&t=6`);
});
test("#7 ★ (s) and the walk note on the bench: not a verdict, not a correction; the Cut tab's trailer candidates", async ({ page }) => {
await page.goto(urls.clipBenchHref(PROJECT, "c01"));
const star = page.locator("[data-verdict-star]");
await expect(star).toHaveAttribute("data-verdict-star", "off");
// The bench's keys answer once it has hydrated: `s` stars.
await expect(async () => {
await page.locator("body").press("s");
await expect(star).toHaveAttribute("data-verdict-star", "on", { timeout: 1500 });
}).toPass({ timeout: 15_000 });
await expect.poll(() => entry("c01")?.star).toBe(true);
await expect(page.locator("[data-bench-star]")).toBeVisible();
const note = page.locator('[data-attrib-field="walkNote"]');
await note.fill("THIS IS GOLD");
await note.press("Tab");
await expect.poll(() => entry("c01")?.walkNote).toBe("THIS IS GOLD");
expect(entry("c01")?.correction).toBeUndefined();
expect(entry("c01")?.verdict).toBeUndefined();
await shot(page, "7-star-walk-note-bench");
await page.goto(urls.projectHref(PROJECT, { tab: "cut" }));
await expect(page.locator('[data-entry="c01"] [data-star="c01"]')).toHaveText("★ trailer");
await expect(page.locator('[data-walk-note-of="c01"]')).toContainText("THIS IS GOLD");
const cands = page.locator("[data-trailer-candidates]");
await expect(cands).toHaveAttribute("data-trailer-candidates", "1");
await expect(cands.locator('[data-trailer-candidate="c01"]')).toContainText("0:01–0:05 (4.0 s)");
await expect(cands).toContainText(`umtool trailer ${PROJECT} --candidates --json`);
await shot(page, "7-trailer-candidates-cut-tab");
// ☆ again clears it.
await page.goto(urls.clipBenchHref(PROJECT, "c01"));
await page.locator('[data-verdict-star="on"]').click();
await expect.poll(() => entry("c01")?.star).toBeUndefined();
await page.locator('[data-verdict-star="off"]').click();
await expect.poll(() => entry("c01")?.star).toBe(true);
});
test("#8 Check links: one link dead on the site blocks, naming the clip and why; offline they are not verified", async ({ page }) => {
await page.goto(urls.projectHref(PROJECT, { tab: "overview" }));
await row(page, "links-unchecked", "QR links").locator('[data-settle-button="links"]').click();
const host = new URL(origin).host;
const dead = row(page, "link-dead", host);
await expect(dead).toBeVisible({ timeout: 30_000 });
await expect(dead).toHaveAttribute("data-severity", "blocking");
await expect(dead).toContainText(`1 of 4 QR links don't open on ${host} (deployed 2026-10-09): c04 — c04: "nope" is not published in testchan on ${host}`);
await expect(row(page, "links-unchecked", "QR links")).toHaveCount(0);
// Polite: corpus.json, the channel's manifest, its one page -- each once.
expect(asked.filter((p) => p === "/corpus.json")).toHaveLength(1);
expect(asked.filter((p) => p.endsWith("/page-0000.json"))).toHaveLength(1);
const doc = JSON.parse(readFileSync(path.join(DIR, "out", "links.json"), "utf8")) as { links: { clip: string; state: string }[] };
expect(doc.links.map((l) => [l.clip, l.state])).toEqual([["c01", "ok"], ["c03", "ok"], ["c02", "ok"], ["c04", "dead"]]);
await shot(page, "8-link-dead");
// The site goes away: a check now cannot verify, and says so -- it does not call them dead.
await new Promise((r) => server.close(() => r()));
await dead.locator('[data-settle-button="links"]').click();
const unverified = row(page, "links-unverified", host);
await expect(unverified).toBeVisible({ timeout: 30_000 });
await expect(unverified).toHaveAttribute("data-severity", "info");
await expect(unverified).toContainText(`4 links not verified`);
await expect(unverified).toContainText(`couldn't reach ${host}`);
await expect(row(page, "link-dead", host)).toHaveCount(0);
await shot(page, "8-links-unverified-offline");
});
test("#6 removing a clip re-anchors the take patch's markers; the hand-made one is still a row", async ({ page }) => {
await page.goto(urls.projectHref(PROJECT, { tab: "cut" }));
await page.locator('[data-row-menu="c04"]').click();
await page.locator('[data-entry="c04"] [data-row-action="remove"]').click();
await expect.poll(() => entries().map((e) => e.id)).toEqual(["c01", "c03", "c02"]);
// The patch is synced just after the manifest: poll it too (a single read raced it under load).
const patchMarkers = () =>
(JSON.parse(readFileSync(path.join(DIR, "draft-1.patch.json"), "utf8")) as { render: { chrome: { deck: { markers: { after: string; label: string }[] } } } }).render.chrome.deck.markers;
await expect.poll(patchMarkers).toEqual([{ after: "c02", label: "After four" }, { after: "gone", label: "Hand-made" }]);
await page.goto(urls.projectHref(PROJECT, { tab: "overview" }));
await expect(row(page, "patch-markers-stale", "draft-1.patch.json")).toContainText('"Hand-made" after gone — a take made with --patch draft-1.patch.json is refused');
await expect(row(page, "patch-markers-stale", "draft-1.patch.json")).not.toContainText("After four");
await shot(page, "6-patch-markers-stale");
// Take the cut's markers: nothing in the cut is called "Hand-made", so it is left, and said.
await row(page, "patch-markers-stale", "draft-1.patch.json").locator('[data-settle-button="draft-1.patch.json"]').click();
await expect(page.locator("[data-settle-said]")).toContainText('0 markers moved · left by hand: "Hand-made" after gone');
});