Skip to content

feat: add tests - #5

Closed
lxfu1 wants to merge 1 commit into
masterfrom
feat/add-tests
Closed

lxfu1 wants to merge 1 commit into
masterfrom
feat/add-tests

Conversation

@lxfu1

@lxfu1 lxfu1 commented Jul 8, 2026

Copy link
Copy Markdown
Member

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +89 to +91
it('should handle empty substring', () => {
expect(countOccurrences('hello', '')).toBe(0);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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.

Suggested change
it('should handle empty substring', () => {
expect(countOccurrences('hello', '')).toBe(0);
});
it.skip('should handle empty substring', () => {
expect(countOccurrences('hello', '')).toBe(0);
});

Comment thread test/embedder.test.ts
Comment on lines +30 to +33
beforeAll(() => {
// Inject mock pipeline into the static field
Embedder.pipeline = mockPipeline as any;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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;
});

Comment thread test/store.test.ts
Comment on lines +15 to +26
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);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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);
  });

Comment thread test/store.test.ts
Comment on lines +28 to +35
afterAll(() => {
if (store) {
store.close();
}
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true, force: true });
}
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Restore the original static pipeline of Embedder in afterAll to prevent test pollution.

Suggested change
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 });
}
});

Comment thread test/utils.test.ts
Comment on lines +207 to +214
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');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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
  1. 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.

Comment thread test/embedder.test.ts
@@ -0,0 +1,78 @@
import { describe, it, expect, vi, beforeAll } from 'vitest';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import afterAll from vitest to clean up the mocked static pipeline after all tests in this suite run, preventing test pollution.

Suggested change
import { describe, it, expect, vi, beforeAll } from 'vitest';
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';

Comment thread test/utils.test.ts
Comment on lines +1 to +6
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';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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';

Comment thread test/utils.test.ts
Comment on lines +183 to +189
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');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the imported path module instead of require('path').

Suggested change
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');
});

Comment thread test/utils.test.ts
Comment on lines +191 to +199
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');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the imported path module instead of require('path').

Suggested change
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');
});

@lxfu1 lxfu1 closed this Jul 8, 2026
@hustcc
hustcc deleted the feat/add-tests branch July 9, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant