Files
cfw-autumn/server/tests/unit/storage/cache/queryWithCacheStore.test.ts
imeepos 4245ab0aed checkpoint: in-progress Cloudflare migration snapshot (non-compiling)
Working-tree checkpoint of the active migration (S3 -> R2 storage,
edge-config stores, blue-green deploy support, cache wiring). Excludes
the pre-commit type-check (--no-verify) because the branch does not yet
typecheck.

Note: the earlier staged KV/Redis/Durable-Object store-abstraction work
is preserved in stash@{0} (wip-cloudflare-migration-diagnostic) and is
NOT included in this commit. Recover with: git stash show -p stash@{0}

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-17 23:25:07 -07:00

57 lines
1.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { MemoryCacheStore } from "@/external/storage/cache/index.js";
import { queryWithCache } from "@/utils/cacheUtils/queryWithCache.js";
describe("queryWithCache", () => {
test("uses the provided CacheStore before falling back to the producer", async () => {
const cacheStore = new MemoryCacheStore();
let calls = 0;
const first = await queryWithCache({
key: "query:test",
cacheStore,
fn: async () => {
calls++;
return { value: "fresh" };
},
});
const second = await queryWithCache({
key: "query:test",
cacheStore,
fn: async () => {
calls++;
return { value: "unexpected" };
},
});
expect(first).toEqual({ value: "fresh" });
expect(second).toEqual({ value: "fresh" });
expect(calls).toBe(1);
});
test("runs the producer directly when no CacheStore is available", async () => {
let calls = 0;
const first = await queryWithCache({
key: "query:no-store",
fn: async () => {
calls++;
return { value: "first" };
},
});
const second = await queryWithCache({
key: "query:no-store",
fn: async () => {
calls++;
return { value: "second" };
},
});
expect(first).toEqual({ value: "first" });
expect(second).toEqual({ value: "second" });
expect(calls).toBe(2);
});
});