// Release 38 lane E: the video assistant's ops — the look's shared rules, // each op's bounds and refusals, its diff line, and the request table. 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 { LOOK_KEYS, SLIDE_BOUNDS, applyLookPatch, basicFadeOf, lookErrors, renderModeOf, validateLook, validateRenderMode, validateTransitionStyle } from "./look.mjs"; import { ASSIST_OPS_SCHEMA, assistView, checkAssistOps, clipWindow, normalizeAssistOp, secondsNamedIn } from "./videoAssistOps.mjs"; import { COMPOSITION_PARAMS, chromeWithParam, compositionsOn, paramValue, paramValueError } from "./fxParams.mjs"; import { markProposals, proposalsDocOf, proposalsErrors, readAssistProposals, writeAssistProposals } from "./videoAssistProposals.mjs"; import { ASSIST_REQUESTS, assistDeckManifest, assistManifest } from "../../testing/clips/assistProject.mjs"; test("the look's rules: one patch function — keys refused, null removes, qr on/off, bounds, then the build's validators", () => { const m = assistManifest(); assert.deepEqual(applyLookPatch(m, { chrome: {} }).errors, ["render.chrome is the deck's: set it from the deck's settings (updateChrome), not here"]); assert.match(applyLookPatch(m, { fps: 25 }).errors[0]!, /render\.fps is not a look setting/); assert.equal(applyLookPatch(m, { loudness: null }).render.loudness, undefined); assert.equal(applyLookPatch(m, { qr: false }).render.qr, false); assert.equal(applyLookPatch({ render: { qr: false } }, { qr: true }).render.qr, undefined); assert.deepEqual(applyLookPatch(m, { transition: 0.157 }).render.transition, 0.16, "2 dp"); assert.match(applyLookPatch(m, { transition: 3.5 }).errors[0]!, /from 0 \(a hard cut\) to 3/); assert.match(applyLookPatch(m, { headerHeight: 241 }).errors[0]!, /from 0 to 240/); // What it does not check itself, the build's validators do — the same sentences. const bad = applyLookPatch(m, { slide: { blur: 9 } }); assert.deepEqual(bad.errors, validateTransitionStyle({ render: { slide: { blur: 9 } } })); assert.deepEqual(applyLookPatch(m, { loudness: { target: -40 } }).errors, validateLook({ render: { loudness: { target: -40 } } })); assert.deepEqual(lookErrors(m), []); assert.equal(LOOK_KEYS.length, 22); }); test("render.mode and render.basic: deck is the default (no key), basic the footage alone; the fades checked", () => { const m = { render: { transition: 0.4 }, timeline: [] }; assert.deepEqual(applyLookPatch(m, { mode: "basic" }).render.mode, "basic"); assert.equal(applyLookPatch({ render: { mode: "basic" } }, { mode: "deck" }).render.mode, undefined, "deck is written as no key"); assert.equal(applyLookPatch({ preset: "basic", render: {} }, { mode: "deck" }).render.mode, "deck", "over a preset, deck is written so it beats the preset's"); assert.match(applyLookPatch(m, { mode: "plain" }).errors[0]!, /render\.mode must be "deck"/); assert.deepEqual(applyLookPatch(m, { basic: { fade: { audio: 0.05 } } }).errors, []); assert.match(applyLookPatch(m, { basic: { fade: { video: 9 } } }).errors.join(), /render\.basic\.fade\.video must be seconds from 0 to 3/); assert.match(applyLookPatch(m, { basic: { blur: 1 } }).errors.join(), /render\.basic\.blur is not a key/); assert.deepEqual(basicFadeOf({}), { audio: 0.03, video: 0.15 }); assert.deepEqual(basicFadeOf({ basic: { fade: { video: 0 } } }), { audio: 0.03, video: 0 }); assert.equal(renderModeOf({ mode: "basic" }), "basic"); assert.equal(renderModeOf({}), "deck"); assert.deepEqual(validateRenderMode({ render: { mode: "basic", basic: { fade: { audio: 0.1, video: 0.2 } } } }), []); }); test("the request table: every request → the model's ops → the diff lines kept and the reasons refused", () => { assert.ok(ASSIST_REQUESTS.length >= 20); for (const row of ASSIST_REQUESTS) { const r = checkAssistOps(row.deck ? assistDeckManifest() : assistManifest(), row.answer, { request: row.request, scope: row.scope ?? "all" }); assert.deepEqual(r.diff, row.diff, row.request); assert.equal(r.refused.length, row.refused.length, `${row.request}: ${JSON.stringify(r.refused)}`); r.refused.forEach((x, i) => assert.match(x.why, row.refused[i]!, row.request)); } }); test("each kept op carries its writer: the look's, the clip editor's, the composition's", () => { const m = assistManifest(); const r = checkAssistOps(m, [ { op: "look", id: "", key: "transition", value: 0.15 }, { op: "slide", id: "", key: "blur", value: 0.3 }, { op: "room", id: "", key: "maxS", value: 2 }, { op: "clip_edges", id: "r002", key: "start", value: 90 }, { op: "clip_room", id: "r003", key: "off", value: true }, ]); assert.deepEqual( r.ops.map((o) => o.apply), [ { writer: "render", patch: { transition: 0.15 } }, { writer: "render", patch: { slide: { blur: 0.3 } } }, { writer: "render", patch: { room: { tail: true, head: true, maxS: 2 } } }, { writer: "clip", body: { op: "trim", clip: "r002", cutStart: 90, cutEnd: 99 } }, { writer: "clip", body: { op: "room", clip: "r003", room: false } }, ], ); assert.deepEqual(r.ops.map((o) => o.op), [ { op: "look", key: "transition", value: 0.15 }, { op: "slide", blur: 0.3 }, { op: "room", maxS: 2 }, { op: "clip_edges", id: "r002", start: 90, end: 99 }, { op: "clip_room", id: "r003", off: true }, ]); // The manifest as the ops left it (the input is never touched). assert.equal(r.manifest.render && (r.manifest.render as { transition: number }).transition, 0.15); assert.equal(m.render.transition, 0.22); const deck = checkAssistOps(assistDeckManifest(), [{ op: "fx", id: "fx.glitch", key: "seconds", value: 0.8 }], { scope: "fx" }); assert.deepEqual(deck.ops[0]!.apply, { writer: "chrome", id: "fx.glitch", param: "seconds", value: 0.8 }); assert.deepEqual(deck.diff, ["the glitch's length 0.5 s → 0.8 s"]); }); test("ops read one after another: a later op sees the earlier ones; a second identical op is 'already that'", () => { const r = checkAssistOps(assistManifest(), [ { op: "look", id: "", key: "transitionStyle", value: "fade" }, { op: "slide", id: "", key: "grain", value: 0 }, { op: "look", id: "", key: "transitionStyle", value: "fade" }, ]); assert.deepEqual(r.diff, ["join style blur slide → crossfade", "grain 2 → 0"]); assert.match(r.ops[1]!.warning!, /the join is a crossfade: the slide's knobs show once the join style is blur slide/); assert.deepEqual(r.refused.map((x) => x.why), ["join style is already that"]); }); test("room: a number for the tail is the tail on up to that; numbers alone turn the tail on; hard cuts warn", () => { const plain = { render: { transition: 0.4 }, timeline: [] }; const a = checkAssistOps(plain, [{ op: "room", id: "", key: "tail", value: 2 }]); assert.deepEqual(a.diff, ["room tail off → on, room tail up to 2.5 s → 2 s"]); assert.deepEqual(a.ops[0]!.op, { op: "room", tail: true, maxS: 2 }); const b = checkAssistOps(plain, [{ op: "room", id: "", key: "minGap", value: 0.5 }]); assert.deepEqual(b.diff, ["room tail off → on, room's silence gap 0.35 s → 0.5 s"]); const c = checkAssistOps({ render: { transition: 0 }, timeline: [] }, [{ op: "room", id: "", key: "head", value: true }]); assert.match(c.ops[0]!.warning!, /hard cuts/); assert.match(checkAssistOps(plain, [{ op: "room", id: "", key: "maxS", value: 11 }]).refused[0]!.why, /outside 0–10 s/); assert.match(checkAssistOps({ render: { montage: { phrase: "x" } }, timeline: [] }, [{ op: "room", id: "", key: "tail", value: true }]).refused[0]!.why, /a montage has no room/); }); test("slide: each knob within SLIDE_BOUNDS, refused past them with the bound", () => { for (const [k, [lo, hi]] of Object.entries(SLIDE_BOUNDS)) { const m = assistManifest(); assert.equal(checkAssistOps(m, [{ op: "slide", id: "", key: k, value: hi }]).ops.length, 1, `${k} at ${hi}`); assert.match(checkAssistOps(m, [{ op: "slide", id: "", key: k, value: hi + 1 }]).refused[0]!.why, new RegExp(`outside ${lo}–${hi}`)); } assert.match(checkAssistOps(assistManifest(), [{ op: "slide", id: "", key: "speed", value: 1 }]).refused[0]!.why, /sets travel, blur \(the smear\) or grain/); }); test("clip edges: inside the source window, at most 3 s a step unless the request names more, never under half a second", () => { const m = assistManifest(); assert.deepEqual(clipWindow(m.timeline[0]!), { cut: { start: 40, end: 47 }, window: { start: 38.5, end: 48.5 }, hasCut: true }); // A clip without a cut moves inside its edges, widened by the fetch pad. assert.deepEqual(clipWindow({ start: 10, end: 20 }, { fetchPad: 3 }).window, { start: 7, end: 23 }); assert.deepEqual(secondsNamedIn("cut the first 6 seconds, then 2.5 s more"), 6); assert.equal(secondsNamedIn("start at 516"), 0); assert.match(checkAssistOps(m, [{ op: "clip_edges", id: "r001", key: "end", value: 40.2 }]).refused[0]!.why, /a cut is at least 0\.5 s/); assert.match(checkAssistOps(m, [{ op: "clip_edges", id: "zz", key: "end", value: 40 }]).refused[0]!.why, /no clip "zz" in the cut/); assert.match(checkAssistOps(m, [{ op: "clip_edges", id: "", key: "end", value: 40 }]).refused[0]!.why, /names its clip/); const card = { render: {}, timeline: [{ type: "card", id: "c1" }] }; assert.match(checkAssistOps(card, [{ op: "clip_room", id: "c1", key: "off", value: true }]).refused[0]!.why, /"c1" is a card entry, not a clip/); }); test("fx: only a declared param of a composition the cut draws, within its declared bounds", () => { const deck = assistDeckManifest(); assert.deepEqual(compositionsOn(deck.render), ["deck", "fx.glitch"]); assert.deepEqual(compositionsOn(assistManifest().render), []); assert.equal(paramValue(deck.render, "deck", "motion.in"), 0.45); assert.equal(paramValueError(COMPOSITION_PARAMS.deck!.find((p) => p.key === "background")!, "neon"), "the deck's background is one of panel, flush"); assert.match(checkAssistOps(deck, [{ op: "fx", id: "fx.zoom", key: "width", value: 0.5 }]).refused[0]!.why, /does not draw fx\.zoom \(turn it on in the Media tab first\)/); assert.match(checkAssistOps(deck, [{ op: "fx", id: "page", key: "html", value: "" }]).refused[0]!.why, /"page" is not a composition/); const set = chromeWithParam(deck.render, "deck", "motion.in", 0.6) as { deck: { motion: { in: number }; qr: unknown } }; assert.deepEqual(set.deck, { qr: { show: false }, motion: { in: 0.6 } }); assert.deepEqual((deck.render.chrome as { deck: unknown }).deck, { qr: { show: false } }, "a copy"); // Booleans and enums as a model may write them. assert.deepEqual(checkAssistOps(deck, [{ op: "fx", id: "fx.glitch", key: "stamp", value: "off" }]).diff, ["the glitch's date stamp on → off"]); }); test("scope: an op outside what was asked is refused; the view shows only the scope", () => { const m = assistDeckManifest(); assert.match(checkAssistOps(m, [{ op: "look", id: "", key: "qr", value: false }], { scope: "clips" }).refused[0]!.why, /a look op is outside what was asked about \(clips\)/); const look = assistView(m, { scope: "look" }).text; assert.match(look, /THE LOOK NOW/); assert.doesNotMatch(look, /CLIPS|COMPOSITIONS/); const fx = assistView(m, { scope: "fx" }).text; assert.match(fx, /- deck: motion\.in = 0\.45 \[0–2\]/); assert.match(fx, /- fx\.glitch: on = "year" \[year\|every\|clips\]; seconds = 0\.5 \[0\.2–1\.5\]/); assert.doesNotMatch(fx, /THE LOOK NOW/); }); test("the view: the clips the request names first; past the budget a clip is its id alone", () => { const m = assistManifest(); for (let i = 5; i <= 60; i++) m.timeline.push({ type: "clip", id: `r${String(i).padStart(3, "0")}`, channel: "c", video: "v", start: i * 100, end: i * 100 + 20, cutStart: i * 100 + 2, cutEnd: i * 100 + 18, quote: "a quote of a good many words that runs on and on for a while" }); const v = assistView(m, { maxChars: 3000, request: "trim r057 a bit", scope: "all" }); assert.ok(v.text.length <= 3200, String(v.text.length)); assert.ok(v.clips.shown.includes("r057")); assert.ok(v.clips.short.length > 10); assert.match(v.text, /- r060 \(not shown in full\)/); assert.match(v.text, /- r057: cut 5702–5718 s \(16 s; may move within 5700–5720\)/); }); test("the model's one shape normalised; the schema keeps four keys an op (a strict tool requires every key)", () => { assert.deepEqual(normalizeAssistOp({ op: "slide", id: "", key: "blur", value: 0.3 }), { op: "slide", blur: 0.3 }); assert.deepEqual(normalizeAssistOp({ op: "clip_edges", id: "r1", key: "end", value: 9 }), { op: "clip_edges", end: 9, id: "r1" }); assert.deepEqual(normalizeAssistOp({ op: "fx", id: "deck", key: "motion.in", value: 1 }), { op: "fx", id: "deck", param: "motion.in", value: 1 }); assert.deepEqual(normalizeAssistOp({ op: "slide", blur: 0.3 }), { op: "slide", blur: 0.3 }); const item = (ASSIST_OPS_SCHEMA as { properties: { ops: { items: { properties: object; required: string[] } } } }).properties.ops.items; assert.deepEqual(Object.keys(item.properties), ["op", "id", "key", "value"]); assert.deepEqual(item.required, ["op", "id", "key", "value"]); }); test("the proposals file: one ask's doc, read back, marked; a file that is not proposals is said so", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "assist-proposals-")); try { assert.deepEqual(await readAssistProposals(dir), { doc: null, error: null }); const checked = checkAssistOps(assistManifest(), [ { op: "look", id: "", key: "transition", value: 0.15 }, { op: "look", id: "", key: "transition", value: 9 }, ]); const doc = proposalsDocOf({ at: "2026-10-20T00:00:00.000Z", model: "qwen3:8b", request: "quicker", checked, reply: " Shorter\njoins. " }); assert.deepEqual(doc.ops, [{ id: "a1", op: { op: "look", key: "transition", value: 0.15 }, diff: "join 0.22 s → 0.15 s", status: "proposed" }]); assert.equal(doc.refused.length, 1); assert.equal(doc.reply, "Shorter joins."); await writeAssistProposals(dir, doc); assert.deepEqual((await readAssistProposals(dir)).doc, doc); const marked = markProposals(doc, { a1: { status: "applied" } }, "2026-10-20T00:01:00.000Z"); assert.deepEqual(marked.ops[0]!.status, "applied"); assert.equal(marked.ops[0]!.at, "2026-10-20T00:01:00.000Z"); await writeFile(path.join(dir, "assist-proposals.json"), '{"version": 2}'); assert.match((await readAssistProposals(dir)).error!, /assist-proposals\.json: version is not 1/); assert.ok(proposalsErrors({ version: 1, request: "x", ops: [{ id: "a1" }], refused: [] }).some((e) => /ops\[0\]/.test(e))); await assert.rejects(writeAssistProposals(dir, { version: 1 } as never), /not proposals/); assert.match(await readFile(path.join(dir, "assist-proposals.json"), "utf8"), /"version": 2/, "a refused write leaves the file"); } finally { await rm(dir, { recursive: true, force: true }); } });