-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.test.ts
More file actions
501 lines (403 loc) · 12.9 KB
/
manager.test.ts
File metadata and controls
501 lines (403 loc) · 12.9 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import type { CacheManager } from '../src/manager'
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { createCache } from '../src/manager'
describe('CacheManager', () => {
let cache: CacheManager
beforeEach(() => {
cache = createCache({
driver: 'memory',
stdTTL: 60,
checkPeriod: 0,
})
})
afterEach(async () => {
await cache.flush()
await cache.close()
})
describe('Core Operations', () => {
test('should set and get a value', async () => {
// Arrange
const key = 'test-key'
const value = 'test-value'
// Act
await cache.set(key, value)
const result = await cache.get(key)
// Assert
expect(result).toBe(value)
})
test('should return undefined for non-existent keys', async () => {
// Act
const result = await cache.get('non-existent')
// Assert
expect(result).toBeUndefined()
})
test('should check if a key exists', async () => {
// Arrange
await cache.set('exists', 'value')
// Act & Assert
expect(await cache.has('exists')).toBe(true)
expect(await cache.has('does-not-exist')).toBe(false)
})
test('should delete a key', async () => {
// Arrange
await cache.set('to-delete', 'value')
expect(await cache.has('to-delete')).toBe(true)
// Act
const result = await cache.del('to-delete')
// Assert
expect(result).toBe(1)
expect(await cache.has('to-delete')).toBe(false)
})
test('should take a value atomically', async () => {
// Arrange
await cache.set('to-take', 'value')
// Act
const result = await cache.take('to-take')
// Assert
expect(result).toBe('value')
expect(await cache.has('to-take')).toBe(false)
})
test('should flush all cache data', async () => {
// Arrange
await cache.set('key1', 'value1')
await cache.set('key2', 'value2')
const keysBefore = await cache.keys()
expect(keysBefore.length).toBe(2)
// Act
await cache.flush()
// Assert
const keysAfter = await cache.keys()
expect(keysAfter.length).toBe(0)
})
test('should get all keys', async () => {
// Arrange
await cache.set('key1', 'value1')
await cache.set('key2', 'value2')
await cache.set('key3', 'value3')
// Act
const keys = await cache.keys()
// Assert
expect(keys.sort()).toEqual(['key1', 'key2', 'key3'])
})
})
describe('TTL Operations', () => {
test('should respect TTL when getting values', async () => {
// Arrange
await cache.set('expires-fast', 'value', 1) // 1 second TTL
// Act & Assert
expect(await cache.get('expires-fast')).toBe('value')
// Wait for expiration
await new Promise(resolve => setTimeout(resolve, 1100))
// Value should be expired now
expect(await cache.get('expires-fast')).toBeUndefined()
})
test('should get TTL for a key', async () => {
// Arrange
await cache.set('ttl-key', 'value', 300)
// Act
const ttl = await cache.getTtl('ttl-key')
// Assert
expect(typeof ttl).toBe('number')
expect(ttl).toBeGreaterThan(Date.now())
})
test('should update TTL for a key', async () => {
// Arrange
await cache.set('update-ttl', 'value', 60)
// Act
const result = await cache.ttl('update-ttl', 300)
const newTtl = await cache.getTtl('update-ttl')
// Assert
expect(result).toBe(true)
expect(typeof newTtl).toBe('number')
expect(newTtl).toBeGreaterThan(Date.now())
})
})
describe('Batch Operations', () => {
test('should set multiple values at once', async () => {
// Arrange
const entries = [
{ key: 'batch1', value: 'value1' },
{ key: 'batch2', value: 'value2' },
{ key: 'batch3', value: 'value3', ttl: 300 },
]
// Act
const result = await cache.mset(entries)
// Assert
expect(result).toBe(true)
expect(await cache.get('batch1')).toBe('value1')
expect(await cache.get('batch2')).toBe('value2')
expect(await cache.get('batch3')).toBe('value3')
})
test('should get multiple values at once', async () => {
// Arrange
await cache.set('multi1', 'value1')
await cache.set('multi2', 'value2')
await cache.set('multi3', 'value3')
// Act
const result = await cache.mget(['multi1', 'multi2', 'non-existent'])
// Assert
expect(result).toEqual({
multi1: 'value1',
multi2: 'value2',
})
expect(result['non-existent']).toBeUndefined()
})
test('should delete multiple keys at once', async () => {
// Arrange
await cache.set('del1', 'value1')
await cache.set('del2', 'value2')
await cache.set('keep', 'value3')
// Act
const result = await cache.del(['del1', 'del2'])
// Assert
expect(result).toBe(2)
expect(await cache.has('del1')).toBe(false)
expect(await cache.has('del2')).toBe(false)
expect(await cache.has('keep')).toBe(true)
})
})
describe('Fetch Operations', () => {
test('should fetch and cache a computed value', async () => {
// Arrange
let computeCount = 0
const fetcher = () => {
computeCount++
return 'computed-value'
}
// Act
const result1 = await cache.fetch('fetch-key', fetcher)
const result2 = await cache.fetch('fetch-key', fetcher)
// Assert
expect(result1).toBe('computed-value')
expect(result2).toBe('computed-value')
expect(computeCount).toBe(1) // Should only compute once
})
test('should remember a value with TTL', async () => {
// Arrange
const fetcher = () => 'remembered-value'
// Act
const result = await cache.remember('remember-key', 300, fetcher)
// Assert
expect(result).toBe('remembered-value')
expect(await cache.get('remember-key')).toBe('remembered-value')
})
test('should remember a value forever', async () => {
// Arrange
const fetcher = () => 'forever-value'
// Act
const result = await cache.rememberForever('forever-key', fetcher)
// Assert
expect(result).toBe('forever-value')
expect(await cache.get('forever-key')).toBe('forever-value')
})
})
describe('Statistics', () => {
test('should get cache statistics', async () => {
// Arrange
await cache.set('key1', 'value1')
await cache.set('key2', 'value2')
// Act
const stats = await cache.getStats()
// Assert
expect(stats.keys).toBeGreaterThanOrEqual(2)
expect(typeof stats.hits).toBe('number')
expect(typeof stats.misses).toBe('number')
})
})
describe('Namespace Operations', () => {
test('should create a namespaced cache instance', async () => {
// Arrange
const nsCache = cache.namespace('test-namespace')
// Act
await nsCache.set('key1', 'value1')
await cache.set('key1', 'global-value')
// Assert
expect(await nsCache.get('key1')).toBe('value1')
expect(await cache.get('key1')).toBe('global-value')
})
test('should isolate namespace keys', async () => {
// Arrange
const ns1 = cache.namespace('ns1')
const ns2 = cache.namespace('ns2')
// Act
await ns1.set('shared', 'ns1-value')
await ns2.set('shared', 'ns2-value')
// Assert
expect(await ns1.get('shared')).toBe('ns1-value')
expect(await ns2.get('shared')).toBe('ns2-value')
})
test('should flush only namespaced keys', async () => {
// Arrange
const ns = cache.namespace('to-flush')
await ns.set('key1', 'value1')
await cache.set('global', 'global-value')
// Act
await ns.flush()
// Assert
expect(await ns.get('key1')).toBeUndefined()
expect(await cache.get('global')).toBe('global-value')
})
})
describe('Tag Operations', () => {
test('should tag cache entries', async () => {
// Arrange
await cache.set('user:1', { name: 'Alice' })
await cache.set('user:2', { name: 'Bob' })
// Act
await cache.tag('user:1', ['users', 'premium'])
await cache.tag('user:2', ['users'])
const userKeys = await cache.getKeysByTag('users')
const premiumKeys = await cache.getKeysByTag('premium')
// Assert
expect(userKeys.sort()).toEqual(['user:1', 'user:2'])
expect(premiumKeys).toEqual(['user:1'])
})
test('should delete keys by tag', async () => {
// Arrange
await cache.set('post:1', 'content1')
await cache.set('post:2', 'content2')
await cache.set('user:1', 'Alice')
await cache.tag('post:1', ['posts'])
await cache.tag('post:2', ['posts'])
await cache.tag('user:1', ['users'])
// Act
const deleted = await cache.deleteByTag('posts')
// Assert
expect(deleted).toBe(2)
expect(await cache.get('post:1')).toBeUndefined()
expect(await cache.get('post:2')).toBeUndefined()
expect(await cache.get('user:1')).toBe('Alice')
})
})
describe('Events', () => {
test('should emit set event', async () => {
// Arrange
let eventKey: string | undefined
let eventValue: any
cache.on('set', (key, value) => {
eventKey = key.toString()
eventValue = value
})
// Act
await cache.set('event-key', 'event-value')
// Assert
expect(eventKey).toBe('event-key')
expect(eventValue).toBe('event-value')
})
test('should emit del event', async () => {
// Arrange
await cache.set('to-delete', 'value')
let eventKey: string | undefined
cache.on('del', (key) => {
eventKey = Array.isArray(key) ? key[0].toString() : key.toString()
})
// Act
await cache.del('to-delete')
// Assert
expect(eventKey).toBe('to-delete')
})
test('should emit flush event', async () => {
// Arrange
let flushed = false
cache.on('flush', () => {
flushed = true
})
// Act
await cache.flush()
// Assert
expect(flushed).toBe(true)
})
})
describe('Driver Selection', () => {
test('should create memory driver by default', () => {
// Arrange & Act
const memCache = createCache()
// Assert
expect(memCache.getDriver()).toBeDefined()
})
test('should create memory driver when specified', () => {
// Arrange & Act
const memCache = createCache({ driver: 'memory' })
// Assert
expect(memCache.getDriver()).toBeDefined()
})
test('should support custom driver', async () => {
// Arrange
const mockDriver = {
get: async () => 'mock-value',
set: async () => true,
del: async () => 1,
has: async () => true,
keys: async () => ['key1'],
mget: async () => ({ key1: 'value1' }),
mset: async () => true,
getTtl: async () => Date.now() + 1000,
ttl: async () => true,
flush: async () => {},
getStats: async () => ({ hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0 }),
close: async () => {},
}
// Act
const customCache = createCache({ customDriver: mockDriver })
const value = await customCache.get('any-key')
// Assert
expect(value).toBe('mock-value')
})
})
describe('Complex Value Handling', () => {
test('should handle complex objects', async () => {
// Arrange
const complex = {
name: 'Complex Object',
nested: {
level1: {
level2: {
array: [1, 2, 3],
value: true,
},
},
},
date: new Date().toISOString(),
}
// Act
await cache.set('complex', complex)
const result = await cache.get('complex')
// Assert
expect(result).toEqual(complex)
})
test('should handle arrays', async () => {
// Arrange
const array = [1, 'string', { obj: true }, ['nested', 'array']]
// Act
await cache.set('array', array)
const result = await cache.get('array')
// Assert
expect(result).toEqual(array)
})
test('should handle null values', async () => {
// Act
await cache.set('null-key', null)
// Assert
expect(await cache.get('null-key')).toBeNull()
})
test('should support generic type parameters', async () => {
// Arrange
interface User {
id: number
name: string
email: string
}
const user: User = { id: 1, name: 'John', email: 'john@example.com' }
// Act
await cache.set<User>('user', user)
const result = await cache.get<User>('user')
// Assert
expect(result).toEqual(user)
if (result) {
expect(result.id).toBe(1)
expect(result.name).toBe('John')
expect(result.email).toBe('john@example.com')
}
})
})
})