import { test, expect as baseExpect } from "@playwright/test";
import { readFileSync, readdirSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { openPanel } from "./panels";
import * as urls from "./urls";
// A write is a request under the app's lock, and this machine runs other suites beside this one: room for it.
const expect = baseExpect.configure({ timeout: 15_000 });
// ---------------------------------------------------------------------------
// THE PROVENANCE PANEL (umtool redesign L6, item 1; components/projects/inspector/ProvenanceInspector.tsx): the
// Cut tab's Project panel sets provenance.siteOrigin, channelSlug and channel in the DRAFT -- the owed provenance
// writer (`umtool provenance
set` is its twin) -- one undo point, no take touched; a bad origin is refused in
// the writer's words. inspector-provenance-fixture (WRITES): no channelSlug, an old origin.
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial", timeout: 120_000 });
const P = "reports/inspector-provenance-fixture";
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "inspector-provenance-fixture");
const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8");
const manifest = () => JSON.parse(manifestText()) as { provenance: Record };
const takeText = () => readFileSync(path.join(DIR, "takes", "short", "take.json"), "utf8");
const undoPoints = (op: string) => readdirSync(path.join(DIR, "revisions")).filter((f) => f.endsWith(`auto-before-${op}.manifest.json`)).length;
test("set the site origin, channel slug and name from the Cut tab: the draft written, no take", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 768 });
const take = takeText();
expect(manifest().provenance.channelSlug).toBeUndefined();
await page.goto(urls.projectHref(P, { tab: "cut" }));
await openPanel(page, "options", "cut-project");
const panel = page.locator('[data-inspector="provenance"]');
await expect(panel.locator('[data-provenance-field="siteOrigin"]')).toHaveValue("https://old.example");
await expect(panel).toContainText("empty: check blocks until it is set");
await page.screenshot({ path: test.info().outputPath("provenance-panel.png") });
// A bad origin: refused, the draft untouched.
const before = manifestText();
await panel.locator('[data-provenance-field="siteOrigin"]').fill("https://jeralyzer.pages.dev/some/path");
await panel.locator("[data-provenance-save]").click();
await expect(panel.locator('[data-provenance-said="refused"]')).toContainText("siteOrigin is the archive's origin, with no path");
expect(manifestText()).toBe(before);
await panel.locator('[data-provenance-field="siteOrigin"]').fill("https://jeralyzer.pages.dev/");
await panel.locator('[data-provenance-field="channelSlug"]').fill("inspchan");
await panel.locator('[data-provenance-field="channel"]').fill("Inspector Channel");
await panel.locator("[data-provenance-save]").click();
await expect(panel.locator('[data-provenance-said="ok"]')).toContainText("Saved to the draft: siteOrigin, channelSlug, channel");
expect(manifest().provenance).toMatchObject({ siteOrigin: "https://jeralyzer.pages.dev", channelSlug: "inspchan", channel: "Inspector Channel" });
expect(undoPoints("provenance")).toBe(1);
expect(takeText()).toBe(take);
// The link policy per channel, under it: the cut's clips are now inspchan's.
await expect(panel.locator('[data-provenance-link-policy] [data-link-policy-direct="inspchan"]')).toBeVisible();
await page.screenshot({ path: test.info().outputPath("provenance-saved.png") });
// Clearing a key removes it.
await panel.locator('[data-provenance-field="channel"]').fill("");
await panel.locator("[data-provenance-save]").click();
await expect(panel.locator('[data-provenance-said="ok"]')).toContainText("channel");
expect(manifest().provenance.channel).toBeUndefined();
expect(takeText()).toBe(take);
});
test("one Undo takes the last provenance write back", async ({ request }) => {
const r = await request.post("/api/report/timeline", { data: { project: P, op: "undo" } });
expect(r.ok(), await r.text()).toBe(true);
expect((await r.json()).op).toBe("provenance");
expect(manifest().provenance.channel).toBe("Inspector Channel");
});