// node --test umtool/lib/create/create.test.mjs import { test } from "node:test"; import assert from "node:assert/strict"; import { policyChip, routeGroups, routeSummary } from "./policy.mjs"; import { estimateOf, estimateText, spendHistory } from "./estimate.mjs"; import { chainable, defaultChainIteration } from "./iterations.mjs"; const TASKS = ["sweep", "revise-report", "revise-video", "narrate", "research", "cut", "onscreen", "teaser", "flips", "threads", "youtube", "social"]; const media = (r) => ({ cut: r, onscreen: r, teaser: r, flips: r, threads: r, youtube: r, social: r }); const BALANCED = { sweep: "code", "revise-report": "api", "revise-video": "local", narrate: "local", research: "api", ...media("local") }; test("the balanced preset in one line: grouped by route, drafts as one word", () => { assert.equal(routeSummary(BALANCED, TASKS), "Claude Code: sweeps · API: report revisions, research · local: video revisions, drafts"); assert.deepEqual(routeGroups(BALANCED, TASKS).map((g) => g.route), ["code", "api", "local"]); }); test("one route for everything says so; agreeing revisions are one word; split drafts are named", () => { const api = Object.fromEntries(TASKS.map((t) => [t, "api"])); assert.equal(routeSummary(api, TASKS), "everything on the API"); assert.equal(routeSummary(Object.fromEntries(TASKS.map((t) => [t, "local"])), TASKS), "everything local"); const custom = { ...api, sweep: "code", narrate: "local" }; assert.equal(routeSummary(custom, TASKS), "Claude Code: sweeps · API: revisions, research, cut drafts, on-screen drafts, teaser drafts, flips drafts, threads drafts, YouTube drafts, social post drafts · local: narration"); }); test("the chip names the preset, or why there is none", () => { assert.equal(policyChip({ preset: "balanced", routes: BALANCED }, TASKS), "Generation: balanced · Claude Code: sweeps · API: report revisions, research · local: video revisions, drafts"); assert.equal(policyChip(null), "Generation: the editor did not answer"); }); test("the ledger as per-job spend: rows summed per job under its main model, by task", () => { const rows = [ { jobId: "a", task: "sweep", model: "claude-sonnet", usd: 0.4, at: "2026-10-01T00:00:00Z" }, { jobId: "a", task: "sweep", model: "claude-haiku", usd: 0.02, at: "2026-10-01T00:00:00Z" }, { jobId: "b", task: "sweep", model: "claude-sonnet", usd: 1.1, at: "2026-10-02T00:00:00Z" }, { jobId: "c", task: "narrate", model: "claude-sonnet", usd: 0.3, at: "2026-10-02T00:00:00Z" }, { jobId: "d", task: "research", model: "claude-opus", usd: 2, at: "2026-10-03T00:00:00Z" }, ]; assert.deepEqual(spendHistory(rows), { sweep: { "claude-sonnet": [0.42, 1.1] }, research: { "claude-opus": [2] } }); }); test("an estimate: the past runs' typical and most, under the cap; a local model spends nothing", () => { const h = { sweep: { "claude-sonnet": [0.42, 1.1, 0.5] } }; const e = estimateOf(h, { task: "sweep", model: "claude-sonnet", cap: 5, local: false }); assert.deepEqual(e, { kind: "api", model: "claude-sonnet", runs: 3, typical: 0.5, most: 1.1, cap: 5 }); assert.equal(estimateText(e), "3 earlier runs on claude-sonnet: typical $0.50, most $1.10 · stops at $5.00 (its cap)"); assert.equal(estimateText(estimateOf(h, { task: "sweep", model: "claude-opus", cap: 3, local: false })), "No earlier run on claude-opus in the ledger · stops at $3.00 (its cap)"); assert.equal(estimateText(estimateOf(h, { task: "sweep", model: "qwen3:8b", cap: 5, local: true })), "Runs on the local model: no API spend"); }); test("video from an article: the newest done iteration with its OWN report is the default, never a video revision", () => { // The fixture's zeta: i1 sweep (report), i2 revise-report (report), i3 revise-video (i1's report), i4 failed. const its = [ { n: 4, status: "failed", hasReport: false, reportFrom: null }, { n: 3, status: "done", hasReport: false, reportFrom: 1 }, { n: 2, status: "done", hasReport: true, reportFrom: 2 }, { n: 1, status: "done", hasReport: true, reportFrom: 1 }, ]; assert.deepEqual(chainable(its).map((i) => i.n), [3, 2, 1]); assert.equal(defaultChainIteration(its), 2); assert.equal(defaultChainIteration([{ n: 2, status: "done", hasReport: false, reportFrom: 1 }]), null); assert.equal(defaultChainIteration([]), null); });