Files
cfw-autumn/server/tests/unit/cache/cache-manager.test.ts

67 lines
2.2 KiB
TypeScript

import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "bun:test";
import { MemoryCacheStore } from "@/external/storage/cache/MemoryCacheStore.js";
import { CacheManager } from "@/utils/cacheUtils/CacheManager.js";
const serverRoot = join(import.meta.dir, "../../..");
const readSource = (relativePath: string) =>
readFileSync(join(serverRoot, relativePath), "utf8");
describe("CacheManager", () => {
test("does not import Redis fallback clients", () => {
const source = readSource("src/utils/cacheUtils/CacheManager.ts");
expect(source).not.toContain("getPrimaryRedis");
expect(source).not.toContain("external/redis");
expect(source).not.toContain("redis.get");
expect(source).not.toContain("redis.set");
expect(source).not.toContain("redis.del");
expect(source).not.toContain("redis.quit");
});
test("uses the provided CacheStore for JSON cache operations", async () => {
const cacheStore = new MemoryCacheStore();
await CacheManager.setJson("cache:key", { value: 1 }, 60, cacheStore);
await expect(
CacheManager.getJson<{ value: number }>("cache:key", cacheStore),
).resolves.toEqual({ value: 1 });
await CacheManager.del("cache:key", cacheStore);
await expect(
CacheManager.getJson<{ value: number }>("cache:key", cacheStore),
).resolves.toBeNull();
});
test("uses the provided CacheStore for action/value invalidation", async () => {
const cacheStore = new MemoryCacheStore();
await CacheManager.setJson("otp:org_123", { value: 1 }, 60, cacheStore);
await CacheManager.invalidate({
action: "otp",
value: "org_123",
cacheStore,
});
await expect(
CacheManager.getJson<{ value: number }>("otp:org_123", cacheStore),
).resolves.toBeNull();
});
test("misses and no-ops when no CacheStore is provided", async () => {
await expect(CacheManager.getJson("cache:key")).resolves.toBeNull();
await expect(
CacheManager.setJson("cache:key", { value: 1 }, 60),
).resolves.toBeUndefined();
await expect(CacheManager.del("cache:key")).resolves.toBeUndefined();
await expect(
CacheManager.invalidate({ action: "cache", value: "key" }),
).resolves.toBeUndefined();
await expect(CacheManager.disconnect()).resolves.toBeUndefined();
});
});