// Research acquisitions: the decision over autonomy and limits, the standing // rules (each with its test), and the tools against a fake editor. import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_RESEARCH_LIMITS, decideAcquisition, isTwitchVodUrl, NOTHING_SPENT, type Proposal, type ResearchLimits, } from "./decide"; import { ACTION_SPECS, acquisitionTools, archiveReadTools, newLedger, rejectProposal, runApprovedProposal, type ResearchEditor, } from "./tools"; const LIMITS: ResearchLimits = { maxItemsPerRun: 3, maxGbPerRun: 2, maxTranscribeHours: 1, newChannelsNeedApproval: true, postWalksNeedApproval: true, }; const OPEN: ResearchLimits = { ...LIMITS, newChannelsNeedApproval: false, postWalksNeedApproval: false }; const p = (action: Proposal["action"], args: Record, estimate?: Proposal["estimate"]) => ({ action, args, ...(estimate ? { estimate } : {}), }); // A fake editor: answers each action, records every call. function fakeEditor(answer: (action: string, args: Record) => Record = () => ({ ok: true, jobId: "j1" })) { const calls: { action: string; args: Record }[] = []; const editor: ResearchEditor = { run: async (action, args) => { calls.push({ action, args }); const a = answer(action, args); if (a.ok === false) throw new Error(String(a.error)); return a; }, }; return { editor, calls }; } test("propose: everything waits for the operator", () => { const d = decideAcquisition(p("import-video", { slug: "synth-kitchen", url: "https://video.example/v/1" }, { gb: 0.1 }), "propose", LIMITS); assert.equal(d.decision, "approval"); }); test("limits: an import into an existing channel under the caps runs; over a cap, or of unknown size, it waits", () => { const small = p("import-archive-org", { slug: "synth-kitchen", item: "synth-kitchen-episodes-2026" }, { items: 2, gb: 0.3 }); assert.deepEqual(decideAcquisition(small, "limits", LIMITS), { decision: "enqueue" }); const spent = { items: 2, gb: 0.3, transcribeHours: 0 }; assert.match((decideAcquisition(small, "limits", LIMITS, spent) as { reason: string }).reason, /cap of 3 item\(s\) \(2 so far, this is 2\)/); const big = p("import-video", { slug: "synth-kitchen", url: "https://video.example/v/2" }, { gb: 2.5 }); assert.match((decideAcquisition(big, "limits", LIMITS) as { reason: string }).reason, /cap of 2 GB/); const unknown = p("import-video", { slug: "synth-kitchen", url: "https://video.example/v/3" }); assert.deepEqual(decideAcquisition(unknown, "limits", LIMITS), { decision: "approval", reason: "its size is not known" }); const tr = p("transcribe-one", { slug: "synth-kitchen", id: "x" }, { transcribeHours: 2 }); assert.match((decideAcquisition(tr, "limits", LIMITS) as { reason: string }).reason, /1 h of transcription/); assert.equal(decideAcquisition(p("transcribe-one", { slug: "s", id: "x" }, { transcribeHours: 0.5 }), "limits", LIMITS).decision, "enqueue"); // A post into an existing channel is one item, no download size to guess. assert.equal(decideAcquisition(p("import-post", { slug: "synth-social", url: "https://x.com/a/status/1" }), "limits", LIMITS).decision, "enqueue"); }); test("limits: new channels and post walks wait while the flags say so, a priority bump always; auto lifts the flags", () => { const create = p("create-channel", { fields: { name: "Synth Other", handling: "youtube", url: "https://video.example/@other" } }); const walk = p("fetch-posts", { slug: "synth-social", older: true }); const fresh = p("fetch-posts", { slug: "synth-social" }); const bump = p("priority-bump", { slug: "synth-kitchen" }); assert.match((decideAcquisition(create, "limits", LIMITS) as { reason: string }).reason, /new channel needs the operator's approval/); assert.equal(decideAcquisition(create, "limits", OPEN).decision, "enqueue", "the flag off: a new channel runs under limits"); assert.match((decideAcquisition(walk, "limits", LIMITS) as { reason: string }).reason, /post history needs the operator's approval/); assert.equal(decideAcquisition(walk, "limits", OPEN).decision, "enqueue"); assert.equal(decideAcquisition(fresh, "limits", LIMITS).decision, "enqueue"); assert.match((decideAcquisition(bump, "limits", LIMITS) as { reason: string }).reason, /priority change/); // auto: the approval flags are lifted (ruled 2026-10-13) … assert.equal(decideAcquisition(create, "auto", LIMITS).decision, "enqueue"); assert.equal(decideAcquisition(walk, "auto", LIMITS).decision, "enqueue"); assert.equal(decideAcquisition(bump, "auto", LIMITS).decision, "enqueue"); // … but the caps are hard ceilings: past one, or of unknown size, it waits. assert.match((decideAcquisition(p("import-video", { slug: "s", url: "https://v.example/1" }, { gb: 9 }), "auto", LIMITS) as { reason: string }).reason, /cap of 2 GB/); assert.equal(decideAcquisition(p("import-video", { slug: "s", url: "https://v.example/1" }), "auto", LIMITS).decision, "approval"); assert.equal(decideAcquisition(p("import-post", { slug: "s", url: "https://x.com/a/status/1" }), "auto", LIMITS, { items: 3, gb: 0, transcribeHours: 0 }).decision, "approval"); }); test("standing rule: create-channel is always on NO site — refused with sites in every mode, even auto", () => { const withSite = p("create-channel", { fields: { name: "Synth", handling: "youtube", url: "https://video.example/@s" }, sites: [{ siteId: "demo" }] }); for (const mode of ["propose", "limits", "auto"] as const) { const d = decideAcquisition(withSite, mode, OPEN); assert.equal(d.decision, "refuse", mode); assert.match((d as { reason: string }).reason, /goes on NO site/); } assert.equal(decideAcquisition({ ...withSite, args: { ...withSite.args, sites: [] } }, "auto", OPEN).decision, "enqueue"); }); test("standing rule: an X channel never goes on a site", () => { const x = p("create-channel", { fields: { name: "Synth (X)", handling: "transcribe", url: "https://x.com/synthbaker" }, sites: [{ siteId: "demo" }] }); const d = decideAcquisition(x, "auto", OPEN); assert.deepEqual(d, { decision: "refuse", reason: "an X channel is never put on a site" }); }); test("standing rule: no Twitch VOD URL, anywhere in the args, in any mode", () => { for (const url of ["https://www.twitch.tv/videos/123456789", "https://twitch.tv/somechannel/v/987", "twitch.tv/videos/1"]) { assert.equal(isTwitchVodUrl(url), true, url); const d = decideAcquisition(p("import-video", { slug: "s", url }, { gb: 0.1 }), "auto", OPEN); assert.equal(d.decision, "refuse", url); assert.match((d as { reason: string }).reason, /Twitch VOD is never fetched again/); } const nested = p("create-channel", { fields: { name: "T", handling: "youtube", url: "https://www.twitch.tv/videos/55" } }); assert.equal(decideAcquisition(nested, "auto", OPEN).decision, "refuse"); assert.equal(isTwitchVodUrl("https://www.twitch.tv/somechannel"), false); }); test("standing rule: nothing bypasses the editor's queues — no queueKey, and only the listed actions", () => { const d = decideAcquisition(p("fetch-posts", { slug: "s", queueKey: "fast-lane" }), "auto", OPEN); assert.match((d as { reason: string }).reason, /nothing bypasses the editor's queues/); const other = decideAcquisition({ action: "delete-channel" as never, args: { slug: "s" } }, "auto", OPEN); assert.match((other as { reason: string }).reason, /is not an acquisition a research run may ask for/); // Every tool reaches the editor through one of the existing ops actions. const actions = Object.values(ACTION_SPECS).map((s) => s.editorAction).sort(); assert.deepEqual(actions, [ "capture-posts", "channel-priority", "create-channel", "fetch-posts", "import-archive-org", "import-post", "import-video", "persist-videos", "transcribe-one", ]); }); test("tools: one per action; limits mode enqueues an import through the editor, records it, and spends it", async () => { const { editor, calls } = fakeEditor(); const ledger = newLedger(); const changes: number[] = []; const tools = acquisitionTools({ editor, autonomy: "limits", limits: LIMITS, ledger, onChange: (l) => void changes.push(l.proposals.length) }); assert.deepEqual(tools.map((t) => t.name).sort(), Object.keys(ACTION_SPECS).sort()); const imp = tools.find((t) => t.name === "import-archive-org")!; const out = await imp.run( { slug: "synth-kitchen", item: "synth-kitchen-episodes-2026", match: "\\.mp4$", reason: "the bread day episode is not in the archive", evidence: ["https://archive.org/details/synth-kitchen-episodes-2026"], estimate: { items: 1, gb: 0.1 }, }, {}, ); assert.equal(out, "p01 import-archive-org: enqueued as job j1 — it runs on the editor's queue; follow it, do not repeat it."); assert.deepEqual(calls, [{ action: "import-archive-org", args: { slug: "synth-kitchen", item: "synth-kitchen-episodes-2026", match: "\\.mp4$" } }]); assert.deepEqual(ledger.proposals[0], { id: "p01", action: "import-archive-org", args: { slug: "synth-kitchen", item: "synth-kitchen-episodes-2026", match: "\\.mp4$" }, reason: "the bread day episode is not in the archive", evidence: ["https://archive.org/details/synth-kitchen-episodes-2026"], estimate: { items: 1, gb: 0.1 }, status: "enqueued", jobId: "j1", }); assert.deepEqual(ledger.spent, { items: 1, gb: 0.1, transcribeHours: 0 }); assert.deepEqual(changes, [1]); }); test("tools: a proposal that waits calls no editor; a refused one says so; the operator's yes runs it, past the autonomy", async () => { const { editor, calls } = fakeEditor((action) => (action === "create-channel" ? { ok: true, slug: "synth-other", jobIds: [] } : { ok: true, jobId: "j9" })); const ledger = newLedger(); const tools = acquisitionTools({ editor, autonomy: "propose", limits: DEFAULT_RESEARCH_LIMITS, ledger }); const create = tools.find((t) => t.name === "create-channel")!; const waits = await create.run( { fields: { name: "Synth Other", handling: "youtube", url: "https://video.example/@other" }, reason: "the other kitchen is quoted" }, {}, ); assert.match(waits, /^p01 create-channel: proposed — waits for the operator's approval/); const refused = await create.run( { fields: { name: "Synth Other", handling: "youtube", url: "https://video.example/@other" }, sites: [{ siteId: "demo" }], reason: "x" }, {}, ); assert.match(refused, /^p02 create-channel: refused — a channel a research run creates goes on NO site/); assert.equal(calls.length, 0); // The yes: no `sites` reaches the editor — the body carries only the action's own keys. const yes = await runApprovedProposal({ editor, ledger }, "p01"); assert.equal(yes.status, "done"); assert.deepEqual(calls, [{ action: "create-channel", args: { fields: { name: "Synth Other", handling: "youtube", url: "https://video.example/@other" } } }]); await assert.rejects(runApprovedProposal({ editor, ledger }, "p02"), /p02 is rejected/); // A no. const vod = tools.find((t) => t.name === "import-video")!; await vod.run({ slug: "s", url: "https://video.example/v/1", reason: "r" }, {}); assert.equal(rejectProposal(ledger, "p03").status, "rejected"); }); test("tools: the editor's refusal fails the proposal with its words", async () => { const { editor } = fakeEditor(() => ({ ok: false, error: "x.com is held until 14:00 (rate limit)" })); const ledger = newLedger(); const tools = acquisitionTools({ editor, autonomy: "auto", limits: OPEN, ledger }); await assert.rejects( tools.find((t) => t.name === "import-post")!.run({ slug: "synth-social", url: "https://x.com/synthbaker/status/1", reason: "r" }, {}), /p01 import-post: failed — the editor refused: x\.com is held until 14:00/, ); assert.equal(ledger.proposals[0]!.status, "failed"); assert.deepEqual(ledger.spent, NOTHING_SPENT); }); test("tools: priority-bump is channel-priority with tier normal", async () => { const { editor, calls } = fakeEditor(() => ({ ok: true })); const ledger = newLedger(); const bump = acquisitionTools({ editor, autonomy: "auto", limits: OPEN, ledger }).find((t) => t.name === "priority-bump")!; assert.match(await bump.run({ slug: "synth-kitchen", operation: "transcription", reason: "its new imports need transcripts" }, {}), /done/); assert.deepEqual(calls, [{ action: "channel-priority", args: { slugs: ["synth-kitchen"], tier: "normal", operation: "transcription" } }]); }); test("archive reads go through the editor", async () => { const { editor, calls } = fakeEditor((action) => action === "archive-search" ? { ok: true, found: 1, items: [{ identifier: "synth-radio-tape-01", title: "Synth radio — the bakery call-in" }] } : { ok: true, item: { identifier: "synth-radio-tape-01", files: [{ name: "tape1.flac", size: 300000000 }] } }, ); const [search, item] = archiveReadTools({ editor }); assert.equal( await search!.run({ query: "bakery", limit: 5 }, {}), "1 item(s) found:\n- synth-radio-tape-01 — Synth radio — the bakery call-in https://archive.org/details/synth-radio-tape-01", ); assert.match(await item!.run({ item: "synth-radio-tape-01" }, {}), /tape1\.flac/); assert.deepEqual(calls, [ { action: "archive-search", args: { query: "bakery", limit: 5 } }, { action: "archive-item", args: { item: "synth-radio-tape-01" } }, ]); });