// The REAL lane resolver, against real settings on disk. // // Run with: node_modules/.bin/tsx --test common/controller/laneForOperation.test.ts // // Its own file because it needs a settings seam: laneForOperation calls // getSettings(), and getPaths() memoizes its first answer at module scope, so // the env must be set before anything imports the module under test. Every other // test of this machinery passes a `laneOf` stub in, and these cases are exactly // the ones a stub would answer vacuously. // // Named for the function rather than for controller/operationLane.ts, which is // the one-function module it now lives in. import { mkdtempSync, writeFileSync } from "node:fs"; import { rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { test, after } from "node:test"; import assert from "node:assert/strict"; // Set BEFORE anything can call getPaths(). node:test runs each file in its own // process, so this is scoped to this file alone. const ROOT = mkdtempSync(path.join(os.tmpdir(), "lane-for-operation-")); process.env.TRANSCRIPTS_DIR = ROOT; // Its own env key, defaulting to the MONOREPO root rather than the corpus — // without it these cases would read the live install's settings.json and the // remote-digest case would flip with whatever the operator has configured. const SETTINGS_FILE = path.join(ROOT, "settings.json"); process.env.SETTINGS_FILE = SETTINGS_FILE; const { laneForOperation } = await import("./operationLane"); const { DIGEST_LOCAL_QUEUE, DIGEST_REMOTE_QUEUE, BACKFILL_QUEUE, TRANSCRIPTION_QUEUE, } = await import("../lib/queueKeys"); after(() => rm(ROOT, { recursive: true, force: true })); // getSettings() re-reads the file on every call (no memoization of its own), so // a test can flip a setting between assertions. function writeSettings(settings: Record): void { writeFileSync(SETTINGS_FILE, JSON.stringify(settings), "utf8"); } test("digest's lane follows remoteEnabled, live", () => { // THE POINT OF laneFor. `digest.lane` is a DECLARATION and can only carry the // default (the local, GPU-bound queue). Which queue a run actually takes is a // configuration choice, and a dispatcher must reserve by the live answer or a // remote digest lands on the local key — where registry.ts's concurrency-1 // would serialize it behind GPU work it competes with for nothing. writeSettings({ digest: { remoteEnabled: false } }); assert.equal(laneForOperation("digest")?.queueKey, DIGEST_LOCAL_QUEUE); assert.equal(laneForOperation("digest")?.contendsFor, "gpu"); writeSettings({ digest: { remoteEnabled: true } }); assert.equal(laneForOperation("digest")?.queueKey, DIGEST_REMOTE_QUEUE); // And it must NOT yield: metered network work competes for nothing local, so // standing aside for transcription would park a lane that costs nothing to // keep running. assert.equal(laneForOperation("digest")?.contendsFor, "network"); }); test("diarization resolves through its laneFor too", () => { // Regression guard on the shape of the resolver rather than on diarization: // before this, laneForOperation special-cased digest and returned `.lane` for // everything else, so diarization's laneFor was invisible to callers. The // queue key is the same either way by design (two diarizations must never run // at once), so `contendsFor` is what shows the live answer got through. writeSettings({ diarization: { enabled: true, engine: "sortformer", backend: "vulkan" }, }); const gpu = laneForOperation("diarization"); assert.equal(gpu?.queueKey, BACKFILL_QUEUE); assert.equal(gpu?.contendsFor, "gpu"); writeSettings({ diarization: { enabled: true, engine: "sortformer", backend: "cpu" }, }); assert.equal(laneForOperation("diarization")?.contendsFor, "cpu"); }); test("a KNOWN external operation resolves its DECLARED lane", () => { // Slice 1.5 flipped this case. download and transcription are in // operationCatalog() — they have to be, or digest.dependsOn = // ["transcription"] names nothing — and they are dispatched by their OWN // runners off their own queue keys. That is a fact about the DISPATCHER, not // about whether they have a lane: they declare one, and every four-lane // surface asking "which lane does this run on" was getting null for the two // lanes that carry all of the live dispatch. // // The old fear was that a caller would "reserve the backfill queue for work no // backfill operation can do" — which these two assertions are what rules out: // neither declared lane is BACKFILL_QUEUE. writeSettings({}); const download = laneForOperation("download"); assert.equal(download?.queueKey, "download:"); assert.equal(download?.contendsFor, "network"); assert.notEqual(download?.queueKey, BACKFILL_QUEUE); const transcription = laneForOperation("transcription"); assert.equal(transcription?.queueKey, TRANSCRIPTION_QUEUE); assert.equal(transcription?.contendsFor, "gpu"); assert.notEqual(transcription?.queueKey, BACKFILL_QUEUE); // SYNC KEEPS ITS NULL. It is catalogued beside them and is a per-channel // cadence, not a per-video pipeline — the same answer pauseLaneFor gives it. assert.equal(laneForOperation("sync"), null); // The unknown-id case: same answer, different reason. assert.equal(laneForOperation("sortformer"), null); }); test("a switched-off kind still resolves a lane — the off-switch is elsewhere", () => { // Pinning the BOUNDARY, because it is the one an obvious-looking "fix" would // move. This resolver answers "which lane would this run on", not "should it // run": getOperation is a registry lookup and has never consulted // `enabled`. What keeps a disabled feature out of dispatch is // `operationsForLane`, which projects only the operations `allOperations` // returns — so a switched-off kind arrives with an EMPTY id list and produces // no unit, well before a lane is asked for. // // Teaching this function to return null for a disabled kind would look like a // tightening and would in fact be a second, redundant gate that the sweeps and // the stage cards — which call laneForOperation to label a queue, not to // decide anything — would then read as "this operation has no lane at all". writeSettings({ diarization: { enabled: false } }); assert.equal(laneForOperation("diarization")?.queueKey, BACKFILL_QUEUE); });