Files
Kordant/browser-ext/tests/cache.test.ts
Michael Freno b03096f19d feat(browser-ext): move browser extension to browser-ext/ and update API client to tRPC
- Create browser-ext/ with full extension code (MV3 manifest, background
  service worker, content script, popup, options page)
- Add tRPC API client that communicates with unified monolith endpoints
- Implement cache, settings, and phishing detection utilities
- Create extension tRPC router in web app (getAuthStatus, linkDevice,
  reportPhishing)
- Configure Vite build with manifest V3 support
- Write unit tests for cache, phishing detector, and API client
- All 20 tests passing, TypeScript lint clean
2026-05-25 18:13:44 -04:00

54 lines
1.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { Cache } from "../src/lib/cache";
describe("Cache", () => {
let cache: Cache<string>;
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();
});
});