import { describe, it, expect, vi } from 'vitest'; import { isAmbiguous } from '../src/core/reranker.js'; import type { CodeChunk, SearchResult } from '../src/types/index.js'; import { asSearcherTestHarness, materializeSourceSnapshot } from './helpers/source-snapshot.js'; function makeResult(score: number, filePath: string): SearchResult { return { summary: `Result from ${filePath}`, snippet: 'export class Foo {}', filePath, startLine: 1, endLine: 10, score, language: 'typescript', metadata: {} } as SearchResult; } describe('Reranker ambiguity detection', () => { it('detects ambiguous results when top scores are clustered', () => { const results = [ makeResult(0.85, '/a.ts'), makeResult(0.83, '/b.ts'), makeResult(0.82, '/c.ts') ]; expect(isAmbiguous(results)).toBe(true); }); it('returns false when there is a clear winner', () => { const results = [ makeResult(0.95, '/a.ts'), makeResult(0.75, '/b.ts'), makeResult(0.6, '/c.ts') ]; expect(isAmbiguous(results)).toBe(false); }); it('returns false for fewer than 3 results', () => { const results = [makeResult(0.85, '/a.ts'), makeResult(0.84, '/b.ts')]; expect(isAmbiguous(results)).toBe(false); }); it('correctly handles edge case at threshold boundary', () => { // Gap of exactly 0.08 is NOT below the < 0.08 threshold const results = [ makeResult(0.9, '/a.ts'), makeResult(0.85, '/b.ts'), makeResult(0.82, '/c.ts') ]; expect(isAmbiguous(results)).toBe(false); // 0.90 - 0.82 = 0.08, not < threshold // Gap of 0.07 IS below the threshold const ambiguous = [ makeResult(0.9, '/a.ts'), makeResult(0.86, '/b.ts'), makeResult(0.83, '/c.ts') ]; expect(isAmbiguous(ambiguous)).toBe(true); // 0.90 - 0.83 = 0.07, below threshold }); }); describe('File-level dedupe in search results', () => { it('removes duplicate files keeping best score via CodebaseSearcher', async () => { // This is tested via the integration in search-ranking.test.ts // The dedupe logic is in scoreAndSortResults — tested indirectly by // ensuring results contain unique file paths const { CodebaseSearcher } = await import('../src/core/search.js'); const chunks: CodeChunk[] = [ { id: 'chunk1', content: 'class Foo {}', filePath: 'C:/repo/src/foo.ts', relativePath: 'src/foo.ts', startLine: 1, endLine: 1, language: 'typescript', framework: 'generic', componentType: 'service', layer: 'core', dependencies: [], imports: [], exports: [], tags: [], metadata: {} }, { id: 'chunk2', content: 'class Bar extends Foo {}', filePath: 'C:/repo/src/foo.ts', relativePath: 'src/foo.ts', startLine: 11, endLine: 11, language: 'typescript', framework: 'generic', componentType: 'service', layer: 'core', dependencies: [], imports: [], exports: [], tags: [], metadata: {} }, { id: 'chunk3', content: 'class Baz {}', filePath: 'C:/repo/src/baz.ts', relativePath: 'src/baz.ts', startLine: 1, endLine: 1, language: 'typescript', framework: 'generic', componentType: 'service', layer: 'core', dependencies: [], imports: [], exports: [], tags: [], metadata: {} } ]; const snapshot = await materializeSourceSnapshot(chunks, 'reranker-source-'); try { const searcher = new CodebaseSearcher(snapshot.rootPath); const harness = asSearcherTestHarness(searcher); harness.initialized = true; harness.sourceManifest = snapshot.manifest; harness.embeddingProvider = { embed: vi.fn(async () => [0.1, 0.2]) }; harness.storageProvider = { search: vi.fn(async () => [ { chunk: chunks[0], score: 0.9 }, { chunk: chunks[1], score: 0.85 }, { chunk: chunks[2], score: 0.8 } ]), count: vi.fn(async () => 3) }; harness.fuseIndex = null; harness.patternIntelligence = null; const results = await searcher.search('Foo class', 5, undefined, { useSemanticSearch: true, useKeywordSearch: false, enableReranker: false, enableLowConfidenceRescue: false, enableQueryExpansion: false }); // Should have 2 unique files, not 3 results (two from foo.ts) const filePaths = results.map((result) => result.filePath); expect(filePaths.length).toBe(2); expect(filePaths[0]).toContain('foo.ts'); expect(filePaths[1]).toContain('baz.ts'); } finally { await snapshot.cleanup(); } }); });