Files
orca/src/shared/search-match-count.test.ts
T
JinjingandOrca 26aa473bca File search match counts (#5085)
* Add matchCount to SearchFileResult for accurate per-file hit counts

Co-authored-by: Orca <help@stably.ai>

* Add file search match count design

* rm design doc

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-10 17:42:49 -07:00

50 lines
1.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { normalizeSearchFileMatchCount, normalizeSearchResult } from './search-match-count'
import type { SearchFileResult } from './types'
const match = { line: 1, column: 1, matchLength: 3, lineContent: 'foo' }
function makeFile(overrides: Partial<SearchFileResult> = {}): SearchFileResult {
return {
filePath: '/r/a.ts',
relativePath: 'a.ts',
matches: [match],
...overrides
}
}
describe('normalizeSearchFileMatchCount', () => {
it('falls back to matches length when matchCount is omitted', () => {
expect(normalizeSearchFileMatchCount(makeFile({ matches: [match, match] }))).toBe(2)
})
it('repairs invalid and too-low counts', () => {
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: 0 }))).toBe(1)
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: -1 }))).toBe(1)
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: Number.NaN }))).toBe(1)
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: Number.POSITIVE_INFINITY }))).toBe(
1
)
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: 1.5 }))).toBe(1)
expect(
normalizeSearchFileMatchCount(makeFile({ matchCount: 'invalid' as unknown as number }))
).toBe(1)
})
it('preserves valid counts greater than preview rows', () => {
expect(normalizeSearchFileMatchCount(makeFile({ matchCount: 5 }))).toBe(5)
})
})
describe('normalizeSearchResult', () => {
it('drops empty file rows even when a malformed count claims matches', () => {
const result = normalizeSearchResult({
files: [makeFile({ matchCount: 3, matches: [] })],
totalMatches: 3,
truncated: false
})
expect(result.files).toEqual([])
})
})