import { test } from "node:test"; import assert from "node:assert/strict"; import { gpuBusyFor, gpuGatedFetch, gpuHolds, holdGpu, waitForGpu, withGpu } from "./gpuArbiter"; const none = { tts: 0, "draft-local": 0 } as const; test("the rules: TTS waits for transcription and a draft call; a draft waits for transcription and a synthesis", () => { const off = () => false; const on = () => true; assert.deepEqual(gpuBusyFor("tts", { transcriptionOnGpu: off, holds: none }), { busy: false, reason: null }); assert.equal(gpuBusyFor("tts", { transcriptionOnGpu: on, holds: none }).reason, "a transcription is running"); assert.equal(gpuBusyFor("tts", { transcriptionOnGpu: off, holds: { ...none, "draft-local": 1 } }).reason, "a local model is drafting"); assert.deepEqual(gpuBusyFor("draft-local", { transcriptionLane: off, holds: none }), { busy: false, reason: null }); assert.equal(gpuBusyFor("draft-local", { transcriptionLane: on, holds: none }).reason, "a transcription is running"); assert.equal(gpuBusyFor("draft-local", { transcriptionLane: off, holds: { ...none, tts: 1 } }).reason, "a synthesis is running"); // Neither waits for itself. assert.equal(gpuBusyFor("tts", { transcriptionOnGpu: off, holds: { ...none, tts: 2 } }).busy, false); assert.equal(gpuBusyFor("draft-local", { transcriptionLane: off, holds: { ...none, "draft-local": 1 } }).busy, false); }); test("holds count, and a release is idempotent", () => { const a = holdGpu("tts"); const b = holdGpu("tts"); assert.equal(gpuHolds().tts, 2); a(); a(); assert.equal(gpuHolds().tts, 1); b(); assert.equal(gpuHolds().tts, 0); }); test("waitForGpu waits to start, logs why, and stops on a cancel", async () => { let n = 0; const lines: string[] = []; const waited = await waitForGpu({ busy: () => ({ busy: n++ < 2, reason: "a synthesis is running" }), onLog: (l) => lines.push(l), pollMs: 1 }); assert.ok(waited >= 0); assert.match(lines[0]!, /Waiting for the GPU: a synthesis is running/); assert.match(lines[1]!, /The GPU is free/); const ctrl = new AbortController(); ctrl.abort(); await assert.rejects(waitForGpu({ busy: () => true, onLog: () => {}, signal: ctrl.signal, pollMs: 1 }), /cancelled while waiting/); }); test("a draft call holds the GPU while in flight — a synthesis sees it — and the unload is never held", async () => { let seenDuring = -1; const base = (async (url: string) => { seenDuring = gpuHolds()["draft-local"]; assert.equal(gpuBusyFor("tts", { transcriptionOnGpu: () => false }).busy, url.endsWith("/api/chat")); return new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }); }) as unknown as typeof fetch; const f = gpuGatedFetch(base, { onLog: () => {}, busy: () => false }); const res = await f("http://127.0.0.1:1/api/chat", { method: "POST" }); assert.deepEqual(await res.json(), { ok: true }); assert.equal(seenDuring, 1); assert.equal(gpuHolds()["draft-local"], 0, "released once the answer is in hand"); await f("http://127.0.0.1:1/api/generate", { method: "POST" }); assert.equal(seenDuring, 0, "the unload is not held"); // A draft call waits out a synthesis. const release = holdGpu("tts"); let started = false; const p = withGpu("draft-local", async () => { started = true; }, { onLog: () => {}, pollMs: 2, busy: () => gpuBusyFor("draft-local", { transcriptionLane: () => false }) }); await new Promise((r) => setTimeout(r, 15)); assert.equal(started, false); release(); await p; assert.equal(started, true); });