summaryrefslogtreecommitdiff
path: root/packages/core/src/utils/filesearch/result-cache.test.ts
diff options
context:
space:
mode:
authorBryant Chandler <[email protected]>2025-08-05 16:18:03 -0700
committerGitHub <[email protected]>2025-08-05 23:18:03 +0000
commit12a9bc3ed94fab3071529b5304d46bcc5b4fe756 (patch)
tree90967b6670668c6c476719ac04422e1744cbabd6 /packages/core/src/utils/filesearch/result-cache.test.ts
parent2141b39c3d713a19f2dd8012a76c2ff8b7c30a5e (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/result-cache.test.ts')
-rw-r--r--packages/core/src/utils/filesearch/result-cache.test.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/core/src/utils/filesearch/result-cache.test.ts b/packages/core/src/utils/filesearch/result-cache.test.ts
new file mode 100644
index 00000000..0b1b4e17
--- /dev/null
+++ b/packages/core/src/utils/filesearch/result-cache.test.ts
@@ -0,0 +1,56 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import path from 'node:path';
+import { test, expect } from 'vitest';
+import { ResultCache } from './result-cache.js';
+
+test('ResultCache basic usage', async () => {
+ const files = [
+ 'foo.txt',
+ 'bar.js',
+ 'baz.md',
+ 'subdir/file.txt',
+ 'subdir/other.js',
+ 'subdir/nested/file.md',
+ ];
+ const cache = new ResultCache(files, path.resolve('.'));
+ const { files: resultFiles, isExactMatch } = await cache.get('*.js');
+ expect(resultFiles).toEqual(files);
+ expect(isExactMatch).toBe(false);
+});
+
+test('ResultCache cache hit/miss', async () => {
+ const files = ['foo.txt', 'bar.js', 'baz.md'];
+ const cache = new ResultCache(files, path.resolve('.'));
+ // First call: miss
+ const { files: result1Files, isExactMatch: isExactMatch1 } =
+ await cache.get('*.js');
+ expect(result1Files).toEqual(files);
+ expect(isExactMatch1).toBe(false);
+
+ // Simulate FileSearch applying the filter and setting the result
+ cache.set('*.js', ['bar.js']);
+
+ // Second call: hit
+ const { files: result2Files, isExactMatch: isExactMatch2 } =
+ await cache.get('*.js');
+ expect(result2Files).toEqual(['bar.js']);
+ expect(isExactMatch2).toBe(true);
+});
+
+test('ResultCache best base query', async () => {
+ const files = ['foo.txt', 'foobar.js', 'baz.md'];
+ const cache = new ResultCache(files, path.resolve('.'));
+
+ // Cache a broader query
+ cache.set('foo', ['foo.txt', 'foobar.js']);
+
+ // Search for a more specific query that starts with the broader one
+ const { files: resultFiles, isExactMatch } = await cache.get('foobar');
+ expect(resultFiles).toEqual(['foo.txt', 'foobar.js']);
+ expect(isExactMatch).toBe(false);
+});