// The video assistant in umtool (release 38 lane E): the Ask's request, the // job the page follows, and APPLY — each proposed op re-checked and written // through the manifest's own writers (updateRender, the clip editor's trim and // room, updateChrome), token-guarded; Dismiss writes nothing but the marks. // Synthetic (common/testing/clips/assistProject.mjs). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { assistDeckManifest, assistManifest } from "yt-dlp-transcript-common/testing/clips/assistProject.mjs"; import { writeReactionProject } from "yt-dlp-transcript-common/testing/clips/reactionProject.mjs"; import { checkAssistOps } from "yt-dlp-transcript-common/lib/report/videoAssistOps.mjs"; import { proposalsDocOf, readAssistProposals, writeAssistProposals } from "yt-dlp-transcript-common/lib/report/videoAssistProposals.mjs"; import { MANIFEST_NAME, StaleToken, manifestToken, updateRender } from "./manifest.mjs"; import { AssistRequestError, applyAssistOps, assistJobView, assistRouteOf, dismissAssistOps, draftBodyOf, parseAssistRequest, proposalsView, } from "./videoAssist.mjs"; const AT = "2026-10-20T00:00:00.000Z"; async function project({ deck = false, ops }) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-assist-")); writeReactionProject(dir); const manifest = deck ? assistDeckManifest() : assistManifest(); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); const checked = checkAssistOps(manifest, ops, { request: "the request" }); await writeAssistProposals(dir, proposalsDocOf({ at: AT, model: "qwen3:8b", request: "the request", checked })); return { id: "reports/assist", dir, kind: "report" }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); test("an Ask's request: words or a note, a scope; the editor's body", () => { assert.deepEqual(parseAssistRequest({ request: " quicker joins " }), { request: "quicker joins", scope: "all" }); assert.deepEqual(parseAssistRequest({ note: "n_abcd1234", scope: "look" }), { note: "n_abcd1234", scope: "look" }); assert.throws(() => parseAssistRequest({ request: " " }), AssistRequestError); assert.throws(() => parseAssistRequest({ request: "x", scope: "everything" }), /scope is look, clips, fx, all/); assert.throws(() => parseAssistRequest({ note: "n_abcd1234", request: "x" }), /not both/); assert.deepEqual(draftBodyOf("/r/p", { request: "quicker joins", scope: "all" }), { task: "video-assist", project: "/r/p", request: "quicker joins" }); assert.deepEqual(draftBodyOf("/r/p", { note: "n_abcd1234", scope: "look" }), { task: "video-assist", project: "/r/p", note: "n_abcd1234", scope: "look" }); assert.deepEqual(assistRouteOf({ routes: { "video-assist": "local" }, models: { local: "qwen3:8b" } }, "/r/p"), { route: "local", model: "qwen3:8b", why: null }); assert.match(assistRouteOf({ routes: { "video-assist": "code" }, models: {} }, "/r/p").why, /routed to Claude Code — in a session: \/gen on \/r\/p/); const v = assistJobView({ jobId: "j1", request: "quicker" }, { id: "j1", status: "done", tail: ["one ask", '@@report-draft {"task":"video-assist","status":"done","summary":"1 edit proposed"}'] }); assert.deepEqual([v.ended, v.summary, v.last, v.request], [true, "1 edit proposed", "one ask", "quicker"]); }); test("Apply all: each op re-checked and written by its own writer, in order, the token carried; the ops marked applied", async () => { const p = await project({ ops: [ { op: "look", id: "", key: "transition", value: 0.15 }, { op: "slide", id: "", key: "blur", value: 0.3 }, { op: "clip_edges", id: "r002", key: "start", value: 90 }, { op: "clip_room", id: "r003", key: "off", value: true }, ], }); try { const token = await manifestToken(p.dir); const r = await applyAssistOps(p, { token, now: AT }); assert.deepEqual(r.applied, ["a1", "a2", "a3", "a4"]); assert.deepEqual(r.failed, []); assert.equal(r.token, await manifestToken(p.dir)); const m = await read(p.dir); assert.equal(m.render.transition, 0.15); assert.deepEqual(m.render.slide, { blur: 0.3 }); const r002 = m.timeline.find((e) => e.id === "r002"); assert.deepEqual([r002.cutStart, r002.cutEnd], [90, 99]); assert.equal(r002.assist.by, "operator", "the clip editor's writer: an edit of an assistant's clip is the operator's"); assert.equal(m.timeline.find((e) => e.id === "r003").room, false); const { doc } = await readAssistProposals(p.dir); assert.deepEqual(doc.ops.map((o) => [o.id, o.status, o.at]), [["a1", "applied", AT], ["a2", "applied", AT], ["a3", "applied", AT], ["a4", "applied", AT]]); await assert.rejects(applyAssistOps(p, {}), /nothing is waiting to apply/); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("Apply one; a stale token is a 409 before anything is written; newer proposals are refused by `at`", async () => { const p = await project({ ops: [{ op: "look", id: "", key: "qr", value: false }, { op: "look", id: "", key: "loudness", value: -14 }] }); try { const before = await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"); await assert.rejects(applyAssistOps(p, { token: "123" }), StaleToken); assert.equal(await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"), before); await assert.rejects(applyAssistOps(p, { at: "2026-01-01T00:00:00.000Z" }), (e) => e instanceof AssistRequestError && e.status === 409); const r = await applyAssistOps(p, { ids: ["a2"], token: await manifestToken(p.dir) }); assert.deepEqual(r.applied, ["a2"]); const m = await read(p.dir); assert.deepEqual([m.render.qr, m.render.loudness], [undefined, { target: -14 }]); assert.deepEqual((await readAssistProposals(p.dir)).doc.ops.map((o) => o.status), ["proposed", "applied"]); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("an op the manifest no longer allows fails at Apply with the reason; the view says so before", async () => { const p = await project({ ops: [{ op: "clip_edges", id: "r001", key: "end", value: 48 }, { op: "look", id: "", key: "transition", value: 0.15 }] }); try { // Somebody narrowed r001's window meanwhile, and set the join themselves. const m = await read(p.dir); m.timeline[0].end = 47.5; await writeFile(path.join(p.dir, MANIFEST_NAME), JSON.stringify(m, null, 2) + "\n"); await updateRender(p.dir, { transition: 0.15 }); const v = await proposalsView(p.dir); assert.match(v.proposals.ops[0].now, /outside the clip's source window \(38\.5–47\.5 s\)/); assert.match(v.proposals.ops[1].now, /join is already that/); const r = await applyAssistOps(p, {}); assert.deepEqual(r.applied, []); assert.deepEqual(r.failed.map((f) => f.id), ["a1", "a2"]); assert.deepEqual((await readAssistProposals(p.dir)).doc.ops.map((o) => o.status), ["failed", "failed"]); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("a composition's param through updateChrome; Dismiss changes nothing but the marks", async () => { const p = await project({ deck: true, ops: [{ op: "fx", id: "deck", key: "motion.in", value: 0.6 }, { op: "fx", id: "fx.glitch", key: "seconds", value: 0.8 }] }); try { const r = await applyAssistOps(p, { ids: ["a1"] }); assert.deepEqual(r.applied, ["a1"]); const m = await read(p.dir); assert.deepEqual(m.render.chrome.deck, { qr: { show: false }, motion: { in: 0.6 } }); const before = await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"); const d = await dismissAssistOps(p.dir, { at: AT }); assert.deepEqual(d.dismissed, ["a2"]); assert.equal(await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"), before, "Dismiss writes no manifest"); assert.deepEqual((await readAssistProposals(p.dir)).doc.ops.map((o) => o.status), ["applied", "dismissed"]); } finally { await rm(p.dir, { recursive: true, force: true }); } });