Open History to see the last commit
1import { describe, expect, it } from "vitest";
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("allows path trigger detection to close after selecting a mention", () => {
161 const text = "what's in my @AGENTS.md ";
162 const collapsedCursorAfterMention = "what's in my ".length + 2;
163 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
164
165 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
166 });
167
168 it("maps collapsed skill cursor to expanded text cursor", () => {
169 const text = "run $review-follow-up then";
170 const collapsedCursorAfterSkill = "run ".length + 2;
171 const expandedCursorAfterSkill = "run $review-follow-up ".length;
172
173 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterSkill)).toBe(
174 expandedCursorAfterSkill,
175 );
176 });
177});
178
179describe("collapseExpandedComposerCursor", () => {
180 it("keeps cursor unchanged when no mention segment is present", () => {
181 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
182 });
183
184 it("maps expanded mention cursor back to collapsed cursor", () => {
185 const text = "what's in my @AGENTS.md fsfdas";
186 const collapsedCursorAfterMention = "what's in my ".length + 2;
187 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
188
189 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
190 collapsedCursorAfterMention,
191 );
192 });
193
194 it("keeps replacement cursors aligned when another mention already exists earlier", () => {
195 const text = "open @AGENTS.md then @src/index.ts ";
196 const expandedCursor = text.length;
197 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
198
199 expect(collapsedCursor).toBe("open ".length + 1 + " then ".length + 2);
200 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
201 });
202
203 it("maps expanded skill cursor back to collapsed cursor", () => {
204 const text = "run $review-follow-up then";
205 const collapsedCursorAfterSkill = "run ".length + 2;
206 const expandedCursorAfterSkill = "run $review-follow-up ".length;
207
208 expect(collapseExpandedComposerCursor(text, expandedCursorAfterSkill)).toBe(
209 collapsedCursorAfterSkill,
210 );
211 });
212});
213
214describe("clampCollapsedComposerCursor", () => {
215 it("clamps to collapsed prompt length when mentions are present", () => {
216 const text = "open @AGENTS.md then ";
217
218 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
219 "open ".length + 1 + " then ".length,
220 );
221 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
222 "open ".length + 1 + " then ".length,
223 );
224 });
225});
226
227describe("replaceTextRange trailing space consumption", () => {
228 it("double space after insertion when replacement ends with space", () => {
229 // Simulates: "and then |@AG| summarize" where | marks replacement range
230 // The replacement is "@AGENTS.md " (with trailing space)
231 // But if we don't extend rangeEnd, the existing space stays
232 const text = "and then @AG summarize";
233 const rangeStart = "and then ".length;
234 const rangeEnd = "and then @AG".length;
235
236 // Without consuming trailing space: double space
237 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
238 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
239
240 // With consuming trailing space: single space
241 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
242 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
243 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
244 });
245});
246
247describe("isCollapsedCursorAdjacentToInlineToken", () => {
248 it("returns false when no mention exists", () => {
249 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
250 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
251 });
252
253 it("keeps @query typing non-adjacent while no mention pill exists", () => {
254 const text = "hello @pac";
255 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
256 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
257 });
258
259 it("detects left adjacency only when cursor is directly after a mention", () => {
260 const text = "open @AGENTS.md next";
261 const mentionStart = "open ".length;
262 const mentionEnd = mentionStart + 1;
263
264 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
265 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
266 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
267 });
268
269 it("detects right adjacency only when cursor is directly before a mention", () => {
270 const text = "open @AGENTS.md next";
271 const mentionStart = "open ".length;
272 const mentionEnd = mentionStart + 1;
273
274 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
275 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
276 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
277 });
278
279 it("treats terminal pills as inline tokens for adjacency checks", () => {
280 const text = `open ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} next`;
281 const tokenStart = "open ".length;
282 const tokenEnd = tokenStart + 1;
283
284 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
285 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
286 });
287
288 it("treats skill pills as inline tokens for adjacency checks", () => {
289 const text = "run $review-follow-up next";
290 const tokenStart = "run ".length;
291 const tokenEnd = tokenStart + 1;
292
293 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
294 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
295 });
296});
297
298describe("parseStandaloneComposerSlashCommand", () => {
299 it("parses standalone /plan command", () => {
300 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
301 });
302
303 it("parses standalone /default command", () => {
304 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
305 });
306
307 it("ignores slash commands with extra message text", () => {
308 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
309 });
310});