-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.test.ts
More file actions
249 lines (210 loc) · 7.54 KB
/
Copy pathcache.test.ts
File metadata and controls
249 lines (210 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Cache } from './cache';
describe('Cache', () => {
let cache: Cache<string>;
beforeEach(() => {
cache = new Cache({ ttl: 1000, maxSize: 10, enabled: true });
});
describe('Basic operations', () => {
it('should set and get values', () => {
cache.set('key1', 'value1');
expect(cache.get('key1')).toBe('value1');
});
it('should return null for non-existent keys', () => {
expect(cache.get('nonexistent')).toBeNull();
});
it('should delete values', () => {
cache.set('key1', 'value1');
expect(cache.delete('key1')).toBe(true);
expect(cache.get('key1')).toBeNull();
});
it('should return false when deleting non-existent key', () => {
expect(cache.delete('nonexistent')).toBe(false);
});
it('should clear all values', () => {
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.clear();
expect(cache.get('key1')).toBeNull();
expect(cache.get('key2')).toBeNull();
});
});
describe('TTL expiration', () => {
it('should expire entries after TTL', async () => {
cache.set('key1', 'value1', 100);
expect(cache.get('key1')).toBe('value1');
await new Promise((resolve) => setTimeout(resolve, 150));
expect(cache.get('key1')).toBeNull();
});
it('should use default TTL when not specified', async () => {
const shortCache = new Cache({ ttl: 100, enabled: true });
shortCache.set('key1', 'value1');
expect(shortCache.get('key1')).toBe('value1');
await new Promise((resolve) => setTimeout(resolve, 150));
expect(shortCache.get('key1')).toBeNull();
});
});
describe('Cache options', () => {
it('should bypass cache when bypass option is true', () => {
cache.set('key1', 'value1');
expect(cache.get('key1', { bypass: true })).toBeNull();
});
it('should force refresh when forceRefresh option is true', () => {
cache.set('key1', 'value1');
expect(cache.get('key1', { forceRefresh: true })).toBeNull();
});
});
describe('Statistics', () => {
beforeEach(() => {
cache.resetStatistics();
});
it('should track hits', () => {
cache.set('key1', 'value1');
cache.get('key1');
const stats = cache.getStatistics();
expect(stats.hits).toBe(1);
expect(stats.misses).toBe(0);
});
it('should track misses', () => {
cache.get('nonexistent');
const stats = cache.getStatistics();
expect(stats.hits).toBe(0);
expect(stats.misses).toBe(1);
});
it('should track sets', () => {
cache.set('key1', 'value1');
const stats = cache.getStatistics();
expect(stats.sets).toBe(1);
});
it('should track deletes', () => {
cache.set('key1', 'value1');
cache.delete('key1');
const stats = cache.getStatistics();
expect(stats.deletes).toBe(1);
});
it('should calculate hit rate correctly', () => {
cache.set('key1', 'value1');
cache.get('key1'); // hit
cache.get('key2'); // miss
const stats = cache.getStatistics();
expect(stats.hitRate).toBe(0.5);
});
it('should reset statistics', () => {
cache.set('key1', 'value1');
cache.get('key1');
cache.resetStatistics();
const stats = cache.getStatistics();
expect(stats.hits).toBe(0);
expect(stats.misses).toBe(0);
expect(stats.sets).toBe(0);
});
});
describe('LRU eviction', () => {
it('should evict least recently used when at capacity', () => {
const smallCache = new Cache({ ttl: 10000, maxSize: 3, enabled: true });
smallCache.set('key1', 'value1');
smallCache.set('key2', 'value2');
smallCache.set('key3', 'value3');
// Access key1 to make it recently used
smallCache.get('key1');
// Add key4, should evict key2 (least recently used)
smallCache.set('key4', 'value4');
expect(smallCache.get('key1')).toBe('value1');
expect(smallCache.get('key2')).toBeNull();
expect(smallCache.get('key3')).toBe('value3');
expect(smallCache.get('key4')).toBe('value4');
const stats = smallCache.getStatistics();
expect(stats.evictions).toBe(1);
});
});
describe('Invalidation', () => {
it('should invalidate all entries when no pattern provided', () => {
cache.set('key1', 'value1');
cache.set('key2', 'value2');
const count = cache.invalidate();
expect(count).toBe(2);
expect(cache.get('key1')).toBeNull();
expect(cache.get('key2')).toBeNull();
});
it('should invalidate entries matching pattern', () => {
cache.set('invoice:1', 'value1');
cache.set('invoice:2', 'value2');
cache.set('user:1', 'value3');
const count = cache.invalidate('invoice:');
expect(count).toBe(2);
expect(cache.get('invoice:1')).toBeNull();
expect(cache.get('invoice:2')).toBeNull();
expect(cache.get('user:1')).toBe('value3');
});
});
describe('Disabled cache', () => {
it('should not store values when disabled', () => {
const disabledCache = new Cache({ ttl: 1000, enabled: false });
disabledCache.set('key1', 'value1');
expect(disabledCache.get('key1')).toBeNull();
});
it('should not track statistics when disabled', () => {
const disabledCache = new Cache({ ttl: 1000, enabled: false });
disabledCache.set('key1', 'value1');
disabledCache.get('key1');
const stats = disabledCache.getStatistics();
expect(stats.hits).toBe(0);
expect(stats.sets).toBe(0);
});
});
describe('Utility methods', () => {
it('should return cache size', () => {
cache.set('key1', 'value1');
cache.set('key2', 'value2');
expect(cache.getSize()).toBe(2);
});
it('should return all keys', () => {
cache.set('key1', 'value1');
cache.set('key2', 'value2');
const keys = cache.getKeys();
expect(keys).toContain('key1');
expect(keys).toContain('key2');
expect(keys.length).toBe(2);
});
});
describe('localStorage integration', () => {
beforeEach(() => {
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value;
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
};
})();
globalThis.localStorage = localStorageMock as any;
});
it('should save to localStorage when storage is localStorage', () => {
const storageCache = new Cache({ ttl: 1000, storage: 'localStorage', enabled: true });
storageCache.set('key1', 'value1');
// Should not throw
});
it('should load from localStorage on initialization', () => {
const storageCache = new Cache({ ttl: 1000, storage: 'localStorage', enabled: true });
storageCache.set('key1', 'value1');
const newCache = new Cache({ ttl: 1000, storage: 'localStorage', enabled: true });
expect(newCache.get('key1')).toBe('value1');
});
it('should handle localStorage errors gracefully', () => {
const storageCache = new Cache({ ttl: 1000, storage: 'localStorage', enabled: true });
vi.spyOn(localStorage, 'setItem').mockImplementation(() => {
throw new Error('Storage quota exceeded');
});
// Should not throw
storageCache.set('key1', 'value1');
});
});
});