diff options
| author | Bryant Chandler <[email protected]> | 2025-08-05 16:18:03 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-05 23:18:03 +0000 |
| commit | 12a9bc3ed94fab3071529b5304d46bcc5b4fe756 (patch) | |
| tree | 90967b6670668c6c476719ac04422e1744cbabd6 /packages/core/src/utils/filesearch/crawlCache.test.ts | |
| parent | 2141b39c3d713a19f2dd8012a76c2ff8b7c30a5e (diff) | |
feat(core, cli): Introduce high-performance FileSearch engine (#5136)
Co-authored-by: Jacob Richman <[email protected]>
Diffstat (limited to 'packages/core/src/utils/filesearch/crawlCache.test.ts')
| -rw-r--r-- | packages/core/src/utils/filesearch/crawlCache.test.ts | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/packages/core/src/utils/filesearch/crawlCache.test.ts b/packages/core/src/utils/filesearch/crawlCache.test.ts new file mode 100644 index 00000000..2feab61a --- /dev/null +++ b/packages/core/src/utils/filesearch/crawlCache.test.ts @@ -0,0 +1,112 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; +import { getCacheKey, read, write, clear } from './crawlCache.js'; + +describe('CrawlCache', () => { + describe('getCacheKey', () => { + it('should generate a consistent hash', () => { + const key1 = getCacheKey('/foo', 'bar'); + const key2 = getCacheKey('/foo', 'bar'); + expect(key1).toBe(key2); + }); + + it('should generate a different hash for different directories', () => { + const key1 = getCacheKey('/foo', 'bar'); + const key2 = getCacheKey('/bar', 'bar'); + expect(key1).not.toBe(key2); + }); + + it('should generate a different hash for different ignore content', () => { + const key1 = getCacheKey('/foo', 'bar'); + const key2 = getCacheKey('/foo', 'baz'); + expect(key1).not.toBe(key2); + }); + }); + + describe('in-memory cache operations', () => { + beforeEach(() => { + // Ensure a clean slate before each test + clear(); + }); + + afterEach(() => { + // Restore real timers after each test that uses fake ones + vi.useRealTimers(); + }); + + it('should write and read data from the cache', () => { + const key = 'test-key'; + const data = ['foo', 'bar']; + write(key, data, 10000); // 10 second TTL + const cachedData = read(key); + expect(cachedData).toEqual(data); + }); + + it('should return undefined for a nonexistent key', () => { + const cachedData = read('nonexistent-key'); + expect(cachedData).toBeUndefined(); + }); + + it('should clear the cache', () => { + const key = 'test-key'; + const data = ['foo', 'bar']; + write(key, data, 10000); + clear(); + const cachedData = read(key); + expect(cachedData).toBeUndefined(); + }); + + it('should automatically evict a cache entry after its TTL expires', async () => { + vi.useFakeTimers(); + const key = 'ttl-key'; + const data = ['foo']; + const ttl = 5000; // 5 seconds + + write(key, data, ttl); + + // Should exist immediately after writing + expect(read(key)).toEqual(data); + + // Advance time just before expiration + await vi.advanceTimersByTimeAsync(ttl - 1); + expect(read(key)).toEqual(data); + + // Advance time past expiration + await vi.advanceTimersByTimeAsync(1); + expect(read(key)).toBeUndefined(); + }); + + it('should reset the timer when an entry is updated', async () => { + vi.useFakeTimers(); + const key = 'update-key'; + const initialData = ['initial']; + const updatedData = ['updated']; + const ttl = 5000; // 5 seconds + + // Write initial data + write(key, initialData, ttl); + + // Advance time, but not enough to expire + await vi.advanceTimersByTimeAsync(3000); + expect(read(key)).toEqual(initialData); + + // Update the data, which should reset the timer + write(key, updatedData, ttl); + expect(read(key)).toEqual(updatedData); + + // Advance time again. If the timer wasn't reset, the total elapsed + // time (3000 + 3000 = 6000) would cause an eviction. + await vi.advanceTimersByTimeAsync(3000); + expect(read(key)).toEqual(updatedData); + + // Advance past the new expiration time + await vi.advanceTimersByTimeAsync(2001); + expect(read(key)).toBeUndefined(); + }); + }); +}); |
