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

composer-logic.test.ts

513 lines18.4 KB

1import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
2import {
3 collectAssistantCitations,
4 expandAssistantCitationsForProvider,
5 serializeAssistantCitation,
6} from "@t3tools/shared/assistantCitations";
7import { describe, expect, it } from "vite-plus/test";
8​
9import {
10 clampCollapsedComposerCursor,
11 collapseExpandedComposerCursor,
12 composerSubmissionIntentForEnter,
13 detectComposerTrigger,
14 expandCollapsedComposerCursor,
15 formatAssistantCitationForComposer,
16 isCollapsedCursorAdjacentToInlineToken,
17 parseStandaloneComposerSlashCommand,
18 replaceTextRange,
19} from "./composer-logic";
20import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
21​
22const citation: AssistantCitation = {
23 version: 1,
24 environmentId: EnvironmentId.make("environment-1"),
25 threadId: ThreadId.make("thread-1"),
26 messageId: MessageId.make("message-1"),
27 text: "Keep Unicode πŸ‘‹ and punctuation (here).",
28 start: 3,
29 end: 40,
30 prefix: "前: ",
31 suffix: " 後",
32};
33const citationSource = serializeAssistantCitation(citation).replaceAll("+", "%20");
34​
35describe("formatAssistantCitationForComposer", () => {
36 it.each([undefined, "", " \n\t "])(
37 "keeps citation-only insertion for a blank comment %j",
38 (comment) => {
39 expect(formatAssistantCitationForComposer(citation, comment)).toBe(
40 `${serializeAssistantCitation(citation)} `,
41 );
42 },
43 );
44​
45 it("binds a multiline comment to its citation without adding standalone prompt text", () => {
46 const comment = 'What does "keep" mean? πŸ‘‹\n Please show an example.';
47 const text = formatAssistantCitationForComposer(citation, ` ${comment}\n`);
48 const boundCitation = { ...citation, comment };
49 expect(text).toBe(`${serializeAssistantCitation(boundCitation)} `);
50 expect(collectAssistantCitations(text).map((entry) => entry.citation)).toEqual([boundCitation]);
51 expect(expandAssistantCitationsForProvider(text)).toMatch(/^\[assistant-quote-1\] \n\n/);
52 });
53});
54​
55describe("composerSubmissionIntentForEnter", () => {
56 it("submits plain Enter on desktop", () => {
57 expect(
58 composerSubmissionIntentForEnter({
59 isMobileViewport: false,
60 shiftKey: false,
61 modifierKey: false,
62 isDraftThread: true,
63 }),
64 ).toBe("foreground");
65 });
66​
67 it("inserts a newline for plain Enter on mobile", () => {
68 expect(
69 composerSubmissionIntentForEnter({
70 isMobileViewport: true,
71 shiftKey: false,
72 modifierKey: false,
73 isDraftThread: true,
74 }),
75 ).toBeNull();
76 });
77​
78 it("inserts a newline for Shift+Enter", () => {
79 expect(
80 composerSubmissionIntentForEnter({
81 isMobileViewport: false,
82 shiftKey: true,
83 modifierKey: false,
84 isDraftThread: true,
85 }),
86 ).toBeNull();
87 });
88​
89 it("submits a new thread in the background with Mod+Enter", () => {
90 expect(
91 composerSubmissionIntentForEnter({
92 isMobileViewport: false,
93 shiftKey: false,
94 modifierKey: true,
95 isDraftThread: true,
96 }),
97 ).toBe("background");
98 });
99​
100 it("keeps Mod+Enter in the foreground for an active thread", () => {
101 expect(
102 composerSubmissionIntentForEnter({
103 isMobileViewport: false,
104 shiftKey: false,
105 modifierKey: true,
106 isDraftThread: false,
107 }),
108 ).toBe("foreground");
109 });
110});
111​
112describe("detectComposerTrigger", () => {
113 it("detects @path trigger at cursor", () => {
114 const text = "Please check @src/com";
115 const trigger = detectComposerTrigger(text, text.length);
116​
117 expect(trigger).toEqual({
118 kind: "path",
119 query: "src/com",
120 rangeStart: "Please check ".length,
121 rangeEnd: text.length,
122 });
123 });
124​
125 it("detects slash command token while typing command name", () => {
126 const text = "/mo";
127 const trigger = detectComposerTrigger(text, text.length);
128​
129 expect(trigger).toEqual({
130 kind: "slash-command",
131 query: "mo",
132 rangeStart: 0,
133 rangeEnd: text.length,
134 });
135 });
136​
137 it("keeps /model as a slash command item", () => {
138 const text = "/model";
139 const trigger = detectComposerTrigger(text, text.length);
140​
141 expect(trigger).toEqual({
142 kind: "slash-command",
143 query: "model",
144 rangeStart: 0,
145 rangeEnd: text.length,
146 });
147 });
148​
149 it("does not keep a subcommand trigger active after /model arguments", () => {
150 const text = "/model spark";
151 const trigger = detectComposerTrigger(text, text.length);
152​
153 expect(trigger).toBeNull();
154 });
155​
156 it("detects non-model slash commands while typing", () => {
157 const text = "/pl";
158 const trigger = detectComposerTrigger(text, text.length);
159​
160 expect(trigger).toEqual({
161 kind: "slash-command",
162 query: "pl",
163 rangeStart: 0,
164 rangeEnd: text.length,
165 });
166 });
167​
168 it("keeps slash command detection active for provider commands", () => {
169 const text = "/rev";
170 const trigger = detectComposerTrigger(text, text.length);
171​
172 expect(trigger).toEqual({
173 kind: "slash-command",
174 query: "rev",
175 rangeStart: 0,
176 rangeEnd: text.length,
177 });
178 });
179​
180 it("detects $skill trigger at cursor", () => {
181 const text = "Use $gh-fi";
182 const trigger = detectComposerTrigger(text, text.length);
183​
184 expect(trigger).toEqual({
185 kind: "skill",
186 query: "gh-fi",
187 rangeStart: "Use ".length,
188 rangeEnd: text.length,
189 });
190 });
191​
192 it("detects @path trigger in the middle of existing text", () => {
193 // User typed @ between "inspect " and "in this sentence"
194 const text = "Please inspect @in this sentence";
195 const cursorAfterAt = "Please inspect @".length;
196​
197 const trigger = detectComposerTrigger(text, cursorAfterAt);
198 expect(trigger).toEqual({
199 kind: "path",
200 query: "",
201 rangeStart: "Please inspect ".length,
202 rangeEnd: cursorAfterAt,
203 });
204 });
205​
206 it("detects @path trigger with query typed mid-text", () => {
207 // User typed @sr between "inspect " and "in this sentence"
208 const text = "Please inspect @srin this sentence";
209 const cursorAfterQuery = "Please inspect @sr".length;
210​
211 const trigger = detectComposerTrigger(text, cursorAfterQuery);
212 expect(trigger).toEqual({
213 kind: "path",
214 query: "sr",
215 rangeStart: "Please inspect ".length,
216 rangeEnd: cursorAfterQuery,
217 });
218 });
219​
220 it("detects trigger with true cursor even when regex-based mention detection would false-match", () => {
221 // MENTION_TOKEN_REGEX can false-match plain text like "@in" as a mention.
222 // The fix bypasses it by computing the expanded cursor from the Lexical node tree.
223 const text = "Please inspect @in this sentence";
224 const cursorAfterAt = "Please inspect @".length;
225​
226 const trigger = detectComposerTrigger(text, cursorAfterAt);
227 expect(trigger).not.toBeNull();
228 expect(trigger?.kind).toBe("path");
229 expect(trigger?.query).toBe("");
230 });
231});
232​
233describe("replaceTextRange", () => {
234 it("replaces a text range and returns new cursor", () => {
235 const replaced = replaceTextRange("hello @src", 6, 10, "");
236 expect(replaced).toEqual({
237 text: "hello ",
238 cursor: 6,
239 });
240 });
241});
242​
243describe("expandCollapsedComposerCursor", () => {
244 it("keeps cursor unchanged when no mention segment is present", () => {
245 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
246 });
247​
248 it("maps collapsed mention cursor to expanded text cursor", () => {
249 const text = "what's in my @AGENTS.md fsfdas";
250 const collapsedCursorAfterMention = "what's in my ".length + 2;
251 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
252​
253 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
254 expandedCursorAfterMention,
255 );
256 });
257​
258 it("maps collapsed quoted mention cursor to expanded text cursor", () => {
259 const text = 'what is in @"My File.md" please';
260 const collapsedCursorAfterMention = "what is in ".length + 2;
261 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
262​
263 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
264 expandedCursorAfterMention,
265 );
266 });
267​
268 it("maps collapsed markdown file links to their expanded source offsets", () => {
269 const text = "what's in [AGENTS.md](AGENTS.md) please";
270 const collapsedCursorAfterMention = "what's in ".length + 2;
271 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
272​
273 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
274 expandedCursorAfterMention,
275 );
276 });
277​
278 it("allows path trigger detection to close after selecting a mention", () => {
279 const text = "what's in my @AGENTS.md ";
280 const collapsedCursorAfterMention = "what's in my ".length + 2;
281 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
282​
283 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
284 });
285​
286 it("maps collapsed skill cursor to expanded text cursor", () => {
287 const text = "run $review-follow-up then";
288 const collapsedCursorAfterSkill = "run ".length + 2;
289 const expandedCursorAfterSkill = "run $review-follow-up ".length;
290​
291 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterSkill)).toBe(
292 expandedCursorAfterSkill,
293 );
294 });
295});
296​
297describe("collapseExpandedComposerCursor", () => {
298 it("keeps cursor unchanged when no mention segment is present", () => {
299 expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);
300 });
301​
302 it("maps expanded mention cursor back to collapsed cursor", () => {
303 const text = "what's in my @AGENTS.md fsfdas";
304 const collapsedCursorAfterMention = "what's in my ".length + 2;
305 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
306​
307 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
308 collapsedCursorAfterMention,
309 );
310 });
311​
312 it("maps expanded quoted mention cursor back to collapsed cursor", () => {
313 const text = 'what is in @"My File.md" please';
314 const collapsedCursorAfterMention = "what is in ".length + 2;
315 const expandedCursorAfterMention = 'what is in @"My File.md" '.length;
316​
317 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
318 collapsedCursorAfterMention,
319 );
320 });
321​
322 it("maps expanded markdown file link cursors back to collapsed offsets", () => {
323 const text = "what's in [AGENTS.md](AGENTS.md) please";
324 const collapsedCursorAfterMention = "what's in ".length + 2;
325 const expandedCursorAfterMention = "what's in [AGENTS.md](AGENTS.md) ".length;
326​
327 expect(collapseExpandedComposerCursor(text, expandedCursorAfterMention)).toBe(
328 collapsedCursorAfterMention,
329 );
330 });
331​
332 it("keeps package-like text expanded when another mention already exists earlier", () => {
333 const text = "open @AGENTS.md then @src/index.ts ";
334 const expandedCursor = text.length;
335 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
336​
337 expect(collapsedCursor).toBe("open ".length + 1 + " then @src/index.ts ".length);
338 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
339 });
340​
341 it("collapses only genuine mentions when package-like text exists earlier", () => {
342 const text = "install @scope/pkg then @README.md ";
343 const expandedCursor = text.length;
344 const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
345​
346 expect(collapsedCursor).toBe("install @scope/pkg then ".length + 1 + " ".length);
347 expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
348 });
349​
350 it("maps expanded skill cursor back to collapsed cursor", () => {
351 const text = "run $review-follow-up then";
352 const collapsedCursorAfterSkill = "run ".length + 2;
353 const expandedCursorAfterSkill = "run $review-follow-up ".length;
354​
355 expect(collapseExpandedComposerCursor(text, expandedCursorAfterSkill)).toBe(
356 collapsedCursorAfterSkill,
357 );
358 });
359});
360​
361describe("clampCollapsedComposerCursor", () => {
362 it("clamps to collapsed prompt length when mentions are present", () => {
363 const text = "open @AGENTS.md then ";
364​
365 expect(clampCollapsedComposerCursor(text, text.length)).toBe(
366 "open ".length + 1 + " then ".length,
367 );
368 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(
369 "open ".length + 1 + " then ".length,
370 );
371 });
372});
373​
374describe("assistant citation cursor offsets", () => {
375 it("roundtrips every collapsed offset across citations, mentions, skills, and Unicode", () => {
376 const prefix = "πŸ‘‹(";
377 const between = "),ι›ͺ";
378 const after = " @AGENTS.md $review ";
379 const text = `${prefix}${citationSource}${between}${citationSource}${after}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}!`;
380 const collapsedLength = `${prefix}β–‘${between}β–‘ β–‘ β–‘ β–‘!`.length;
381 const boundaries = [
382 [prefix.length, prefix.length],
383 [prefix.length + 1, prefix.length + citationSource.length],
384 [prefix.length + 1 + between.length, prefix.length + citationSource.length + between.length],
385 [
386 prefix.length + 2 + between.length,
387 prefix.length + citationSource.length * 2 + between.length,
388 ],
389 [collapsedLength, text.length],
390 ] as const;
391​
392 for (const [collapsed, expanded] of boundaries) {
393 expect(expandCollapsedComposerCursor(text, collapsed)).toBe(expanded);
394 expect(collapseExpandedComposerCursor(text, expanded)).toBe(collapsed);
395 }
396 for (let cursor = 0; cursor <= collapsedLength; cursor += 1) {
397 expect(
398 collapseExpandedComposerCursor(text, expandCollapsedComposerCursor(text, cursor)),
399 ).toBe(cursor);
400 }
401 expect(clampCollapsedComposerCursor(text, Number.POSITIVE_INFINITY)).toBe(collapsedLength);
402 });
403​
404 it("snaps serialized offsets inside a citation to the end of its chip", () => {
405 const prefix = "ε‰πŸ‘‹";
406 const text = `${prefix}${citationSource}.`;
407​
408 for (const offset of [1, citationSource.length - 1, citationSource.length]) {
409 expect(collapseExpandedComposerCursor(text, prefix.length + offset)).toBe(prefix.length + 1);
410 }
411 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length, "right")).toBe(true);
412 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length + 1, "left")).toBe(true);
413 expect(isCollapsedCursorAdjacentToInlineToken(text, prefix.length + 2, "left")).toBe(false);
414 });
415​
416 it("deletes one collapsed citation without changing its neighbor or surrounding punctuation", () => {
417 const text = `(${citationSource}),${citationSource}!`;
418 const result = replaceTextRange(
419 text,
420 expandCollapsedComposerCursor(text, 1),
421 expandCollapsedComposerCursor(text, 2),
422 "",
423 );
424​
425 expect(result).toEqual({ text: `(),${citationSource}!`, cursor: 1 });
426 expect(collapseExpandedComposerCursor(result.text, result.text.length)).toBe("(),β–‘!".length);
427 });
428});
429​
430describe("replaceTextRange trailing space consumption", () => {
431 it("double space after insertion when replacement ends with space", () => {
432 // Simulates: "and then |@AG| summarize" where | marks replacement range
433 // The replacement is "@AGENTS.md " (with trailing space)
434 // But if we don't extend rangeEnd, the existing space stays
435 const text = "and then @AG summarize";
436 const rangeStart = "and then ".length;
437 const rangeEnd = "and then @AG".length;
438​
439 // Without consuming trailing space: double space
440 const withoutConsume = replaceTextRange(text, rangeStart, rangeEnd, "@AGENTS.md ");
441 expect(withoutConsume.text).toBe("and then @AGENTS.md summarize");
442​
443 // With consuming trailing space: single space
444 const extendedEnd = text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
445 const withConsume = replaceTextRange(text, rangeStart, extendedEnd, "@AGENTS.md ");
446 expect(withConsume.text).toBe("and then @AGENTS.md summarize");
447 });
448});
449​
450describe("isCollapsedCursorAdjacentToInlineToken", () => {
451 it("returns false when no mention exists", () => {
452 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "left")).toBe(false);
453 expect(isCollapsedCursorAdjacentToInlineToken("plain text", 6, "right")).toBe(false);
454 });
455​
456 it("keeps @query typing non-adjacent while no mention pill exists", () => {
457 const text = "hello @pac";
458 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "left")).toBe(false);
459 expect(isCollapsedCursorAdjacentToInlineToken(text, text.length, "right")).toBe(false);
460 });
461​
462 it("detects left adjacency only when cursor is directly after a mention", () => {
463 const text = "open @AGENTS.md next";
464 const mentionStart = "open ".length;
465 const mentionEnd = mentionStart + 1;
466​
467 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "left")).toBe(true);
468 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "left")).toBe(false);
469 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd + 1, "left")).toBe(false);
470 });
471​
472 it("detects right adjacency only when cursor is directly before a mention", () => {
473 const text = "open @AGENTS.md next";
474 const mentionStart = "open ".length;
475 const mentionEnd = mentionStart + 1;
476​
477 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart, "right")).toBe(true);
478 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionEnd, "right")).toBe(false);
479 expect(isCollapsedCursorAdjacentToInlineToken(text, mentionStart - 1, "right")).toBe(false);
480 });
481​
482 it("treats terminal pills as inline tokens for adjacency checks", () => {
483 const text = `open ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} next`;
484 const tokenStart = "open ".length;
485 const tokenEnd = tokenStart + 1;
486​
487 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
488 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
489 });
490​
491 it("treats skill pills as inline tokens for adjacency checks", () => {
492 const text = "run $review-follow-up next";
493 const tokenStart = "run ".length;
494 const tokenEnd = tokenStart + 1;
495​
496 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenEnd, "left")).toBe(true);
497 expect(isCollapsedCursorAdjacentToInlineToken(text, tokenStart, "right")).toBe(true);
498 });
499});
500​
501describe("parseStandaloneComposerSlashCommand", () => {
502 it("parses standalone /plan command", () => {
503 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
504 });
505​
506 it("parses standalone /default command", () => {
507 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
508 });
509​
510 it("ignores slash commands with extra message text", () => {
511 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
512 });
513});