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