2
2
// used in the useWatchMode hook
3
3
4
4
import { describe , it , expect } from "vitest" ;
5
+ import {
6
+ findAllTriggers ,
7
+ extractContextAroundTrigger ,
8
+ } from "../src/utils/watch-mode-utils" ;
5
9
6
- // These functions are private in the hook, so we need to recreate them for testing
7
- const TRIGGER_PATTERN =
8
- / \/ \/ \s * ( .* ) , ? \s * A I [ ! ? ] | # \s * ( .* ) , ? \s * A I [ ! ? ] | \/ \* \s * ( .* ) , ? \s * A I [ ! ? ] \s * \* \/ / ;
9
-
10
- function findAllTriggers ( content : string ) : Array < RegExpMatchArray > {
11
- const matches : Array < RegExpMatchArray > = [ ] ;
12
- const regex = new RegExp ( TRIGGER_PATTERN , "g" ) ;
13
-
14
- let match ;
15
- while ( ( match = regex . exec ( content ) ) != null ) {
16
- matches . push ( match ) ;
17
- }
18
-
19
- return matches ;
20
- }
21
-
22
- function extractContextAroundTrigger (
23
- content : string ,
24
- triggerMatch : RegExpMatchArray ,
25
- ) : { context : string ; instruction : string } {
26
- // Default context size (number of lines before and after the trigger)
27
- const contextSize = 20 ;
28
-
29
- // Get the lines of the file
30
- const lines = content . split ( "\n" ) ;
31
-
32
- // Find the line number of the trigger
33
- const triggerPos =
34
- content . substring ( 0 , triggerMatch . index ) . split ( "\n" ) . length - 1 ;
35
-
36
- // Calculate start and end lines for context
37
- const startLine = Math . max ( 0 , triggerPos - contextSize ) ;
38
- const endLine = Math . min ( lines . length - 1 , triggerPos + contextSize ) ;
39
-
40
- // Extract the context lines
41
- const contextLines = lines . slice ( startLine , endLine + 1 ) ;
42
-
43
- // Join the context lines back together
44
- const context = contextLines . join ( "\n" ) ;
45
-
46
- // Extract the instruction from the capture groups
47
- // The regex has 3 capture groups for different comment styles:
48
- // Group 1: // instruction AI!
49
- // Group 2: # instruction AI!
50
- // Group 3: /* instruction AI! */
51
- const instruction =
52
- triggerMatch [ 1 ] ||
53
- triggerMatch [ 2 ] ||
54
- triggerMatch [ 3 ] ||
55
- "fix or improve this code" ;
56
-
57
- return { context, instruction } ;
58
- }
10
+ // For testing, we'll use a larger context size
11
+ const TEST_CONTEXT_SIZE = 20 ;
59
12
60
13
describe ( "Watch mode trigger pattern matching" , ( ) => {
61
14
it ( "should detect double-slash (JS-style) AI triggers" , ( ) => {
@@ -213,6 +166,7 @@ export default Counter;`;
213
166
const { context, instruction } = extractContextAroundTrigger (
214
167
content ,
215
168
matches [ 0 ] ! ,
169
+ TEST_CONTEXT_SIZE ,
216
170
) ;
217
171
218
172
// Should include appropriate context around the trigger (the entire file in this case)
@@ -235,12 +189,13 @@ export default Counter;`;
235
189
const { context, instruction } = extractContextAroundTrigger (
236
190
content ,
237
191
matches [ 0 ] ! ,
192
+ TEST_CONTEXT_SIZE ,
238
193
) ;
239
194
240
- // Should only include the default number of lines around the trigger (15 before, 15 after)
195
+ // Should only include the default number of lines around the trigger (20 before, 20 after)
241
196
const contextLines = context . split ( "\n" ) ;
242
197
expect ( contextLines . length ) . toBeLessThan ( 50 ) ; // Less than the full 100 lines
243
- expect ( contextLines . length ) . toBeGreaterThanOrEqual ( 31 ) ; // At least the trigger line + 15 before + 15 after
198
+ expect ( contextLines . length ) . toBeGreaterThanOrEqual ( 41 ) ; // At least the trigger line + 20 before + 20 after
244
199
245
200
// Should include the trigger line
246
201
expect ( context ) . toContain ( "// Optimize this code, AI!" ) ;
@@ -260,6 +215,7 @@ function complexFunction() {
260
215
const { context, instruction } = extractContextAroundTrigger (
261
216
content ,
262
217
matches [ 0 ] ! ,
218
+ TEST_CONTEXT_SIZE ,
263
219
) ;
264
220
265
221
// Should include the entire short file
@@ -280,6 +236,7 @@ function complexFunction() {
280
236
const { context, instruction } = extractContextAroundTrigger (
281
237
content ,
282
238
matches [ 0 ] ! ,
239
+ TEST_CONTEXT_SIZE ,
283
240
) ;
284
241
285
242
// Should include the entire short file
@@ -289,4 +246,3 @@ function complexFunction() {
289
246
expect ( instruction ) . toBe ( "Explain this code, " ) ;
290
247
} ) ;
291
248
} ) ;
292
-
0 commit comments