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); }); });