Skip to content

test: add unit tests for rerankerService.rerank() - #2257

Merged
felladrin merged 1 commit into
mainfrom
test/2167-rerank-unit-tests
Jul 30, 2026
Merged

test: add unit tests for rerankerService.rerank()#2257
felladrin merged 1 commit into
mainfrom
test/2167-rerank-unit-tests

Conversation

@felladrin

Copy link
Copy Markdown
Owner

Description

Fixes #2167.

rerank() had no unit test in the default suite. sanitizeUnicodeSurrogates was covered, and there's a rerankerService.integration.test.ts that runs rerank() against the real model, but that one downloads ~130MB and is excluded from npm run test. So the everyday suite exercised none of rerank()'s own logic: index mapping, batching, the empty/not-ready guards, and the sanitization step.

This adds fast, boundary-mocked tests for it.

One note on the boundary: the issue points at webSearchService.test.ts as the pattern, but that file mocks fetch. The reranker's boundary is the ONNX InferenceSession (plus the tokenizer), so these tests mock InferenceSession.create and spy on Tokenizer.prototype.encode instead. Same idea (mock at the boundary, assert real behavior), different seam.

What the tests check:

Test What it verifies
empty / null / undefined documents Returns [] early, without calling the model
service not ready Throws Reranker service is not ready
score-to-index mapping Each score lands on its document's index, in input order (scores are non-monotonic, so this also confirms rerank() does not sort; that lives in rankSearchResults)
batch splitting 25 docs run in 3 model calls (10 + 10 + 5), and the scores concatenate back in order across every batch boundary
exact multiple of BATCH_SIZE 20 docs run in exactly 2 calls, with no trailing empty batch
surrogate sanitization A lone surrogate reaches the tokenizer as U+FFFD, never raw, proving sanitization runs on the real path and not just that the model was hit

The warm-up call startRerankerService() makes is cleared before each test, so the call-count assertions start from zero.

Type of Change

  • Bug fix
  • New feature
  • Documentation
  • Other (tests)

How to test

  1. Run npm ci.
  2. Run npx vitest run server/rerankerService.test.ts (or the whole suite with npm run test).
  3. Run npm run lint.

Checklist

  • npm run lint passes
  • Tests pass (npm run test)

Security, performance, or breaking changes

None. Tests only, no production code touched.

@felladrin felladrin changed the title test: add unit tests for rerankerService.rerank() (#2167) test: add unit tests for rerankerService.rerank() Jul 30, 2026
@felladrin
felladrin marked this pull request as ready for review July 30, 2026 10:03
@felladrin
felladrin merged commit 21ff201 into main Jul 30, 2026
7 checks passed
@felladrin
felladrin deleted the test/2167-rerank-unit-tests branch July 30, 2026 10:03
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.

Add tests for rerankerService.rerank()

1 participant