vibeguard/tests/cache.test.ts

40 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ResultCache } from "../src/inference/cache";
import type { InferenceResult } from "../src/shared/types";
const result = (id: string): InferenceResult => ({
requestId: id,
id,
textHash: id,
scores: { toxicity: .9, severe_toxicity: 0, obscene: 0, threat: 0, insult: 0, identity_attack: 0, sexual_explicit: 0 },
navigationId: "nav"
});
describe("ResultCache", () => {
it("evicts the oldest entry when the cache reaches its limit", () => {
const cache = new ResultCache(2);
cache.set("first", result("first"));
cache.set("second", result("second"));
cache.set("third", result("third"));
expect(cache.size).toBe(2);
expect(cache.get("first")).toBeUndefined();
expect(cache.get("second")?.id).toBe("second");
expect(cache.get("third")?.id).toBe("third");
});
it("uses normalized text for bounded cache entries", () => {
const cache = new ResultCache(1);
cache.set(" Same text ", result("same"));
expect(cache.get("same text")?.id).toBe("same");
expect(cache.size).toBe(1);
});
it("rejects invalid cache sizes", () => {
expect(() => new ResultCache(0)).toThrow();
expect(() => new ResultCache(1.5)).toThrow();
});
});