import { describe, it, expect, vi, beforeEach } from "vitest"; import { Cache } from "../src/lib/cache"; describe("Cache", () => { let cache: Cache; beforeEach(() => { vi.useFakeTimers(); cache = new Cache(1000); }); it("should store and retrieve values", () => { cache.set("key1", "value1"); expect(cache.get("key1")).toBe("value1"); }); it("should return null for missing keys", () => { expect(cache.get("nonexistent")).toBeNull(); }); it("should expire entries after TTL", () => { cache.set("key1", "value1"); vi.advanceTimersByTime(1500); expect(cache.get("key1")).toBeNull(); }); it("should not expire entries before TTL", () => { cache.set("key1", "value1"); vi.advanceTimersByTime(500); expect(cache.get("key1")).toBe("value1"); }); it("should delete entries", () => { cache.set("key1", "value1"); cache.delete("key1"); expect(cache.get("key1")).toBeNull(); }); it("should clear all entries", () => { cache.set("key1", "value1"); cache.set("key2", "value2"); cache.clear(); expect(cache.get("key1")).toBeNull(); expect(cache.get("key2")).toBeNull(); }); it("should not return expired entries when they are cleaned up on get", () => { cache.set("key1", "value1"); vi.advanceTimersByTime(1001); const result = cache.get("key1"); expect(result).toBeNull(); }); });