import { test, expect, type Page } from "@playwright/test";
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
// ---------------------------------------------------------------------------
// THE BASIC OUTPUT MODE AND QUICK CLIP (lane output-modes). The Look panel's
// Basic | Deck switch writes render.mode (read back from the manifest); Play of
// a basic cut shows the bare footage -- no deck, no teaser card -- and Play's
// Copy share line puts the clip's platform page and archive moment on the
// clipboard; POST /api/report/quick-clip runs `umtool clip` as one job against
// the stub editor (its window fetched by the stub, the cut rendered basic as a
// take), ends `done` with the output and the share line, and the take panel's
// Copy share line copies it. Fixture: e2e/fixtures/basic-mode-fixture.mjs.
// E2E_BASIC_SHOTS=
: the surfaces' screenshots go there (1366x768).
// ---------------------------------------------------------------------------
const HERE = path.dirname(fileURLToPath(import.meta.url));
const FIXTURE = path.join(HERE, "..", ".e2e-song");
const PROJECT = "reports/basic-fixture";
const DIR = path.join(FIXTURE, "reports", "basic-fixture");
const MANIFEST = path.join(DIR, "video.manifest.json");
const QUICK = path.join(FIXTURE, "quick-clips");
const QUICK_WINDOWS = path.join(FIXTURE, "channels", "basicyt", "data", "quickvid", "clips");
const SHOTS = process.env.E2E_BASIC_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`) });
};
test.beforeEach(() => writeFileSync(MANIFEST, original));
test.afterAll(() => {
writeFileSync(MANIFEST, original);
// What the quick-clip job made: its project and the window the stub wrote. Other specs count projects.
rmSync(QUICK, { recursive: true, force: true });
rmSync(QUICK_WINDOWS, { recursive: true, force: true });
});
test("the Look panel's Basic | Deck switch writes render.mode; Play of a basic cut is the bare footage; the share line is copied", async ({ page, context }) => {
test.setTimeout(300_000);
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await page.setViewportSize({ width: 1366, height: 768 });
// ---- a deck cut: Play composes its deck ------------------------------------
await page.goto(urls.playHref(PROJECT, { bare: true }));
await expect(page.locator("[data-play-cut]")).toBeVisible({ timeout: 60_000 });
await expect(page.locator("[data-play-cut]")).not.toHaveAttribute("data-play-basic", "");
// ---- the switch -------------------------------------------------------------
await page.goto(urls.section(PROJECT, "build"));
await expect(page.locator("[data-look-panel]")).toHaveAttribute("data-look-panel", "", { timeout: 60_000 });
await expect(page.locator('[data-look-mode="deck"]')).toHaveAttribute("aria-pressed", "true");
await page.locator('[data-look-mode="basic"]').click();
await expect(page.locator('[data-look-mode="basic"]')).toHaveAttribute("aria-pressed", "true");
await expect(page.locator("[data-look-mode-note]")).toContainText("the footage alone");
await page.locator("[data-look-save]").click();
await expect(page.locator("[data-look-status]")).toHaveText("Saved — the next render uses it", { timeout: 60_000 });
expect(manifest().render.mode).toBe("basic");
expect(manifest().render.chrome?.layout, "the deck's settings are kept, not drawn").toBe("deck");
await page.locator("[data-look-mode-row]").scrollIntoViewIfNeeded();
await shot(page, "look-basic-switch");
// Read back on a fresh load.
await page.reload();
await expect(page.locator('[data-look-mode="basic"]')).toHaveAttribute("aria-pressed", "true");
// ---- Play: the bare footage --------------------------------------------------
await page.goto(urls.playHref(PROJECT, { bare: true }));
const root = page.locator("[data-play-cut]");
await expect(root).toHaveAttribute("data-play-basic", "", { timeout: 60_000 });
await expect(root).toHaveAttribute("data-play-compose", "no-deck");
await expect(page.getByTestId("play-plain")).toHaveAttribute("data-play-bare", "");
await expect(page.getByTestId("play-deck")).toHaveCount(0);
await expect(page.locator("[data-play-hud]")).toContainText("basic: the footage alone");
// The teaser is not in a basic cut: two clips play.
await expect(page.locator("[data-play-readout]")).toContainText("1/2");
// ---- the share line: copied, and shown -----------------------------------------
await page.locator('[data-share-copy="play"]').click();
await expect(page.locator('[data-share-copy="play"]')).toHaveAttribute("data-share-state", /copied|shown/, { timeout: 60_000 });
const line = page.locator('[data-share-line-box="play"] [data-share-line]');
await expect(line).toContainText("https://www.youtube.com/watch?v=basicvid&t=1s");
await expect(line).toContainText("archive: https://archive.example/?v=basicyt%2Fbasicvid&t=1");
if ((await page.locator('[data-share-copy="play"]').getAttribute("data-share-state")) === "copied") {
expect(await page.evaluate(() => navigator.clipboard.readText())).toBe((await line.textContent())?.trim());
}
await shot(page, "play-basic");
// ---- back to deck: the switch writes it away -------------------------------------
await page.goto(urls.section(PROJECT, "build"));
await page.locator('[data-look-mode="deck"]').click();
await page.locator("[data-look-save]").click();
await expect(page.locator("[data-look-status]")).toHaveText("Saved — the next render uses it", { timeout: 60_000 });
expect(manifest().render.mode, "deck is the default: no key").toBeUndefined();
});
test("quick clip: POST /api/report/quick-clip ends done -- the window asked of the editor, rendered basic, the share line; the take panel copies it", async ({ page, context }) => {
test.setTimeout(300_000);
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await page.setViewportSize({ width: 1366, height: 768 });
expect((await page.request.post("/api/report/quick-clip", { data: { spec: "basicyt/quickvid" } })).status()).toBe(400);
expect((await page.request.post("/api/report/quick-clip", { data: { spec: "basicyt/quickvid@10-14", snap: "letters" } })).status()).toBe(400);
for (const spec of ["../quickvid@10-14", "..@1-2", "-rf/quickvid@10-14", "basicyt/..@10-14", "basicyt/-x@10-14"]) {
expect((await page.request.post("/api/report/quick-clip", { data: { spec } })).status(), spec).toBe(400);
}
const started = await page.request.post("/api/report/quick-clip", { data: { spec: "basicyt/quickvid@10-14", title: "E2E quick clip" } });
expect(started.status(), await started.text()).toBe(202);
const { job } = (await started.json()) as { job: { id: string } };
type Done = { job: { state: string; log: string[] }; done?: { project: string; output: string; share: { platform: string | null; archive: string | null; text: string } }; error?: string };
let last: Done | null = null;
await expect
.poll(
async () => {
last = (await (await page.request.get(`/api/report/quick-clip?job=${encodeURIComponent(job.id)}`)).json()) as Done;
return last.job.state;
},
{ timeout: 240_000, intervals: [1000, 2000, 3000] },
)
.not.toBe("running");
const r = last as unknown as Done;
expect(r.job.state, `${r.error ?? ""}\n${r.job.log.join("\n")}`).toBe("done");
expect(r.done?.project).toBe("quick-clips/e2e-quick-clip");
expect(r.done?.share.platform).toBe("https://www.youtube.com/watch?v=quickvid&t=10s");
expect(r.done?.output).toBe(path.join(QUICK, "e2e-quick-clip", "takes", "clip", "preview.mp4"));
expect(existsSync(String(r.done?.output))).toBe(true);
const made = JSON.parse(readFileSync(path.join(QUICK, "e2e-quick-clip", "video.manifest.json"), "utf8"));
expect(made.render.mode).toBe("basic");
expect(made.timeline.map((e: { id: string; video: string }) => `${e.id}:${e.video}`)).toEqual(["c01:quickvid"]);
// The stub editor was asked for the window (and wrote it): nothing fetched by hand.
const asked = JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")) as { route?: string; requestedBy?: string }[];
expect(asked.some((q) => q.route === "fetch-windows" && q.requestedBy === "umtool:quick-clips/e2e-quick-clip")).toBe(true);
// ---- the take panel: its share line ------------------------------------------------
await page.goto(urls.takesHref("quick-clips/e2e-quick-clip"));
const copy = page.locator('[data-share-copy="take:clip"]');
await expect(copy).toBeVisible();
await copy.click();
await expect(copy).toHaveAttribute("data-share-state", /copied|shown/, { timeout: 60_000 });
await expect(page.locator('[data-share-line-box="take:clip"] [data-share-line]')).toHaveText("https://www.youtube.com/watch?v=quickvid&t=10s");
await shot(page, "take-share-line");
});