import { test } from "node:test"; import assert from "node:assert/strict"; import { defaultDigest, type SiteSettings } from "../lib/settings"; import { digestLaneFor } from "../lib/operations"; import { BACKFILL_QUEUE } from "../lib/queueKeys"; import { digestGate, digestPreflight, laneSharesDuplicates, } from "./laneGuards"; // Run with: node_modules/.bin/tsx --test common/controller/laneGuards.test.ts // // The yield branch is deliberately NOT unit-tested here: it consults the live // worker pool and job registry through transcriptionActivity(), which fails // open to "nothing running" in a bare process. Stubbing those would test the // stub. The yield is covered end to end by digest.spec.ts and backfill.spec.ts, // and the DECISION it turns on — that only a gpu-contending lane yields — is // asserted structurally below. function settingsWith(patch: Partial): SiteSettings { return { digest: { ...defaultDigest(), ...patch } } as SiteSettings; } // The same partial object with the digest lane's gate shut. The gate is // `autoQueue.digest.held` and nothing else since S0-pause deleted // `digest.digestsPaused`; `isGateHeld` reads `autoQueue` with `?.`, which is // what lets a {digest}-only cast keep working everywhere else in this file. function heldWith(patch: Partial = {}): SiteSettings { return { digest: { ...defaultDigest(), ...patch }, autoQueue: { digest: { held: true } }, } as unknown as SiteSettings; } test("a pause holds the lane, and beats every other reason", () => { const gate = digestGate({ // Paused AND over a spend cap: an operator who paused the lane must be told // about the pause, not sent to look at a billing setting. settings: heldWith({ spendCapUsd: 1 }), appLane: "remote-api", metered: true, costUsd: 99, }); assert.equal(gate.hold, true); assert.equal(gate.hold && gate.reason, "paused"); }); test("the spend cap parks the METERED lane only", () => { const settings = settingsWith({ spendCapUsd: 5 }); const over = digestGate({ settings, appLane: "remote-api", metered: true, costUsd: 5, }); assert.equal(over.hold, true); assert.equal(over.hold && over.reason, "spend-cap"); assert.match(over.hold ? over.message : "", /\$5\.00 of \$5\.00/); // A local run costs nothing per call, so the cap has nothing to cap. assert.equal( digestGate({ settings, appLane: "local-gpu", metered: false, costUsd: 500 }) .hold, false, ); // And the cap is opt-in: 0 means no cap, not "cap at zero". assert.equal( digestGate({ settings: settingsWith({ spendCapUsd: 0 }), appLane: "remote-api", metered: true, costUsd: 500, }).hold, false, ); }); test("under the cap the lane goes, at the cap it holds", () => { const settings = settingsWith({ spendCapUsd: 5 }); const input = { settings, appLane: "remote-api" as const, metered: true }; assert.equal(digestGate({ ...input, costUsd: 4.99 }).hold, false); // >=, not >: the cap is a ceiling on cumulative spend, so reaching it stops. assert.equal(digestGate({ ...input, costUsd: 5 }).hold, true); }); test("only a gpu-contending lane can yield", () => { // The decision the yield turns on, asserted where it is DECLARED rather than // where it is consumed. A network lane competes for nothing local, so parking // it would idle a lane that costs nothing to keep running. assert.equal(digestLaneFor("local-gpu").contendsFor, "gpu"); assert.equal(digestLaneFor("remote-api").contendsFor, "network"); // With yielding switched off entirely, even the GPU lane goes. assert.equal( digestGate({ settings: settingsWith({ yieldToTranscription: false }), appLane: "local-gpu", metered: false, costUsd: 0, }).hold, false, ); }); test("preflight refuses the metered lane while it is disabled, without probing", async () => { let probed = false; const verdict = await digestPreflight({ settings: settingsWith({ remoteEnabled: false }), lane: "remote", app: { id: "claude", lane: "remote-api", probe: async () => { probed = true; return true; }, }, }); assert.equal(verdict.ok, false); assert.match(verdict.ok ? "" : verdict.error, /remoteEnabled/); // A disabled lane must not cost a network call to find out it is disabled. assert.equal(probed, false); }); test("preflight refuses an unreachable engine, and names the fix", async () => { const local = await digestPreflight({ settings: settingsWith({}), lane: "local", app: { id: "ollama", lane: "local-gpu", probe: async () => false }, }); assert.equal(local.ok, false); assert.match(local.ok ? "" : local.error, /ollama service/); const remote = await digestPreflight({ settings: settingsWith({ remoteEnabled: true }), lane: "remote", app: { id: "claude", lane: "remote-api", probe: async () => false }, }); assert.equal(remote.ok, false); assert.match(remote.ok ? "" : remote.error, /CLAUDE_BIN/); }); test("preflight passes a reachable, enabled lane", async () => { const verdict = await digestPreflight({ settings: settingsWith({ remoteEnabled: true }), lane: "remote", app: { id: "claude", lane: "remote-api", probe: async () => true }, }); assert.equal(verdict.ok, true); }); test("only the digest lanes share a duplicate's output", () => { // A chapter list is about what was SAID, so an aligned mirror gets the // canonical member's digest for free. Diarization and attribution are // grounded in one audio track and one set of cue timings and can share // nothing — a mirror's clock is not the same clock. assert.equal(laneSharesDuplicates(digestLaneFor("local-gpu")), true); assert.equal(laneSharesDuplicates(digestLaneFor("remote-api")), true); assert.equal( laneSharesDuplicates({ queueKey: BACKFILL_QUEUE, contendsFor: "cpu" }), false, ); });