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";
// ---------------------------------------------------------------------------
// ⌘K COMMANDS (umtool redesign, lane palette L8a): the verb registry's forms in
// the palette. ⌘K on a project's page, "look" -> the `umtool look` form, the
// project filled in from the page -> mode=basic -> Copy as CLI is the exact
// line -> Run: the job's log, done, and the manifest changed. On a clip bench
// the clip is filled in too. POST /api/cli refuses a ui:"none" verb (absent
// from the palette), a raw argv, a value its schema refuses and a request no
// page of this app made; a verb that renders goes through the durable queue.
// Fixture: e2e/fixtures/basic-mode-fixture.mjs (basic-fixture, a deck cut);
// the spec puts its manifest back. E2E_PALETTE_SHOTS=
: screenshots there (1366x768).
// ---------------------------------------------------------------------------
const HERE = path.dirname(fileURLToPath(import.meta.url));
const PROJECT = "reports/basic-fixture";
const MANIFEST = path.join(HERE, "..", ".e2e-song", "reports", "basic-fixture", "video.manifest.json");
const SHOTS = process.env.E2E_PALETTE_SHOTS ?? "";
const original = readFileSync(MANIFEST, "utf8");
const manifest = () => JSON.parse(readFileSync(MANIFEST, "utf8"));
const shot = async (page: Page, name: string) => {
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, `${name}.png`) });
};
const palette = (page: Page) => page.locator("dialog[data-jump]");
const open = async (page: Page) => {
await expect(page.locator("dialog[data-jump][data-ready]")).toBeAttached({ timeout: 60_000 });
await page.keyboard.press("Control+k");
await expect(palette(page)).toBeVisible();
};
test.beforeEach(() => writeFileSync(MANIFEST, original));
test.afterAll(() => writeFileSync(MANIFEST, original));
test("⌘K → look → its form, the project from the page → Copy as CLI → Run: the manifest changed", async ({ page, context }) => {
test.setTimeout(180_000);
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await page.setViewportSize({ width: 1366, height: 768 });
await page.goto(urls.section(PROJECT, "build"));
await open(page);
// Typed words find the verb among the places; > is commands only.
await page.keyboard.type("look");
const row = palette(page).locator("[data-jump-item='cmd:look']");
await expect(row).toBeVisible({ timeout: 30_000 });
await expect(row).toContainText("umtool look");
// A verb with no form is not offered.
await page.locator("[data-jump-input]").fill(">help");
await expect(palette(page).locator("[data-jump-item='cmd:help']")).toHaveCount(0);
await page.locator("[data-jump-input]").fill(">teaser");
await expect(palette(page).locator("[data-jump-item='cmd:timeline teaser']")).toBeVisible();
await page.locator("[data-jump-input]").fill(">look");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:look");
await page.keyboard.press("Enter");
const form = palette(page).locator("[data-cli-form='look']");
await expect(form).toBeVisible();
await expect(form.locator("[data-cli-arg='project'] input")).toHaveValue(PROJECT);
// Required settings missing: no line, no Run.
await expect(form.locator("[data-cli-invalid]")).toContainText("settings is required");
await expect(form.locator("[data-cli-run]")).toBeDisabled();
await form.locator("[data-cli-arg='settings'] textarea").fill("mode=basic");
const line = `node umtool/bin/umtool.mjs look ${PROJECT} mode=basic`;
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 box = await palette(page).boundingBox();
expect(box!.y + box!.height, "the form fits 1366×768").toBeLessThanOrEqual(768);
await shot(page, "palette-form");
expect(manifest().render.mode ?? "deck").not.toBe("basic");
await form.locator("[data-cli-run]").click();
await expect(form.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 90_000 });
await expect(form.locator("[data-cli-log]")).toContainText(`look ${PROJECT} mode=basic`);
expect(manifest().render.mode).toBe("basic");
await shot(page, "palette-run-done");
// Back to the list.
await form.locator("[data-cli-back]").click();
await expect(palette(page).locator("[data-jump-input]")).toBeFocused();
});
test("on a clip bench the clip is filled in; a refused value says why", async ({ page }) => {
test.setTimeout(120_000);
await page.setViewportSize({ width: 1366, height: 768 });
await page.goto(urls.clipBenchHref(PROJECT, "c01"));
await open(page);
await page.keyboard.type(">window");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:window", { timeout: 30_000 });
await page.keyboard.press("Enter");
const form = palette(page).locator("[data-cli-form='window']");
await expect(form.locator("[data-cli-arg='project'] input")).toHaveValue(PROJECT);
await expect(form.locator("[data-cli-arg='clip'] input")).toHaveValue("c01");
await form.locator("[data-cli-arg='--title'] input").fill("A palette title");
await expect(form.locator("[data-cli-line]")).toHaveText(`node umtool/bin/umtool.mjs window ${PROJECT} c01 --title 'A palette title'`);
await form.locator("[data-cli-arg='--start'] input").fill("soon");
await expect(form.locator("[data-cli-invalid]")).toContainText("--start: a number");
await shot(page, "palette-clip-form");
});
test("POST /api/cli runs only registry form verbs, from this app's pages, with values its schema allows", async ({ page }) => {
test.setTimeout(120_000);
await page.goto(urls.homeHref());
const origin = new URL(page.url()).origin;
const post = (data: unknown, headers: Record = { origin }) => page.request.post("/api/cli", { data, headers });
const said = async (r: Awaited>) => ((await r.json()) as { error?: string }).error ?? "";
// The list the palette is built from: forms only.
const list = (await (await page.request.get("/api/cli")).json()) as { verbs: Array<{ verb: string }> };
const verbs = list.verbs.map((v) => v.verb);
expect(verbs).toContain("look");
expect(verbs).toContain("timeline insert");
expect(verbs).not.toContain("help");
expect(verbs).not.toContain("queue add");
expect(verbs).not.toContain("takes delete");
// A ui:"none" verb.
let r = await post({ verb: "queue add", values: {} });
expect(r.status()).toBe(400);
expect(await said(r)).toContain("has no form");
// A raw argv, in any spelling.
for (const body of [{ argv: ["look", PROJECT, "mode=basic"] }, { verb: "look", argv: ["--json"] }, { verb: "look", values: {}, cmd: "rm -rf /" }]) {
r = await post(body);
expect(r.status(), JSON.stringify(body)).toBe(400);
}
// A value its schema refuses, an arg it does not have, a project there is not.
r = await post({ verb: "look", values: { project: PROJECT, settings: "--unset render.rail" } });
expect(r.status()).toBe(400);
r = await post({ verb: "look", values: { project: PROJECT, settings: "mode=basic", "--json": true } });
expect(r.status()).toBe(400);
expect(await said(r)).toContain('takes no "--json"');
r = await post({ verb: "show", values: { project: "reports/no-such-project" } });
expect(r.status()).toBe(400);
expect(await said(r)).toContain("no project matches");
// Not a page of this app: another site, and no page at all.
r = await post({ verb: "show", values: { project: PROJECT } }, { origin: "https://evil.example" });
expect(r.status()).toBe(403);
r = await post({ verb: "show", values: { project: PROJECT } }, {});
expect(r.status()).toBe(403);
expect(manifest().render.mode ?? "deck", "nothing refused wrote").not.toBe("basic");
// A read, as one job: its log is the command's output.
r = await post({ verb: "show", values: { project: PROJECT } });
expect(r.status()).toBe(202);
const { job, line } = (await r.json()) as { job: { id: string }; line: string };
expect(line).toBe(`node umtool/bin/umtool.mjs show ${PROJECT}`);
await expect
.poll(async () => ((await (await page.request.get(`/api/cli?job=${job.id}`)).json()) as { job: { state: string } }).job.state, { timeout: 60_000 })
.toBe("done");
const shown = (await (await page.request.get(`/api/cli?job=${job.id}`)).json()) as { job: { log: string[] } };
expect(shown.job.log.join("\n")).toContain("The Basic Fixture");
// A verb that renders (or holds the heavy slot) is a step of the durable queue; the same one asked
// again at once (a double Enter) is that step.
r = await post({ verb: "prep", values: { project: PROJECT, "--status": true } });
expect(r.status()).toBe(202);
const { queued } = (await r.json()) as { queued: { qid: string } };
expect(queued.qid).toMatch(/^q\d+$/);
const again = await post({ verb: "prep", values: { project: PROJECT, "--status": true } });
expect(again.status()).toBe(200);
expect(((await again.json()) as { queued: { qid: string }; deduped: boolean })).toMatchObject({ queued: { qid: queued.qid }, deduped: true });
await expect
.poll(async () => ((await (await page.request.get(`/api/cli?qid=${queued.qid}`)).json()) as { step: { state: string } }).step.state, { timeout: 90_000 })
.toBe("done");
});
test("POST /api/cli: files stay in the project, @file is a terminal's, no-undo verbs want confirm, and a rebound host is refused", async ({ page }) => {
test.setTimeout(120_000);
await page.goto(urls.homeHref());
const origin = new URL(page.url()).origin;
const post = (data: unknown, headers: Record = { origin }) => page.request.post("/api/cli", { data, headers });
const said = async (r: Awaited>) => ((await r.json()) as { error?: string }).error ?? "";
// An output is a bare name; an input a file of the project, by its realpath; @file is refused.
let r = await post({ verb: "trailer", values: { project: PROJECT, "--take": "t1", "--out": "../../escape.mp4" } });
expect(r.status()).toBe(400);
expect(await said(r)).toContain("bare name");
for (const patch of ["../../../../etc/passwd", "/etc/passwd"]) {
r = await post({ verb: "take", values: { project: PROJECT, "--id": "t1", "--patch": patch, "--draft": true } });
expect(r.status(), patch).toBe(400);
expect(await said(r)).toMatch(/not a file of the project|no such file in the project/);
expect(await said(r)).not.toContain("root:");
}
r = await post({ verb: "candidates add", values: { project: PROJECT, proposal: "@/etc/passwd" } });
expect(r.status()).toBe(400);
expect(await said(r)).toContain("terminal only");
r = await post({ verb: "posts set-item", values: { project: PROJECT, item: "c01", settings: "parent.0=@/etc/passwd" } });
expect(r.status()).toBe(400);
// No undo: 409 until confirmed.
r = await post({ verb: "prune", values: { project: PROJECT, "--yes": true } });
expect(r.status()).toBe(409);
expect(((await r.json()) as { confirm?: string }).confirm).toContain("delete");
r = await post({ verb: "prune", values: { project: PROJECT, "--yes": true }, confirm: true });
expect(r.status()).toBe(202);
const { job } = (await r.json()) as { job: { id: string } };
await expect.poll(async () => ((await (await page.request.get(`/api/cli?job=${job.id}`)).json()) as { job: { state: string } }).job.state, { timeout: 60_000 }).not.toBe("running");
// DNS rebinding: a name that is not this machine, as the forwarded host or the origin.
const port = new URL(origin).port;
r = await post({ verb: "show", values: { project: PROJECT } }, { origin, "x-forwarded-host": `rebind.example:${port}` });
expect(r.status()).toBe(403);
r = await post({ verb: "show", values: { project: PROJECT } }, { origin: `http://rebind.example:${port}`, "x-forwarded-host": `rebind.example:${port}` });
expect(r.status()).toBe(403);
expect(await said(r)).toContain("UMTOOL_ALLOWED_HOSTS");
});
test("a no-undo verb asks a second press; a run outlives Escape (Runs); a client-side move leaves no stale project", async ({ page }) => {
test.setTimeout(180_000);
await page.setViewportSize({ width: 1366, height: 768 });
await page.goto(urls.section(PROJECT, "build"));
await open(page);
await page.keyboard.type(">prune");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:prune", { timeout: 30_000 });
await page.keyboard.press("Enter");
const form = palette(page).locator("[data-cli-form='prune']");
await expect(form.locator("[data-cli-arg='project'] input")).toHaveValue(PROJECT);
await form.locator("[data-cli-arg='--yes'] input").check();
await form.locator("[data-cli-run]").click();
const confirm = form.locator("[data-cli-confirm]");
await expect(confirm).toContainText("Yes, delete those work files");
await expect(form.locator("[data-cli-state]")).toHaveCount(0);
await shot(page, "palette-confirm");
await confirm.click();
await expect(form.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", /done|failed/, { timeout: 90_000 });
// A read started, then Escape: it is under Runs, and opens on its log.
await form.locator("[data-cli-back]").click();
await page.locator("[data-jump-input]").fill(">show");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:show");
await page.keyboard.press("Enter");
const show = palette(page).locator("[data-cli-form='show']");
await show.locator("[data-cli-run]").click();
await page.keyboard.press("Escape");
await expect(palette(page)).toBeHidden();
await page.keyboard.press("Control+k");
const runs = palette(page).locator("[data-jump-item^='run:job-']");
await expect(runs).toHaveCount(2);
await expect(runs.first()).toContainText("umtool show");
await runs.first().click();
await expect(show.locator("[data-cli-state]")).toHaveAttribute("data-cli-state", "done", { timeout: 60_000 });
await expect(show.locator("[data-cli-log]")).toContainText("The Basic Fixture");
// A jump to the Inbox (a client-side move: the palette stays mounted), then a form at once: no project.
await show.locator("[data-cli-back]").click();
await page.locator("[data-jump-input]").fill("inbox");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "inbox");
await page.keyboard.press("Enter");
await expect(page).toHaveURL(/\/$/);
await page.keyboard.press("Control+k");
await page.keyboard.type(">look");
await expect(palette(page).locator("[role=option][aria-selected=true]")).toHaveAttribute("data-jump-item", "cmd:look");
await page.keyboard.press("Enter");
await expect(palette(page).locator("[data-cli-form='look'] [data-cli-arg='project'] input")).toHaveValue("");
});