Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive test suite covering the embedder, document loaders, reranker helpers, storage store, and utility functions, along with corresponding test fixtures. The review feedback highlights several critical improvements: fixing an infinite loop in countOccurrences when handling empty substrings, mocking the static Embedder.pipeline to prevent real network requests and TypeScript compilation errors while ensuring it is cleaned up in afterAll to avoid test pollution, throwing immediate errors on file read failures during multi-file loading, and consistently using ES imports for the path module instead of mixing require statements.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| it('should handle empty substring', () => { | ||
| expect(countOccurrences('hello', '')).toBe(0); | ||
| }); |
There was a problem hiding this comment.
Calling countOccurrences('hello', '') with an empty substring '' causes an infinite loop in the implementation of countOccurrences in src/reranker/helpers.ts. This is because text.indexOf('', pos) always returns pos and sub.length is 0, so pos never advances. This test will hang the entire test suite. To prevent this, the implementation of countOccurrences should be updated to return 0 immediately if sub is empty, and this test should be skipped or updated until the bug is fixed.
| it('should handle empty substring', () => { | |
| expect(countOccurrences('hello', '')).toBe(0); | |
| }); | |
| it.skip('should handle empty substring', () => { | |
| expect(countOccurrences('hello', '')).toBe(0); | |
| }); |
| beforeAll(() => { | ||
| // Inject mock pipeline into the static field | ||
| Embedder.pipeline = mockPipeline as any; | ||
| }); |
There was a problem hiding this comment.
Accessing the private static property Embedder.pipeline directly causes a TypeScript compilation error (Property 'pipeline' is private and only accessible within class 'Embedder'). Bypassing this with a cast to any fixes the compilation error. Additionally, we should store the original pipeline and restore it in afterAll to prevent test pollution.
| beforeAll(() => { | |
| // Inject mock pipeline into the static field | |
| Embedder.pipeline = mockPipeline as any; | |
| }); | |
| let originalPipeline: any; | |
| beforeAll(() => { | |
| // Inject mock pipeline into the static field | |
| originalPipeline = (Embedder as any).pipeline; | |
| (Embedder as any).pipeline = mockPipeline; | |
| }); | |
| afterAll(() => { | |
| (Embedder as any).pipeline = originalPipeline; | |
| }); |
| beforeAll(async () => { | ||
| if (fs.existsSync(TEST_DIR)) { | ||
| fs.rmSync(TEST_DIR, { recursive: true, force: true }); | ||
| } | ||
| fs.mkdirSync(TEST_DIR, { recursive: true }); | ||
|
|
||
| embedder = new Embedder(); | ||
| // Warm up the embedder pipeline so dimensions are available | ||
| await embedder.embed('probe'); | ||
|
|
||
| store = new Store(TEST_DIR, embedder); | ||
| }); |
There was a problem hiding this comment.
Calling await embedder.embed('probe') triggers a real network request to download and load the HuggingFace model, which makes the tests slow, flaky, and dependent on external servers. Since embedder.dimensions is a static/readonly property initialized to 512, warming up the pipeline is unnecessary. Instead, mock the static pipeline of Embedder to return mock vectors locally.
let originalPipeline: any;
beforeAll(async () => {
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true, force: true });
}
fs.mkdirSync(TEST_DIR, { recursive: true });
// Mock the static pipeline to avoid downloading HuggingFace models
originalPipeline = (Embedder as any).pipeline;
const mockPipeline = async (texts: string[]) => {
const dim = 512;
const vectors = texts.map(() => Array(dim).fill(0.1));
return {
tolist: () => vectors,
};
};
(Embedder as any).pipeline = mockPipeline;
embedder = new Embedder();
store = new Store(TEST_DIR, embedder);
});| afterAll(() => { | ||
| if (store) { | ||
| store.close(); | ||
| } | ||
| if (fs.existsSync(TEST_DIR)) { | ||
| fs.rmSync(TEST_DIR, { recursive: true, force: true }); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Restore the original static pipeline of Embedder in afterAll to prevent test pollution.
| afterAll(() => { | |
| if (store) { | |
| store.close(); | |
| } | |
| if (fs.existsSync(TEST_DIR)) { | |
| fs.rmSync(TEST_DIR, { recursive: true, force: true }); | |
| } | |
| }); | |
| afterAll(() => { | |
| if (store) { | |
| store.close(); | |
| } | |
| (Embedder as any).pipeline = originalPipeline; | |
| if (fs.existsSync(TEST_DIR)) { | |
| fs.rmSync(TEST_DIR, { recursive: true, force: true }); | |
| } | |
| }); |
| it('should return partial content when some files fail', async () => { | ||
| const files = [ | ||
| require('path').join(__dirname, 'fixtures/docs/getting-started.md'), | ||
| '/nonexistent/path/missing.txt', | ||
| ]; | ||
| const result = await loadSampleText(files, 2); | ||
| expect(result).toContain('Getting Started'); | ||
| }); |
There was a problem hiding this comment.
When loading multiple files, any single file read error should terminate the entire operation and throw an error immediately, rather than silently skipping the failed files. Update this test to expect an error to be thrown instead of returning partial content, and use the imported path module.
it('should throw an error when some files fail to load', async () => {
const files = [
path.join(__dirname, 'fixtures/docs/getting-started.md'),
'/nonexistent/path/missing.txt',
];
await expect(loadSampleText(files, 2)).rejects.toThrow();
});References
- When loading multiple files, any single file read error should terminate the entire operation and throw an error immediately, rather than silently skipping the failed files.
| @@ -0,0 +1,78 @@ | |||
| import { describe, it, expect, vi, beforeAll } from 'vitest'; | |||
There was a problem hiding this comment.
| import { describe, it, expect } from 'vitest'; | ||
| import { safeJsonParse } from '../src/utils/common'; | ||
| import { computeContentHash } from '../src/utils/hash'; | ||
| import { pathToId } from '../src/utils/doc'; | ||
| import { containsCJK } from '../src/utils/str'; | ||
| import { loadSampleText } from '../src/utils/sample'; |
There was a problem hiding this comment.
Import path using ES import syntax instead of mixing require('path') inside tests. Mixing require and import can cause runtime errors in pure ESM environments where require is not defined.
| import { describe, it, expect } from 'vitest'; | |
| import { safeJsonParse } from '../src/utils/common'; | |
| import { computeContentHash } from '../src/utils/hash'; | |
| import { pathToId } from '../src/utils/doc'; | |
| import { containsCJK } from '../src/utils/str'; | |
| import { loadSampleText } from '../src/utils/sample'; | |
| import { describe, it, expect } from 'vitest'; | |
| import * as path from 'path'; | |
| import { safeJsonParse } from '../src/utils/common'; | |
| import { computeContentHash } from '../src/utils/hash'; | |
| import { pathToId } from '../src/utils/doc'; | |
| import { containsCJK } from '../src/utils/str'; | |
| import { loadSampleText } from '../src/utils/sample'; |
| it('should load content from existing files', async () => { | ||
| const files = [ | ||
| require('path').join(__dirname, 'fixtures/docs/getting-started.md'), | ||
| ]; | ||
| const result = await loadSampleText(files); | ||
| expect(result).toContain('Getting Started'); | ||
| }); |
There was a problem hiding this comment.
Use the imported path module instead of require('path').
| it('should load content from existing files', async () => { | |
| const files = [ | |
| require('path').join(__dirname, 'fixtures/docs/getting-started.md'), | |
| ]; | |
| const result = await loadSampleText(files); | |
| expect(result).toContain('Getting Started'); | |
| }); | |
| it('should load content from existing files', async () => { | |
| const files = [ | |
| path.join(__dirname, 'fixtures/docs/getting-started.md'), | |
| ]; | |
| const result = await loadSampleText(files); | |
| expect(result).toContain('Getting Started'); | |
| }); |
| it('should join content from multiple sample files', async () => { | ||
| const files = [ | ||
| require('path').join(__dirname, 'fixtures/docs/getting-started.md'), | ||
| require('path').join(__dirname, 'fixtures/docs/api.json'), | ||
| ]; | ||
| const result = await loadSampleText(files, 2); | ||
| expect(result).toContain('Getting Started'); | ||
| expect(result).toContain('API Reference'); | ||
| }); |
There was a problem hiding this comment.
Use the imported path module instead of require('path').
| it('should join content from multiple sample files', async () => { | |
| const files = [ | |
| require('path').join(__dirname, 'fixtures/docs/getting-started.md'), | |
| require('path').join(__dirname, 'fixtures/docs/api.json'), | |
| ]; | |
| const result = await loadSampleText(files, 2); | |
| expect(result).toContain('Getting Started'); | |
| expect(result).toContain('API Reference'); | |
| }); | |
| it('should join content from multiple sample files', async () => { | |
| const files = [ | |
| path.join(__dirname, 'fixtures/docs/getting-started.md'), | |
| path.join(__dirname, 'fixtures/docs/api.json'), | |
| ]; | |
| const result = await loadSampleText(files, 2); | |
| expect(result).toContain('Getting Started'); | |
| expect(result).toContain('API Reference'); | |
| }); |
No description provided.