1- // npx vitest src/utils/__tests__/context-mentions.spec.ts
2-
3- import { describe , it , expect } from "vitest"
41import {
52 insertMention ,
63 removeMention ,
@@ -9,148 +6,11 @@ import {
96 ContextMenuOptionType ,
107 ContextMenuQueryItem ,
118 SearchResult ,
12- } from ".. /context-mentions"
9+ } from "@src/utils /context-mentions"
1310
1411describe ( "insertMention" , ( ) => {
15- // === NEW TESTS FOR AUTO-SPACE INSERTION ===
16- describe ( "auto-insert space before @mention" , ( ) => {
17- it ( "should insert space before @ when text exists without trailing space" , ( ) => {
18- const text = "Hello"
19- const position = 5
20- const value = "/path/to/file.txt"
21-
22- const result = insertMention ( text , position , value , false )
23-
24- expect ( result . newValue ) . toBe ( "Hello @/path/to/file.txt " )
25- expect ( result . mentionIndex ) . toBe ( 6 ) // After the inserted space
26- } )
27-
28- it ( "should not insert space before @ when text ends with space" , ( ) => {
29- const text = "Hello "
30- const position = 6
31- const value = "/path/to/file.txt"
32-
33- const result = insertMention ( text , position , value , false )
34-
35- expect ( result . newValue ) . toBe ( "Hello @/path/to/file.txt " )
36- expect ( result . mentionIndex ) . toBe ( 6 ) // No extra space needed
37- } )
38-
39- it ( "should not insert space before @ when text ends with newline" , ( ) => {
40- const text = "Hello\n"
41- const position = 6
42- const value = "/path/to/file.txt"
43-
44- const result = insertMention ( text , position , value , false )
45-
46- expect ( result . newValue ) . toBe ( "Hello\n@/path/to/file.txt " )
47- expect ( result . mentionIndex ) . toBe ( 6 ) // No extra space needed
48- } )
49-
50- it ( "should not insert space at the beginning of empty text" , ( ) => {
51- const text = ""
52- const position = 0
53- const value = "/path/to/file.txt"
54-
55- const result = insertMention ( text , position , value , false )
56-
57- expect ( result . newValue ) . toBe ( "@/path/to/file.txt " )
58- expect ( result . mentionIndex ) . toBe ( 0 )
59- } )
60-
61- it ( "should handle @ already present in text with auto-space" , ( ) => {
62- const text = "Hello@"
63- const position = 6
64- const value = "/path/to/file.txt"
65-
66- const result = insertMention ( text , position , value , false )
67-
68- expect ( result . newValue ) . toBe ( "Hello @/path/to/file.txt " )
69- expect ( result . mentionIndex ) . toBe ( 6 ) // After the inserted space
70- } )
71-
72- it ( "should handle @ already present with space before it" , ( ) => {
73- const text = "Hello @"
74- const position = 7
75- const value = "/path/to/file.txt"
76-
77- const result = insertMention ( text , position , value , false )
78-
79- expect ( result . newValue ) . toBe ( "Hello @/path/to/file.txt " )
80- expect ( result . mentionIndex ) . toBe ( 6 ) // Position of @
81- } )
82-
83- it ( "should escape spaces in file paths with auto-space" , ( ) => {
84- const text = "Check"
85- const position = 5
86- const value = "/path with spaces/file.txt"
87-
88- const result = insertMention ( text , position , value , false )
89-
90- expect ( result . newValue ) . toBe ( "Check @/path\\ with\\ spaces/file.txt " )
91- expect ( result . mentionIndex ) . toBe ( 6 )
92- } )
93-
94- it ( "should not escape already escaped spaces with auto-space" , ( ) => {
95- const text = "Check"
96- const position = 5
97- const value = "/path\\ with\\ spaces/file.txt"
98-
99- const result = insertMention ( text , position , value , false )
100-
101- expect ( result . newValue ) . toBe ( "Check @/path\\ with\\ spaces/file.txt " )
102- expect ( result . mentionIndex ) . toBe ( 6 )
103- } )
104-
105- it ( "should handle problems mention with auto-space" , ( ) => {
106- const text = "Check"
107- const position = 5
108- const value = "problems"
109-
110- const result = insertMention ( text , position , value , false )
111-
112- expect ( result . newValue ) . toBe ( "Check @problems " )
113- expect ( result . mentionIndex ) . toBe ( 6 )
114- } )
115-
116- it ( "should handle terminal mention with auto-space" , ( ) => {
117- const text = "See output in"
118- const position = 13
119- const value = "terminal"
120-
121- const result = insertMention ( text , position , value , false )
122-
123- expect ( result . newValue ) . toBe ( "See output in @terminal " )
124- expect ( result . mentionIndex ) . toBe ( 14 )
125- } )
126-
127- it ( "should handle git commit hash with auto-space" , ( ) => {
128- const text = "Fixed in commit"
129- const position = 15
130- const value = "a1b2c3d"
131-
132- const result = insertMention ( text , position , value , false )
133-
134- expect ( result . newValue ) . toBe ( "Fixed in commit @a1b2c3d " )
135- expect ( result . mentionIndex ) . toBe ( 16 )
136- } )
137-
138- it ( "should handle cursor in middle of text with auto-space" , ( ) => {
139- const text = "Hello world"
140- const position = 5
141- const value = "/file.txt"
142-
143- const result = insertMention ( text , position , value , false )
144-
145- expect ( result . newValue ) . toBe ( "Hello @/file.txt world" )
146- expect ( result . mentionIndex ) . toBe ( 6 )
147- } )
148- } )
149-
150- // === ORIGINAL TESTS ===
15112 it ( "should insert mention at cursor position when no @ symbol exists" , ( ) => {
15213 const result = insertMention ( "Hello world" , 5 , "test" )
153- // With auto-space, it should add a space before @ since "Hello" doesn't end with space
15414 expect ( result . newValue ) . toBe ( "Hello @test world" )
15515 expect ( result . mentionIndex ) . toBe ( 6 )
15616 } )
@@ -166,7 +26,6 @@ describe("insertMention", () => {
16626 expect ( result . newValue ) . toBe ( "@test " )
16727 expect ( result . mentionIndex ) . toBe ( 0 )
16828 } )
169-
17029 it ( "should replace partial mention after @" , ( ) => {
17130 const result = insertMention ( "Mention @fi" , 11 , "/path/to/file.txt" ) // Cursor after 'i'
17231 expect ( result . newValue ) . toBe ( "Mention @/path/to/file.txt " ) // Space added after mention
@@ -187,11 +46,22 @@ describe("insertMention", () => {
18746
18847 it ( "should handle insertion at the end" , ( ) => {
18948 const result = insertMention ( "Hello" , 5 , "problems" )
190- // With auto-space insertion when text doesn't end with space
19149 expect ( result . newValue ) . toBe ( "Hello @problems " )
19250 expect ( result . mentionIndex ) . toBe ( 6 )
19351 } )
19452
53+ it ( "should insert space before @ when text doesn't end with space" , ( ) => {
54+ const result = insertMention ( "Check" , 5 , "problems" )
55+ expect ( result . newValue ) . toBe ( "Check @problems " )
56+ expect ( result . mentionIndex ) . toBe ( 6 )
57+ } )
58+
59+ it ( "should not insert extra space when text already ends with space" , ( ) => {
60+ const result = insertMention ( "Check " , 6 , "problems" )
61+ expect ( result . newValue ) . toBe ( "Check @problems " )
62+ expect ( result . mentionIndex ) . toBe ( 6 )
63+ } )
64+
19565 it ( "should handle slash command replacement" , ( ) => {
19666 const result = insertMention ( "/mode some" , 5 , "code" , true ) // Simulating mode selection
19767 expect ( result . newValue ) . toBe ( "code" ) // Should replace the whole text
@@ -286,17 +156,6 @@ describe("insertMention", () => {
286156 expect ( result . newValue ) . toBe ( "/code @src/file.ts " )
287157 expect ( result . mentionIndex ) . toBe ( 6 )
288158 } )
289-
290- it ( "should handle slash commands without modification" , ( ) => {
291- const text = ""
292- const position = 0
293- const value = "/command"
294-
295- const result = insertMention ( text , position , value , true )
296-
297- expect ( result . newValue ) . toBe ( "/command" )
298- expect ( result . mentionIndex ) . toBe ( 0 )
299- } )
300159 } )
301160} )
302161
@@ -320,56 +179,6 @@ describe("removeMention", () => {
320179 expect ( result . newPosition ) . toBe ( 5 )
321180 } )
322181
323- it ( "should remove mention at cursor position" , ( ) => {
324- const text = "Check @/path/to/file.txt here"
325- const position = 24 // Right after the mention
326-
327- const result = removeMention ( text , position )
328-
329- expect ( result . newText ) . toBe ( "Check here" )
330- expect ( result . newPosition ) . toBe ( 6 )
331- } )
332-
333- it ( "should remove mention and trailing space" , ( ) => {
334- const text = "Check @/path/to/file.txt "
335- const position = 24 // Right after the mention
336-
337- const result = removeMention ( text , position )
338-
339- expect ( result . newText ) . toBe ( "Check " )
340- expect ( result . newPosition ) . toBe ( 6 )
341- } )
342-
343- it ( "should handle problems mention" , ( ) => {
344- const text = "Check @problems here"
345- const position = 15 // Right after @problems
346-
347- const result = removeMention ( text , position )
348-
349- expect ( result . newText ) . toBe ( "Check here" )
350- expect ( result . newPosition ) . toBe ( 6 )
351- } )
352-
353- it ( "should handle terminal mention" , ( ) => {
354- const text = "See @terminal output"
355- const position = 13 // Right after @terminal
356-
357- const result = removeMention ( text , position )
358-
359- expect ( result . newText ) . toBe ( "See output" )
360- expect ( result . newPosition ) . toBe ( 4 )
361- } )
362-
363- it ( "should handle git commit hash" , ( ) => {
364- const text = "Fixed in @a1b2c3d commit"
365- const position = 17 // Right after the hash
366-
367- const result = removeMention ( text , position )
368-
369- expect ( result . newText ) . toBe ( "Fixed in commit" )
370- expect ( result . newPosition ) . toBe ( 9 )
371- } )
372-
373182 // --- Tests for Escaped Spaces ---
374183 it ( "should not remove mention with escaped spaces if cursor is at the end - KNOWN LIMITATION" , ( ) => {
375184 // NOTE: This is a known limitation - the current regex in removeMention
0 commit comments