import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_GENERATION_SETTINGS, GENERATION_PRESETS, GENERATION_TASKS, MEDIA_STEP_TASKS, capFor, isStepTask, looksLikeApiKey, presetOf, sanitizeGeneration, } from "./generationSettings"; test("generation settings: the defaults are the stopgap's balanced policy, Sonnet 5.5, qwen3:8b, Haiku, D6's caps", () => { const d = sanitizeGeneration(undefined); assert.deepEqual(d, DEFAULT_GENERATION_SETTINGS); assert.equal(d.preset, "balanced"); assert.deepEqual(d.routes, GENERATION_PRESETS.balanced); assert.deepEqual(d.caps, { sweep: 5, revision: 2, narration: 1, daily: 20, max: 10 }); assert.equal(sanitizeGeneration(d).preset, "balanced", "idempotent"); }); test("the routes are the truth: the preset is the one they match, else custom", () => { assert.equal(sanitizeGeneration({ routes: GENERATION_PRESETS.api }).preset, "api"); assert.equal(sanitizeGeneration({ preset: "api" }).preset, "balanced", "a preset alone does not move the routes"); const custom = sanitizeGeneration({ routes: { ...GENERATION_PRESETS.balanced, narrate: "api" } }); assert.equal(custom.preset, "custom"); assert.equal(presetOf(custom.routes), "custom"); assert.equal(sanitizeGeneration({ routes: { sweep: "nope" } }).routes.sweep, "code"); }); test("the API key is never a setting: a value that looks like one is dropped for the default path", () => { assert.equal(looksLikeApiKey(["sk", "ant", "api03-abcdef"].join("-")), true); assert.equal(looksLikeApiKey("A".repeat(40)), true); assert.equal(looksLikeApiKey("~/.config/archilyzer/anthropic.env"), false); assert.equal(looksLikeApiKey("/etc/keys/anthropic.env"), false); assert.equal(sanitizeGeneration({ apiKeyFile: ["sk", "ant", "api03-xyz"].join("-") }).apiKeyFile, DEFAULT_GENERATION_SETTINGS.apiKeyFile); assert.equal(sanitizeGeneration({ apiKeyFile: "/srv/key.env" }).apiKeyFile, "/srv/key.env"); }); test("caps: a task's default, an asked one within max, a larger one refused; URLs and models checked", () => { const g = sanitizeGeneration({ caps: { sweep: 50, max: 8, daily: -1 } }); assert.equal(g.caps.sweep, 8, "no default above max"); assert.equal(g.caps.daily, 20, "a negative daily is the default"); assert.equal(capFor(g.caps, "revision"), 2); assert.equal(capFor(g.caps, "sweep", 3), 3); assert.throws(() => capFor(g.caps, "sweep", 9), /over the settings' max of \$8/); assert.throws(() => capFor(g.caps, "sweep", 0), /positive/); const u = sanitizeGeneration({ anthropicBaseUrl: "http://127.0.0.1:9/", ollamaUrl: "not a url", models: { api: "bad model id!" } }); assert.equal(u.anthropicBaseUrl, "http://127.0.0.1:9"); assert.equal(u.ollamaUrl, ""); assert.equal(u.models.api, "claude-sonnet-5-5"); assert.equal(sanitizeGeneration({ cacheTtl: "1h" }).cacheTtl, "1h"); }); test("research (S11): a route of its own, a file from before it keeps its preset, and the run's autonomy and limits", () => { const d = sanitizeGeneration(undefined); assert.equal(d.routes.research, "api", "balanced runs research on the API"); assert.deepEqual(d.research, { autonomy: "propose", maxItemsPerRun: 20, maxGbPerRun: 5, maxTranscribeHours: 6, newChannelsNeedApproval: true, postWalksNeedApproval: true, }); // routes written before research had one: the preset they are keeps its name. const { research: _r, ...localOld } = GENERATION_PRESETS.local; const old = sanitizeGeneration({ routes: localOld }); assert.equal(old.routes.research, "local"); assert.equal(old.preset, "local"); const { research: _c, ...codeOld } = GENERATION_PRESETS.code; assert.equal(sanitizeGeneration({ routes: codeOld }).preset, "code"); assert.equal(sanitizeGeneration({ routes: { ...GENERATION_PRESETS.balanced, research: "local" } }).preset, "custom"); const r = sanitizeGeneration({ research: { autonomy: "auto", maxItemsPerRun: 3.7, maxGbPerRun: -1, newChannelsNeedApproval: false, extra: 1 } }).research; assert.deepEqual(r, { autonomy: "auto", maxItemsPerRun: 3, maxGbPerRun: 5, maxTranscribeHours: 6, newChannelsNeedApproval: false, postWalksNeedApproval: true }); assert.equal(sanitizeGeneration({ research: { autonomy: "yolo" } }).research.autonomy, "propose"); }); test("the media drafts (release 28): steps, each with narrate's route in every preset; a file from before keeps its preset", () => { assert.deepEqual([...MEDIA_STEP_TASKS], ["narrate", "cut", "onscreen", "teaser", "flips", "threads", "youtube", "social"]); for (const t of MEDIA_STEP_TASKS) { assert.ok(isStepTask(t)); assert.ok((GENERATION_TASKS as readonly string[]).includes(t)); for (const p of Object.values(GENERATION_PRESETS)) assert.equal(p[t], p.narrate, `${t} routes as narrate`); } for (const t of ["sweep", "revise-report", "revise-video", "research", "nope", undefined]) assert.equal(isStepTask(t), false); assert.equal(sanitizeGeneration(undefined).routes.cut, "local", "balanced drafts media on the local model"); // routes written before the media tasks had any (release 27's five). const five = (p: keyof typeof GENERATION_PRESETS) => Object.fromEntries(["sweep", "revise-report", "revise-video", "narrate", "research"].map((t) => [t, GENERATION_PRESETS[p][t as "sweep"]])); for (const p of ["code", "balanced", "api", "local"] as const) { const old = sanitizeGeneration({ routes: five(p) }); assert.equal(old.preset, p); assert.deepEqual(old.routes, GENERATION_PRESETS[p]); } // A custom five keep their own routes; the media tasks take the default's. const custom = sanitizeGeneration({ routes: { ...five("api"), narrate: "local" } }); assert.equal(custom.preset, "custom"); assert.equal(custom.routes.youtube, "local"); // A partial patch names no preset. assert.equal(sanitizeGeneration({ routes: { narrate: "api" } }).routes.cut, "local"); // One media task routed on its own is custom. assert.equal(sanitizeGeneration({ routes: { ...GENERATION_PRESETS.api, teaser: "local" } }).preset, "custom"); // A file from release 28 (every task but social, release 29 G2) keeps its preset; social takes its route. for (const p of ["code", "balanced", "api", "local"] as const) { const { social: _s, ...r28 } = GENERATION_PRESETS[p]; const old = sanitizeGeneration({ routes: r28 }); assert.equal(old.preset, p); assert.equal(old.routes.social, GENERATION_PRESETS[p].narrate); } }); test("cut (release 28 M1): the most entries a drafted cut keeps, 20 by default, a whole number from 3", () => { assert.equal(sanitizeGeneration(undefined).cut.maxClips, 20); assert.equal(sanitizeGeneration({ cut: { maxClips: 12 } }).cut.maxClips, 12); for (const bad of [2, 12.5, -1, "12", null]) assert.equal(sanitizeGeneration({ cut: { maxClips: bad } }).cut.maxClips, 20, String(bad)); }); test("generation settings: the verdict bench's bar (fractions, clamped) and the ticked models; none by default", () => { assert.deepEqual(DEFAULT_GENERATION_SETTINGS.verdicts, { bar: { agreement: 0, contradictedRecall: 0, notFoundRecall: 0 }, models: [], window: { mode: "seconds", seconds: 60 } }); assert.deepEqual(sanitizeGeneration({}).verdicts, DEFAULT_GENERATION_SETTINGS.verdicts); const v = sanitizeGeneration({ verdicts: { bar: { agreement: 0.756789, contradictedRecall: 1.5, notFoundRecall: "x" }, models: ["claude-sonnet-5-5", " qwen3:8b ", "bad model", 3, "claude-sonnet-5-5"] }, }).verdicts; assert.deepEqual(v, { bar: { agreement: 0.7568, contradictedRecall: 1, notFoundRecall: 0 }, models: ["claude-sonnet-5-5", "qwen3:8b"], window: { mode: "seconds", seconds: 60 } }); // The window proposals read (release 29): the bench run's the bar was set on. assert.deepEqual(sanitizeGeneration({ verdicts: { window: { mode: "turn", seconds: 30.4 } } }).verdicts.window, { mode: "turn", seconds: 30 }); assert.deepEqual(sanitizeGeneration({ verdicts: { window: { mode: "sentence", seconds: 900 } } }).verdicts.window, { mode: "seconds", seconds: 60 }); }); test("the frame scan (release 30): its own route and settings; a file from before keeps its preset", () => { assert.ok((GENERATION_TASKS as readonly string[]).includes("frames")); assert.equal(GENERATION_PRESETS.code.frames, "code"); assert.equal(GENERATION_PRESETS.balanced.frames, "api"); assert.equal(GENERATION_PRESETS.local.frames, "local"); assert.deepEqual(DEFAULT_GENERATION_SETTINGS.frames, { apiModel: "claude-haiku-4-5", localModel: "qwen2.5vl:7b", maxUsd: 5, everySeconds: 10 }); assert.deepEqual(sanitizeGeneration({}).frames, DEFAULT_GENERATION_SETTINGS.frames); assert.deepEqual(sanitizeGeneration({ frames: { apiModel: "claude-haiku-5-5", localModel: "bad model", maxUsd: -1, everySeconds: 0.5 } }).frames, { apiModel: "claude-haiku-5-5", localModel: "qwen2.5vl:7b", maxUsd: 5, everySeconds: 10, }); // A file that routes every task but frames keeps its preset. for (const p of ["code", "balanced", "api", "local"] as const) { const routes = Object.fromEntries(GENERATION_TASKS.filter((t) => t !== "frames").map((t) => [t, GENERATION_PRESETS[p][t]])); const g = sanitizeGeneration({ routes }); assert.equal(g.preset, p); assert.equal(g.routes.frames, GENERATION_PRESETS[p].frames); } }); test("the clip finder's labels (release 36 lane D): routed like a media draft; a file from before keeps its preset", () => { assert.ok((GENERATION_TASKS as readonly string[]).includes("clips-suggest")); assert.deepEqual( (["code", "balanced", "api", "local"] as const).map((p) => GENERATION_PRESETS[p]["clips-suggest"]), ["code", "local", "api", "local"], ); for (const p of ["code", "balanced", "api", "local"] as const) { const routes = Object.fromEntries(GENERATION_TASKS.filter((t) => t !== "clips-suggest").map((t) => [t, GENERATION_PRESETS[p][t]])); const g = sanitizeGeneration({ routes }); assert.equal(g.preset, p); assert.equal(g.routes["clips-suggest"], GENERATION_PRESETS[p]["clips-suggest"]); } }); test("the video assistant (release 38 lane E): local in every preset but api; a file from before keeps its preset", () => { assert.ok((GENERATION_TASKS as readonly string[]).includes("video-assist")); assert.deepEqual( (["code", "balanced", "api", "local"] as const).map((p) => GENERATION_PRESETS[p]["video-assist"]), ["local", "local", "api", "local"], ); for (const p of ["code", "balanced", "api", "local"] as const) { const routes = Object.fromEntries(GENERATION_TASKS.filter((t) => t !== "video-assist").map((t) => [t, GENERATION_PRESETS[p][t]])); const g = sanitizeGeneration({ routes }); assert.equal(g.preset, p); assert.equal(g.routes["video-assist"], GENERATION_PRESETS[p]["video-assist"]); } // A file that routes nothing: the default (balanced) — local. assert.equal(sanitizeGeneration({}).routes["video-assist"], "local"); }); test("claim groups for a cut (lane L4): local in every preset but api; a file from before keeps its preset", () => { assert.ok((GENERATION_TASKS as readonly string[]).includes("propose-groups")); assert.deepEqual( (["code", "balanced", "api", "local"] as const).map((p) => GENERATION_PRESETS[p]["propose-groups"]), ["local", "local", "api", "local"], ); for (const p of ["code", "balanced", "api", "local"] as const) { const routes = Object.fromEntries(GENERATION_TASKS.filter((t) => t !== "propose-groups").map((t) => [t, GENERATION_PRESETS[p][t]])); const g = sanitizeGeneration({ routes }); assert.equal(g.preset, p); assert.equal(g.routes["propose-groups"], GENERATION_PRESETS[p]["propose-groups"]); } assert.equal(sanitizeGeneration({}).routes["propose-groups"], "local"); }); test("generation.localSweep: the claim form and the read-aloud rule, each one of its values or the default", async () => { const { DEFAULT_GENERATION_LOCAL_SWEEP, sanitizeLocalSweep } = await import("./generationSettings"); assert.deepEqual(sanitizeGeneration({}).localSweep, DEFAULT_GENERATION_LOCAL_SWEEP); assert.deepEqual(sanitizeGeneration({ localSweep: { claimForm: "sentence", readAloud: "drop" } }).localSweep, { claimForm: "sentence", readAloud: "drop" }); assert.deepEqual(sanitizeLocalSweep({ claimForm: "poem", readAloud: true }), DEFAULT_GENERATION_LOCAL_SWEEP); assert.deepEqual(sanitizeLocalSweep({ readAloud: "off" }), { ...DEFAULT_GENERATION_LOCAL_SWEEP, readAloud: "off" }); assert.deepEqual(sanitizeLocalSweep("neutral"), DEFAULT_GENERATION_LOCAL_SWEEP); }); test("plannedModelOf (demo epic D3): a task's route and the model it will run on; code has none; a frame scan's vision model; the API needs a key", async () => { const { plannedModelOf, sanitizeGeneration } = await import("./generationSettings"); const gen = sanitizeGeneration({ routes: { sweep: "code", narrate: "local", "revise-report": "api", frames: "local" }, models: { api: "claude-sonnet-5-5", local: "qwen2.5:7b" } }); assert.deepEqual(plannedModelOf(gen, "sweep"), { route: "code", model: null, needsKey: false }); assert.deepEqual(plannedModelOf(gen, "narrate"), { route: "local", model: "qwen2.5:7b", needsKey: false }); assert.deepEqual(plannedModelOf(gen, "revise-report"), { route: "api", model: "claude-sonnet-5-5", needsKey: true }); assert.deepEqual(plannedModelOf(gen, "frames"), { route: "local", model: "qwen2.5vl:7b", needsKey: false }); });