import { test } from "node:test"; import assert from "node:assert/strict"; import type { Worker } from "../lib/workers"; import { WorkerPool, type Lease } from "./workerPool"; // RUN WHILE PAUSED (release 36, lane A): a manual acquire with `whilePaused` // may take a worker the pause-all alone switched off — never one drained, // degraded or disabled by hand — and the pause's snapshot is never touched. // // Each test builds a fresh pool seeded with reconfigure(…, {applyEnabled}), // as workerPool.test.ts does, so settings.json is never read. function worker(id: string, priority = 0, enabled = true): Worker { return { id, name: id, kind: "local", enabled, priority }; } function snapshotOf(pool: WorkerPool): string { const snap = (pool as unknown as { pausedSnapshot: Map | null }).pausedSnapshot; return JSON.stringify(snap ? [...snap.entries()] : null); } function stateOf(pool: WorkerPool, id: string) { const w = pool.summary().find((s) => s.id === id); assert.ok(w, id); return { state: w.state, busy: w.busy, degraded: w.degraded }; } // Settles a promise race: the lease if it resolved within a tick, else null. async function settledWithin(p: Promise, ms = 20): Promise { return Promise.race([p, new Promise((r) => setTimeout(() => r(null), ms))]); } test("a paused pool + whilePaused leases a pause-disabled worker; the snapshot is untouched", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("gpu", 0), worker("cpu", 1)], { applyEnabled: true }); pool.pauseAll(); const before = snapshotOf(pool); assert.deepEqual(pool.pauseHeldIds().sort(), ["cpu", "gpu"]); assert.deepEqual(pool.summary().map((w) => [w.id, w.pauseHeld ?? false]), [["gpu", true], ["cpu", true]]); const lease = (await settledWithin(pool.acquire(undefined, { whilePaused: true }))) as Lease | null; assert.ok(lease, "granted at once"); assert.equal(lease.worker.id, "gpu", "the highest-priority pause-held worker"); // Still disabled by the pause while it runs: it never left the paused set. assert.deepEqual(stateOf(pool, "gpu"), { state: "disabled", busy: true, degraded: false }); assert.equal(snapshotOf(pool), before); lease.release(); assert.deepEqual(stateOf(pool, "gpu"), { state: "disabled", busy: false, degraded: false }); assert.equal(snapshotOf(pool), before, "byte-equal after the job"); assert.ok(pool.isPaused()); // And the pause lifts exactly as it would have. pool.resumeAll(); assert.equal(stateOf(pool, "gpu").state, "enabled"); assert.equal(stateOf(pool, "cpu").state, "enabled"); }); test("a worker disabled by hand before the pause is never leased while paused", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("gpu", 0), worker("cpu", 1)], { applyEnabled: true }); pool.disableWorker("gpu"); // by hand, before the pause pool.pauseAll(); assert.deepEqual(pool.pauseHeldIds(), ["cpu"]); const lease = (await settledWithin(pool.acquire(undefined, { whilePaused: true }))) as Lease | null; assert.ok(lease); assert.equal(lease.worker.id, "cpu"); lease.release(); }); test("hand-disabled, drained or degraded workers are never leased, even with the flag", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("a", 0), worker("b", 1), worker("c", 2), worker("off", 3, false)], { applyEnabled: true, }); pool.pauseAll(); pool.disableWorker("a"); // by hand DURING the pause: the snapshot says disabled pool.drainWorker("b"); // drained during the pause pool.markDegraded("c"); const before = snapshotOf(pool); assert.deepEqual(pool.pauseHeldIds(), [], "'off' was disabled in settings, not by the pause"); assert.ok(pool.summary().every((w) => !("pauseHeld" in w)), "the summary marks none"); const ac = new AbortController(); const p = pool.acquire(ac.signal, { whilePaused: true }); assert.equal(await settledWithin(p), null, "parks: nothing it may take"); ac.abort(); await assert.rejects(p, { name: "AbortError" }); assert.equal(snapshotOf(pool), before); }); test("without the flag a paused pool holds the acquire, as today", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("gpu", 0)], { applyEnabled: true }); pool.pauseAll(); const before = snapshotOf(pool); const held = pool.acquire(); const heldBg = pool.acquire(undefined, { background: true }); assert.equal(await settledWithin(held), null); assert.equal(await settledWithin(heldBg), null); // A whilePaused acquire arriving later is served — the held ones stay parked. const lease = (await settledWithin(pool.acquire(undefined, { whilePaused: true }))) as Lease | null; assert.ok(lease, "the flagged acquire runs past the held ones"); lease.release(); assert.equal(await settledWithin(held), null, "still held after the flagged job ended"); assert.equal(await settledWithin(heldBg), null); assert.equal(snapshotOf(pool), before); // Resume releases the held ones in tier order. pool.resumeAll(); const l1 = (await held) as Lease; l1.release(); const l2 = (await heldBg) as Lease; l2.release(); }); test("a parked whilePaused waiter takes the pause-held worker when its lease frees", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("gpu", 0)], { applyEnabled: true }); pool.pauseAll(); const first = (await pool.acquire(undefined, { whilePaused: true })) as Lease; const second = pool.acquire(undefined, { whilePaused: true }); const bg = pool.acquire(undefined, { background: true }); assert.equal(await settledWithin(second), null, "one slot"); first.release(); const l2 = (await settledWithin(second)) as Lease | null; assert.ok(l2, "the flagged waiter, not the background one"); l2.release(); assert.equal(await settledWithin(bg), null, "the lane stays held"); pool.resumeAll(); ((await bg) as Lease).release(); }); test("outside a pause the flag changes nothing", async () => { const pool = new WorkerPool(); pool.reconfigure([worker("gpu", 0), worker("cpu", 1)], { applyEnabled: true }); pool.disableWorker("gpu"); assert.deepEqual(pool.pauseHeldIds(), []); const lease = (await pool.acquire(undefined, { whilePaused: true })) as Lease; assert.equal(lease.worker.id, "cpu", "a hand-disabled worker stays off"); lease.release(); });