|
4 | 4 | * @author thehabes |
5 | 5 | */ |
6 | 6 |
|
| 7 | +// Ensure cache runs in local mode (not PM2 cluster) for tests |
| 8 | +// This must be set before importing cache to avoid IPC timeouts |
| 9 | +delete process.env.pm_id |
| 10 | + |
7 | 11 | import { jest } from '@jest/globals' |
8 | 12 | import cache from '../index.js' |
9 | 13 |
|
@@ -91,19 +95,19 @@ describe('Cache TTL (Time-To-Live) Limit Enforcement', () => { |
91 | 95 | const testId = Date.now() |
92 | 96 |
|
93 | 97 | // Set entries with short TTL |
94 | | - await cache.clusterCache.set( |
95 | | - cache.generateKey('query', { type: 'Test', testId }), |
96 | | - [{ id: 1 }], |
| 98 | + await cache.set( |
| 99 | + cache.generateKey('query', { type: 'Test', testId }), |
| 100 | + [{ id: 1 }], |
97 | 101 | shortTTL |
98 | 102 | ) |
99 | | - await cache.clusterCache.set( |
100 | | - cache.generateKey('search', { searchText: 'test', testId }), |
101 | | - [{ id: 2 }], |
| 103 | + await cache.set( |
| 104 | + cache.generateKey('search', { searchText: 'test', testId }), |
| 105 | + [{ id: 2 }], |
102 | 106 | shortTTL |
103 | 107 | ) |
104 | | - await cache.clusterCache.set( |
105 | | - cache.generateKey('id', `ttl-${testId}`), |
106 | | - { id: 3 }, |
| 108 | + await cache.set( |
| 109 | + cache.generateKey('id', `ttl-${testId}`), |
| 110 | + { id: 3 }, |
107 | 111 | shortTTL |
108 | 112 | ) |
109 | 113 | await waitForCache(50) |
@@ -301,4 +305,76 @@ describe('Cache Limit Breaking Change Detection', () => { |
301 | 305 | expect(cache.maxBytes).toBeGreaterThan(0) |
302 | 306 | expect(cache.ttl).toBeGreaterThan(0) |
303 | 307 | }) |
| 308 | + |
| 309 | + it('should correctly calculate size for deeply nested query objects', async () => { |
| 310 | + await cache.clear() |
| 311 | + |
| 312 | + // Create queries with deeply nested properties (5+ levels) |
| 313 | + const deeplyNestedQuery = cache.generateKey('query', { |
| 314 | + __cached: { |
| 315 | + 'level1.level2.level3.level4.level5': 'deepValue', |
| 316 | + 'body.target.source.metadata.author.name': 'John Doe', |
| 317 | + 'nested.array.0.property.value': 123 |
| 318 | + }, |
| 319 | + limit: 100, |
| 320 | + skip: 0 |
| 321 | + }) |
| 322 | + |
| 323 | + // Create a large result set with nested objects |
| 324 | + const nestedResults = Array.from({ length: 50 }, (_, i) => ({ |
| 325 | + id: `obj${i}`, |
| 326 | + level1: { |
| 327 | + level2: { |
| 328 | + level3: { |
| 329 | + level4: { |
| 330 | + level5: 'deepValue', |
| 331 | + additionalData: new Array(100).fill('x').join('') |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + }, |
| 336 | + body: { |
| 337 | + target: { |
| 338 | + source: { |
| 339 | + metadata: { |
| 340 | + author: { |
| 341 | + name: 'John Doe', |
| 342 | + email: 'john@example.com' |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + } |
| 347 | + } |
| 348 | + })) |
| 349 | + |
| 350 | + await cache.set(deeplyNestedQuery, nestedResults) |
| 351 | + |
| 352 | + // Verify the cache entry exists |
| 353 | + expect(await cache.get(deeplyNestedQuery)).not.toBeNull() |
| 354 | + |
| 355 | + // Add more deeply nested queries until we approach maxBytes |
| 356 | + const queries = [] |
| 357 | + for (let i = 0; i < 10; i++) { |
| 358 | + const key = cache.generateKey('query', { |
| 359 | + __cached: { |
| 360 | + [`level1.level2.level3.property${i}`]: `value${i}`, |
| 361 | + 'deep.nested.structure.array.0.id': i |
| 362 | + }, |
| 363 | + limit: 100, |
| 364 | + skip: 0 |
| 365 | + }) |
| 366 | + queries.push(key) |
| 367 | + await cache.set(key, nestedResults) |
| 368 | + } |
| 369 | + |
| 370 | + // Verify cache entries exist - check a few queries to confirm caching works |
| 371 | + expect(await cache.get(deeplyNestedQuery)).not.toBeNull() |
| 372 | + expect(await cache.get(queries[queries.length - 1])).not.toBeNull() |
| 373 | + |
| 374 | + // Verify maxBytes enforcement: cache operations should continue working |
| 375 | + // even if some entries were evicted due to byte limits |
| 376 | + const midpoint = Math.floor(queries.length / 2) |
| 377 | + expect(await cache.get(queries[midpoint])).toBeTruthy() |
| 378 | + }) |
304 | 379 | }) |
| 380 | + |
0 commit comments