import { test } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import type { AddressInfo } from "node:net"; import type { Worker } from "../lib/workers"; import { cachedRemoteSlots, clearRemoteCapacityCache, probeRemoteCapacity, remoteCapacityStale, } from "./remoteCapacity"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/remoteCapacity.test.ts // // Probes run against a real node:http stub on a loopback port — no network, no // mocking of fetch. What is pinned: the count is enabled AND non-degraded // workers, a dead remote caches null (so it is not re-probed every // reconfigure), and a remote with nothing enabled still counts as one slot // rather than vanishing from the pool. function remoteWorker(id: string, baseUrl: string, token?: string): Worker { return { id, name: id, kind: "remote", enabled: true, priority: 0, remote: { baseUrl, ...(token ? { token } : {}) }, }; } async function withHealthServer( handler: http.RequestListener, fn: (baseUrl: string) => Promise, ): Promise { const server = http.createServer(handler); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve), ); const { port } = server.address() as AddressInfo; try { await fn(`http://127.0.0.1:${port}`); } finally { await new Promise((resolve) => server.close(() => resolve())); } } function healthBody(workers: Array<{ state: string; degraded: boolean }>) { return JSON.stringify({ ok: true, paused: false, workers }); } test("probeRemoteCapacity counts enabled, non-degraded workers", async () => { clearRemoteCapacityCache(); let auth: string | undefined; await withHealthServer( (req, res) => { auth = req.headers.authorization; res.setHeader("content-type", "application/json"); res.end( healthBody([ { state: "enabled", degraded: false }, { state: "enabled", degraded: true }, { state: "disabled", degraded: false }, { state: "enabled", degraded: false }, ]), ); }, async (baseUrl) => { const worker = remoteWorker("box", baseUrl, "sekrit"); assert.equal(remoteCapacityStale("box"), true); const slots = await probeRemoteCapacity(worker); assert.equal(slots, 2); assert.equal(auth, "Bearer sekrit"); assert.equal(cachedRemoteSlots("box"), 2); assert.equal(remoteCapacityStale("box"), false); }, ); }); test("an unreachable remote caches the failure instead of re-probing", async () => { clearRemoteCapacityCache(); // Nothing listens here; the connection is refused immediately. const worker = remoteWorker("dead", "http://127.0.0.1:59599"); assert.equal(await probeRemoteCapacity(worker), null); assert.equal(cachedRemoteSlots("dead"), null); // The failure IS cached: not stale, so reconfigure won't fire another probe // inside the TTL window. assert.equal(remoteCapacityStale("dead"), false); }); test("a remote with zero enabled workers still counts as one slot", async () => { clearRemoteCapacityCache(); await withHealthServer( (_req, res) => { res.setHeader("content-type", "application/json"); res.end(healthBody([{ state: "disabled", degraded: false }])); }, async (baseUrl) => { // A configured remote must never silently vanish from the pool; the // health ping in transcribeOne is what handles a genuinely-down remote. assert.equal( await probeRemoteCapacity(remoteWorker("idlebox", baseUrl)), 1, ); }, ); }); test("concurrent probes for one worker share a single request", async () => { clearRemoteCapacityCache(); let hits = 0; await withHealthServer( (_req, res) => { hits++; res.setHeader("content-type", "application/json"); res.end(healthBody([{ state: "enabled", degraded: false }])); }, async (baseUrl) => { const worker = remoteWorker("shared", baseUrl); const [a, b] = await Promise.all([ probeRemoteCapacity(worker), probeRemoteCapacity(worker), ]); assert.equal(a, 1); assert.equal(b, 1); assert.equal(hits, 1); }, ); });