|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { MemoryCacheAdapter } from './memory-cache-adapter'; |
| 5 | +import type { ICacheService } from '@objectstack/spec/contracts'; |
| 6 | + |
| 7 | +describe('MemoryCacheAdapter', () => { |
| 8 | + it('should implement ICacheService contract', () => { |
| 9 | + const cache: ICacheService = new MemoryCacheAdapter(); |
| 10 | + expect(typeof cache.get).toBe('function'); |
| 11 | + expect(typeof cache.set).toBe('function'); |
| 12 | + expect(typeof cache.delete).toBe('function'); |
| 13 | + expect(typeof cache.has).toBe('function'); |
| 14 | + expect(typeof cache.clear).toBe('function'); |
| 15 | + expect(typeof cache.stats).toBe('function'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should set and get a value', async () => { |
| 19 | + const cache = new MemoryCacheAdapter(); |
| 20 | + await cache.set('key1', 'value1'); |
| 21 | + expect(await cache.get('key1')).toBe('value1'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return undefined for missing key', async () => { |
| 25 | + const cache = new MemoryCacheAdapter(); |
| 26 | + expect(await cache.get('nonexistent')).toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should delete a key', async () => { |
| 30 | + const cache = new MemoryCacheAdapter(); |
| 31 | + await cache.set('key1', 'value1'); |
| 32 | + expect(await cache.delete('key1')).toBe(true); |
| 33 | + expect(await cache.get('key1')).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return false when deleting missing key', async () => { |
| 37 | + const cache = new MemoryCacheAdapter(); |
| 38 | + expect(await cache.delete('missing')).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should check if a key exists with has()', async () => { |
| 42 | + const cache = new MemoryCacheAdapter(); |
| 43 | + expect(await cache.has('key1')).toBe(false); |
| 44 | + await cache.set('key1', 'value1'); |
| 45 | + expect(await cache.has('key1')).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should clear all entries', async () => { |
| 49 | + const cache = new MemoryCacheAdapter(); |
| 50 | + await cache.set('a', 1); |
| 51 | + await cache.set('b', 2); |
| 52 | + await cache.clear(); |
| 53 | + expect(await cache.has('a')).toBe(false); |
| 54 | + expect(await cache.has('b')).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should expire entries based on TTL', async () => { |
| 58 | + const cache = new MemoryCacheAdapter(); |
| 59 | + await cache.set('temp', 'data', 0.001); // 1ms TTL |
| 60 | + await new Promise(r => setTimeout(r, 20)); |
| 61 | + expect(await cache.get('temp')).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should track hit/miss stats', async () => { |
| 65 | + const cache = new MemoryCacheAdapter(); |
| 66 | + await cache.set('key1', 'value1'); |
| 67 | + await cache.get('key1'); // hit |
| 68 | + await cache.get('missing'); // miss |
| 69 | + const stats = await cache.stats(); |
| 70 | + expect(stats.hits).toBe(1); |
| 71 | + expect(stats.misses).toBe(1); |
| 72 | + expect(stats.keyCount).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should apply defaultTtl when no TTL is provided', async () => { |
| 76 | + const cache = new MemoryCacheAdapter({ defaultTtl: 0.001 }); |
| 77 | + await cache.set('key', 'value'); |
| 78 | + await new Promise(r => setTimeout(r, 20)); |
| 79 | + expect(await cache.get('key')).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should evict oldest entry when maxSize is reached', async () => { |
| 83 | + const cache = new MemoryCacheAdapter({ maxSize: 2 }); |
| 84 | + await cache.set('a', 1); |
| 85 | + await cache.set('b', 2); |
| 86 | + await cache.set('c', 3); // should evict 'a' |
| 87 | + expect(await cache.has('a')).toBe(false); |
| 88 | + expect(await cache.get('b')).toBe(2); |
| 89 | + expect(await cache.get('c')).toBe(3); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should not evict when updating existing key at maxSize', async () => { |
| 93 | + const cache = new MemoryCacheAdapter({ maxSize: 2 }); |
| 94 | + await cache.set('a', 1); |
| 95 | + await cache.set('b', 2); |
| 96 | + await cache.set('a', 10); // update, not new entry |
| 97 | + expect(await cache.get('a')).toBe(10); |
| 98 | + expect(await cache.get('b')).toBe(2); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle has() with expired TTL', async () => { |
| 102 | + const cache = new MemoryCacheAdapter(); |
| 103 | + await cache.set('expiring', 'val', 0.001); |
| 104 | + await new Promise(r => setTimeout(r, 20)); |
| 105 | + expect(await cache.has('expiring')).toBe(false); |
| 106 | + }); |
| 107 | +}); |
0 commit comments