import { test, expect } from "@playwright/test"; import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; // --------------------------------------------------------------------------- // The assistant in the clip editor (release 36 lane D) — optional, every // part: the labels from clip-suggestions.json on each clip and unused // proposal (the why under the selected clip), "Sort by the assistant's rank" // reorders both lists, a later proposal pass marks the labels stale (shown, // not hidden), Suggest starts the editor's `clips-suggest` job (the editor // stub labels every proposal through common's writer) and shows its end; Ask // the assistant on a comment sends the note to the editor; an agent's reply // with a trim proposal shows Apply, and Apply performs the trim through the // clip editor's writer — only then. Fixture: e2e/fixtures/clips-suggest-fixture.mjs. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PROJECT = "reports/clips-suggest-fixture"; const DIR = path.join(FIXTURE, "reports", "clips-suggest-fixture"); const TOKEN = "umtool-e2e-token"; const json = (name: string) => { try { return JSON.parse(readFileSync(path.join(DIR, name), "utf8")); } catch { return null; } }; type Clip = { id: string; cutStart?: number; cutEnd?: number; assist?: { by: string } }; const clip = (id: string): Clip | null => (json("video.manifest.json")?.timeline ?? []).find((e: Clip & { type: string }) => e.type === "clip" && e.id === id) ?? null; const stubRequests = (): { route: string; task?: string; project?: string; note?: string }[] => { try { return JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")); } catch { return []; } }; test("the assistant's labels, the sort, stale, Suggest, Ask, and Apply on the operator's click", async ({ page, request }) => { test.setTimeout(120_000); await page.setViewportSize({ width: 1366, height: 900 }); await page.goto(`/projects/${PROJECT}/clips`); const editor = page.locator("[data-clip-editor]"); await expect(editor).toHaveAttribute("data-clip-editor", "r001"); // The labels: on each clip, on each unused proposal; the selected clip's why. const panel = page.locator("[data-suggest-panel]"); await expect(panel).toHaveAttribute("data-suggest-panel", "5"); await expect(panel).toHaveAttribute("data-suggest-state", "fresh"); await expect(page.locator('[data-editor-row="r001"] [data-suggest-label="p001"]')).toHaveText("the complete sentence"); await expect(page.locator('[data-proposal="p005"] [data-suggest-label="p005"]')).toHaveText("a quiet chuckle"); await expect(page.locator('[data-proposal="p005"] [data-suggest-label="p005"]')).toHaveAttribute("title", "Why p005 lands."); await expect(page.locator('[data-suggest-why="p001"]')).toContainText("the complete sentence · the assistant ranks it 3 · Why p001 lands."); // Sort by the assistant's rank: both lists reorder; off again, the cut's order. const rows = page.locator("[data-editor-list] [data-editor-row]"); const proposals = page.locator("[data-editor-proposals] [data-proposal]"); await expect(rows).toHaveText([/^r001/, /^r002/, /^r003/]); await page.locator("[data-suggest-sort]").check(); await expect(rows.first()).toHaveAttribute("data-editor-row", "r003"); expect(await rows.evaluateAll((els) => els.map((e) => e.getAttribute("data-editor-row")))).toEqual(["r003", "r001", "r002"]); expect(await proposals.evaluateAll((els) => els.map((e) => e.getAttribute("data-proposal")))).toEqual(["p005", "p004"]); await page.locator("[data-suggest-sort]").uncheck(); expect(await rows.evaluateAll((els) => els.map((e) => e.getAttribute("data-editor-row")))).toEqual(["r001", "r002", "r003"]); // A later proposal pass: the labels are marked stale, never hidden. const reactions = json("reactions.json"); writeFileSync(path.join(DIR, "reactions.json"), JSON.stringify({ ...reactions, proposedAt: "2026-02-01T00:00:00.000Z" })); await page.reload(); await expect(panel).toHaveAttribute("data-suggest-state", "stale"); await expect(page.locator("[data-suggest-stale-line]")).toHaveText("Stale: the proposals were made again after these labels."); await expect(page.locator('[data-editor-row="r001"] [data-suggest-label="p001"]')).toHaveAttribute("data-suggest-stale", ""); // Suggest: the editor's clips-suggest job on this project; its end shows, the labels are fresh. await page.locator("[data-suggest-button]").click(); await expect(page.locator("[data-suggest-job]")).toHaveAttribute("data-suggest-job", "done", { timeout: 20_000 }); await expect(page.locator("[data-suggest-job]")).toHaveText("5 proposals labelled on qwen3:8b"); await expect(panel).toHaveAttribute("data-suggest-state", "fresh"); await expect(page.locator('[data-editor-row="r001"] [data-suggest-label="p001"]')).toHaveText("stub label p001"); expect(stubRequests().filter((r) => r.route === "report-draft" && r.task === "clips-suggest").map((r) => [r.project, r.note ?? null])).toEqual([[DIR, null]]); expect(json("clip-suggestions.json")?.items?.p005?.rank).toBe(1); // A comment on r002, and Ask the assistant: the note goes to the editor. await page.locator('[data-editor-row="r002"]').click(); await expect(editor).toHaveAttribute("data-clip-editor", "r002"); await page.locator("[data-clip-note-input]").fill("start it half a second later"); await page.locator("[data-clip-note-add]").click(); const thread = page.locator('[data-clip-notes="r002"] [data-note-id]'); await expect(thread).toHaveCount(1); const noteId = (await thread.getAttribute("data-note-id"))!; await thread.locator('[data-note-action="ask"]').click(); await expect(page.locator("[data-ask-job]")).toHaveAttribute("data-ask-job", "done", { timeout: 20_000 }); expect(stubRequests().filter((r) => r.task === "clips-suggest" && r.note).map((r) => [r.project, r.note])).toEqual([[DIR, noteId]]); // The agent's reply carries a trim (the reply the job posts, posted here: the stub cannot reach umtool). const read = await (await request.get(`/api/notes?project=${encodeURIComponent(PROJECT)}`)).json(); const replied = await request.post(`/api/notes?project=${encodeURIComponent(PROJECT)}`, { headers: { authorization: `Bearer ${TOKEN}` }, data: { token: read.token, op: { op: "reply", id: noteId, text: "Start it on the breath at 4.5 s.", proposal: { kind: "trim", clip: "r002", from: 4.5, to: 6, basis: { cutStart: 4, cutEnd: 6 } } } }, }); expect(replied.ok()).toBe(true); // The page's own write may not carry one: a proposal is an agent's. const forged = await request.post(`/api/notes?project=${encodeURIComponent(PROJECT)}`, { data: { op: { op: "reply", id: noteId, text: "x", proposal: { kind: "cut", clip: "r002" } } }, }); expect(forged.status()).toBe(400); await page.reload(); await expect(editor).toHaveAttribute("data-clip-editor", "r002"); const proposal = page.locator('[data-clip-notes="r002"] [data-reply-proposal="trim"]'); await expect(proposal).toContainText("trim r002 to 0:04.5–0:06.0 (1.5 s)"); // Nothing moved until the click. expect([clip("r002")?.cutStart, clip("r002")?.cutEnd]).toEqual([4, 6]); await proposal.locator('[data-apply-proposal="trim"]').click(); await expect.poll(() => clip("r002")?.cutStart).toBe(4.5); expect(clip("r002")?.cutEnd).toBe(6); expect(clip("r002")?.assist?.by).toBe("operator"); await expect(page.locator("[data-editor-status]")).toHaveText("Applied: trim r002 to 0:04.5–0:06.0 (1.5 s)"); // The operator's "Applied" reply answers it: no second Apply. await expect(proposal.locator("[data-reply-proposal-answered]")).toHaveCount(1); await expect(proposal.locator("[data-apply-proposal]")).toHaveCount(0); const notes = json("notes.json")?.notes ?? []; expect(notes[0].replies.map((r: { author: string; text: string }) => [r.author, r.text])).toEqual([ ["agent", "Start it on the breath at 4.5 s."], ["operator", "Applied: trim r002 to 0:04.5–0:06.0 (1.5 s)"], ]); });