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";
// ---------------------------------------------------------------------------
// A BLOCK'S MENU (umtool redesign L8b; components/projects/workspace/BlockMenu.tsx): right-click a block of the
// workspace's strip (or its ⋯, or Shift+F10 on it) and its menu lists the strip's own writers, then the registry
// verbs whose row names that block's kind (lib/cli/verbs.mjs `blocks`) -- a clip's menu no post verb, a post's no
// clip verb. A verb opens THE ⌘K form (shell/CommandForm), filled in from the block by the row's `fromBlock`;
// Run is POST /api/cli as ⌘K's, the draft changes, and the workspace's Undo takes it back.
//
// block-menus-fixture (WRITES; make-fixture.mjs): the workspace fixture's cut, its own copy --
// c01 c02 p-5 k01 c03 c04 (p-5 a post item, k01 a chapter card). Each test leaves the draft as it found it.
// E2E_BLOCK_MENUS_SHOTS=
: screenshots there (1366×768).
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial" });
const P = "reports/block-menus-fixture";
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "block-menus-fixture");
const SHOTS = process.env.E2E_BLOCK_MENUS_SHOTS ?? "";
const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8");
const manifest = () => JSON.parse(manifestText()) as { timeline: { id: string; type: string; lines?: unknown[] }[] };
const order = () => manifest().timeline.map((e) => e.id).join(" ");
const ORIGINAL = "c01 c02 p-5 k01 c03 c04";
const block = (page: Page, id: string) => page.locator(`[data-ws-block="${id}"]`);
const menuOf = (page: Page, id: string) => page.locator(`[role="menu"][data-ws-menu="${id}"]`);
const palette = (page: Page) => page.locator("dialog[data-jump]");
const shot = async (page: Page, name: string) => {
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, `${name}.png`) });
};
async function open(page: Page) {
await page.goto(urls.workspaceHref(P));
await expect(page.locator(`[data-workspace="${P}"]`)).toBeVisible();
await expect(page.locator("[data-ws-strip] [data-ws-block]")).toHaveCount(6);
// The palette listens (the form opens in it) and the workspace has read the draft's token (its Undo asks on it).
await expect(page.locator("dialog[data-jump][data-ready]")).toBeAttached({ timeout: 60_000 });
await expect(page.locator("[data-workspace]")).toHaveAttribute("data-ws-token", /./);
}
test.beforeEach(async ({ page }) => {
test.setTimeout(150_000);
await page.setViewportSize({ width: 1366, height: 768 });
});
test("a clip's menu: the clip's verbs, no post's; teaser-insert's form is filled in after this block; Copy as CLI; Run; Undo restores", async ({ page, context }) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
const original = manifestText();
expect(order()).toBe(ORIGINAL);
await open(page);
await block(page, "c01").click({ button: "right" });
const menu = menuOf(page, "c01");
await expect(menu).toBeVisible();
await expect(menu).toHaveAttribute("aria-label", "Clip c01");
// The direct items: no form needed. c01 is first: Move left has nowhere to go.
for (const a of ["edit", "play", "move-left", "move-right", "remove"]) await expect(menu.locator(`[data-ws-menu-action="${a}"]`)).toBeVisible();
await expect(menu.locator('[data-ws-menu-action="move-left"]')).toHaveAttribute("aria-disabled", "true");
await expect(menu.locator('[data-ws-menu-link="bench"]')).toHaveAttribute("href", urls.clipBenchHref(P, "c01"));
// `ingredient` is navigation: its life-cycle page, a link (not a run).
await expect(menu.locator('[data-ws-menu-link="nav:ingredient"]')).toHaveAttribute("href", urls.ingredientHref("testchan", "vid1"));
await expect(menu.locator('[data-ws-menu-verb="ingredient"]')).toHaveCount(0);
// The registry's clip verbs, and not a post's.
for (const v of ["window", "set", "still", "quote-check", "links", "timeline move", "timeline remove", "timeline teaser-insert", "timeline split"]) {
await expect(menu.locator(`[data-ws-menu-verb="${v}"]`)).toHaveCount(1);
}
await expect(menu.locator('[data-ws-menu-verb^="posts "]')).toHaveCount(0);
await expect(menu.locator('[data-ws-menu-verb="timeline teaser"]')).toHaveCount(0);
// It fits the window, and the first item has the focus.
const box = await menu.boundingBox();
expect(box!.y).toBeGreaterThanOrEqual(0);
expect(box!.y + box!.height).toBeLessThanOrEqual(768);
await expect(menu.locator('[data-ws-menu-action="edit"]')).toBeFocused();
await shot(page, "block-menu-clip");
// teaser-insert: THE ⌘K form, filled in from the block -- the project, and after this block.
await menu.locator('[data-ws-menu-verb="timeline teaser-insert"]').click();
await expect(menu).toHaveCount(0);
const form = palette(page).locator('[data-cli-form="timeline teaser-insert"]');
await expect(form).toBeVisible();
await expect(form.locator('[data-cli-arg="project"] input')).toHaveValue(P);
await expect(form.locator('[data-cli-arg="--after"] input')).toHaveValue("c01");
await form.locator('[data-cli-arg="--line"] textarea').fill("From the menu");
const line = `node umtool/bin/umtool.mjs timeline ${P} teaser-insert --after c01 --line 'From the menu'`;
await expect(form.locator("[data-cli-line]")).toHaveText(line);
await form.locator("[data-cli-copy]").click();
await expect(form.locator("[data-cli-copy]")).toHaveText("Copied");
expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(line);
const pbox = await palette(page).boundingBox();
expect(pbox!.y + pbox!.height, "the form fits 1366×768").toBeLessThanOrEqual(768);
await shot(page, "block-menu-form");
// Run: POST /api/cli, as ⌘K's; the draft has the card after c01.
await form.locator("[data-cli-run]").click();
await expect(form.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 90_000 });
const tl = manifest().timeline;
expect(tl[0].id).toBe("c01");
expect(tl[1].type).toBe("teaser");
const card = tl[1].id;
expect(order()).toBe(`c01 ${card} c02 p-5 k01 c03 c04`);
// The run announced the write: the strip under the palette has the card already.
await expect(block(page, card)).toBeVisible({ timeout: 15_000 });
// Esc: the palette closes and the focus is back on the block the menu was for.
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
await expect(block(page, "c01")).toBeFocused();
// One Undo restores the draft byte for byte.
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
await expect(block(page, card)).toHaveCount(0);
});
test("a post's menu and a chapter card's: their verbs; Move right, Remove and ⋯ from the menu, each undone", async ({ page }) => {
const original = manifestText();
expect(order()).toBe(ORIGINAL);
await open(page);
await block(page, "p-5").click({ button: "right" });
const post = menuOf(page, "p-5");
await expect(post).toBeVisible();
await expect(post).toHaveAttribute("aria-label", "Post item p-5");
for (const v of ["posts set-item", "posts narrate", "posts fx", "timeline teaser-insert", "timeline move"]) await expect(post.locator(`[data-ws-menu-verb="${v}"]`)).toHaveCount(1);
for (const v of ["window", "set", "still", "quote-check", "timeline split"]) await expect(post.locator(`[data-ws-menu-verb="${v}"]`)).toHaveCount(0);
await expect(post.locator('[data-ws-menu-link="bench"]')).toHaveAttribute("href", urls.postBenchHref(P, "p-5"));
await shot(page, "block-menu-post");
// Move right: the strip's writer (the timeline's move), one undo point.
await post.locator('[data-ws-menu-action="move-right"]').click();
await expect.poll(order, { timeout: 15_000 }).toBe("c01 c02 k01 p-5 c03 c04");
await expect(page.locator("[data-ws-undo-said]")).toContainText("Moved p-5");
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
// The chapter card, from its ⋯: the cut's verbs, no clip's or post's; Remove, then Undo.
await block(page, "k01").hover();
await page.locator('[data-ws-block-more="k01"]').click();
const card = menuOf(page, "k01");
await expect(card).toBeVisible();
await expect(card).toHaveAttribute("aria-label", "Chapter card k01");
await expect(card.locator('[data-ws-menu-verb="timeline teaser-insert"]')).toHaveCount(1);
await expect(card.locator('[data-ws-menu-verb="window"], [data-ws-menu-verb^="posts "]')).toHaveCount(0);
await expect(card.locator('[data-ws-menu-action="edit"]')).toHaveCount(0);
await card.locator('[data-ws-menu-action="remove"]').click();
await expect.poll(order, { timeout: 15_000 }).toBe("c01 c02 p-5 c03 c04");
await expect(page.locator("[data-ws-undo-said]")).toContainText("Removed k01");
await expect(block(page, "k01")).toHaveCount(0);
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
await expect(block(page, "k01")).toBeVisible();
// A press outside closes a menu and writes nothing.
await block(page, "c03").click({ button: "right" });
await expect(menuOf(page, "c03")).toBeVisible();
await page.locator("[data-ws-clock]").click();
await expect(menuOf(page, "c03")).toHaveCount(0);
expect(manifestText()).toBe(original);
});
test("keyboard only: Shift+F10 opens it, the arrows move, Enter chooses, Esc closes; the focus goes back to the block", async ({ page }) => {
const original = manifestText();
await open(page);
await block(page, "c02").focus();
await page.keyboard.press("Shift+F10");
const menu = menuOf(page, "c02");
await expect(menu).toBeVisible();
const action = (a: string) => menu.locator(`[data-ws-menu-action="${a}"]`);
await expect(action("edit")).toBeFocused();
await page.keyboard.press("ArrowDown");
await expect(action("play")).toBeFocused();
await page.keyboard.press("ArrowUp");
await expect(action("edit")).toBeFocused();
// Up from the first wraps to the last; Home goes back.
await page.keyboard.press("ArrowUp");
await expect(menu.locator('[role="menuitem"]').last()).toBeFocused();
await page.keyboard.press("Home");
await expect(action("edit")).toBeFocused();
await page.keyboard.press("Escape");
await expect(menu).toHaveCount(0);
await expect(block(page, "c02")).toBeFocused();
// Play from here: ↓ Enter -- the block selected, the viewer playing it.
await page.keyboard.press("Shift+F10");
await expect(action("play")).toBeVisible();
await page.keyboard.press("ArrowDown");
await expect(action("play")).toBeFocused();
await page.keyboard.press("Enter");
await expect(menu).toHaveCount(0);
await expect(block(page, "c02")).toHaveAttribute("aria-selected", "true");
await expect(page.locator("[data-ws-viewer]")).toHaveAttribute("data-play-item", "c02", { timeout: 20_000 });
await expect(block(page, "c02")).toBeFocused();
await page.keyboard.press("Space");
await expect(page.locator("[data-ws-viewer]")).toHaveAttribute("data-play-state", "paused");
// A letter jumps to the item it starts: w is `window`; Enter opens its form, the clip filled in; Esc returns.
await page.keyboard.press("Shift+F10");
await expect(menu).toBeVisible();
await page.keyboard.press("w");
await expect(menu.locator('[data-ws-menu-verb="window"]')).toBeFocused();
await page.keyboard.press("Enter");
const form = palette(page).locator('[data-cli-form="window"]');
await expect(form).toBeVisible();
await expect(form.locator('[data-cli-arg="project"] input')).toHaveValue(P);
await expect(form.locator('[data-cli-arg="clip"] input')).toHaveValue("c02");
await expect(form.locator("[data-cli-line]")).toHaveText(`node umtool/bin/umtool.mjs window ${P} c02`);
await shot(page, "block-menu-keyboard-form");
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
await expect(block(page, "c02")).toBeFocused();
// The context-menu key opens it too; Tab closes it.
await page.keyboard.press("ContextMenu");
await expect(menu).toBeVisible();
await page.keyboard.press("Tab");
await expect(menu).toHaveCount(0);
expect(manifestText()).toBe(original);
});
test("a variant twin: Remove takes the entry on screen, never its twin; a twin the view cannot place is not moved or removed", async ({ page }) => {
const original = manifestText();
type Entry = Record & { id: string };
const m = JSON.parse(original) as { timeline: Entry[] };
// c03's full-cut twin BEFORE it: the first entry with the id is not the one the default cut plays.
const i3 = m.timeline.findIndex((e) => e.id === "c03");
m.timeline.splice(i3, 0, { ...m.timeline[i3], variant: "full", quote: "the full cut's twin" });
// c04 turned by `variants` (the view makes it anew: it says no place for it) with a full-cut twin of its own.
const i4 = m.timeline.findIndex((e) => e.id === "c04");
const c04 = m.timeline[i4];
m.timeline.splice(i4, 1, { ...c04, variant: "full", quote: "c04's full twin" }, { ...c04, variants: { sourced: { quote: "turned" } } });
const written = JSON.stringify(m, null, 2) + "\n";
writeFileSync(path.join(DIR, "video.manifest.json"), written);
try {
expect(order()).toBe("c01 c02 p-5 k01 c03 c03 c04 c04");
await open(page);
await block(page, "c03").click({ button: "right" });
const menu = menuOf(page, "c03");
await expect(menu.locator('[data-ws-menu-action="remove"]')).not.toHaveAttribute("aria-disabled", "true");
await menu.locator('[data-ws-menu-action="remove"]').click();
// The one on screen left; its twin (the full cut's) stays.
await expect.poll(() => manifest().timeline.filter((e) => e.id === "c03").map((e) => (e as { variant?: string }).variant ?? "").join(","), { timeout: 15_000 }).toBe("full");
await expect(page.locator("[data-ws-undo-said]")).toContainText("Removed c03");
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(written);
// The strip has read the undo back (c03 is on it again) before c04's menu is asked for.
await expect(block(page, "c03")).toBeVisible();
// c04: no place of its own, its id twice -- never guessed at.
await block(page, "c04").click({ button: "right" });
const twin = menuOf(page, "c04");
for (const a of ["remove", "move-left", "move-right"]) {
await expect(twin.locator(`[data-ws-menu-action="${a}"]`)).toHaveAttribute("aria-disabled", "true");
await expect(twin.locator(`[data-ws-menu-action="${a}"]`)).toHaveAttribute("title", "this entry has a twin — use the Cut tab");
}
// Pressed anyway (past Playwright's own aria-disabled wait): nothing happens.
await twin.locator('[data-ws-menu-action="remove"]').click({ force: true });
await page.keyboard.press("Escape");
await expect(twin).toHaveCount(0);
expect(manifestText()).toBe(written);
} finally {
writeFileSync(path.join(DIR, "video.manifest.json"), original);
}
});
test("links from a Rumble clip's menu writes the draft (target project): the run's end is heard, and ONE Undo takes it back", async ({ page }) => {
const R = "reports/block-menus-rumble-fixture";
const file = path.join(DIR, "..", "block-menus-rumble-fixture", "video.manifest.json");
const before = readFileSync(file, "utf8");
const siteVideo = () => (JSON.parse(readFileSync(file, "utf8")) as { timeline: { id: string; siteVideo?: string }[] }).timeline.find((e) => e.id === "r01")?.siteVideo ?? null;
await page.goto(urls.workspaceHref(R));
await expect(page.locator("[data-ws-strip] [data-ws-block]")).toHaveCount(3);
await expect(page.locator("dialog[data-jump][data-ready]")).toBeAttached({ timeout: 60_000 });
await expect(page.locator("[data-workspace]")).toHaveAttribute("data-ws-token", /./);
const token = await page.locator("[data-workspace]").getAttribute("data-ws-token");
await block(page, "r01").click({ button: "right" });
await menuOf(page, "r01").locator('[data-ws-menu-verb="links"]').click();
const form = palette(page).locator('[data-cli-form="links"]');
await expect(form.locator('[data-cli-arg="--embed-ids"] input')).toBeChecked();
await expect(form.locator('[data-cli-arg="--clips"] input')).toHaveValue("r01");
await form.locator("[data-cli-run]").click();
await expect(form.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 90_000 });
expect(siteVideo()).toBe("v7emb01");
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
// The palette told the workspace (by the project's id): it has the draft's new token before Undo is pressed.
await expect(page.locator("[data-workspace]")).not.toHaveAttribute("data-ws-token", token ?? "", { timeout: 15_000 });
await page.locator("[data-ws-undo]").click();
await expect(page.locator("[data-ws-undo-said]")).toContainText("Undone");
await expect.poll(() => readFileSync(file, "utf8"), { timeout: 15_000 }).toBe(before);
});
test("at phone width (390): the menu opens inside the window, its ⋯ toggles it, and it works", async ({ page }) => {
await page.setViewportSize({ width: 390, height: 844 });
await open(page);
// The inspector is a sheet at the bottom: folded, the strip is in reach.
await page.locator("[data-ws-sheet-toggle]").click();
await expect(page.locator("[data-ws-inspector]")).toHaveAttribute("data-ws-sheet", "closed");
await block(page, "c02").scrollIntoViewIfNeeded();
const more = page.locator('[data-ws-block-more="c02"]');
await more.click();
const menu = menuOf(page, "c02");
await expect(menu).toBeVisible();
const box = await menu.boundingBox();
expect(box!.x).toBeGreaterThanOrEqual(0);
expect(box!.x + box!.width).toBeLessThanOrEqual(390);
expect(box!.y).toBeGreaterThanOrEqual(0);
expect(box!.y + box!.height).toBeLessThanOrEqual(844);
await shot(page, "block-menu-phone");
// Its ⋯ again closes it (a toggle), and opens it again.
await more.click();
await expect(menu).toHaveCount(0);
await more.click();
await expect(menu).toBeVisible();
await menu.locator('[data-ws-menu-action="play"]').click();
await expect(menu).toHaveCount(0);
await expect(page.locator("[data-workspace]")).toHaveAttribute("data-ws-selected", "c02");
});
test("what is typed in a block's form survives the palette's page context arriving after it", async ({ page }) => {
await open(page);
let release: () => void = () => {};
const gate = new Promise((r) => (release = r));
await page.route(/\/api\/cli\?path=/, async (route) => {
await gate;
await route.continue();
});
await block(page, "c01").click({ button: "right" });
await menuOf(page, "c01").locator('[data-ws-menu-verb="timeline teaser-insert"]').click();
const form = palette(page).locator('[data-cli-form="timeline teaser-insert"]');
await expect(form).toBeVisible();
await form.locator('[data-cli-arg="--line"] textarea').fill("Typed before the context");
// Now the page's context answers.
const answered = page.waitForResponse(/\/api\/cli\?path=/);
release();
await answered;
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
await expect(form.locator('[data-cli-arg="--line"] textarea')).toHaveValue("Typed before the context");
await expect(form.locator('[data-cli-arg="--after"] input')).toHaveValue("c01");
await page.keyboard.press("Escape");
await page.unroute(/\/api\/cli\?path=/);
});
test("a ⌘K run that only reads the draft keeps the workspace's Redo; one that writes it ends the Redo", async ({ page }) => {
const original = manifestText();
await open(page);
const redo = page.locator("[data-ws-redo]");
const said = page.locator("[data-ws-undo-said]");
// An edit, undone: the Redo is there.
await block(page, "p-5").click({ button: "right" });
await menuOf(page, "p-5").locator('[data-ws-menu-action="move-right"]').click();
await expect.poll(order, { timeout: 15_000 }).toBe("c01 c02 k01 p-5 c03 c04");
await expect(said).toContainText("Moved p-5");
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
await expect(said).toContainText("Undone");
await expect(redo).toBeEnabled();
// ⌘K `show` (it reads the project): its end is announced, the workspace reads the draft again -- unchanged --
// and the Redo stays, with its word.
await page.keyboard.press("Control+k");
await expect(palette(page)).toBeVisible();
await page.keyboard.type(">show");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:show", { timeout: 30_000 });
await page.keyboard.press("Enter");
const show = palette(page).locator('[data-cli-form="show"]');
await expect(show.locator('[data-cli-arg="project"] input')).toHaveValue(P);
const reread = page.waitForResponse((r) => r.url().includes("/api/report/timeline?project=") && r.request().method() === "GET", { timeout: 60_000 });
await show.locator("[data-cli-run]").click();
await expect(show.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 90_000 });
await reread;
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
await expect(redo).toBeEnabled();
await expect(said).toContainText("Undone");
// …and it still works.
await redo.click();
await expect.poll(order, { timeout: 15_000 }).toBe("c01 c02 k01 p-5 c03 c04");
await expect(said).toContainText("Redone");
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
await expect(redo).toBeEnabled();
// A run that writes the draft (a card from c01's menu): the Redo is gone.
await block(page, "c01").click({ button: "right" });
await menuOf(page, "c01").locator('[data-ws-menu-verb="timeline teaser-insert"]').click();
const form = palette(page).locator('[data-cli-form="timeline teaser-insert"]');
await form.locator('[data-cli-arg="--line"] textarea').fill("Ends the redo");
await form.locator("[data-cli-run]").click();
await expect(form.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 90_000 });
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
await expect(redo).toBeDisabled({ timeout: 15_000 });
await expect(page.locator("[data-ws-strip] [data-ws-block]")).toHaveCount(7);
await page.locator("[data-ws-undo]").click();
await expect.poll(manifestText, { timeout: 15_000 }).toBe(original);
});