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("detects slash model query after /model", () => {
40 const text = "/model spark";
41 const trigger = detectComposerTrigger(text, text.length);
42
43 expect(trigger).toEqual({
44 kind: "slash-model",
45 query: "spark",
46 rangeStart: 0,
47 rangeEnd: text.length,
48 });
49 });
50
51 it("detects non-model slash commands while typing", () => {
52 const text = "/pl";
53 const trigger = detectComposerTrigger(text, text.length);
54
55 expect(trigger).toEqual({
56 kind: "slash-command",
57 query: "pl",
58 rangeStart: 0,
59 rangeEnd: text.length,
60 });
61 });
62
63 it("detects @path trigger in the middle of existing text", () => {
64 // User typed @ between "inspect " and "in this sentence"
65 const text = "Please inspect @in this sentence";
66 const cursorAfterAt = "Please inspect @".length;
67
68 const trigger = detectComposerTrigger(text, cursorAfterAt);
69 expect(trigger).toEqual({
70 kind: "path",
71 query: "",
72 rangeStart: "Please inspect ".length,
73 rangeEnd: cursorAfterAt,
74 });
75 });
76
77 it("detects @path trigger with query typed mid-text", () => {
78 // User typed @sr between "inspect " and "in this sentence"
79 const text = "Please inspect @srin this sentence";
80 const cursorAfterQuery = "Please inspect @sr".length;
81
82 const trigger = detectComposerTrigger(text, cursorAfterQuery);
83 expect(trigger).toEqual({
84 kind: "path",
85 query: "sr",
86 rangeStart: "Please inspect ".length,
87 rangeEnd: cursorAfterQuery,
88 });
89 });
90
91 it("detects trigger with true cursor even when regex-based mention detection would false-match", () => {
92 // MENTION_TOKEN_REGEX can false-match plain text like "@in" as a mention.
93 // The fix bypasses it by computing the expanded cursor from the Lexical node tree.
94 const text = "Please inspect @in this sentence";
95 const cursorAfterAt = "Please inspect @".length;
96
97 const trigger = detectComposerTrigger(text, cursorAfterAt);
98 expect(trigger).not.toBeNull();
99 expect(trigger?.kind).toBe("path");
100 expect(trigger?.query).toBe("");
101 });
102});
103
104describe("replaceTextRange", () => {
105 it("replaces a text range and returns new cursor", () => {
106 const replaced = replaceTextRange("hello @src", 6, 10, "");
107 expect(replaced).toEqual({
108 text: "hello ",
109 cursor: 6,
110 });
111 });
112});
113
114describe("expandCollapsedComposerCursor", () => {
115 it("keeps cursor unchanged when no mention segment is present", () => {
116 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
117 });
118
119 it("maps collapsed mention cursor to expanded text cursor", () => {
120 const text = "what's in my @AGENTS.md fsfdas";
121 const collapsedCursorAfterMention = "what's in my ".length + 2;
122 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
123
124 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
125 expandedCursorAfterMention,
126 );
127 });
128
129 it("allows path trigger detection to close after selecting a mention", () => {
130 const text = "what's in my @AGENTS.md ";
131 const collapsedCursorAfterMention = "what's in my ".length + 2;
132 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
133
134 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
135 });
136});
137
138describe("collapseExpandedComposerCursor", () => {
139 it("keeps cursor unchanged when no mention segment is present", () => {
140 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
141 });
142
143 it("maps expanded mention cursor back to collapsed cursor", () => {
144 const text = "what's in my @AGENTS.md fsfdas";
145 const collapsedCursorAfterMention = "what's in my ".length + 2;
146 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
147
148 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
149 collapsedCursorAfterMention,
150 );
151 });
152
153 it("keeps replacement cursors aligned when another mention already exists earlier", () => {
154 const text = "open @AGENTS.md then @src/index.ts ";
155 const expandedCursor = text.length;
156 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
157
158 expect(collapsedCursor).toBe("open ".length + 1 + " then ".length + 2);
159 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
160 });
161});
162
163describe("clampCollapsedComposerCursor", () => {
164 it("clamps to collapsed prompt length when mentions are present", () => {
165 const text = "open @AGENTS.md then ";
166
167 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
168 "open ".length + 1 + " then ".length,
169 );
170 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
171 "open ".length + 1 + " then ".length,
172 );
173 });
174});
175
176describe("replaceTextRange trailing space consumption", () => {
177 it("double space after insertion when replacement ends with space", () => {
178 // Simulates: "and then |@AG| summarize" where | marks replacement range
179 // The replacement is "@AGENTS.md " (with trailing space)
180 // But if we don't extend rangeEnd, the existing space stays
181 const text = "and then @AG summarize";
182 const rangeStart = "and then ".length;
183 const rangeEnd = "and then @AG".length;
184
185 // Without consuming trailing space: double space
186 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
187 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
188
189 // With consuming trailing space: single space
190 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
191 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
192 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
193 });
194});
195
196describe("isCollapsedCursorAdjacentToInlineToken", () => {
197 it("returns false when no mention exists", () => {
198 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
199 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
200 });
201
202 it("keeps @query typing non-adjacent while no mention pill exists", () => {
203 const text = "hello @pac";
204 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
205 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
206 });
207
208 it("detects left adjacency only when cursor is directly after a mention", () => {
209 const text = "open @AGENTS.md next";
210 const mentionStart = "open ".length;
211 const mentionEnd = mentionStart + 1;
212
213 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
214 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
215 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
216 });
217
218 it("detects right adjacency only when cursor is directly before a mention", () => {
219 const text = "open @AGENTS.md next";
220 const mentionStart = "open ".length;
221 const mentionEnd = mentionStart + 1;
222
223 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
224 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
225 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
226 });
227
228 it("treats terminal pills as inline tokens for adjacency checks", () => {
229 const text = `open ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} next`;
230 const tokenStart = "open ".length;
231 const tokenEnd = tokenStart + 1;
232
233 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
234 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
235 });
236});
237
238describe("parseStandaloneComposerSlashCommand", () => {
239 it("parses standalone /plan command", () => {
240 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
241 });
242
243 it("parses standalone /default command", () => {
244 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
245 });
246
247 it("ignores slash commands with extra message text", () => {
248 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
249 });
250});