// Claim-group proposals, umtool's half (lane L4): Accept writes ONE group // through setGroup (an undo point, the build's checks) and marks it with the // group's id; a proposal the cut no longer takes is marked failed with the // build's words; Reject marks and writes nothing; the view says which still fit. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, readdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { checkGroupProposals, groupProposalsDocOf, readGroupProposals, writeGroupProposals } from "yt-dlp-transcript-common/lib/report/groupProposals.mjs"; import { MANIFEST_NAME, StaleToken, manifestToken, setGroup } from "./manifest.mjs"; import { acceptGroupProposal, groupProposalsView, groupsDraftBodyOf, groupsRouteOf, parseGroupsRequest, rejectGroupProposals } from "./groupProposals.mjs"; const clip = (id, i) => ({ type: "clip", id, video: "v1", start: 10 * i, end: 10 * i + 8 }); const cut = () => ({ slug: "s", render: { width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [{ after: "c02", label: "Two" }] } } }, timeline: ["c01", "c02", "c03", "c04"].map(clip), }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-proposals-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(cut(), null, 2) + "\n"); const m = cut(); const checked = checkGroupProposals(m, { groups: [{ section: 0, claim: "One", ids: ["c01", "c02"] }, { section: 1, claim: "Two", ids: ["c03", "c04"] }] }); await writeGroupProposals(dir, groupProposalsDocOf({ at: "2026-11-02T00:00:00.000Z", model: "qwen3:8b", checked })); return dir; } const manifest = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); test("Accept: one group through setGroup, an undo point, the proposal marked with its group; Reject writes nothing", async () => { const dir = await project(); const r = await acceptGroupProposal(dir, { id: "p2", token: await manifestToken(dir), at: "2026-11-02T00:00:00.000Z" }); assert.equal(r.accepted, "p2"); assert.equal(r.group, "g1"); assert.deepEqual((await manifest(dir)).groups, [{ id: "g1", section: 1, claim: "Two", ids: ["c03", "c04"] }]); assert.ok((await readdir(path.join(dir, "revisions"))).some((n) => n.includes("auto-before-groups-set"))); const { doc } = await readGroupProposals(dir); assert.deepEqual(doc.groups.map((g) => [g.id, g.status, g.group ?? null]), [["p1", "proposed", null], ["p2", "accepted", "g1"]]); await assert.rejects(acceptGroupProposal(dir, { id: "p2" }), /p2 is accepted already/); await assert.rejects(acceptGroupProposal(dir, { id: "p1", at: "2020-01-01T00:00:00.000Z" }), /replaced by a newer ask/); await assert.rejects(acceptGroupProposal(dir, { id: "p1", token: "stale" }), StaleToken); const text = await readFile(path.join(dir, MANIFEST_NAME), "utf8"); const j = await rejectGroupProposals(dir, { id: "p1" }); assert.deepEqual(j.rejected, ["p1"]); assert.equal(await readFile(path.join(dir, MANIFEST_NAME), "utf8"), text); await assert.rejects(rejectGroupProposals(dir, {}), /nothing is waiting/); }); test("a proposal the cut no longer takes: the view says why, and Accept marks it failed in the build's words", async () => { const dir = await project(); await setGroup(dir, null, ["c02"], { claim: "Taken by hand" }); const v = await groupProposalsView(dir); assert.match(v.proposals.groups[0].now, /c02 \(g1\) is in a group already/); assert.equal(v.proposals.groups[1].now, null); const r = await acceptGroupProposal(dir, { id: "p1" }); assert.equal(r.failed, "p1"); assert.match(r.error, /c02 is in two groups|in a group/); assert.equal((await readGroupProposals(dir)).doc.groups[0].status, "failed"); assert.deepEqual((await manifest(dir)).groups.map((g) => g.id), ["g1"]); }); test("the start's body and the route's words", () => { assert.deepEqual(parseGroupsRequest({ request: " by day " }), { request: "by day" }); assert.deepEqual(groupsDraftBodyOf("/p", { request: "" }), { task: "propose-groups", project: "/p" }); assert.throws(() => parseGroupsRequest({ request: "x".repeat(2001) }), /at most 2000/); assert.match(groupsRouteOf({ routes: { "propose-groups": "code" } }, "/p").why, /routed to Claude Code/); assert.deepEqual(groupsRouteOf({ routes: { "propose-groups": "local" }, models: { local: "qwen3:8b" } }, "/p"), { route: "local", model: "qwen3:8b", why: null }); });