|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { CacheManager } from '../../src/cache/index.js'; |
| 3 | +import { StorageManager } from '../../src/storage/index.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Integration: CacheManager + StorageManager |
| 7 | + * |
| 8 | + * Tests the layered caching pattern: |
| 9 | + * - CacheManager provides fast in-memory cache with TTL and SWR |
| 10 | + * - StorageManager provides persistent localStorage fallback |
| 11 | + * - Together they form a two-tier cache with offline support |
| 12 | + */ |
| 13 | + |
| 14 | +interface UserData { |
| 15 | + readonly id: number; |
| 16 | + readonly name: string; |
| 17 | +} |
| 18 | + |
| 19 | +function createMockLocalStorage(): Storage { |
| 20 | + const store = new Map<string, string>(); |
| 21 | + return { |
| 22 | + get length() { |
| 23 | + return store.size; |
| 24 | + }, |
| 25 | + clear(): void { |
| 26 | + store.clear(); |
| 27 | + }, |
| 28 | + getItem(key: string): string | null { |
| 29 | + return store.get(key) ?? null; |
| 30 | + }, |
| 31 | + key(index: number): string | null { |
| 32 | + return Array.from(store.keys())[index] ?? null; |
| 33 | + }, |
| 34 | + removeItem(key: string): void { |
| 35 | + store.delete(key); |
| 36 | + }, |
| 37 | + setItem(key: string, value: string): void { |
| 38 | + store.set(key, value); |
| 39 | + }, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe('CacheManager + StorageManager', () => { |
| 44 | + let mockStorage: Storage; |
| 45 | + let originalLocalStorage: PropertyDescriptor | undefined; |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + vi.useFakeTimers(); |
| 49 | + mockStorage = createMockLocalStorage(); |
| 50 | + originalLocalStorage = Object.getOwnPropertyDescriptor(window, 'localStorage'); |
| 51 | + Object.defineProperty(window, 'localStorage', { |
| 52 | + value: mockStorage, |
| 53 | + writable: true, |
| 54 | + configurable: true, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + vi.useRealTimers(); |
| 60 | + if (originalLocalStorage) { |
| 61 | + Object.defineProperty(window, 'localStorage', originalLocalStorage); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('should use cache for fast access and storage for persistence', async () => { |
| 66 | + const cache = CacheManager.create<UserData>({ maxSize: 100 }); |
| 67 | + const storage = StorageManager.create<UserData>({ prefix: 'users' }); |
| 68 | + |
| 69 | + const user: UserData = { id: 1, name: 'Alice' }; |
| 70 | + |
| 71 | + // Write to both tiers |
| 72 | + await cache.set('user:1', user, { ttl: 60_000 }); |
| 73 | + storage.set('user1', user); |
| 74 | + |
| 75 | + // Fast path: read from cache |
| 76 | + const cached = await cache.get('user:1'); |
| 77 | + expect(cached?.value).toEqual(user); |
| 78 | + |
| 79 | + // Persistence path: read from storage |
| 80 | + const persisted = storage.get('user1'); |
| 81 | + expect(persisted).toEqual(user); |
| 82 | + |
| 83 | + cache.destroy(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should fall back to storage when cache misses', async () => { |
| 87 | + const cache = CacheManager.create<UserData>({ maxSize: 100 }); |
| 88 | + const storage = StorageManager.create<UserData>({ prefix: 'users' }); |
| 89 | + |
| 90 | + const user: UserData = { id: 2, name: 'Bob' }; |
| 91 | + storage.set('user2', user); |
| 92 | + |
| 93 | + // Cache miss |
| 94 | + const cached = await cache.get('user:2'); |
| 95 | + expect(cached).toBeUndefined(); |
| 96 | + |
| 97 | + // Fall back to storage |
| 98 | + const persisted = storage.get('user2'); |
| 99 | + expect(persisted).toEqual(user); |
| 100 | + |
| 101 | + // Populate cache from storage |
| 102 | + if (persisted !== null) { |
| 103 | + await cache.set('user:2', persisted, { ttl: 30_000 }); |
| 104 | + } |
| 105 | + |
| 106 | + // Now cache has it |
| 107 | + const reCached = await cache.get('user:2'); |
| 108 | + expect(reCached?.value).toEqual(user); |
| 109 | + |
| 110 | + cache.destroy(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should serve stale cache while revalidating and update storage', async () => { |
| 114 | + const cache = CacheManager.create<UserData>({ |
| 115 | + maxSize: 100, |
| 116 | + defaultStaleAfter: 100, |
| 117 | + defaultTtl: 60_000, |
| 118 | + }); |
| 119 | + const storage = StorageManager.create<UserData>({ prefix: 'users' }); |
| 120 | + |
| 121 | + const staleUser: UserData = { id: 3, name: 'Charlie' }; |
| 122 | + const freshUser: UserData = { id: 3, name: 'Charlie Updated' }; |
| 123 | + |
| 124 | + // Seed cache and storage |
| 125 | + await cache.set('user:3', staleUser); |
| 126 | + storage.set('user3', staleUser); |
| 127 | + |
| 128 | + // Advance past staleAfter threshold |
| 129 | + vi.advanceTimersByTime(200); |
| 130 | + |
| 131 | + // SWR: get stale value immediately, revalidate in background |
| 132 | + const result = await cache.get('user:3', { |
| 133 | + staleWhileRevalidate: true, |
| 134 | + revalidate: async () => { |
| 135 | + // Simulate API fetch + update storage |
| 136 | + storage.set('user3', freshUser); |
| 137 | + return freshUser; |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(result?.value).toEqual(staleUser); |
| 142 | + expect(result?.isStale).toBe(true); |
| 143 | + |
| 144 | + // Let revalidation complete |
| 145 | + await vi.advanceTimersByTimeAsync(10); |
| 146 | + |
| 147 | + // Cache is now updated |
| 148 | + const updated = await cache.get('user:3'); |
| 149 | + expect(updated?.value).toEqual(freshUser); |
| 150 | + |
| 151 | + // Storage was also updated during revalidation |
| 152 | + expect(storage.get('user3')).toEqual(freshUser); |
| 153 | + |
| 154 | + cache.destroy(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should survive cache eviction with storage as backup', async () => { |
| 158 | + const cache = CacheManager.create<UserData>({ maxSize: 2 }); |
| 159 | + const storage = StorageManager.create<UserData>({ prefix: 'users' }); |
| 160 | + |
| 161 | + const users: UserData[] = [ |
| 162 | + { id: 1, name: 'Alice' }, |
| 163 | + { id: 2, name: 'Bob' }, |
| 164 | + { id: 3, name: 'Charlie' }, |
| 165 | + ]; |
| 166 | + |
| 167 | + // Write all to storage, but cache only holds 2 |
| 168 | + for (const user of users) { |
| 169 | + await cache.set(`user:${user.id}`, user); |
| 170 | + storage.set(`user${user.id}`, user); |
| 171 | + } |
| 172 | + |
| 173 | + // Cache evicted the oldest entry |
| 174 | + const stats = cache.getStats(); |
| 175 | + expect(stats.evictions).toBeGreaterThan(0); |
| 176 | + |
| 177 | + // But storage still has all |
| 178 | + for (const user of users) { |
| 179 | + expect(storage.get(`user${user.id}`)).toEqual(user); |
| 180 | + } |
| 181 | + |
| 182 | + cache.destroy(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should coordinate invalidation across cache and storage', async () => { |
| 186 | + const cache = CacheManager.create<UserData>({ maxSize: 100 }); |
| 187 | + const storage = StorageManager.create<UserData>({ prefix: 'users' }); |
| 188 | + |
| 189 | + const user: UserData = { id: 4, name: 'Diana' }; |
| 190 | + |
| 191 | + await cache.set('user:4', user, { tags: ['users'] }); |
| 192 | + storage.set('user4', user); |
| 193 | + |
| 194 | + // Invalidate by tag in cache |
| 195 | + const invalidated = await cache.invalidateByTag('users'); |
| 196 | + expect(invalidated).toBe(1); |
| 197 | + |
| 198 | + // Cache is empty |
| 199 | + expect(await cache.get('user:4')).toBeUndefined(); |
| 200 | + |
| 201 | + // Storage still has the data (manual cleanup needed) |
| 202 | + expect(storage.get('user4')).toEqual(user); |
| 203 | + |
| 204 | + // Clean up storage separately |
| 205 | + storage.remove('user4'); |
| 206 | + expect(storage.get('user4')).toBeNull(); |
| 207 | + |
| 208 | + cache.destroy(); |
| 209 | + }); |
| 210 | +}); |
0 commit comments