87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import {
|
|
getRequestCacheStoreResolution,
|
|
resolveRequestCacheStore,
|
|
} from "@/external/storage/cache/resolveRequestCacheStore.js";
|
|
|
|
const fakeRedis = {
|
|
status: "ready",
|
|
get: async () => null,
|
|
set: async () => "OK",
|
|
del: async () => 0,
|
|
};
|
|
|
|
const fakeKv = {
|
|
get: async () => null,
|
|
put: async () => undefined,
|
|
delete: async () => undefined,
|
|
};
|
|
|
|
describe("resolveRequestCacheStore", () => {
|
|
test("uses memory as the default request cache store", () => {
|
|
const store = resolveRequestCacheStore({
|
|
env: {} as Env,
|
|
});
|
|
|
|
expect(store.metadata.backend).toBe("memory");
|
|
expect(store.metadata.consistencyMode).toBe("strong");
|
|
});
|
|
|
|
test("uses Redis only when a legacy caller explicitly supplies it", () => {
|
|
const store = resolveRequestCacheStore({
|
|
env: {} as Env,
|
|
redis: fakeRedis as never,
|
|
});
|
|
|
|
expect(store.metadata.backend).toBe("redis");
|
|
expect(store.metadata.consistencyMode).toBe("strong");
|
|
});
|
|
|
|
test("uses KV only when the binding exists and USE_KV_CACHE is enabled", () => {
|
|
const store = resolveRequestCacheStore({
|
|
env: {
|
|
CACHE_KV: fakeKv,
|
|
USE_KV_CACHE: "true",
|
|
} as unknown as Env,
|
|
redis: fakeRedis as never,
|
|
});
|
|
|
|
expect(store.metadata.backend).toBe("kv");
|
|
expect(store.metadata.consistencyMode).toBe("eventual");
|
|
});
|
|
|
|
test("uses explicit Redis when USE_KV_CACHE is enabled without a binding", () => {
|
|
const store = resolveRequestCacheStore({
|
|
env: {
|
|
USE_KV_CACHE: "true",
|
|
} as unknown as Env,
|
|
redis: fakeRedis as never,
|
|
});
|
|
|
|
expect(store.metadata.backend).toBe("redis");
|
|
});
|
|
|
|
test("reports cache store resolution for ready checks", () => {
|
|
expect(
|
|
getRequestCacheStoreResolution({
|
|
CACHE_KV: fakeKv,
|
|
USE_KV_CACHE: "true",
|
|
} as unknown as Env),
|
|
).toEqual({
|
|
backend: "kv",
|
|
kvEnabled: true,
|
|
kvBound: true,
|
|
});
|
|
|
|
expect(
|
|
getRequestCacheStoreResolution({
|
|
USE_KV_CACHE: "true",
|
|
} as unknown as Env),
|
|
).toEqual({
|
|
backend: "memory",
|
|
kvEnabled: true,
|
|
kvBound: false,
|
|
});
|
|
});
|
|
});
|