|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + ControlPlaneCache, |
| 4 | + type ResolvedAuthenticatedEnv, |
| 5 | + type ResolvedEnv, |
| 6 | + type ResolvedRunLockedWorker, |
| 7 | + type ResolvedWorkerVersion, |
| 8 | +} from "./controlPlaneCache.server"; |
| 9 | + |
| 10 | +// Minimal, structurally-irrelevant stand-ins: the cache stores and returns opaque values by |
| 11 | +// reference, so these only need to be distinguishable objects — the slot types are exercised for |
| 12 | +// key routing, not field shape. |
| 13 | +const anEnv = { id: "env_1" } as unknown as ResolvedEnv; |
| 14 | +const aVersion = { worker: { id: "bw_1" } } as unknown as ResolvedWorkerVersion; |
| 15 | +const anAuthEnv = { id: "env_1", slug: "prod" } as unknown as ResolvedAuthenticatedEnv; |
| 16 | +const aLockedWorker = { lockedBy: null, lockedToVersion: null } as ResolvedRunLockedWorker; |
| 17 | + |
| 18 | +describe("ControlPlaneCache", () => { |
| 19 | + it("round-trips a value through every slot", () => { |
| 20 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 21 | + |
| 22 | + cache.setEnv("env_1", anEnv); |
| 23 | + cache.setWorkerVersion("env_1:current", aVersion); |
| 24 | + cache.setEnvExists("env_1", true); |
| 25 | + cache.setAuthEnv("env_1", anAuthEnv); |
| 26 | + cache.setLockedWorker("bw_1:v_1", aLockedWorker); |
| 27 | + |
| 28 | + expect(cache.getEnv("env_1")).toBe(anEnv); |
| 29 | + expect(cache.getWorkerVersion("env_1:current")).toBe(aVersion); |
| 30 | + expect(cache.getEnvExists("env_1")).toBe(true); |
| 31 | + expect(cache.getAuthEnv("env_1")).toBe(anAuthEnv); |
| 32 | + expect(cache.getLockedWorker("bw_1:v_1")).toBe(aLockedWorker); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns undefined for a key that was never set, in every slot", () => { |
| 36 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 37 | + |
| 38 | + expect(cache.getEnv("missing")).toBeUndefined(); |
| 39 | + expect(cache.getWorkerVersion("missing")).toBeUndefined(); |
| 40 | + expect(cache.getEnvExists("missing")).toBeUndefined(); |
| 41 | + expect(cache.getAuthEnv("missing")).toBeUndefined(); |
| 42 | + expect(cache.getLockedWorker("missing")).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("distinguishes a cached null (confirmed absence) from an unset miss", () => { |
| 46 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 47 | + |
| 48 | + expect(cache.getEnv("env_2")).toBeUndefined(); |
| 49 | + cache.setEnv("env_2", null); |
| 50 | + expect(cache.getEnv("env_2")).toBeNull(); |
| 51 | + |
| 52 | + expect(cache.getAuthEnv("env_2")).toBeUndefined(); |
| 53 | + cache.setAuthEnv("env_2", null); |
| 54 | + expect(cache.getAuthEnv("env_2")).toBeNull(); |
| 55 | + |
| 56 | + expect(cache.getWorkerVersion("env_2:current")).toBeUndefined(); |
| 57 | + cache.setWorkerVersion("env_2:current", null); |
| 58 | + expect(cache.getWorkerVersion("env_2:current")).toBeNull(); |
| 59 | + |
| 60 | + expect(cache.getLockedWorker("_:_")).toBeUndefined(); |
| 61 | + cache.setLockedWorker("_:_", null); |
| 62 | + expect(cache.getLockedWorker("_:_")).toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("caches a false env-existence result distinctly from an unset miss", () => { |
| 66 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 67 | + |
| 68 | + expect(cache.getEnvExists("env_3")).toBeUndefined(); |
| 69 | + cache.setEnvExists("env_3", false); |
| 70 | + expect(cache.getEnvExists("env_3")).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("invalidateEnv forces the next getEnv to miss", () => { |
| 74 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 75 | + |
| 76 | + cache.setEnv("env_4", anEnv); |
| 77 | + expect(cache.getEnv("env_4")).toBe(anEnv); |
| 78 | + |
| 79 | + cache.invalidateEnv("env_4"); |
| 80 | + expect(cache.getEnv("env_4")).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("makes a re-setEnv after invalidation readable again", () => { |
| 84 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 85 | + const replacement = { id: "env_5b" } as unknown as ResolvedEnv; |
| 86 | + |
| 87 | + cache.setEnv("env_5", anEnv); |
| 88 | + cache.invalidateEnv("env_5"); |
| 89 | + expect(cache.getEnv("env_5")).toBeUndefined(); |
| 90 | + |
| 91 | + cache.setEnv("env_5", replacement); |
| 92 | + expect(cache.getEnv("env_5")).toBe(replacement); |
| 93 | + }); |
| 94 | + |
| 95 | + it("invalidateEnv is scoped to its own id", () => { |
| 96 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 97 | + const other = { id: "env_keep" } as unknown as ResolvedEnv; |
| 98 | + |
| 99 | + cache.setEnv("env_drop", anEnv); |
| 100 | + cache.setEnv("env_keep", other); |
| 101 | + cache.invalidateEnv("env_drop"); |
| 102 | + |
| 103 | + expect(cache.getEnv("env_drop")).toBeUndefined(); |
| 104 | + expect(cache.getEnv("env_keep")).toBe(other); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not collide keys across slots for the same id", () => { |
| 108 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 100 }); |
| 109 | + |
| 110 | + cache.setEnv("x", anEnv); |
| 111 | + cache.setEnvExists("x", true); |
| 112 | + cache.setAuthEnv("x", anAuthEnv); |
| 113 | + |
| 114 | + expect(cache.getEnv("x")).toBe(anEnv); |
| 115 | + expect(cache.getEnvExists("x")).toBe(true); |
| 116 | + expect(cache.getAuthEnv("x")).toBe(anAuthEnv); |
| 117 | + |
| 118 | + // Invalidating the env slot leaves the sibling slots for the same id intact. |
| 119 | + cache.invalidateEnv("x"); |
| 120 | + expect(cache.getEnv("x")).toBeUndefined(); |
| 121 | + expect(cache.getEnvExists("x")).toBe(true); |
| 122 | + expect(cache.getAuthEnv("x")).toBe(anAuthEnv); |
| 123 | + }); |
| 124 | + |
| 125 | + it("evicts the oldest entry once maxEntries is exceeded", () => { |
| 126 | + const cache = new ControlPlaneCache({ ttlMs: 60_000, maxEntries: 2 }); |
| 127 | + |
| 128 | + cache.setEnv("first", { id: "first" } as unknown as ResolvedEnv); |
| 129 | + cache.setEnv("second", { id: "second" } as unknown as ResolvedEnv); |
| 130 | + cache.setEnv("third", { id: "third" } as unknown as ResolvedEnv); |
| 131 | + |
| 132 | + expect(cache.getEnv("first")).toBeUndefined(); |
| 133 | + expect(cache.getEnv("second")).toMatchObject({ id: "second" }); |
| 134 | + expect(cache.getEnv("third")).toMatchObject({ id: "third" }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("treats a zero-TTL entry as immediately expired", () => { |
| 138 | + const cache = new ControlPlaneCache({ ttlMs: 0, maxEntries: 100 }); |
| 139 | + |
| 140 | + cache.setEnv("env_ttl", anEnv); |
| 141 | + expect(cache.getEnv("env_ttl")).toBeUndefined(); |
| 142 | + }); |
| 143 | +}); |
0 commit comments