41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { simpleGit } from 'simple-git'
|
|
import { listFiles, rgAvailable, searchContent } from '../src/main/search-service'
|
|
|
|
let dir = ''
|
|
afterEach(async () => { if (dir) await rm(dir, { recursive: true, force: true }) })
|
|
|
|
describe('search-service (real ripgrep via dynamic import)', () => {
|
|
it('ripgrep is available (catches the ESM require() regression)', async () => {
|
|
expect(await rgAvailable()).toBe(true)
|
|
})
|
|
|
|
it('finds content matches with line number + column', async () => {
|
|
dir = await mkdtemp(join(tmpdir(), 'helder-search-'))
|
|
await simpleGit(dir).init()
|
|
await writeFile(join(dir, 'a.ts'), 'const x = 1\nconst balance = 2\n')
|
|
await writeFile(join(dir, 'b.ts'), 'nothing relevant\n')
|
|
const groups = await searchContent(dir, 'balance')
|
|
expect(groups).toHaveLength(1)
|
|
expect(groups[0].path).toBe('a.ts')
|
|
expect(groups[0].hits[0].no).toBe(2)
|
|
expect(groups[0].hits[0].ln).toContain('balance')
|
|
expect(groups[0].hits[0].ix).toBe('const '.length)
|
|
})
|
|
|
|
it('returns [] for queries under 2 characters', async () => {
|
|
dir = await mkdtemp(join(tmpdir(), 'helder-search-'))
|
|
expect(await searchContent(dir, 'a')).toEqual([])
|
|
})
|
|
|
|
it('lists project files', async () => {
|
|
dir = await mkdtemp(join(tmpdir(), 'helder-search-'))
|
|
await simpleGit(dir).init()
|
|
await writeFile(join(dir, 'keep.ts'), 'x')
|
|
expect(await listFiles(dir)).toContain('keep.ts')
|
|
})
|
|
})
|