import { test } from "node:test"; import assert from "node:assert/strict"; import { WORKER_RESOURCE_TAGS, sanitizeWorkers, unknownWorkerTags, validateWorkers, workerMatches, type Worker, } from "./workers"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/workers.test.ts // // Pins the capability-matching rule (workerMatches) and the tag sanitizer. The // rule is enforced by the worker pool but DECLARED here, and the sweep console // reads it too — so the pinned semantics are: no requirement matches everyone, // an untagged worker matches everything, a tagged worker matches on a // non-empty intersection. test("workerMatches: no requirement matches every worker", () => { assert.equal(workerMatches({ kind: "local" }), true); assert.equal(workerMatches({ kind: "local", tags: ["cpu"] }), true); assert.equal(workerMatches({ kind: "remote", tags: ["gpu"] }, []), true); }); test("workerMatches: an untagged worker matches any requirement", () => { assert.equal(workerMatches({ kind: "local" }, ["diarization"]), true); assert.equal(workerMatches({ kind: "remote", tags: [] }, ["gpu"]), true); }); test("workerMatches: a tagged worker matches on intersection only", () => { const w = { kind: "remote" as const, tags: ["cpu", "diarization"] }; assert.equal(workerMatches(w, ["diarization"]), true); // Any one acceptable qualification suffices. assert.equal(workerMatches(w, ["gpu", "cpu"]), true); assert.equal(workerMatches(w, ["gpu"]), false); assert.equal(workerMatches(w, ["network"]), false); }); test("workerMatches: an llm worker never takes a requirement-less lease", () => { // Today's transcription acquire passes no requirement; an ollama endpoint // handed a transcription job cannot run it. This is the one place the // untagged-is-universal rule inverts. assert.equal(workerMatches({ kind: "llm" }), false); assert.equal(workerMatches({ kind: "llm", tags: [] }, []), false); }); test("workerMatches: an untagged llm worker serves all three LLM operations", () => { assert.equal(workerMatches({ kind: "llm" }, ["digest"]), true); assert.equal(workerMatches({ kind: "llm" }, ["attribution-text"]), true); assert.equal(workerMatches({ kind: "llm" }, ["attribution-diarized"]), true); // …and nothing else. assert.equal(workerMatches({ kind: "llm" }, ["transcription"]), false); assert.equal(workerMatches({ kind: "llm" }, ["diarization"]), false); }); test("workerMatches: tags narrow an llm worker to specific operations", () => { const w = { kind: "llm" as const, tags: ["digest"] }; assert.equal(workerMatches(w, ["digest"]), true); assert.equal(workerMatches(w, ["attribution-text"]), false); }); test("sanitizeWorkers: an llm worker keeps its endpoint config", () => { const [w] = sanitizeWorkers([ { name: "mac", kind: "llm", llm: { baseUrl: " http://mac.lan:11434 ", slots: 2.7 }, }, ]); assert.equal(w.kind, "llm"); assert.equal(w.llm?.baseUrl, "http://mac.lan:11434"); assert.equal(w.llm?.slots, 2); assert.equal(w.remote, undefined); }); test("validateWorkers: an llm-only list cannot satisfy the enabled-worker rule", () => { const [llm] = sanitizeWorkers([ { name: "mac", kind: "llm", llm: { baseUrl: "http://mac.lan:11434" } }, ]); assert.match( validateWorkers([llm]) ?? "", /transcription worker .* must be enabled/i, ); const [local] = sanitizeWorkers([{ name: "cpu", kind: "local" }]); assert.equal(validateWorkers([local, llm]), null); }); test("validateWorkers: an llm worker needs a valid http(s) base URL", () => { const [bad] = sanitizeWorkers([{ name: "mac", kind: "llm", llm: {} }]); const [local] = sanitizeWorkers([{ name: "cpu", kind: "local" }]); assert.match(validateWorkers([local, bad]) ?? "", /needs a base URL/); }); test("sanitizeWorkers trims tags, drops empties, and de-duplicates", () => { const [w] = sanitizeWorkers([ { name: "box", kind: "local", tags: [" cpu ", "", "cpu", "diarization", 7, " "], }, ]); assert.deepEqual(w.tags, ["cpu", "diarization"]); }); test("sanitizeWorkers omits tags entirely when nothing survives", () => { const [w] = sanitizeWorkers([ { name: "box", kind: "local", tags: ["", " "] }, ]); assert.equal(w.tags, undefined); }); test("unknownWorkerTags names only tags outside the vocabulary", () => { const workers: Worker[] = [ { id: "a", name: "a", kind: "local", enabled: true, priority: 0, tags: ["cpu", "diarizatoin"], }, { id: "b", name: "b", kind: "local", enabled: true, priority: 1 }, ]; const known = ["diarization", ...WORKER_RESOURCE_TAGS]; assert.deepEqual(unknownWorkerTags(workers, known), ["diarizatoin"]); assert.deepEqual(unknownWorkerTags(workers, [...known, "diarizatoin"]), []); });