import { test, expect, type APIRequestContext } from "@playwright/test"; import { warm, WARM_TIMEOUT } from "./warm"; // The inbox engine (plans/umtool-redesign.md, P2a): GET /api/browse/decisions // keeps one row per decision and adds the inbox's fields; the generation era's // decisions come from disk; a parked piece's decisions are hidden, never // dropped; POST /api/pieces/lifecycle round-trips. Over the fixture's article in // the making, zeta (e2e/fixtures/iterations-fixture.mjs): i1 and i2 done with // reports, i3 a video revision, i4 failed -- and no current.json. // // WRITES the fixture's REPORTS_ROOT/.umtool/lifecycle.json (zeta parked, then // active again) and a note on articles/zeta, deleted before the test ends. type Row = { kind: string; project: string; projectKind?: string; target: string; href: string; severity: "blocking" | "open" | "info"; piece?: string; category?: string; group?: string; action?: { label: string; href: string }; lifecycle?: "active" | "parked" | "done"; hidden?: boolean; }; type Group = { id: string; label: string; decisions: number; hidden: number; cards: { piece: string | null; kind: string; count: number }[] }; type Inbox = { items: Row[]; projects: number; counts: Record; groups: Group[]; hidden: number }; const ZETA = "a:articles/zeta"; const inbox = async (request: APIRequestContext): Promise => { const r = await request.get("/api/browse/decisions"); expect(r.ok()).toBe(true); return r.json(); }; const lifecycle = (request: APIRequestContext, piece: unknown, state: unknown) => request.post("/api/pieces/lifecycle", { data: { piece, state } }); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, ["/api/browse/decisions", "/api/pieces", "/api/notes?article=articles/zeta"]); }); test.afterAll(async ({ request }) => { await lifecycle(request, ZETA, "active"); }); test("one row per decision, each with the inbox's fields; the counts add up", async ({ request }) => { const { items, counts, groups, hidden } = await inbox(request); expect(items.length).toBeGreaterThan(0); expect(counts.blocking + counts.open + counts.info).toBe(items.length); expect(groups.reduce((n, g) => n + g.decisions + g.hidden, 0)).toBe(items.length); expect(hidden).toBe(items.filter((d) => d.hidden).length); expect(groups.map((g) => g.id)).toEqual(["your-call", "review", "blocking", "notes", "open", "ready-to-finish", "quiet"]); for (const d of items) { // A research case's new transcripts (L4, case-new-cues) are its case's: `c:`. expect(d.piece, `${d.kind} ${d.project}`).toMatch(d.kind === "case-new-cues" ? /^c:/ : /^[ap]:/); expect(d.group).toBeTruthy(); expect(d.category).toBeTruthy(); expect(d.lifecycle).toBeTruthy(); expect(typeof d.hidden).toBe("boolean"); expect(d.action?.label).toBeTruthy(); expect(d.action?.href).toBe(d.href); } // The rows openDecisions() always made are still there, project ids intact. // (The fixture's project `find` is no longer shadowed since P3 moved the // tools to /tools: no project has a shadowed-name row.) expect(items.some((d) => d.kind === "manifest-invalid" && d.project === "reports/no-origin-fixture")).toBe(true); expect(items.some((d) => d.kind === "shadowed-name")).toBe(false); }); test("the generation era's decisions come from disk", async ({ request }) => { const { items } = await inbox(request); const zeta = items.filter((d) => d.piece === ZETA); const of = (kind: string) => zeta.filter((d) => d.kind === kind).map((d) => d.target).sort(); // No current.json: every done iteration that could be current is ready. expect(of("iteration-ready")).toEqual(["i1", "i2", "i3"]); expect(of("iteration-failed")).toEqual(["i4"]); const failed = zeta.find((d) => d.kind === "iteration-failed")!; expect(failed.group).toBe("your-call"); expect(failed.category).toBe("spend"); expect(failed.project).toBe("sites/articles/zeta"); expect(failed.projectKind).toBe("article"); expect(failed.href).toBe("/articles/zeta?tab=iterations"); const ready = zeta.find((d) => d.kind === "iteration-ready" && d.target === "i1")!; expect(ready.group).toBe("review"); expect(ready.action?.label).toBe("Make i1 current"); // The project zeta's iterations made belongs to zeta's piece (linked by its // iteration, not by slug), so its manifest decisions do too. expect(items.some((d) => d.project === "gen-projects/zeta/i1" && d.piece === ZETA)).toBe(true); // A published article with its video looks done: a suggestion, not a state. const done = items.find((d) => d.kind === "looks-done" && d.piece === "a:priv/polemic-alpha"); expect(done?.group).toBe("ready-to-finish"); expect(done?.lifecycle).toBe("active"); }); test("a parked piece's decisions are hidden, never dropped; active again shows them", async ({ request }) => { // A note on zeta, so the MCP's contract is checked on a hidden row too. const add = await request.post("/api/notes?article=articles/zeta", { data: { op: { op: "add", text: "Inbox engine.", anchor: { kind: "whole" } } } }); expect(add.status()).toBe(200); const noteId = (await add.json()).note.id as string; try { const before = await inbox(request); const zetaRows = before.items.filter((d) => d.piece === ZETA); expect(zetaRows.length).toBeGreaterThan(5); expect(zetaRows.every((d) => !d.hidden && d.lifecycle === "active")).toBe(true); const park = await lifecycle(request, ZETA, "parked"); expect(park.status()).toBe(200); expect(await park.json()).toMatchObject({ piece: ZETA, state: "parked", by: "operator" }); const after = await inbox(request); expect(after.items.length).toBe(before.items.length); const parked = after.items.filter((d) => d.piece === ZETA); expect(parked.length).toBe(zetaRows.length); expect(parked.every((d) => d.hidden && d.lifecycle === "parked")).toBe(true); expect(after.hidden).toBe(before.hidden + zetaRows.length); // Hidden rows are counted per group and left out of the cards. expect(after.groups.flatMap((g) => g.cards).some((c) => c.piece === ZETA)).toBe(false); // The note row keeps the MCP's contract: project is the notes target, href ends in note=. const note = after.items.find((d) => d.kind === "open-note" && d.piece === ZETA)!; expect(note.project).toBe("sites/articles/zeta"); expect(note.href.endsWith(`note=${noteId}`)).toBe(true); expect(note.action?.href.endsWith(`note=${noteId}`)).toBe(true); const pieces = await (await request.get("/api/pieces")).json(); const z = pieces.pieces.find((p: { id: string }) => p.id === ZETA); expect(z).toMatchObject({ kind: "article", site: "articles", report: "zeta", status: "in the making", stage: "video", lifecycle: "parked", lifecycleBy: "operator" }); expect(z.projects.map((p: { id: string; via: string }) => `${p.id}:${p.via}`).sort()).toEqual(["gen-projects/zeta/i1:iteration", "gen-projects/zeta/i3:iteration"]); expect(z.decisions.hidden).toBe(zetaRows.length); expect((await lifecycle(request, ZETA, "active")).status()).toBe(200); const again = await inbox(request); expect(again.items.filter((d) => d.piece === ZETA).every((d) => !d.hidden && d.lifecycle === "active")).toBe(true); } finally { await request.post("/api/notes?article=articles/zeta", { data: { op: { op: "delete", id: noteId } } }); } }); test("POST /api/pieces/lifecycle refuses junk", async ({ request }) => { for (const [piece, state, status] of [ ["articles/zeta", "parked", 400], // no namespace: ambiguous, refused ["a:articles/zeta/extra", "parked", 400], ["p:../etc", "parked", 400], [ZETA, "shipped", 400], [ZETA, null, 400], [null, "parked", 400], ["a:articles/no-such-article", "parked", 404], ["p:no/such/project", "done", 404], ["c:some-case", "parked", 404], // A project zeta's iterations made is part of zeta's piece. ["p:gen-projects/zeta/i1", "parked", 404], ] as const) { const r = await lifecycle(request, piece, state); expect(r.status(), `${piece} ${state}`).toBe(status); expect((await r.json()).error).toBeTruthy(); } const r = await request.post("/api/pieces/lifecycle", { data: "not json", headers: { "content-type": "text/plain" } }); expect(r.status()).toBe(400); });