Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 clampCollapsedComposerCursor,
5 collapseExpandedComposerCursor,
6 detectComposerTrigger,
7 expandCollapsedComposerCursor,
8 isCollapsedCursorAdjacentToInlineToken,
9 parseStandaloneComposerSlashCommand,
10 replaceTextRange,
11} from "./composer-logic";
12import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
13
14describe("detectComposerTrigger", () => {
15 it("detects @path trigger at cursor", () => {
16 const text = "Please check @src/com";
17 const trigger = detectComposerTrigger(text, text.length);
18
19 expect(trigger).toEqual({
20 kind: "path",
21 query: "src/com",
22 rangeStart: "Please check ".length,
23 rangeEnd: text.length,
24 });
25 });
26
27 it("detects slash command token while typing command name", () => {
28 const text = "/mo";
29 const trigger = detectComposerTrigger(text, text.length);
30
31 expect(trigger).toEqual({
32 kind: "slash-command",
33 query: "mo",
34 rangeStart: 0,
35 rangeEnd: text.length,
36 });
37 });
38
39 it("keeps /model as a slash command item", () => {
40 const text = "/model";
41 const trigger = detectComposerTrigger(text, text.length);
42
43 expect(trigger).toEqual({
44 kind: "slash-command",
45 query: "model",
46 rangeStart: 0,
47 rangeEnd: text.length,
48 });
49 });
50
51 it("does not keep a subcommand trigger active after /model arguments", () => {
52 const text = "/model spark";
53 const trigger = detectComposerTrigger(text, text.length);
54
55 expect(trigger).toBeNull();
56 });
57
58 it("detects non-model slash commands while typing", () => {
59 const text = "/pl";
60 const trigger = detectComposerTrigger(text, text.length);
61
62 expect(trigger).toEqual({
63 kind: "slash-command",
64 query: "pl",
65 rangeStart: 0,
66 rangeEnd: text.length,
67 });
68 });
69
70 it("keeps slash command detection active for provider commands", () => {
71 const text = "/rev";
72 const trigger = detectComposerTrigger(text, text.length);
73
74 expect(trigger).toEqual({
75 kind: "slash-command",
76 query: "rev",
77 rangeStart: 0,
78 rangeEnd: text.length,
79 });
80 });
81
82 it("detects $skill trigger at cursor", () => {
83 const text = "Use $gh-fi";
84 const trigger = detectComposerTrigger(text, text.length);
85
86 expect(trigger).toEqual({
87 kind: "skill",
88 query: "gh-fi",
89 rangeStart: "Use ".length,
90 rangeEnd: text.length,
91 });
92 });
93
94 it("detects @path trigger in the middle of existing text", () => {
95 // User typed @ between "inspect " and "in this sentence"
96 const text = "Please inspect @in this sentence";
97 const cursorAfterAt = "Please inspect @".length;
98
99 const trigger = detectComposerTrigger(text, cursorAfterAt);
100 expect(trigger).toEqual({
101 kind: "path",
102 query: "",
103 rangeStart: "Please inspect ".length,
104 rangeEnd: cursorAfterAt,
105 });
106 });
107
108 it("detects @path trigger with query typed mid-text", () => {
109 // User typed @sr between "inspect " and "in this sentence"
110 const text = "Please inspect @srin this sentence";
111 const cursorAfterQuery = "Please inspect @sr".length;
112
113 const trigger = detectComposerTrigger(text, cursorAfterQuery);
114 expect(trigger).toEqual({
115 kind: "path",
116 query: "sr",
117 rangeStart: "Please inspect ".length,
118 rangeEnd: cursorAfterQuery,
119 });
120 });
121
122 it("detects trigger with true cursor even when regex-based mention detection would false-match", () => {
123 // MENTION_TOKEN_REGEX can false-match plain text like "@in" as a mention.
124 // The fix bypasses it by computing the expanded cursor from the Lexical node tree.
125 const text = "Please inspect @in this sentence";
126 const cursorAfterAt = "Please inspect @".length;
127
128 const trigger = detectComposerTrigger(text, cursorAfterAt);
129 expect(trigger).not.toBeNull();
130 expect(trigger?.kind).toBe("path");
131 expect(trigger?.query).toBe("");
132 });
133});
134
135describe("replaceTextRange", () => {
136 it("replaces a text range and returns new cursor", () => {
137 const replaced = replaceTextRange("hello @src", 6, 10, "");
138 expect(replaced).toEqual({
139 text: "hello ",
140 cursor: 6,
141 });
142 });
143});
144
145describe("expandCollapsedComposerCursor", () => {
146 it("keeps cursor unchanged when no mention segment is present", () => {
147 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
148 });
149
150 it("maps collapsed mention cursor to expanded text cursor", () => {
151 const text = "what's in my @AGENTS.md fsfdas";
152 const collapsedCursorAfterMention = "what's in my ".length + 2;
153 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
154
155 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
156 expandedCursorAfterMention,
157 );
158 });
159
160 it("maps collapsed quoted mention cursor to expanded text cursor", () => {
161 const text = 'what is in @"My File.md" please';
162 const collapsedCursorAfterMention = "what is in ".length + 2;
163 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
164
165 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
166 expandedCursorAfterMention,
167 );
168 });
169
170 it("maps collapsed markdown file links to their expanded source offsets", () => {
171 const text = "what's in [AGENTS.md](AGENTS.md) please";
172 const collapsedCursorAfterMention = "what's in ".length + 2;
173 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
174
175 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
176 expandedCursorAfterMention,
177 );
178 });
179
180 it("allows path trigger detection to close after selecting a mention", () => {
181 const text = "what's in my @AGENTS.md ";
182 const collapsedCursorAfterMention = "what's in my ".length + 2;
183 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
184
185 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
186 });
187
188 it("maps collapsed skill cursor to expanded text cursor", () => {
189 const text = "run $review-follow-up then";
190 const collapsedCursorAfterSkill = "run ".length + 2;
191 const expandedCursorAfterSkill = "run $review-follow-up ".length;
192
193 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterSkill)).toBe(
194 expandedCursorAfterSkill,
195 );
196 });
197});
198
199describe("collapseExpandedComposerCursor", () => {
200 it("keeps cursor unchanged when no mention segment is present", () => {
201 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
202 });
203
204 it("maps expanded mention cursor back to collapsed cursor", () => {
205 const text = "what's in my @AGENTS.md fsfdas";
206 const collapsedCursorAfterMention = "what's in my ".length + 2;
207 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
208
209 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
210 collapsedCursorAfterMention,
211 );
212 });
213
214 it("maps expanded quoted mention cursor back to collapsed cursor", () => {
215 const text = 'what is in @"My File.md" please';
216 const collapsedCursorAfterMention = "what is in ".length + 2;
217 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
218
219 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
220 collapsedCursorAfterMention,
221 );
222 });
223
224 it("maps expanded markdown file link cursors back to collapsed offsets", () => {
225 const text = "what's in [AGENTS.md](AGENTS.md) please";
226 const collapsedCursorAfterMention = "what's in ".length + 2;
227 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
228
229 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
230 collapsedCursorAfterMention,
231 );
232 });
233
234 it("keeps replacement cursors aligned when another mention already exists earlier", () => {
235 const text = "open @AGENTS.md then @src/index.ts ";
236 const expandedCursor = text.length;
237 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
238
239 expect(collapsedCursor).toBe("open ".length + 1 + " then ".length + 2);
240 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
241 });
242
243 it("maps expanded skill cursor back to collapsed cursor", () => {
244 const text = "run $review-follow-up then";
245 const collapsedCursorAfterSkill = "run ".length + 2;
246 const expandedCursorAfterSkill = "run $review-follow-up ".length;
247
248 expect(collapseExpandedComposerCursor(text, expandedCursorAfterSkill)).toBe(
249 collapsedCursorAfterSkill,
250 );
251 });
252});
253
254describe("clampCollapsedComposerCursor", () => {
255 it("clamps to collapsed prompt length when mentions are present", () => {
256 const text = "open @AGENTS.md then ";
257
258 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
259 "open ".length + 1 + " then ".length,
260 );
261 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
262 "open ".length + 1 + " then ".length,
263 );
264 });
265});
266
267describe("replaceTextRange trailing space consumption", () => {
268 it("double space after insertion when replacement ends with space", () => {
269 // Simulates: "and then |@AG| summarize" where | marks replacement range
270 // The replacement is "@AGENTS.md " (with trailing space)
271 // But if we don't extend rangeEnd, the existing space stays
272 const text = "and then @AG summarize";
273 const rangeStart = "and then ".length;
274 const rangeEnd = "and then @AG".length;
275
276 // Without consuming trailing space: double space
277 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
278 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
279
280 // With consuming trailing space: single space
281 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
282 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
283 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
284 });
285});
286
287describe("isCollapsedCursorAdjacentToInlineToken", () => {
288 it("returns false when no mention exists", () => {
289 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
290 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
291 });
292
293 it("keeps @query typing non-adjacent while no mention pill exists", () => {
294 const text = "hello @pac";
295 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
296 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
297 });
298
299 it("detects left adjacency only when cursor is directly after a mention", () => {
300 const text = "open @AGENTS.md next";
301 const mentionStart = "open ".length;
302 const mentionEnd = mentionStart + 1;
303
304 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
305 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
306 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
307 });
308
309 it("detects right adjacency only when cursor is directly before a mention", () => {
310 const text = "open @AGENTS.md next";
311 const mentionStart = "open ".length;
312 const mentionEnd = mentionStart + 1;
313
314 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
315 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
316 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
317 });
318
319 it("treats terminal pills as inline tokens for adjacency checks", () => {
320 const text = `open ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} next`;
321 const tokenStart = "open ".length;
322 const tokenEnd = tokenStart + 1;
323
324 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
325 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
326 });
327
328 it("treats skill pills as inline tokens for adjacency checks", () => {
329 const text = "run $review-follow-up next";
330 const tokenStart = "run ".length;
331 const tokenEnd = tokenStart + 1;
332
333 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
334 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
335 });
336});
337
338describe("parseStandaloneComposerSlashCommand", () => {
339 it("parses standalone /plan command", () => {
340 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
341 });
342
343 it("parses standalone /default command", () => {
344 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
345 });
346
347 it("ignores slash commands with extra message text", () => {
348 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
349 });
350});