|
| 1 | +import { createHash } from "crypto"; |
| 2 | +import { describe, expect, it, mock } from "bun:test"; |
| 3 | +import type { Node } from "../types"; |
| 4 | + |
| 5 | +process.env.S3_ENDPOINT ??= "http://127.0.0.1:1"; |
| 6 | +process.env.S3_ACCESS_KEY ??= "test"; |
| 7 | +process.env.S3_SECRET_KEY ??= "test"; |
| 8 | +process.env.TOKEN_ENCRYPTION_KEY ??= "0".repeat(64); |
| 9 | +process.env.NODE_ID ??= "test-node"; |
| 10 | +process.env.REST_API_URL ??= "http://127.0.0.1:1"; |
| 11 | +process.env.NODE_API_KEY ??= "test"; |
| 12 | + |
| 13 | +const sha = (s: string) => createHash("sha256").update(s).digest("hex"); |
| 14 | + |
| 15 | +const KEY_A = "command-key-a"; |
| 16 | +const KEY_B = "command-key-b"; |
| 17 | + |
| 18 | +function makeNode(commandKeyHash: string | null): Node { |
| 19 | + return { |
| 20 | + id: "test-node", |
| 21 | + name: "obs-node-1", |
| 22 | + max_instances: 4, |
| 23 | + memory_mb: 32_000, |
| 24 | + cpu_quota: 4, |
| 25 | + vram_mb: 8_000, |
| 26 | + total_vram_mb: 24_000, |
| 27 | + shm_size: "2g", |
| 28 | + gpu_bus_id: "00000000:01:00.0", |
| 29 | + max_encoder_sessions: 5, |
| 30 | + command_key_hash: commandKeyHash, |
| 31 | + created_at: "2026-07-21T00:00:00Z", |
| 32 | + } as Node; |
| 33 | +} |
| 34 | + |
| 35 | +let refreshCalls = 0; |
| 36 | +let cachedCalls = 0; |
| 37 | +let nextNode: Node = makeNode(sha(KEY_A)); |
| 38 | +let failWith: Error | null = null; |
| 39 | + |
| 40 | +// Mock the cache, not the client — the point of these tests is WHICH cache |
| 41 | +// entry point command-key reaches for. |
| 42 | +mock.module("./node-cache", () => ({ |
| 43 | + refreshNode: async () => { |
| 44 | + refreshCalls++; |
| 45 | + if (failWith) throw failWith; |
| 46 | + return nextNode; |
| 47 | + }, |
| 48 | + getCachedNode: async () => { |
| 49 | + cachedCalls++; |
| 50 | + return nextNode; |
| 51 | + }, |
| 52 | + peekCachedNode: () => nextNode, |
| 53 | + resetNodeCache: () => {}, |
| 54 | +})); |
| 55 | + |
| 56 | +const { loadCommandKeyHash, verifyCommandKey, hasCommandKey, retryCommandKeyLoadIfMissing } = |
| 57 | + await import("./command-key"); |
| 58 | + |
| 59 | +/** |
| 60 | + * These tests share module state (`commandKeyHash`, `lastFetchAt`) and run in |
| 61 | + * declaration order, which is load-bearing: the mismatch-refresh path is gated |
| 62 | + * on a 30s cooldown measured from the last SUCCESSFUL fetch, so the only way to |
| 63 | + * exercise it without sleeping 30s is to go first, while `lastFetchAt` is still |
| 64 | + * 0 from a failed load. Do not reorder these blocks. |
| 65 | + */ |
| 66 | + |
| 67 | +describe("command-key, before any successful load", () => { |
| 68 | + it("reports no key and rejects everything", async () => { |
| 69 | + expect(hasCommandKey()).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("a failed load leaves lastFetchAt untouched, so the cooldown stays open", async () => { |
| 73 | + failWith = new Error("rest-api down"); |
| 74 | + await loadCommandKeyHash(); |
| 75 | + |
| 76 | + expect(refreshCalls).toBe(1); |
| 77 | + expect(hasCommandKey()).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + it("picks up a rotated key through the mismatch path", async () => { |
| 81 | + // lastFetchAt is still 0 (the load above failed), so the cooldown has |
| 82 | + // elapsed by definition and a mismatch is allowed to re-fetch. |
| 83 | + failWith = null; |
| 84 | + nextNode = makeNode(sha(KEY_B)); |
| 85 | + const before = refreshCalls; |
| 86 | + |
| 87 | + const ok = await verifyCommandKey(KEY_B); |
| 88 | + |
| 89 | + expect(ok).toBe(true); |
| 90 | + expect(refreshCalls).toBe(before + 1); |
| 91 | + expect(hasCommandKey()).toBe(true); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("command-key, with a warm hash", () => { |
| 96 | + it("accepts the matching key without touching the network", async () => { |
| 97 | + nextNode = makeNode(sha(KEY_B)); |
| 98 | + await loadCommandKeyHash(); |
| 99 | + const before = refreshCalls; |
| 100 | + |
| 101 | + expect(await verifyCommandKey(KEY_B)).toBe(true); |
| 102 | + expect(refreshCalls).toBe(before); |
| 103 | + }); |
| 104 | + |
| 105 | + it("rejects a wrong key", async () => { |
| 106 | + expect(await verifyCommandKey("nope")).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("does not re-fetch on repeated mismatches inside the cooldown", async () => { |
| 110 | + // The /obs route is public, so unauthenticated callers can reach this path. |
| 111 | + // The cooldown is what stops them amplifying into rest-api load. |
| 112 | + await loadCommandKeyHash(); |
| 113 | + const before = refreshCalls; |
| 114 | + |
| 115 | + await verifyCommandKey("bad-1"); |
| 116 | + await verifyCommandKey("bad-2"); |
| 117 | + await verifyCommandKey("bad-3"); |
| 118 | + |
| 119 | + expect(refreshCalls).toBe(before); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("the rotation invariant", () => { |
| 124 | + it("loadCommandKeyHash forces a live read and never accepts a cached one", async () => { |
| 125 | + const refreshBefore = refreshCalls; |
| 126 | + const cachedBefore = cachedCalls; |
| 127 | + |
| 128 | + await loadCommandKeyHash(); |
| 129 | + |
| 130 | + // If this ever flips to getCachedNode, a rotated obs_command key can never |
| 131 | + // reach this node and /obs fails closed permanently. |
| 132 | + expect(refreshCalls).toBe(refreshBefore + 1); |
| 133 | + expect(cachedCalls).toBe(cachedBefore); |
| 134 | + }); |
| 135 | + |
| 136 | + it("survives a node with no command key provisioned", async () => { |
| 137 | + nextNode = makeNode(null); |
| 138 | + await loadCommandKeyHash(); |
| 139 | + |
| 140 | + expect(hasCommandKey()).toBe(false); |
| 141 | + expect(await verifyCommandKey(KEY_B)).toBe(false); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +describe("the retry loop", () => { |
| 146 | + it("keeps fetching while no hash is held", async () => { |
| 147 | + // Carries state from the block above: the last load found no key, so the |
| 148 | + // loop must still be trying — this is the failed-boot-fetch self-heal. |
| 149 | + nextNode = makeNode(sha(KEY_A)); |
| 150 | + const before = refreshCalls; |
| 151 | + |
| 152 | + await retryCommandKeyLoadIfMissing(); |
| 153 | + |
| 154 | + expect(refreshCalls).toBe(before + 1); |
| 155 | + expect(hasCommandKey()).toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it("goes quiet once a hash is held — rotation rides the mismatch path, not polling", async () => { |
| 159 | + const before = refreshCalls; |
| 160 | + |
| 161 | + await retryCommandKeyLoadIfMissing(); |
| 162 | + |
| 163 | + expect(refreshCalls).toBe(before); |
| 164 | + expect(hasCommandKey()).toBe(true); |
| 165 | + }); |
| 166 | +}); |
0 commit comments