import { test, expect } from "@playwright/test";
import { spawnSync } from "node:child_process";
import { readFileSync, statSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
import { openPanel } from "./panels";
// ---------------------------------------------------------------------------
// A TAKE'S OWN ITEMS (umtool/docs/cli.md, "Edit a take, not the project"):
// `umtool take
--id --import-item :- ` clones an item of
// another project into a draft take with its files hard-linked in, and
// `umtool posts … --in-take ` edits it in the take -- the take's project
// manifest and the source's byte for byte the same throughout. Then Play on
// the take plays the imported item, its narration and picture there.
//
// Fixture (fixtures/take-items-fixture.mjs), written by this file alone:
// take-items-fixture (p-1, p-3; the draft take t1) and take-items-src-fixture
// (p-2, narrated, two takes, a picture). The tests run in order.
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial" });
const HERE = path.dirname(fileURLToPath(import.meta.url));
const FIXTURE = path.join(HERE, "..", ".e2e-song");
const DIR = path.join(FIXTURE, "reports", "take-items-fixture");
const SRC = path.join(FIXTURE, "reports", "take-items-src-fixture");
const P = "reports/take-items-fixture";
const S = "reports/take-items-src-fixture";
const TAKE = "t1";
const env = { ...process.env, REPORTS_DIR: FIXTURE, CHANNELS_DIR: path.join(FIXTURE, "channels"), UMTOOL_CACHE_DIR: path.join(FIXTURE, "cache"), UMTOOL_MEDIA_DIR: "" };
const umtool = (args: string[]) => spawnSync("node", ["bin/umtool.mjs", ...args], { cwd: path.join(HERE, ".."), encoding: "utf8", env });
const ok = (args: string[]) => {
const r = umtool(args);
expect(r.status, `umtool ${args.join(" ")}\n${r.stdout}\n${r.stderr}`).toBe(0);
return r.stdout;
};
const text = (dir: string) => readFileSync(path.join(dir, "video.manifest.json"), "utf8");
type Entry = { id: string; hold?: number; narration?: { pick?: number; takes: { file: string }[] } };
const takeTimeline = () => (JSON.parse(readFileSync(path.join(DIR, "takes", TAKE, "take.json"), "utf8")).patch?.timeline ?? []) as Entry[];
let projectBefore = "";
let sourceBefore = "";
test.beforeAll(() => {
projectBefore = text(DIR);
sourceBefore = text(SRC);
});
test("import: the source's item cloned into the take after p-1, its narration and picture hard-linked in; again: unchanged; lint --take passes", () => {
const plan = JSON.parse(ok(["take", P, "--id", TAKE, "--import-item", `${S}:p-2`, "--after", "p-1", "--dry-run", "--json"]));
expect(plan.dryRun).toBe(true);
expect(plan.items[0].at).toEqual({ index: 1, after: "p-1", before: "p-3" });
expect(() => statSync(path.join(DIR, "narration/posts/2/take-1.orpheus-thread.wav"))).toThrow();
const r = JSON.parse(ok(["take", P, "--id", TAKE, "--import-item", `${S}:p-2`, "--after", "p-1", "--json"]));
expect(r.items.map((x: { id: string; action: string }) => [x.id, x.action])).toEqual([["p-2", "added"]]);
expect(r.items[0].narration).toEqual({ takes: 2, pick: 1, file: "narration/posts/2/take-1.orpheus-thread.wav" });
expect(r.revision).toMatch(/^takes\/t1\/revisions\/.*-auto-before-import-item\.take\.json$/);
expect(takeTimeline().map((e) => e.id)).toEqual(["p-1", "p-2", "p-3"]);
for (const rel of ["narration/posts/2/take-1.orpheus-thread.wav", "narration/posts/2/take-1.parts.json", "narration/posts/2/take-2.orpheus-thread.wav", "posts-media/takeitems-x/2/2_1.png"]) {
expect(statSync(path.join(DIR, rel)).ino, rel).toBe(statSync(path.join(SRC, rel)).ino);
}
const again = JSON.parse(ok(["take", P, "--id", TAKE, "--import-item", `${S}:p-2`, "--after", "p-1", "--json"]));
expect([again.unchanged, again.items[0].action, again.revision]).toEqual([true, "unchanged", null]);
const lint = umtool(["lint", P, "--take", TAKE, "--json"]);
const doc = JSON.parse(lint.stdout);
expect(doc.errors, lint.stdout).toEqual([]);
expect(lint.status).toBe(0);
expect(text(DIR)).toBe(projectBefore);
expect(text(SRC)).toBe(sourceBefore);
});
test("--in-take: set-item and narrate --pick land in the take's patch only; narrate --take is refused naming --in-take; undo takes the pick back", () => {
const set = JSON.parse(ok(["posts", "set-item", P, "p-2", "hold=4", "--in-take", TAKE, "--json"]));
expect(set.take).toMatchObject({ id: TAKE, edits: 1, undo: `umtool timeline ${P} --take ${TAKE} undo` });
ok(["posts", "narrate", P, "p-2", "--pick", "2", "--in-take", TAKE]);
const e = takeTimeline().find((x) => x.id === "p-2");
expect(e?.hold).toBe(4);
expect(e?.narration?.pick).toBe(1);
const wrong = umtool(["posts", "narrate", P, "p-2", "--take", TAKE]);
expect(wrong.status).toBe(2);
expect(wrong.stderr).toContain(`to edit take ${TAKE}, say --in-take ${TAKE}`);
ok(["timeline", P, "--take", TAKE, "undo"]);
expect(takeTimeline().find((x) => x.id === "p-2")?.narration?.pick).toBe(0);
expect(text(DIR)).toBe(projectBefore);
expect(text(SRC)).toBe(sourceBefore);
// The project's own cut has no p-2: the same edit without --in-take is refused.
expect(umtool(["posts", "set-item", P, "p-2", "hold=4"]).status).not.toBe(0);
});
test("Play on the take plays the imported item: its row between p-1 and p-3, narrated, its picture beside the card -- the project's cut has no p-2", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
await page.goto(urls.playHref(P, { take: TAKE, clip: "p-2" }));
await expect(page.locator(`[data-play-take="${TAKE}"]`)).toBeVisible();
const rows = page.locator("[data-play-row]");
await expect(rows).toHaveCount(3);
expect(await rows.evaluateAll((els) => els.map((x) => x.getAttribute("data-play-row")))).toEqual(["p-1", "p-2", "p-3"]);
await expect(page.locator("[data-play-cut]")).toHaveAttribute("data-play-item", "p-2");
await expect(page.locator('[data-post-play="p-2"]')).toBeVisible();
// Narrated (its imported take plays) and its imported picture beside the card.
await expect(page.locator('[data-post-play="p-2"]')).toHaveAttribute("data-post-narrated", "1");
await expect(page.locator('[data-post-play="p-2"]')).toHaveAttribute("data-post-step", "image");
await page.screenshot({ path: test.info().outputPath("take-items-play.png") });
await page.goto(urls.playHref(P, { bare: true }));
await expect(page.locator("[data-play-row]")).toHaveCount(2);
});
test("the Cut tab's Add items from another project imports INTO THE DRAFT (L3): Preview writes nothing, Import adds p-2 to the project's cut, its files linked; the source untouched", async ({ page }) => {
await page.goto(urls.projectHref(P, { tab: "cut" }));
await openPanel(page, "options", "cut");
const box = page.locator('[data-take-import="draft"]');
await box.locator('[data-action="import-open"]').click();
await box.locator("[data-import-source]").fill(S);
await box.locator("[data-import-items]").fill("p-2");
await box.locator('[data-action="import-preview"]').click();
await expect(page.locator('[data-import-result="plan"] [data-import-row="p-2"]')).toContainText("p-2 added");
// REMOVED_LIST_SHOTS= (the lane's shots): the draft import at 1366×768.
if (process.env.REMOVED_LIST_SHOTS) {
await page.setViewportSize({ width: 1366, height: 768 });
await box.scrollIntoViewIfNeeded();
await page.screenshot({ path: path.join(process.env.REMOVED_LIST_SHOTS, "draft-import.png") });
}
expect(text(DIR), "a preview writes nothing").toBe(projectBefore);
await expect(box.locator('[data-action="import-go"]')).toHaveText("Import into the draft");
await box.locator('[data-action="import-go"]').click();
await expect(page.locator('[data-import-result="done"]')).toHaveAttribute("data-import-exit", "0");
await expect.poll(() => (JSON.parse(text(DIR)).timeline as Entry[]).map((e) => e.id)).toEqual(["p-1", "p-2", "p-3"]);
expect(statSync(path.join(DIR, "narration/posts/2/take-1.orpheus-thread.wav")).ino).toBe(statSync(path.join(SRC, "narration/posts/2/take-1.orpheus-thread.wav")).ino);
expect(text(SRC)).toBe(sourceBefore);
// The take cards no longer carry it: a take is opened in the draft first.
await page.goto(urls.takesHref(P));
await openPanel(page, "options", "take-card");
await expect(page.locator(`[data-take-tools="${TAKE}"]`)).toBeVisible();
await expect(page.locator("[data-take-import]")).toHaveCount(0);
});