|
| 1 | +/** |
| 2 | + * Standalone test for the deduped cache that backs brand-logo lookups. This is the |
| 3 | + * credit-burn safeguard from issue #11, so we prove — without touching the network — |
| 4 | + * that repeated/concurrent requests never fan out into repeated loads. |
| 5 | + * |
| 6 | + * Run: npx tsx scripts/test-deduped-cache.ts |
| 7 | + */ |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import { createDedupedCache } from "../src/lib/deduped-cache"; |
| 10 | + |
| 11 | +let passed = 0; |
| 12 | +async function test(name: string, fn: () => Promise<void> | void) { |
| 13 | + await fn(); |
| 14 | + passed++; |
| 15 | + console.log(` ok ${name}`); |
| 16 | +} |
| 17 | + |
| 18 | +/** A loader that counts how many times each key was actually loaded. */ |
| 19 | +function countingLoader<V>(map: Record<string, V>, delayMs = 0) { |
| 20 | + const calls: Record<string, number> = {}; |
| 21 | + const load = async (key: string): Promise<V> => { |
| 22 | + calls[key] = (calls[key] ?? 0) + 1; |
| 23 | + if (delayMs) await new Promise((r) => setTimeout(r, delayMs)); |
| 24 | + return map[key]; |
| 25 | + }; |
| 26 | + return { load, calls }; |
| 27 | +} |
| 28 | + |
| 29 | +async function main() { |
| 30 | + console.log("deduped-cache"); |
| 31 | + |
| 32 | + await test("concurrent requests for one key load it exactly once", async () => { |
| 33 | + const cache = createDedupedCache<string>({ ttlMs: 10_000 }); |
| 34 | + const { load, calls } = countingLoader({ "openai.com": "logo-a" }, 20); |
| 35 | + const results = await Promise.all( |
| 36 | + Array.from({ length: 100 }, () => cache.get("openai.com", load)), |
| 37 | + ); |
| 38 | + assert.equal(calls["openai.com"], 1, "expected a single load for 100 concurrent callers"); |
| 39 | + assert.ok(results.every((r) => r === "logo-a"), "all callers get the same value"); |
| 40 | + }); |
| 41 | + |
| 42 | + await test("repeated sequential requests within TTL never reload (refresh/HMR case)", async () => { |
| 43 | + let clock = 0; |
| 44 | + const cache = createDedupedCache<string>({ ttlMs: 10_000, now: () => clock }); |
| 45 | + const { load, calls } = countingLoader({ "stripe.com": "logo-b" }); |
| 46 | + // Simulate 50 page renders / hot-reloads over 5 seconds. |
| 47 | + for (let i = 0; i < 50; i++) { |
| 48 | + clock += 100; |
| 49 | + const v = await cache.get("stripe.com", load); |
| 50 | + assert.equal(v, "logo-b"); |
| 51 | + } |
| 52 | + assert.equal(calls["stripe.com"], 1, "50 renders must cost exactly one load"); |
| 53 | + }); |
| 54 | + |
| 55 | + await test("value is reloaded only after the TTL expires", async () => { |
| 56 | + let clock = 0; |
| 57 | + const cache = createDedupedCache<string>({ ttlMs: 1_000, now: () => clock }); |
| 58 | + const { load, calls } = countingLoader({ "vercel.com": "logo-c" }); |
| 59 | + await cache.get("vercel.com", load); |
| 60 | + clock += 500; // still fresh |
| 61 | + await cache.get("vercel.com", load); |
| 62 | + assert.equal(calls["vercel.com"], 1, "no reload before TTL"); |
| 63 | + clock += 1_000; // now expired |
| 64 | + await cache.get("vercel.com", load); |
| 65 | + assert.equal(calls["vercel.com"], 2, "one reload after TTL"); |
| 66 | + }); |
| 67 | + |
| 68 | + await test("distinct keys each load once; a mixed burst is deduped per key", async () => { |
| 69 | + const cache = createDedupedCache<string>({ ttlMs: 10_000 }); |
| 70 | + const domains = ["a.com", "b.com", "c.com"]; |
| 71 | + const { load, calls } = countingLoader({ "a.com": "1", "b.com": "2", "c.com": "3" }, 10); |
| 72 | + // 30 callers spread across 3 keys, all at once. |
| 73 | + await Promise.all( |
| 74 | + Array.from({ length: 30 }, (_, i) => cache.get(domains[i % 3], load)), |
| 75 | + ); |
| 76 | + assert.deepEqual(calls, { "a.com": 1, "b.com": 1, "c.com": 1 }); |
| 77 | + assert.equal(cache.size, 3); |
| 78 | + }); |
| 79 | + |
| 80 | + await test("failed loads are not cached and are retried on the next request", async () => { |
| 81 | + const cache = createDedupedCache<string>({ ttlMs: 10_000 }); |
| 82 | + let attempts = 0; |
| 83 | + const flaky = async () => { |
| 84 | + attempts++; |
| 85 | + if (attempts === 1) throw new Error("transient"); |
| 86 | + return "recovered"; |
| 87 | + }; |
| 88 | + await assert.rejects(() => cache.get("hn.com", flaky), /transient/); |
| 89 | + const v = await cache.get("hn.com", flaky); // should retry, not serve a poisoned entry |
| 90 | + assert.equal(v, "recovered"); |
| 91 | + assert.equal(attempts, 2, "error must not be cached"); |
| 92 | + assert.equal(cache.size, 1); |
| 93 | + }); |
| 94 | + |
| 95 | + await test("null results are cached (a domain with no logo isn't refetched)", async () => { |
| 96 | + const cache = createDedupedCache<string | null>({ ttlMs: 10_000 }); |
| 97 | + let calls = 0; |
| 98 | + const load = async () => { |
| 99 | + calls++; |
| 100 | + return null; |
| 101 | + }; |
| 102 | + assert.equal(await cache.get("nologo.com", load), null); |
| 103 | + assert.equal(await cache.get("nologo.com", load), null); |
| 104 | + assert.equal(calls, 1, "a cached null still counts as a hit"); |
| 105 | + }); |
| 106 | + |
| 107 | + console.log(`\n${passed} tests passed`); |
| 108 | +} |
| 109 | + |
| 110 | +main().catch((err) => { |
| 111 | + console.error("\nTEST FAILED:", err); |
| 112 | + process.exit(1); |
| 113 | +}); |
0 commit comments