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

composer-logic.test.ts

303 lines10.7 KB

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("keeps slash command detection active for provider commands", () => {
64 const text = "/rev";
65 const trigger = detectComposerTrigger(text, text.length);
66​
67 expect(trigger).toEqual({
68 kind: "slash-command",
69 query: "rev",
70 rangeStart: 0,
71 rangeEnd: text.length,
72 });
73 });
74​
75 it("detects $skill trigger at cursor", () => {
76 const text = "Use $gh-fi";
77 const trigger = detectComposerTrigger(text, text.length);
78​
79 expect(trigger).toEqual({
80 kind: "skill",
81 query: "gh-fi",
82 rangeStart: "Use ".length,
83 rangeEnd: text.length,
84 });
85 });
86​
87 it("detects @path trigger in the middle of existing text", () => {
88 // User typed @ between "inspect " and "in this sentence"
89 const text = "Please inspect @in this sentence";
90 const cursorAfterAt = "Please inspect @".length;
91​
92 const trigger = detectComposerTrigger(text, cursorAfterAt);
93 expect(trigger).toEqual({
94 kind: "path",
95 query: "",
96 rangeStart: "Please inspect ".length,
97 rangeEnd: cursorAfterAt,
98 });
99 });
100​
101 it("detects @path trigger with query typed mid-text", () => {
102 // User typed @sr between "inspect " and "in this sentence"
103 const text = "Please inspect @srin this sentence";
104 const cursorAfterQuery = "Please inspect @sr".length;
105​
106 const trigger = detectComposerTrigger(text, cursorAfterQuery);
107 expect(trigger).toEqual({
108 kind: "path",
109 query: "sr",
110 rangeStart: "Please inspect ".length,
111 rangeEnd: cursorAfterQuery,
112 });
113 });
114​
115 it("detects trigger with true cursor even when regex-based mention detection would false-match", () => {
116 // MENTION_TOKEN_REGEX can false-match plain text like "@in" as a mention.
117 // The fix bypasses it by computing the expanded cursor from the Lexical node tree.
118 const text = "Please inspect @in this sentence";
119 const cursorAfterAt = "Please inspect @".length;
120​
121 const trigger = detectComposerTrigger(text, cursorAfterAt);
122 expect(trigger).not.toBeNull();
123 expect(trigger?.kind).toBe("path");
124 expect(trigger?.query).toBe("");
125 });
126});
127​
128describe("replaceTextRange", () => {
129 it("replaces a text range and returns new cursor", () => {
130 const replaced = replaceTextRange("hello @src", 6, 10, "");
131 expect(replaced).toEqual({
132 text: "hello ",
133 cursor: 6,
134 });
135 });
136});
137​
138describe("expandCollapsedComposerCursor", () => {
139 it("keeps cursor unchanged when no mention segment is present", () => {
140 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
141 });
142​
143 it("maps collapsed mention cursor to expanded text 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(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
149 expandedCursorAfterMention,
150 );
151 });
152​
153 it("allows path trigger detection to close after selecting a mention", () => {
154 const text = "what's in my @AGENTS.md ";
155 const collapsedCursorAfterMention = "what's in my ".length + 2;
156 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
157​
158 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
159 });
160​
161 it("maps collapsed skill cursor to expanded text cursor", () => {
162 const text = "run $review-follow-up then";
163 const collapsedCursorAfterSkill = "run ".length + 2;
164 const expandedCursorAfterSkill = "run $review-follow-up ".length;
165​
166 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterSkill)).toBe(
167 expandedCursorAfterSkill,
168 );
169 });
170});
171​
172describe("collapseExpandedComposerCursor", () => {
173 it("keeps cursor unchanged when no mention segment is present", () => {
174 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
175 });
176​
177 it("maps expanded mention cursor back to collapsed cursor", () => {
178 const text = "what's in my @AGENTS.md fsfdas";
179 const collapsedCursorAfterMention = "what's in my ".length + 2;
180 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
181​
182 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
183 collapsedCursorAfterMention,
184 );
185 });
186​
187 it("keeps replacement cursors aligned when another mention already exists earlier", () => {
188 const text = "open @AGENTS.md then @src/index.ts ";
189 const expandedCursor = text.length;
190 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
191​
192 expect(collapsedCursor).toBe("open ".length + 1 + " then ".length + 2);
193 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
194 });
195​
196 it("maps expanded skill cursor back to collapsed cursor", () => {
197 const text = "run $review-follow-up then";
198 const collapsedCursorAfterSkill = "run ".length + 2;
199 const expandedCursorAfterSkill = "run $review-follow-up ".length;
200​
201 expect(collapseExpandedComposerCursor(text, expandedCursorAfterSkill)).toBe(
202 collapsedCursorAfterSkill,
203 );
204 });
205});
206​
207describe("clampCollapsedComposerCursor", () => {
208 it("clamps to collapsed prompt length when mentions are present", () => {
209 const text = "open @AGENTS.md then ";
210​
211 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
212 "open ".length + 1 + " then ".length,
213 );
214 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
215 "open ".length + 1 + " then ".length,
216 );
217 });
218});
219​
220describe("replaceTextRange trailing space consumption", () => {
221 it("double space after insertion when replacement ends with space", () => {
222 // Simulates: "and then |@AG| summarize" where | marks replacement range
223 // The replacement is "@AGENTS.md " (with trailing space)
224 // But if we don't extend rangeEnd, the existing space stays
225 const text = "and then @AG summarize";
226 const rangeStart = "and then ".length;
227 const rangeEnd = "and then @AG".length;
228​
229 // Without consuming trailing space: double space
230 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
231 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
232​
233 // With consuming trailing space: single space
234 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
235 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
236 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
237 });
238});
239​
240describe("isCollapsedCursorAdjacentToInlineToken", () => {
241 it("returns false when no mention exists", () => {
242 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
243 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
244 });
245​
246 it("keeps @query typing non-adjacent while no mention pill exists", () => {
247 const text = "hello @pac";
248 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
249 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
250 });
251​
252 it("detects left adjacency only when cursor is directly after a mention", () => {
253 const text = "open @AGENTS.md next";
254 const mentionStart = "open ".length;
255 const mentionEnd = mentionStart + 1;
256​
257 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
258 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
259 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
260 });
261​
262 it("detects right adjacency only when cursor is directly before a mention", () => {
263 const text = "open @AGENTS.md next";
264 const mentionStart = "open ".length;
265 const mentionEnd = mentionStart + 1;
266​
267 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
268 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
269 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
270 });
271​
272 it("treats terminal pills as inline tokens for adjacency checks", () => {
273 const text = `open ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} next`;
274 const tokenStart = "open ".length;
275 const tokenEnd = tokenStart + 1;
276​
277 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
278 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
279 });
280​
281 it("treats skill pills as inline tokens for adjacency checks", () => {
282 const text = "run $review-follow-up next";
283 const tokenStart = "run ".length;
284 const tokenEnd = tokenStart + 1;
285​
286 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
287 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
288 });
289});
290​
291describe("parseStandaloneComposerSlashCommand", () => {
292 it("parses standalone /plan command", () => {
293 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
294 });
295​
296 it("parses standalone /default command", () => {
297 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
298 });
299​
300 it("ignores slash commands with extra message text", () => {
301 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
302 });
303});