// Release 36 lane D: the assistant in the clip editor, umtool's half — the // labels read with their stale state, the request a Suggest or an Ask sends, // the job the page follows — and an agent reply's proposal in notes.json // (only an agent's, validated, kept on read). import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { writeReactionProject, labelAnswer, PROJECT_NOTE } from "yt-dlp-transcript-common/testing/clips/reactionProject.mjs"; import { suggestionsDocOf, writeSuggestions } from "yt-dlp-transcript-common/lib/clips/suggest.mjs"; import { SuggestRequestError, clearSuggestJob, draftBodyOf, parseSuggestRequest, readSuggestJob, suggestJobView, suggestMarkerOf, suggestRouteOf, suggestionsView, writeSuggestJob, } from "./clipSuggest.mjs"; import { applyOp, emptyDoc, NoteError, parseNotesDoc, validateReplyProposal } from "../annotations/shape.mjs"; test("the labels for the page: none, fresh, then stale once the proposals are made again", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "umtool-suggest-")); try { const { reactions } = writeReactionProject(dir); let v = await suggestionsView(dir); assert.deepEqual(v, { suggestions: null, error: null, state: { stale: false, why: null, passNewer: false, staleIds: [], missing: [] }, hasReactions: true }); await writeSuggestions(dir, suggestionsDocOf({ model: "qwen3:8b", at: "2026-10-19T02:00:00.000Z", reactions, fresh: labelAnswer(["p001", "p003"]).items })); v = await suggestionsView(dir); assert.equal(v.suggestions.items.p003.rank, 1); assert.equal(v.state.stale, false); assert.deepEqual(v.state.missing, ["p002", "p004", "p005"]); await writeFile(path.join(dir, "reactions.json"), JSON.stringify({ ...reactions, proposedAt: "2026-10-20T00:00:00.000Z" })); v = await suggestionsView(dir); assert.equal(v.state.stale, true); assert.equal(v.state.passNewer, true); // A project the clip finder never ran on: no panel. const empty = await mkdtemp(path.join(os.tmpdir(), "umtool-suggest-none-")); assert.equal((await suggestionsView(empty)).hasReactions, false); await rm(empty, { recursive: true, force: true }); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a request: labels (all, or some proposals) or an Ask on one note; the editor's body; the route's button", () => { assert.deepEqual(parseSuggestRequest({ project: "x" }), { mode: "labels" }); assert.deepEqual(parseSuggestRequest({ mode: "labels", proposals: ["p001", "p001", "p002"] }), { mode: "labels", proposals: ["p001", "p002"] }); assert.deepEqual(parseSuggestRequest({ mode: "ask", note: PROJECT_NOTE }), { mode: "ask", note: PROJECT_NOTE }); assert.throws(() => parseSuggestRequest({ mode: "ask" }), SuggestRequestError); assert.throws(() => parseSuggestRequest({ mode: "labels", proposals: ["../x"] }), /proposal ids/); assert.throws(() => parseSuggestRequest({ mode: "apply" }), /"labels" or "ask"/); assert.deepEqual(draftBodyOf("/r/p", { mode: "ask", note: PROJECT_NOTE }), { task: "clips-suggest", project: "/r/p", note: PROJECT_NOTE }); assert.deepEqual(draftBodyOf("/r/p", { mode: "labels", proposals: ["p002"] }), { task: "clips-suggest", project: "/r/p", proposals: ["p002"] }); const generation = { routes: { "clips-suggest": "local" }, models: { api: "claude-sonnet-5-5", local: "qwen3:8b" } }; assert.deepEqual(suggestRouteOf(generation, "/r/p"), { route: "local", model: "qwen3:8b", why: null }); assert.deepEqual(suggestRouteOf({ ...generation, routes: { "clips-suggest": "code" } }, "/r/p"), { route: "code", model: null, why: "routed to Claude Code — in a session: /gen label the clips of /r/p", }); assert.equal(suggestRouteOf(null, "/r/p"), null); }); test("the followed job: remembered, shown with its progress and result, forgotten only when it is that job", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "umtool-suggest-job-")); try { assert.equal(await readSuggestJob(dir), null); await writeSuggestJob(dir, { jobId: "job-1", mode: "ask", note: PROJECT_NOTE, at: "t" }); assert.deepEqual(await readSuggestJob(dir), { jobId: "job-1", mode: "ask", note: PROJECT_NOTE, at: "t" }); await clearSuggestJob(dir, "job-2"); assert.ok(await readSuggestJob(dir)); const tail = ["clips-suggest ask …", "replied to n_synth0001, proposing a trim to 94.8–99 s", '@@report-draft {"task":"clips-suggest","kind":"ask","status":"done","summary":"replied to n_synth0001 on r002"}']; assert.equal(suggestMarkerOf(tail).kind, "ask"); assert.deepEqual(suggestJobView(await readSuggestJob(dir), { id: "job-1", status: "done", queue: null, progress: null, tail }), { id: "job-1", mode: "ask", note: PROJECT_NOTE, status: "done", ended: true, queue: null, progress: null, last: "replied to n_synth0001, proposing a trim to 94.8–99 s", summary: "replied to n_synth0001 on r002", }); await clearSuggestJob(dir, "job-1"); assert.equal(await readSuggestJob(dir), null); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a reply's proposal: an agent's only, validated, kept when notes.json is read again", () => { const doc = emptyDoc({ kind: "video-project", project: "p" }); const note = applyOp(doc, { op: "add", text: "start it on the laugh", anchor: { kind: "entry", entry: "r002" } }, "operator"); const trim = { kind: "trim", clip: "r002", from: 94.8, to: 99, basis: { cutStart: 88, cutEnd: 99 } }; assert.throws(() => applyOp(doc, { op: "reply", id: note.id, text: "I did it", proposal: trim }, "operator"), NoteError); assert.throws(() => applyOp(doc, { op: "reply", id: note.id, text: "x", proposal: { kind: "trim", clip: "r002", from: 99, to: 99.2 } }, "agent"), /half a second/); assert.throws(() => applyOp(doc, { op: "reply", id: note.id, text: "x", proposal: { kind: "merge", clip: "r002" } }, "agent"), /the next clip's id/); assert.throws(() => applyOp(doc, { op: "reply", id: note.id, text: "x", proposal: { kind: "delete", clip: "r002" } }, "agent"), /trim, merge, cut/); applyOp(doc, { op: "reply", id: note.id, text: "Start on the laugh.", proposal: trim }, "agent"); applyOp(doc, { op: "reply", id: note.id, text: "plain reply" }, "agent"); const again = parseNotesDoc(JSON.parse(JSON.stringify(doc))); assert.ok("doc" in again); assert.deepEqual(again.doc.notes[0].replies.map((r) => r.proposal ?? null), [trim, null]); // A hand-broken proposal makes the file unreadable rather than being dropped. const broken = JSON.parse(JSON.stringify(doc)); broken.notes[0].replies[0].proposal.to = "soon"; assert.match(parseNotesDoc(broken).error, /replies: a trim proposal needs/); assert.deepEqual(validateReplyProposal({ kind: "cut", clip: "r003", extra: 1 }), { proposal: { kind: "cut", clip: "r003" } }); });