11import { afterAll , beforeAll , describe , expect , test } from 'bun:test' ;
2- import { join } from 'node:path' ;
32import { mkdir , rm , writeFile } from 'node:fs/promises' ;
43import { tmpdir } from 'node:os' ;
4+ import { join } from 'node:path' ;
55import { runRg , runRgCount } from './cli' ;
6- import { formatGrepResult } from './utils' ;
76import { grep } from './tools' ;
7+ import { formatGrepResult } from './utils' ;
88
99describe ( 'grep tool' , ( ) => {
1010 const testDir = join ( tmpdir ( ) , `grep-test-${ Date . now ( ) } ` ) ;
@@ -13,8 +13,14 @@ describe('grep tool', () => {
1313
1414 beforeAll ( async ( ) => {
1515 await mkdir ( testDir , { recursive : true } ) ;
16- await writeFile ( testFile1 , 'Hello world\nThis is a test file\nAnother line with match' ) ;
17- await writeFile ( testFile2 , 'const x = \'Hello world\';\nconsole.log(\'test\');' ) ;
16+ await writeFile (
17+ testFile1 ,
18+ 'Hello world\nThis is a test file\nAnother line with match' ,
19+ ) ;
20+ await writeFile (
21+ testFile2 ,
22+ "const x = 'Hello world';\nconsole.log('test');" ,
23+ ) ;
1824 } ) ;
1925
2026 afterAll ( async ( ) => {
@@ -46,9 +52,9 @@ describe('grep tool', () => {
4652 test ( 'formats matches correctly' , ( ) => {
4753 const result = {
4854 matches : [
49- { file : 'file1.ts' , line : 10 , text : ' const foo = \ 'bar\'' } ,
55+ { file : 'file1.ts' , line : 10 , text : " const foo = 'bar'" } ,
5056 { file : 'file1.ts' , line : 15 , text : 'console.log(foo)' } ,
51- { file : 'file2.ts' , line : 5 , text : ' import { foo } from \ './file1\'' } ,
57+ { file : 'file2.ts' , line : 5 , text : " import { foo } from './file1'" } ,
5258 ] ,
5359 totalMatches : 3 ,
5460 filesSearched : 2 ,
@@ -57,10 +63,10 @@ describe('grep tool', () => {
5763
5864 const output = formatGrepResult ( result ) ;
5965 expect ( output ) . toContain ( 'file1.ts:' ) ;
60- expect ( output ) . toContain ( ' 10: const foo = \ 'bar\'' ) ;
66+ expect ( output ) . toContain ( " 10: const foo = 'bar'" ) ;
6167 expect ( output ) . toContain ( ' 15: console.log(foo)' ) ;
6268 expect ( output ) . toContain ( 'file2.ts:' ) ;
63- expect ( output ) . toContain ( ' 5: import { foo } from \ './file1\'' ) ;
69+ expect ( output ) . toContain ( " 5: import { foo } from './file1'" ) ;
6470 expect ( output ) . toContain ( 'Found 3 matches in 2 files' ) ;
6571 } ) ;
6672
@@ -83,8 +89,16 @@ describe('grep tool', () => {
8389 } ) ;
8490
8591 expect ( result . totalMatches ) . toBeGreaterThanOrEqual ( 2 ) ;
86- expect ( result . matches . some ( ( m ) => m . file . includes ( 'test1.txt' ) && m . text . includes ( 'Hello' ) ) ) . toBe ( true ) ;
87- expect ( result . matches . some ( ( m ) => m . file . includes ( 'test2.ts' ) && m . text . includes ( 'Hello' ) ) ) . toBe ( true ) ;
92+ expect (
93+ result . matches . some (
94+ ( m ) => m . file . includes ( 'test1.txt' ) && m . text . includes ( 'Hello' ) ,
95+ ) ,
96+ ) . toBe ( true ) ;
97+ expect (
98+ result . matches . some (
99+ ( m ) => m . file . includes ( 'test2.ts' ) && m . text . includes ( 'Hello' ) ,
100+ ) ,
101+ ) . toBe ( true ) ;
88102 } ) ;
89103
90104 test ( 'respects file inclusion patterns' , async ( ) => {
@@ -94,8 +108,12 @@ describe('grep tool', () => {
94108 globs : [ '*.txt' ] ,
95109 } ) ;
96110
97- expect ( result . matches . some ( ( m ) => m . file . includes ( 'test1.txt' ) ) ) . toBe ( true ) ;
98- expect ( result . matches . some ( ( m ) => m . file . includes ( 'test2.ts' ) ) ) . toBe ( false ) ;
111+ expect ( result . matches . some ( ( m ) => m . file . includes ( 'test1.txt' ) ) ) . toBe (
112+ true ,
113+ ) ;
114+ expect ( result . matches . some ( ( m ) => m . file . includes ( 'test2.ts' ) ) ) . toBe (
115+ false ,
116+ ) ;
99117 } ) ;
100118
101119 test ( 'handles no matches' , async ( ) => {
@@ -116,14 +134,14 @@ describe('grep tool', () => {
116134 const resultInsensitive = await runRg ( {
117135 pattern : 'hello' ,
118136 paths : [ testDir ] ,
119- caseSensitive : false
137+ caseSensitive : false ,
120138 } ) ;
121139 expect ( resultInsensitive . totalMatches ) . toBeGreaterThan ( 0 ) ;
122140
123141 const resultSensitive = await runRg ( {
124142 pattern : 'hello' , // File has "Hello"
125143 paths : [ testDir ] ,
126- caseSensitive : true
144+ caseSensitive : true ,
127145 } ) ;
128146 expect ( resultSensitive . totalMatches ) . toBe ( 0 ) ;
129147 } ) ;
@@ -132,14 +150,14 @@ describe('grep tool', () => {
132150 const resultPartial = await runRg ( {
133151 pattern : 'Hell' ,
134152 paths : [ testDir ] ,
135- wholeWord : false
153+ wholeWord : false ,
136154 } ) ;
137155 expect ( resultPartial . totalMatches ) . toBeGreaterThan ( 0 ) ;
138156
139157 const resultWhole = await runRg ( {
140158 pattern : 'Hell' ,
141159 paths : [ testDir ] ,
142- wholeWord : true
160+ wholeWord : true ,
143161 } ) ;
144162 expect ( resultWhole . totalMatches ) . toBe ( 0 ) ;
145163 } ) ;
@@ -148,30 +166,32 @@ describe('grep tool', () => {
148166 const result = await runRg ( {
149167 pattern : 'Hello' ,
150168 paths : [ testDir ] ,
151- maxCount : 1
169+ maxCount : 1 ,
152170 } ) ;
153171 // maxCount is per file
154- expect ( result . matches . filter ( m => m . file . includes ( 'test1.txt' ) ) . length ) . toBeLessThanOrEqual ( 1 ) ;
172+ expect (
173+ result . matches . filter ( ( m ) => m . file . includes ( 'test1.txt' ) ) . length ,
174+ ) . toBeLessThanOrEqual ( 1 ) ;
155175 } ) ;
156176 } ) ;
157177
158178 describe ( 'runRgCount' , ( ) => {
159179 test ( 'counts matches correctly' , async ( ) => {
160180 const results = await runRgCount ( {
161181 pattern : 'Hello' ,
162- paths : [ testDir ]
182+ paths : [ testDir ] ,
163183 } ) ;
164184
165185 expect ( results . length ) . toBeGreaterThan ( 0 ) ;
166- const file1Result = results . find ( r => r . file . includes ( 'test1.txt' ) ) ;
186+ const file1Result = results . find ( ( r ) => r . file . includes ( 'test1.txt' ) ) ;
167187 expect ( file1Result ) . toBeDefined ( ) ;
168188 expect ( file1Result ?. count ) . toBe ( 1 ) ;
169189 } ) ;
170190 } ) ;
171191
172192 describe ( 'grep tool execute' , ( ) => {
173193 test ( 'executes successfully' , async ( ) => {
174- // @ts -ignore
194+ // @ts -expect-error
175195 const result = await grep . execute ( {
176196 pattern : 'Hello' ,
177197 path : testDir ,
@@ -183,7 +203,7 @@ describe('grep tool', () => {
183203 } ) ;
184204
185205 test ( 'handles errors gracefully' , async ( ) => {
186- // @ts -ignore
206+ // @ts -expect-error
187207 const result = await grep . execute ( {
188208 pattern : 'Hello' ,
189209 path : '/non/existent/path/12345' ,
@@ -195,11 +215,11 @@ describe('grep tool', () => {
195215 } ) ;
196216
197217 test ( 'respects include pattern in execute' , async ( ) => {
198- // @ts -ignore
218+ // @ts -expect-error
199219 const result = await grep . execute ( {
200220 pattern : 'Hello' ,
201221 path : testDir ,
202- include : '*.txt'
222+ include : '*.txt' ,
203223 } ) ;
204224
205225 expect ( result ) . toContain ( 'test1.txt' ) ;
0 commit comments