import { describe, it, expect, vi, beforeEach } from 'vitest'; import { MemoryCache } from './memory-cache'; describe('MemoryCache', () => { it('stores and retrieves values', () => { const cache = new MemoryCache(1000); cache.set('foo', 'bar'); expect(cache.get('foo')).toBe('bar'); }); it('returns undefined for missing keys', () => { const cache = new MemoryCache(1000); expect(cache.get('missing')).toBeUndefined(); }); it('expires entries after TTL', async () => { const cache = new MemoryCache(50); // 50ms TTL cache.set('foo', 'bar'); expect(cache.get('foo')).toBe('bar'); await new Promise(resolve => setTimeout(resolve, 100)); expect(cache.get('foo')).toBeUndefined(); }); it('respects max size', () => { const cache = new MemoryCache(1000, 2); cache.set('a', '1'); cache.set('b', '2'); cache.set('c', '3'); // Should evict 'a' expect(cache.get('a')).toBeUndefined(); expect(cache.get('b')).toBe('2'); expect(cache.get('c')).toBe('3'); }); it('deletes entries', () => { const cache = new MemoryCache(1000); cache.set('foo', 'bar'); cache.delete('foo'); expect(cache.get('foo')).toBeUndefined(); }); it('clears all entries', () => { const cache = new MemoryCache(1000); cache.set('a', '1'); cache.set('b', '2'); cache.clear(); expect(cache.get('a')).toBeUndefined(); expect(cache.get('b')).toBeUndefined(); }); });