import { test, expect as baseExpect } from "@playwright/test";
import { readFileSync } 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 LEDGER PANEL (umtool redesign L6, item 3; components/projects/inspector/LedgerInspector.tsx and the new
// /api/report/ledger): a measure ledger's rows -- date, scope, value, basis, source -- added, changed and removed
// from the Cut tab's Project panel through the ledger CLI's own checks (`umtool ledger
add|set|rm`), each in
// the draft with its undo point, no take touched. inspector-ledger-fixture (WRITES): w01 (weight, pinned to c01),
// d01 (keto, a regime row).
// ---------------------------------------------------------------------------
test.describe.configure({ mode: "serial", timeout: 120_000 });
const P = "reports/inspector-ledger-fixture";
const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "inspector-ledger-fixture");
type Row = Record & { id: string };
const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8");
const ledger = () => (JSON.parse(manifestText()) as { ledger: Row[] }).ledger;
const row = (id: string) => ledger().find((r) => r.id === id);
const takeText = () => readFileSync(path.join(DIR, "takes", "short", "take.json"), "utf8");
test("add a row (date, scope, value, basis, source), refused without its basis; change one; remove one", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 768 });
const take = takeText();
await page.goto(urls.projectHref(P, { tab: "cut" }));
await openPanel(page, "options", "cut-project");
const panel = page.locator('[data-inspector="ledger"]');
await expect(panel.locator("[data-ledger-row]")).toHaveCount(2);
await expect(panel.locator('[data-ledger-row="w01"]')).toContainText("291 lb");
// A new row with no basis: the writer's sentence, nothing written.
await panel.locator("[data-ledger-add]").click();
const ed = panel.locator('[data-ledger-editor="new"]');
await ed.locator('[data-ledger-field="id"]').fill("w02");
await ed.locator('[data-ledger-field="date"]').fill("2026-11-01");
await ed.locator('[data-ledger-field="scope"]').selectOption("weight");
await ed.locator('[data-ledger-field="population"]').selectOption("current");
await ed.locator('[data-ledger-field="value"]').fill("288");
const before = manifestText();
await ed.locator('[data-ledger-save="new"]').click();
await expect(panel.locator('[data-ledger-said="refused"]')).toContainText("scopeBasis");
expect(manifestText()).toBe(before);
await ed.locator('[data-ledger-field="scopeBasis"]').fill("his words: down to 288");
await ed.locator('[data-ledger-field="channel"]').fill("inspchan");
await ed.locator('[data-ledger-field="video"]').fill("playvid");
await ed.locator('[data-ledger-field="cite"]').fill("2");
await ed.locator('[data-ledger-field="src"]').fill("A weigh-in @ 0:02");
await page.screenshot({ path: test.info().outputPath("ledger-add.png") });
await ed.locator('[data-ledger-save="new"]').click();
await expect(panel.locator('[data-ledger-said="ok"]')).toContainText("Added w02: 288 lb");
expect(row("w02")).toMatchObject({ id: "w02", date: "2026-11-01", scope: "weight", population: "current", value: 288, display: "288 lb", scopeBasis: "his words: down to 288", channel: "inspchan", video: "playvid", cite: 2, src: "A weigh-in @ 0:02", scopeConfidence: "read", valueKind: "uttered" });
// Change w01's value: its display is drawn from the new value; a cleared label is removed.
await panel.locator('[data-ledger-edit="w01"]').click();
const e1 = panel.locator('[data-ledger-editor="w01"]');
await expect(e1.locator('[data-ledger-field="value"]')).toHaveValue("291");
await e1.locator('[data-ledger-field="value"]').fill("290");
await e1.locator('[data-ledger-field="label"]').fill("");
await e1.locator('[data-ledger-save="w01"]').click();
await expect(panel.locator('[data-ledger-said="ok"]')).toContainText("Saved w01");
expect(row("w01")).toMatchObject({ value: 290, display: "290 lb", entryId: "c01" });
expect(row("w01")?.label).toBeUndefined();
// A regime row: its value a regime, its event.
await panel.locator('[data-ledger-edit="d01"]').click();
const e2 = panel.locator('[data-ledger-editor="d01"]');
await expect(e2.locator('[data-ledger-field="value"]')).toHaveValue("keto");
await e2.locator('[data-ledger-field="event"]').selectOption("stop");
await e2.locator('[data-ledger-save="d01"]').click();
await expect(panel.locator('[data-ledger-said="ok"]')).toContainText("Saved d01");
expect(row("d01")?.event).toBe("stop");
// A removal without the token the panel read is refused.
const bare = await page.request.post("/api/report/ledger", { data: { project: P, op: "rm", id: "d01" } });
expect(bare.status()).toBe(400);
expect(row("d01")).toBeDefined();
await panel.locator('[data-ledger-edit="d01"]').click();
await panel.locator('[data-ledger-remove="d01"]').click();
await expect(panel.locator('[data-ledger-said="ok"]')).toContainText("Removed d01");
expect(row("d01")).toBeUndefined();
await expect(panel.locator("[data-ledger-row]")).toHaveCount(2);
expect(takeText()).toBe(take);
});
test("each ledger write is one undo step", 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("ledger");
expect(row("d01")).toMatchObject({ event: "stop" });
});