// The Runpod adapter against sample responses, never against the live API. // The fixtures in test/fixtures/runpod/ are the examples Runpod's REST API v2 reference prints, copied on // 29 November 2026. They are not recordings of live calls: // catalog-gpus.json https://docs.runpod.io/api-reference-v2/catalog/list-gpu-types (200 example) // pod-created.json https://docs.runpod.io/api-reference-v2/pods/create-a-pod (201 example) // pod-running.json https://docs.runpod.io/api-reference-v2/pods/get-a-pod (200 example) // pods-list.json https://docs.runpod.io/api-reference-v2/pods/list-pods (200 example) // error-*.json the shared error examples of those pages // gpu-types-reference.json the seven rows Idle uses from https://docs.runpod.io/references/gpu-types // Where a test needs a response the reference gives no example of (a second GPU, an exited pod), it is built // here from those examples and says so. import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { DISK_GB, GPU_TYPES, RUNPOD_API, RUNPOD_IMAGE, createRunpodProvider } from "../lib/provider-runpod.mjs"; import { ProviderError, checkProvider } from "../lib/provider.mjs"; import { createMockProvider } from "../lib/provider-mock.mjs"; import { GPUS } from "../lib/rates.mjs"; const fixture = (name) => JSON.parse(readFileSync(new URL("./fixtures/runpod/" + name, import.meta.url), "utf8")); const catalog = fixture("catalog-gpus.json"); const created = fixture("pod-created.json"); const runningPod = fixture("pod-running.json"); const podList = fixture("pods-list.json"); const reference = fixture("gpu-types-reference.json"); /** A stand-in for fetch. `routes` maps "METHOD /path" (or a function) to {status, json}. */ function api(routes) { const calls = []; const fetch = async (url, init) => { const path = url.replace(RUNPOD_API, ""); calls.push({ method: init.method, path, headers: init.headers, body: init.body ? JSON.parse(init.body) : null }); const key = `${init.method} ${path}`; const hit = typeof routes === "function" ? routes(key, calls.length) : routes[key]; if (!hit) throw new Error("unexpected call " + key); if (hit.throws) throw new Error(hit.throws); return { status: hit.status, json: async () => (hit.status === 204 ? Promise.reject(new Error("no body")) : hit.json) }; }; return { fetch, calls }; } const provider = (routes, over = {}) => { const a = api(routes); return { p: createRunpodProvider({ apiKey: "test-key-never-sent-anywhere", fetch: a.fetch, probe: async () => true, ...over }), calls: a.calls }; }; const CATALOG = "GET /catalog/gpus?include=AVAILABILITY&product=POD&cloud=SECURE"; describe("the Runpod adapter", () => { test("is a provider, and says its machines are real", () => { const { p } = provider({}); assert.equal(checkProvider(p), p); assert.equal(p.name, "runpod"); assert.equal(p.real, true); assert.throws(() => createRunpodProvider({}), /RUNPOD_API_KEY/); assert.throws(() => checkProvider({ name: "x", real: true }), /needs listGpus/); }); test("every GPU of the rate card maps to the Runpod type of the same name and memory", () => { assert.deepEqual(Object.keys(GPU_TYPES), GPUS.map((g) => g.id)); for (const g of GPUS) { const row = reference.find((r) => r.id === GPU_TYPES[g.id]); assert.ok(row, g.id + " has a row in Runpod's GPU table"); assert.equal(row.name, g.name); assert.equal(row.memory, g.vram); } }); test("lists prices from the catalogue: the secure-cloud price plus the disk, with the bearer key", async () => { const { p, calls } = provider({ [CATALOG]: { status: 200, json: catalog } }); const gpus = await p.listGpus(); assert.equal(calls[0].headers.Authorization, "Bearer test-key-never-sent-anywhere"); // The documentation's example lists one GPU; the six others are simply absent, not invented. assert.deepEqual(gpus, [{ id: "rtx-4090", name: "RTX 4090", vramGb: 24, usdPerHour: 0.446850, available: 1, stock: "high" }]); assert.ok(gpus[0].usdPerHour > 0.44 && gpus[0].usdPerHour < 0.45, "0.44 for the GPU and 50 GB of disk at 0.10 USD per GB per month"); }); test("reads stock levels, and a GPU with none or off the secure cloud is not available", async () => { // Built from the documented example and schema (AvailabilityLevel is NONE, LOW, MEDIUM or HIGH). const base = catalog.gpus[0]; const row = (id, over) => ({ ...base, ...reference.find((r) => r.id === id), ...over }); const json = { gpus: [ row("NVIDIA GeForce RTX 4090", { availability: "LOW" }), row("NVIDIA H100 80GB HBM3", { availability: "NONE", price: { secure: 2.39, community: 1.99 } }), row("NVIDIA L40S", { secure: false, availability: "HIGH", price: { secure: 0.79, community: 0.69 } }), row("NVIDIA B200", { availability: "MEDIUM", price: { secure: 0, community: 0 } }), { ...base, id: "NVIDIA A40", name: "A40", memory: 48 }, // not on Idle's rate card ], }; const { p } = provider({ [CATALOG]: { status: 200, json } }); assert.deepEqual((await p.listGpus()).map((g) => [g.id, g.available, g.stock]), [["rtx-4090", 1, "low"], ["l40s", 0, "none"], ["h100", 0, "none"]]); }); test("creates one pod on the secure cloud with the renter's keys and a published SSH port", async () => { const { p, calls } = provider({ "POST /pods": { status: 201, json: created } }); const keys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA", "ssh-rsa AAAAB3NzaC1yc2E"]; assert.deepEqual(await p.create({ gpuId: "rtx-4090", sshPublicKeys: keys, name: "idle-i0007" }), { providerId: "7h9k2m4n6p" }); assert.deepEqual(calls[0].body, { name: "idle-i0007", image: RUNPOD_IMAGE, cloud: "SECURE", gpu: { id: "NVIDIA GeForce RTX 4090", count: 1 }, disk: DISK_GB, ports: ["22/tcp"], env: { PUBLIC_KEY: keys.join("\n"), SSH_PUBLIC_KEY: keys.join("\n") }, startSsh: true, }); assert.equal(calls[0].headers["Content-Type"], "application/json"); assert.equal(RUNPOD_IMAGE, created.image, "the image is the one the reference uses in its examples"); await assert.rejects(p.create({ gpuId: "gtx-1080", sshPublicKeys: keys, name: "x" }), ProviderError); await assert.rejects(p.create({ gpuId: "rtx-4090", sshPublicKeys: [], name: "x" }), ProviderError); assert.equal(calls.length, 1, "nothing was sent for the two refusals"); }); test("turns Runpod's refusals into codes the API can act on", async () => { const problem = (status, detail) => ({ status, json: { title: "x", status, detail } }); const code = async (hit) => { const { p } = provider({ "POST /pods": hit }); try { await p.create({ gpuId: "h100", sshPublicKeys: ["ssh-ed25519 AAAA"], name: "idle-i1" }); } catch (e) { assert.ok(e instanceof ProviderError); assert.ok(!e.message.includes("balance") && !e.message.includes("secret"), "the message for users says nothing of the provider account"); return e.code; } return "no error"; }; assert.equal(await code(problem(400, "no instances available for the requested GPU")), "no-capacity"); assert.equal(await code(problem(402, "insufficient balance")), "provider-funds"); assert.equal(await code({ status: 401, json: fixture("error-unauthorized.json") }), "provider-auth"); assert.equal(await code(problem(403, "access denied")), "provider-auth"); assert.equal(await code(problem(409, "GPU concurrency limit")), "no-capacity"); assert.equal(await code({ status: 429, json: fixture("error-rate-limited.json") }), "rate-limited"); assert.equal(await code(problem(500, "boom")), "provider"); assert.equal(await code({ throws: "socket hang up" }), "provider"); assert.equal(await code({ status: 201, json: {} }), "provider"); }); test("a pod is starting until it runs with a direct SSH address that answers", async () => { const get = async (json, probe = async () => true) => provider({ "GET /pods/7h9k2m4n6p": { status: 200, json } }, { probe }).p.get("7h9k2m4n6p"); // The 201 example: PROVISIONING, no direct address yet. assert.deepEqual(await get(created), { status: "starting", ssh: null, startedAt: null }); assert.equal((await get({ ...created, status: "STARTING" })).status, "starting"); // The list example shape: RUNNING but `direct` still null (the public port is not assigned yet). assert.equal((await get(podList.pods[0])).status, "starting"); // The 200 example: RUNNING with a direct address. The proxy address is never used. const probed = []; const up = await get(runningPod, async (host, port) => (probed.push([host, port]), true)); assert.deepEqual(up, { status: "running", ssh: { host: "195.26.233.3", port: 34446, user: "root" }, startedAt: Date.parse("2026-06-01T12:02:00Z") }); assert.deepEqual(probed, [["195.26.233.3", 34446]]); assert.ok(!JSON.stringify(up).includes("ssh.runpod.io")); // Same pod, but nothing answers on its SSH port yet: not ready, so not billed. assert.equal((await get(runningPod, async () => false)).status, "starting"); }); test("a pod that exited, errored, was terminated or is unknown is gone", async () => { const { p } = provider({ "GET /pods/nope": { status: 404, json: fixture("error-not-found.json") } }); assert.deepEqual(await p.get("nope"), { status: "gone", ssh: null, startedAt: null }); for (const status of ["EXITED", "ERROR", "TERMINATED"]) { const { p: q } = provider({ "GET /pods/7h9k2m4n6p": { status: 200, json: { ...runningPod, status } } }); assert.equal((await q.get("7h9k2m4n6p")).status, "gone", status); } const { p: down } = provider({ "GET /pods/x": { status: 500, json: {} } }); await assert.rejects(down.get("x"), (e) => e.code === "provider", "an error is not a verdict: the pod is not declared gone"); }); test("terminating deletes the pod; one that is already gone is not an error", async () => { const { p, calls } = provider({ "DELETE /pods/7h9k2m4n6p": { status: 204 }, "DELETE /pods/nope": { status: 404, json: fixture("error-not-found.json") }, "DELETE /pods/stuck": { status: 409, json: { title: "Conflict", status: 409, detail: "cluster" } } }); await p.terminate("7h9k2m4n6p"); await p.terminate("nope"); await assert.rejects(p.terminate("stuck"), ProviderError); assert.deepEqual(calls.map((c) => c.method + " " + c.path), ["DELETE /pods/7h9k2m4n6p", "DELETE /pods/nope", "DELETE /pods/stuck"]); }); test("lists every pod of the account, following the cursor", async () => { const one = provider({ "GET /pods?limit=1000": { status: 200, json: podList } }); assert.deepEqual(await one.p.list(), [{ providerId: "7h9k2m4n6p", name: "pytorch-training", createdAt: Date.parse("2026-06-01T12:00:00Z") }]); // Two pages, built from the same example with the documented pagination block. const second = { pods: [{ ...podList.pods[0], id: "second", name: "idle-i0002" }], pagination: { nextCursor: null, hasNextPage: false } }; const two = provider({ "GET /pods?limit=1000": { status: 200, json: { ...podList, pagination: { nextCursor: "Y3Vyc29y", hasNextPage: true } } }, "GET /pods?limit=1000&cursor=Y3Vyc29y": { status: 200, json: second }, }); assert.deepEqual((await two.p.list()).map((x) => x.providerId), ["7h9k2m4n6p", "second"]); }); }); describe("the mock provider", () => { test("is a provider that owns nothing, and refuses to exist on a deployment", () => { const p = createMockProvider({ env: {} }); assert.equal(checkProvider(p), p); assert.equal(p.real, false); assert.throws(() => createMockProvider({ env: { VERCEL: "1" } }), /does not start on a deployment/); assert.throws(() => createMockProvider({ env: { NODE_ENV: "production" } }), /does not start on a deployment/); }); test("answers from its clock alone: starting, then running, then gone", async () => { let t = 1_000_000; const p = createMockProvider({ env: {}, now: () => t, readyAfterMs: 30_000, neverReady: ["b200"] }); assert.deepEqual((await p.listGpus()).map((g) => g.id), GPUS.map((g) => g.id)); const { providerId } = await p.create({ gpuId: "h100", sshPublicKeys: ["ssh-ed25519 AAAA"], name: "idle-i1" }); assert.equal(providerId, "mock-0001"); assert.equal((await p.get(providerId)).status, "starting"); t += 29_999; assert.equal((await p.get(providerId)).status, "starting"); t += 1; assert.deepEqual(await p.get(providerId), { status: "running", ssh: { host: "127.0.0.1", port: 22001, user: "root" }, startedAt: 1_030_000 }); assert.deepEqual(await p.list(), [{ providerId, name: "idle-i1", createdAt: 1_000_000 }]); await p.terminate(providerId); await p.terminate(providerId); assert.equal((await p.get(providerId)).status, "gone"); assert.deepEqual(await p.list(), []); const slow = await p.create({ gpuId: "b200", sshPublicKeys: ["ssh-ed25519 AAAA"], name: "idle-i2" }); t += 10_000_000; assert.equal((await p.get(slow.providerId)).status, "starting"); p.failCreate = "no-capacity"; await assert.rejects(p.create({ gpuId: "h100", sshPublicKeys: ["k"], name: "x" }), (e) => e.code === "no-capacity"); }); });