60 lines · 1.7 KB · TypeScript
RawDownload
import { describe, expect, it } from "vite-plus/test";import { LRUCache } from "./lruCache"; describe("LRUCache", () => {  it("returns null for missing keys", () => {    const cache = new LRUCache<string>(2, 100);    expect(cache.get("missing")).toBeNull();  });   it("evicts oldest by max entries", () => {    const cache = new LRUCache<string>(2, 1_000);    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("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("does not cache entries larger than the memory budget", () => {    const cache = new LRUCache<string>(2, 25);    cache.set("a", "A", 10);    cache.set("oversized", "X", 30);     expect(cache.get("a")).toBe("A");    expect(cache.get("oversized")).toBeNull();  });   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");  });});