import { describe, expect, it } from 'vitest' import { createStatCache } from './piagentui-stat-cache.js' const stamp = (mtimeMs: number, size = 10) => ({ mtimeMs, size }) describe('createStatCache', () => { it('misses for unknown paths and hits for the same stamp', () => { const cache = createStatCache() expect(cache.get('a', stamp(1))).toBeUndefined() cache.set('a', stamp(1), 'x') expect(cache.get('a', stamp(1))).toEqual({ value: 'x' }) }) it('treats a changed mtime or size as a miss and drops the stale entry', () => { const cache = createStatCache() cache.set('a', stamp(1, 10), 'x') expect(cache.get('a', stamp(2, 10))).toBeUndefined() expect(cache.size).toBe(0) cache.set('a', stamp(1, 10), 'x') expect(cache.get('a', stamp(1, 11))).toBeUndefined() }) it('can cache null values', () => { const cache = createStatCache() cache.set('a', stamp(1), null) expect(cache.get('a', stamp(1))).toEqual({ value: null }) }) it('evicts the oldest entries beyond the limit', () => { const cache = createStatCache(2) cache.set('a', stamp(1), 1) cache.set('b', stamp(1), 2) cache.set('c', stamp(1), 3) expect(cache.size).toBe(2) expect(cache.get('a', stamp(1))).toBeUndefined() expect(cache.get('b', stamp(1))).toEqual({ value: 2 }) expect(cache.get('c', stamp(1))).toEqual({ value: 3 }) }) it('refreshing a key moves it to the newest position', () => { const cache = createStatCache(2) cache.set('a', stamp(1), 1) cache.set('b', stamp(1), 2) cache.set('a', stamp(2), 11) cache.set('c', stamp(1), 3) expect(cache.get('b', stamp(1))).toBeUndefined() expect(cache.get('a', stamp(2))).toEqual({ value: 11 }) }) })