Open History to see the last commit
1import { filterComposerPullRequestMatches } from "@t3tools/shared/composerPullRequestMatches";
2import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
3import {
4 collectAssistantCitations,
5 expandAssistantCitationsForProvider,
6 serializeAssistantCitation,
7} from "@t3tools/shared/assistantCitations";
8import { describe, expect, it } from "vite-plus/test";
9
10import {
11 clampCollapsedComposerCursor,
12 collapseExpandedComposerCursor,
13 composerStateAtPromptEnd,
14 composerSubmissionIntentForEnter,
15 detectComposerTrigger,
16 expandCollapsedComposerCursor,
17 formatAssistantCitationForComposer,
18 isCollapsedCursorAdjacentToInlineToken,
19 parseStandaloneComposerSlashCommand,
20 replaceTextRange,
21} from "./composer-logic";
22import { carryDisplacedCustomAnswerIntoPrompt } from "./pendingUserInput";
23import { formatTerminalContextReference } from "./lib/terminalContext";
24
25const terminalReference = formatTerminalContextReference({
26 id: "ctx-1",
27 terminalLabel: "Terminal 1",
28 lineStart: 3,
29 lineEnd: 4,
30});
31
32const citation: AssistantCitation = {
33 version: 1,
34 environmentId: EnvironmentId.make("environment-1"),
35 threadId: ThreadId.make("thread-1"),
36 messageId: MessageId.make("message-1"),
37 text: "Keep Unicode 👋 and punctuation (here).",
38 start: 3,
39 end: 40,
40 prefix: "前: ",
41 suffix: " 後",
42};
43const citationSource = serializeAssistantCitation(citation).replaceAll("+", "%20");
44
45describe("formatAssistantCitationForComposer", () => {
46 it.each([undefined, "", " \n\t "])(
47 "keeps citation-only insertion for a blank comment %j",
48 (comment) => {
49 expect(formatAssistantCitationForComposer(citation, comment)).toBe(
50 `${serializeAssistantCitation(citation)} `,
51 );
52 },
53 );
54
55 it("binds a multiline comment to its citation without adding standalone prompt text", () => {
56 const comment = 'What does "keep" mean? 👋\n Please show an example.';
57 const text = formatAssistantCitationForComposer(citation, ` ${comment}\n`);
58 const boundCitation = { ...citation, comment };
59 expect(text).toBe(`${serializeAssistantCitation(boundCitation)} `);
60 expect(collectAssistantCitations(text).map((entry) => entry.citation)).toEqual([boundCitation]);
61 expect(expandAssistantCitationsForProvider(text)).toMatch(/^\[assistant-quote-1\] \n\n/);
62 });
63});
64
65describe("composerSubmissionIntentForEnter", () => {
66 it.each([
67 ["enter", "one line", false, "foreground"],
68 ["enter", "two\nlines", false, "foreground"],
69 ["mod-enter-multiline", "one line", false, "foreground"],
70 ["mod-enter-multiline", "two\nlines", false, null],
71 ["mod-enter-multiline", "two\nlines", true, "foreground"],
72 ["mod-enter", "one line", false, null],
73 ["mod-enter", "one line", true, "foreground"],
74 ] as const)("uses %s for %j with modifier=%s", (sendShortcut, prompt, modifierKey, expected) => {
75 expect(
76 composerSubmissionIntentForEnter({
77 isMobileViewport: false,
78 shiftKey: false,
79 modifierKey,
80 isDraftThread: false,
81 sendShortcut,
82 prompt,
83 }),
84 ).toBe(expected);
85 });
86
87 it.each([
88 ["enter", false, "alternate"],
89 ["enter", true, null],
90 ["mod-enter-multiline", false, "foreground"],
91 ["mod-enter-multiline", true, "alternate"],
92 ["mod-enter", false, "foreground"],
93 ["mod-enter", true, "alternate"],
94 ] as const)(
95 "resolves running follow-ups with %s and shift=%s",
96 (sendShortcut, shiftKey, expected) => {
97 expect(
98 composerSubmissionIntentForEnter({
99 isMobileViewport: false,
100 shiftKey,
101 modifierKey: true,
102 isDraftThread: false,
103 isRunning: true,
104 sendShortcut,
105 prompt: "two\nlines",
106 }),
107 ).toBe(expected);
108 },
109 );
110
111 it("submits plain Enter on desktop", () => {
112 expect(
113 composerSubmissionIntentForEnter({
114 isMobileViewport: false,
115 shiftKey: false,
116 modifierKey: false,
117 isDraftThread: true,
118 }),
119 ).toBe("foreground");
120 });
121
122 it("inserts a newline for plain Enter on mobile", () => {
123 expect(
124 composerSubmissionIntentForEnter({
125 isMobileViewport: true,
126 shiftKey: false,
127 modifierKey: false,
128 isDraftThread: true,
129 }),
130 ).toBeNull();
131 });
132
133 it("inserts a newline for Shift+Enter", () => {
134 expect(
135 composerSubmissionIntentForEnter({
136 isMobileViewport: false,
137 shiftKey: true,
138 modifierKey: false,
139 isDraftThread: true,
140 }),
141 ).toBeNull();
142 });
143
144 it("submits a new thread in the background with Mod+Enter", () => {
145 expect(
146 composerSubmissionIntentForEnter({
147 isMobileViewport: false,
148 shiftKey: false,
149 modifierKey: true,
150 isDraftThread: true,
151 }),
152 ).toBe("background");
153 });
154
155 it("keeps Mod+Enter in the foreground for an active thread", () => {
156 expect(
157 composerSubmissionIntentForEnter({
158 isMobileViewport: false,
159 shiftKey: false,
160 modifierKey: true,
161 isDraftThread: false,
162 }),
163 ).toBe("foreground");
164 });
165});
166
167describe("detectComposerTrigger", () => {
168 it("detects @path trigger at cursor", () => {
169 const text = "Please check @src/com";
170 const trigger = detectComposerTrigger(text, text.length);
171
172 expect(trigger).toEqual({
173 kind: "path",
174 query: "src/com",
175 rangeStart: "Please check ".length,
176 rangeEnd: text.length,
177 });
178 });
179
180 it("detects slash command token while typing command name", () => {
181 const text = "/mo";
182 const trigger = detectComposerTrigger(text, text.length);
183
184 expect(trigger).toEqual({
185 kind: "slash-command",
186 query: "mo",
187 rangeStart: 0,
188 rangeEnd: text.length,
189 });
190 });
191
192 it("keeps /model as a slash command item", () => {
193 const text = "/model";
194 const trigger = detectComposerTrigger(text, text.length);
195
196 expect(trigger).toEqual({
197 kind: "slash-command",
198 query: "model",
199 rangeStart: 0,
200 rangeEnd: text.length,
201 });
202 });
203
204 it("does not keep a subcommand trigger active after /model arguments", () => {
205 const text = "/model spark";
206 const trigger = detectComposerTrigger(text, text.length);
207
208 expect(trigger).toBeNull();
209 });
210
211 it("detects non-model slash commands while typing", () => {
212 const text = "/pl";
213 const trigger = detectComposerTrigger(text, text.length);
214
215 expect(trigger).toEqual({
216 kind: "slash-command",
217 query: "pl",
218 rangeStart: 0,
219 rangeEnd: text.length,
220 });
221 });
222
223 it("keeps slash command detection active for provider commands", () => {
224 const text = "/rev";
225 const trigger = detectComposerTrigger(text, text.length);
226
227 expect(trigger).toEqual({
228 kind: "slash-command",
229 query: "rev",
230 rangeStart: 0,
231 rangeEnd: text.length,
232 });
233 });
234
235 it.each(["$", "€", "£", "¥", "₹", "₩", "₿", "𑿝"])(
236 "detects %sskill trigger at cursor",
237 (prefix) => {
238 const text = `Use ${prefix}gh-fi`;
239 const trigger = detectComposerTrigger(text, text.length);
240
241 expect(trigger).toEqual({
242 kind: "skill",
243 query: "gh-fi",
244 rangeStart: "Use ".length,
245 rangeEnd: text.length,
246 });
247 },
248 );
249
250 it("detects a pull request number at a token boundary", () => {
251 const text = "Compare this with #8737";
252
253 expect(detectComposerTrigger(text, text.length)).toEqual({
254 kind: "pull-request",
255 query: "8737",
256 rangeStart: "Compare this with ".length,
257 rangeEnd: text.length,
258 });
259 });
260
261 it("opens pull request completion from a bare hash", () => {
262 const text = "Compare with #";
263
264 expect(detectComposerTrigger(text, text.length)).toEqual({
265 kind: "pull-request",
266 query: "",
267 rangeStart: "Compare with ".length,
268 rangeEnd: text.length,
269 });
270 });
271
272 it("detects a one-word pull request search", () => {
273 const text = "Compare with #composer";
274
275 expect(detectComposerTrigger(text, text.length)).toEqual({
276 kind: "pull-request",
277 query: "composer",
278 rangeStart: "Compare with ".length,
279 rangeEnd: text.length,
280 });
281 });
282
283 it("supports hyphenated pull request search terms", () => {
284 const text = "Find #inline-context";
285
286 expect(detectComposerTrigger(text, text.length)).toEqual({
287 kind: "pull-request",
288 query: "inline-context",
289 rangeStart: "Find ".length,
290 rangeEnd: text.length,
291 });
292 });
293
294 it("does not keep pull request completion active for headings or embedded hashes", () => {
295 expect(detectComposerTrigger("# Heading", "# Heading".length)).toBeNull();
296 expect(detectComposerTrigger("issue#123", "issue#123".length)).toBeNull();
297 });
298
299 it("detects @path trigger in the middle of existing text", () => {
300 // User typed @ between "inspect " and "in this sentence"
301 const text = "Please inspect @in this sentence";
302 const cursorAfterAt = "Please inspect @".length;
303
304 const trigger = detectComposerTrigger(text, cursorAfterAt);
305 expect(trigger).toEqual({
306 kind: "path",
307 query: "",
308 rangeStart: "Please inspect ".length,
309 rangeEnd: cursorAfterAt,
310 });
311 });
312
313 it("detects @path trigger with query typed mid-text", () => {
314 // User typed @sr between "inspect " and "in this sentence"
315 const text = "Please inspect @srin this sentence";
316 const cursorAfterQuery = "Please inspect @sr".length;
317
318 const trigger = detectComposerTrigger(text, cursorAfterQuery);
319 expect(trigger).toEqual({
320 kind: "path",
321 query: "sr",
322 rangeStart: "Please inspect ".length,
323 rangeEnd: cursorAfterQuery,
324 });
325 });
326
327 it("detects trigger with true cursor even when regex-based mention detection would false-match", () => {
328 // MENTION_TOKEN_REGEX can false-match plain text like "@in" as a mention.
329 // The fix bypasses it by computing the expanded cursor from the Lexical node tree.
330 const text = "Please inspect @in this sentence";
331 const cursorAfterAt = "Please inspect @".length;
332
333 const trigger = detectComposerTrigger(text, cursorAfterAt);
334 expect(trigger).not.toBeNull();
335 expect(trigger?.kind).toBe("path");
336 expect(trigger?.query).toBe("");
337 });
338});
339
340describe("filterComposerPullRequestMatches", () => {
341 const entries = [
342 {
343 number: 7,
344 projectId: "project-1",
345 repository: "t3tools/t3code",
346 updatedAt: "2026-09-01T12:00:00.000Z",
347 },
348 {
349 number: 8987,
350 projectId: "project-1",
351 repository: "T3Tools/T3Code",
352 updatedAt: "2026-09-03T12:00:00.000Z",
353 },
354 {
355 number: 27,
356 projectId: "project-1",
357 repository: "t3tools/t3code",
358 updatedAt: "2026-09-02T12:00:00.000Z",
359 },
360 {
361 number: 27,
362 projectId: "project-1",
363 repository: "t3tools/t3code",
364 updatedAt: "2026-09-01T13:00:00.000Z",
365 },
366 {
367 number: 70,
368 projectId: "project-2",
369 repository: "t3tools/other",
370 updatedAt: "2026-09-04T12:00:00.000Z",
371 },
372 ];
373
374 it("matches and de-duplicates number fragments within one repository, exact match first", () => {
375 expect(
376 filterComposerPullRequestMatches({
377 entries,
378 projectId: "project-1",
379 repository: "t3tools/t3code",
380 query: "7",
381 limit: 10,
382 }).map((entry) => entry.number),
383 ).toEqual([7, 8987, 27]);
384 });
385
386 it("keeps an older exact match when newer substring matches would fill the limit", () => {
387 const exact = {
388 number: 7,
389 projectId: "project-1",
390 repository: "t3tools/t3code",
391 updatedAt: "2020-01-01T00:00:00.000Z",
392 };
393 const newerSubstringMatches = Array.from({ length: 12 }, (_unused, index) => ({
394 number: 700 + index,
395 projectId: "project-1",
396 repository: "t3tools/t3code",
397 updatedAt: `2026-09-${String(index + 1).padStart(2, "0")}T12:00:00.000Z`,
398 }));
399 const matches = filterComposerPullRequestMatches({
400 entries: [...newerSubstringMatches, exact],
401 projectId: "project-1",
402 repository: "t3tools/t3code",
403 query: "7",
404 limit: 10,
405 });
406 expect(matches[0]?.number).toBe(7);
407 expect(matches).toHaveLength(10);
408 });
409
410 it("uses an empty query for a capped recent list", () => {
411 expect(
412 filterComposerPullRequestMatches({
413 entries,
414 projectId: "project-1",
415 repository: "t3tools/t3code",
416 query: "",
417 limit: 2,
418 }).map((entry) => entry.number),
419 ).toEqual([8987, 27]);
420 });
421});
422
423describe("replaceTextRange", () => {
424 it("replaces a text range and returns new cursor", () => {
425 const replaced = replaceTextRange("hello @src", 6, 10, "");
426 expect(replaced).toEqual({
427 text: "hello ",
428 cursor: 6,
429 });
430 });
431});
432
433describe("expandCollapsedComposerCursor", () => {
434 it("keeps cursor unchanged when no mention segment is present", () => {
435 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
436 });
437
438 it("maps collapsed mention cursor to expanded text cursor", () => {
439 const text = "what's in my @AGENTS.md fsfdas";
440 const collapsedCursorAfterMention = "what's in my ".length + 2;
441 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
442
443 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
444 expandedCursorAfterMention,
445 );
446 });
447
448 it("maps collapsed quoted mention cursor to expanded text cursor", () => {
449 const text = 'what is in @"My File.md" please';
450 const collapsedCursorAfterMention = "what is in ".length + 2;
451 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
452
453 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
454 expandedCursorAfterMention,
455 );
456 });
457
458 it("maps collapsed markdown file links to their expanded source offsets", () => {
459 const text = "what's in [AGENTS.md](AGENTS.md) please";
460 const collapsedCursorAfterMention = "what's in ".length + 2;
461 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
462
463 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
464 expandedCursorAfterMention,
465 );
466 });
467
468 it("allows path trigger detection to close after selecting a mention", () => {
469 const text = "what's in my @AGENTS.md ";
470 const collapsedCursorAfterMention = "what's in my ".length + 2;
471 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
472
473 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
474 });
475
476 it.each(["$", "€", "𑿝"])("maps collapsed %s skill cursor to expanded text cursor", (prefix) => {
477 const text = `run ${prefix}review-follow-up then`;
478 const collapsedCursorAfterSkill = "run ".length + 2;
479 const expandedCursorAfterSkill = `run ${prefix}review-follow-up `.length;
480
481 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterSkill)).toBe(
482 expandedCursorAfterSkill,
483 );
484 });
485});
486
487describe("composerStateAtPromptEnd", () => {
488 it("puts the caret at the end of a restored parked draft", () => {
489 const prompt = carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half");
490
491 expect(composerStateAtPromptEnd(prompt)).toEqual({
492 cursor: prompt.length,
493 trigger: null,
494 });
495 });
496
497 it("collapses mention chips so the next keystroke lands after the draft", () => {
498 const prompt = carryDisplacedCustomAnswerIntoPrompt("", "see @AGENTS.md please");
499
500 expect(composerStateAtPromptEnd(prompt).cursor).toBe("see ".length + 1 + " please".length);
501 expect(composerStateAtPromptEnd(prompt).cursor).not.toBe(0);
502 expect(composerStateAtPromptEnd(prompt).cursor).not.toBe(prompt.length);
503 });
504
505 it("keeps a trailing mention trigger when the restored draft ends with @", () => {
506 const prompt = "look at @";
507
508 expect(composerStateAtPromptEnd(prompt)).toEqual({
509 cursor: prompt.length,
510 trigger: { kind: "path", query: "", rangeStart: "look at ".length, rangeEnd: prompt.length },
511 });
512 });
513});
514
515describe("collapseExpandedComposerCursor", () => {
516 it("keeps cursor unchanged when no mention segment is present", () => {
517 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
518 });
519
520 it("maps expanded mention cursor back to collapsed cursor", () => {
521 const text = "what's in my @AGENTS.md fsfdas";
522 const collapsedCursorAfterMention = "what's in my ".length + 2;
523 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
524
525 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
526 collapsedCursorAfterMention,
527 );
528 });
529
530 it("maps expanded quoted mention cursor back to collapsed cursor", () => {
531 const text = 'what is in @"My File.md" please';
532 const collapsedCursorAfterMention = "what is in ".length + 2;
533 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
534
535 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
536 collapsedCursorAfterMention,
537 );
538 });
539
540 it("maps expanded markdown file link cursors back to collapsed offsets", () => {
541 const text = "what's in [AGENTS.md](AGENTS.md) please";
542 const collapsedCursorAfterMention = "what's in ".length + 2;
543 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
544
545 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
546 collapsedCursorAfterMention,
547 );
548 });
549
550 it("keeps package-like text expanded when another mention already exists earlier", () => {
551 const text = "open @AGENTS.md then @src/index.ts ";
552 const expandedCursor = text.length;
553 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
554
555 expect(collapsedCursor).toBe("open ".length + 1 + " then @src/index.ts ".length);
556 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
557 });
558
559 it("collapses only genuine mentions when package-like text exists earlier", () => {
560 const text = "install @scope/pkg then @README.md ";
561 const expandedCursor = text.length;
562 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
563
564 expect(collapsedCursor).toBe("install @scope/pkg then ".length + 1 + " ".length);
565 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
566 });
567
568 it.each(["$", "€", "𑿝"])("maps expanded %s skill cursor back to collapsed cursor", (prefix) => {
569 const text = `run ${prefix}review-follow-up then`;
570 const collapsedCursorAfterSkill = "run ".length + 2;
571 const expandedCursorAfterSkill = `run ${prefix}review-follow-up `.length;
572
573 expect(collapseExpandedComposerCursor(text, expandedCursorAfterSkill)).toBe(
574 collapsedCursorAfterSkill,
575 );
576 });
577});
578
579it("preserves the caret before trailing text after mixed-width skill chips", () => {
580 const text = "𑿝ui a $review tail";
581 const expanded = "𑿝ui a $review ".length;
582 expect(collapseExpandedComposerCursor(text, expanded)).toBe(6);
583 expect(expandCollapsedComposerCursor(text, 6)).toBe(expanded);
584});
585
586describe("clampCollapsedComposerCursor", () => {
587 it("clamps to collapsed prompt length when mentions are present", () => {
588 const text = "open @AGENTS.md then ";
589
590 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
591 "open ".length + 1 + " then ".length,
592 );
593 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
594 "open ".length + 1 + " then ".length,
595 );
596 });
597});
598
599describe("assistant citation cursor offsets", () => {
600 it("roundtrips every collapsed offset across citations, mentions, skills, and Unicode", () => {
601 const prefix = "👋(";
602 const between = "),雪";
603 const after = " @AGENTS.md $review ";
604 const text = `${prefix}${citationSource}${between}${citationSource}${after}${terminalReference}!`;
605 const collapsedLength = `${prefix}□${between}□ □ □ □!`.length;
606 const boundaries = [
607 [prefix.length, prefix.length],
608 [prefix.length + 1, prefix.length + citationSource.length],
609 [prefix.length + 1 + between.length, prefix.length + citationSource.length + between.length],
610 [
611 prefix.length + 2 + between.length,
612 prefix.length + citationSource.length * 2 + between.length,
613 ],
614 [collapsedLength, text.length],
615 ] as const;
616
617 for (const [collapsed, expanded] of boundaries) {
618 expect(expandCollapsedComposerCursor(text, collapsed)).toBe(expanded);
619 expect(collapseExpandedComposerCursor(text, expanded)).toBe(collapsed);
620 }
621 for (let cursor = 0; cursor <= collapsedLength; cursor += 1) {
622 expect(
623 collapseExpandedComposerCursor(text, expandCollapsedComposerCursor(text, cursor)),
624 ).toBe(cursor);
625 }
626 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(collapsedLength);
627 });
628
629 it("snaps serialized offsets inside a citation to the end of its chip", () => {
630 const prefix = "前👋";
631 const text = `${prefix}${citationSource}.`;
632
633 for (const offset of [1, citationSource.length - 1, citationSource.length]) {
634 expect(collapseExpandedComposerCursor(text, prefix.length + offset)).toBe(prefix.length + 1);
635 }
636 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length, "right")).toBe(true);
637 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length + 1, "left")).toBe(true);
638 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length + 2, "left")).toBe(false);
639 });
640
641 it("deletes one collapsed citation without changing its neighbor or surrounding punctuation", () => {
642 const text = `(${citationSource}),${citationSource}!`;
643 const result = replaceTextRange(
644 text,
645 expandCollapsedComposerCursor(text, 1),
646 expandCollapsedComposerCursor(text, 2),
647 "",
648 );
649
650 expect(result).toEqual({ text: `(),${citationSource}!`, cursor: 1 });
651 expect(collapseExpandedComposerCursor(result.text, result.text.length)).toBe("(),□!".length);
652 });
653});
654
655describe("replaceTextRange trailing space consumption", () => {
656 it("double space after insertion when replacement ends with space", () => {
657 // Simulates: "and then |@AG| summarize" where | marks replacement range
658 // The replacement is "@AGENTS.md " (with trailing space)
659 // But if we don't extend rangeEnd, the existing space stays
660 const text = "and then @AG summarize";
661 const rangeStart = "and then ".length;
662 const rangeEnd = "and then @AG".length;
663
664 // Without consuming trailing space: double space
665 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
666 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
667
668 // With consuming trailing space: single space
669 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
670 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
671 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
672 });
673});
674
675describe("isCollapsedCursorAdjacentToInlineToken", () => {
676 it("returns false when no mention exists", () => {
677 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
678 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
679 });
680
681 it("keeps @query typing non-adjacent while no mention pill exists", () => {
682 const text = "hello @pac";
683 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
684 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
685 });
686
687 it("detects left adjacency only when cursor is directly after a mention", () => {
688 const text = "open @AGENTS.md next";
689 const mentionStart = "open ".length;
690 const mentionEnd = mentionStart + 1;
691
692 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
693 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
694 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
695 });
696
697 it("detects right adjacency only when cursor is directly before a mention", () => {
698 const text = "open @AGENTS.md next";
699 const mentionStart = "open ".length;
700 const mentionEnd = mentionStart + 1;
701
702 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
703 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
704 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
705 });
706
707 it("treats context reference pills as inline tokens for adjacency checks", () => {
708 const text = `open ${terminalReference} next`;
709 const tokenStart = "open ".length;
710 const tokenEnd = tokenStart + 1;
711
712 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
713 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
714 });
715
716 it("treats skill pills as inline tokens for adjacency checks", () => {
717 const text = "run $review-follow-up next";
718 const tokenStart = "run ".length;
719 const tokenEnd = tokenStart + 1;
720
721 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
722 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
723 });
724});
725
726describe("parseStandaloneComposerSlashCommand", () => {
727 it("parses standalone /plan command", () => {
728 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
729 });
730
731 it("parses standalone /default command", () => {
732 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
733 });
734
735 it("ignores slash commands with extra message text", () => {
736 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
737 });
738});