Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-logic.test.ts

374 lines13.5 KB

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