import { describe, expect, it } from "vite-plus/test";
import { LRUCache } from "./lruCache";
describe("LRUCache", () => {
it("promotes on get and evicts least recently used", () => {
const cache = new LRUCache<string>(2, 1_000);
cache.set("a", "A", 10);
cache.set("b", "B", 10);
expect(cache.get("a")).toBe("A");
cache.set("c", "C", 10);
expect(cache.get("a")).toBe("A");
expect(cache.get("b")).toBeNull();
expect(cache.get("c")).toBe("C");
});
it("evicts by memory budget", () => {
const cache = new LRUCache<string>(10, 25);
cache.set("a", "A", 10);
cache.set("b", "B", 10);
cache.set("c", "C", 10);
expect(cache.get("a")).toBeNull();
expect(cache.get("b")).toBe("B");
expect(cache.get("c")).toBe("C");
});
it("preserves an existing entry when an oversized replacement is rejected", () => {
const cache = new LRUCache<string>(2, 25);
cache.set("a", "A", 10);
cache.set("a", "oversized", 30);
expect(cache.get("a")).toBe("A");
});
});