Open History to see the last commit
1import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
2import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 selectionTouchesMentionBoundary,
7 splitPromptIntoComposerSegments,
8} from "./composer-editor-mentions";
9import { formatTerminalContextReference } from "./lib/terminalContext";
10
11const terminalReference = formatTerminalContextReference({
12 id: "ctx-1",
13 terminalLabel: "Terminal 1",
14 lineStart: 3,
15 lineEnd: 4,
16});
17const terminalSegment = {
18 type: "context-reference" as const,
19 kind: "terminal",
20 contextId: "terminal_ctx-1",
21 label: "Terminal 1 lines 3-4",
22 source: terminalReference,
23};
24
25const citation: AssistantCitation = {
26 version: 1,
27 environmentId: EnvironmentId.make("remote/環境"),
28 threadId: ThreadId.make("thread-1"),
29 messageId: MessageId.make("message-1"),
30 text: 'Use @AGENTS.md, $review and "雪 ❄️" (carefully).',
31 start: 4,
32 end: 50,
33 prefix: "前: ",
34 suffix: " 後",
35};
36
37describe("splitPromptIntoComposerSegments", () => {
38 it("splits mention tokens followed by whitespace into mention segments", () => {
39 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md please")).toEqual([
40 { type: "text", text: "Inspect " },
41 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
42 { type: "text", text: " please" },
43 ]);
44 });
45
46 it("does not convert an incomplete trailing mention token", () => {
47 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md")).toEqual([
48 { type: "text", text: "Inspect @AGENTS.md" },
49 ]);
50 });
51
52 it("splits quoted mention tokens containing whitespace", () => {
53 expect(splitPromptIntoComposerSegments('Inspect @"My File.md" please')).toEqual([
54 { type: "text", text: "Inspect " },
55 { type: "mention", path: "My File.md", source: '@"My File.md"' },
56 { type: "text", text: " please" },
57 ]);
58 });
59
60 it("unescapes quoted mention token content", () => {
61 expect(splitPromptIntoComposerSegments('Inspect @"docs/My \\"File\\".md" please')).toEqual([
62 { type: "text", text: "Inspect " },
63 {
64 type: "mention",
65 path: 'docs/My "File".md',
66 source: '@"docs/My \\"File\\".md"',
67 },
68 { type: "text", text: " please" },
69 ]);
70 });
71
72 it("splits generated markdown file links into mention segments", () => {
73 expect(
74 splitPromptIntoComposerSegments(
75 "Inspect [package.json](path/to/package.json) before continuing",
76 ),
77 ).toEqual([
78 { type: "text", text: "Inspect " },
79 {
80 type: "mention",
81 path: "path/to/package.json",
82 source: "[package.json](path/to/package.json)",
83 },
84 { type: "text", text: " before continuing" },
85 ]);
86 });
87
88 it("does not turn normal web links into file mention segments", () => {
89 expect(
90 splitPromptIntoComposerSegments("Read [the docs](https://example.com/docs) first"),
91 ).toEqual([{ type: "text", text: "Read [the docs](https://example.com/docs) first" }]);
92 });
93
94 it("keeps multiple assistant citations atomic next to punctuation and Unicode", () => {
95 const source = serializeAssistantCitation(citation);
96 const otherCitation = {
97 ...citation,
98 messageId: MessageId.make("message-2"),
99 text: "A second quote",
100 };
101 const otherSource = serializeAssistantCitation(otherCitation);
102
103 expect(splitPromptIntoComposerSegments(`前(${source}),${otherSource}後`)).toEqual([
104 { type: "text", text: "前(" },
105 { type: "citation", citation, source },
106 { type: "text", text: ")," },
107 { type: "citation", citation: otherCitation, source: otherSource },
108 { type: "text", text: "後" },
109 ]);
110 });
111
112 it("preserves exact citation source encoding for adjacent chips at the end of a prompt", () => {
113 const source = serializeAssistantCitation(citation).replaceAll("+", "%20");
114
115 expect(splitPromptIntoComposerSegments(`${source}${source}`)).toEqual([
116 { type: "citation", citation, source },
117 { type: "citation", citation, source },
118 ]);
119 });
120
121 it.each(["@", "@AGENTS.md"])(
122 "keeps a citation after the unfinished mention %s intact",
123 (prefix) => {
124 const source = serializeAssistantCitation(citation);
125
126 expect(splitPromptIntoComposerSegments(`${prefix}${source}`)).toEqual([
127 { type: "text", text: prefix },
128 { type: "citation", citation, source },
129 ]);
130 },
131 );
132
133 it("parses citations alongside file mentions, skills, and context references", () => {
134 const source = serializeAssistantCitation(citation);
135 const reference = formatTerminalContextReference({
136 id: "ctx-1",
137 terminalLabel: "Terminal 1",
138 lineStart: 3,
139 lineEnd: 4,
140 });
141
142 expect(
143 splitPromptIntoComposerSegments(`@AGENTS.md ${source}\n$review ${reference}${source}`),
144 ).toEqual([
145 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
146 { type: "text", text: " " },
147 { type: "citation", citation, source },
148 { type: "text", text: "\n" },
149 { type: "skill", name: "review", source: "$review" },
150 { type: "text", text: " " },
151 {
152 type: "context-reference",
153 kind: "terminal",
154 contextId: "terminal_ctx-1",
155 label: "Terminal 1 lines 3-4",
156 source: reference,
157 },
158 { type: "citation", citation, source },
159 ]);
160 });
161
162 it("keeps malformed citation links as editable text", () => {
163 const prompt = "[Assistant quote](t3-citation://v1/env/thread/message?text=missing+metadata)";
164
165 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
166 });
167
168 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
169 "does not turn scoped package reference %s into file mention segments",
170 (reference) => {
171 const prompt = `Install ${reference} next`;
172 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
173 },
174 );
175
176 it("turns canonical scoped folder links into file mention segments", () => {
177 expect(splitPromptIntoComposerSegments("Inspect [sub](@scope/pkg/sub) next")).toEqual([
178 { type: "text", text: "Inspect " },
179 {
180 type: "mention",
181 path: "@scope/pkg/sub",
182 source: "[sub](@scope/pkg/sub)",
183 },
184 { type: "text", text: " next" },
185 ]);
186 });
187
188 it("decodes reserved path characters from generated links", () => {
189 expect(
190 splitPromptIntoComposerSegments(
191 "Inspect [config#draft?.json](config%23draft%3F.json) before continuing",
192 ),
193 ).toEqual([
194 { type: "text", text: "Inspect " },
195 {
196 type: "mention",
197 path: "config#draft?.json",
198 source: "[config#draft?.json](config%23draft%3F.json)",
199 },
200 { type: "text", text: " before continuing" },
201 ]);
202 });
203
204 it("splits skill tokens followed by whitespace into skill segments", () => {
205 expect(splitPromptIntoComposerSegments("Use $review-follow-up please")).toEqual([
206 { type: "text", text: "Use " },
207 { type: "skill", name: "review-follow-up", source: "$review-follow-up" },
208 { type: "text", text: " please" },
209 ]);
210 });
211
212 it("splits digit-leading skill tokens into skill segments", () => {
213 expect(splitPromptIntoComposerSegments("Use $2spec please")).toEqual([
214 { type: "text", text: "Use " },
215 { type: "skill", name: "2spec", source: "$2spec" },
216 { type: "text", text: " please" },
217 ]);
218 });
219
220 it("keeps digits-only dollar amounts and compact monetary expressions as text", () => {
221 expect(splitPromptIntoComposerSegments("I'll pay $20 tomorrow")).toEqual([
222 { type: "text", text: "I'll pay $20 tomorrow" },
223 ]);
224 expect(splitPromptIntoComposerSegments("Budget is $20k tomorrow")).toEqual([
225 { type: "text", text: "Budget is $20k tomorrow" },
226 ]);
227 expect(splitPromptIntoComposerSegments("Cost is $100M total")).toEqual([
228 { type: "text", text: "Cost is $100M total" },
229 ]);
230 expect(splitPromptIntoComposerSegments("Limit is $1e6 here")).toEqual([
231 { type: "text", text: "Limit is $1e6 here" },
232 ]);
233 });
234
235 it("keeps context references at their prompt positions", () => {
236 expect(
237 splitPromptIntoComposerSegments(`Inspect ${terminalReference} @AGENTS.md please`),
238 ).toEqual([
239 { type: "text", text: "Inspect " },
240 terminalSegment,
241 { type: "text", text: " " },
242 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
243 { type: "text", text: " please" },
244 ]);
245 });
246});
247
248describe("selectionTouchesMentionBoundary", () => {
249 it("does not treat text before a citation as an overlapping file mention", () => {
250 const prompt = `before @${serializeAssistantCitation(citation)}`;
251
252 expect(selectionTouchesMentionBoundary(prompt, "before".length, "before ".length)).toBe(false);
253 });
254
255 it("returns true when selection includes the whitespace after a mention", () => {
256 expect(
257 selectionTouchesMentionBoundary(
258 "hi @package.json there",
259 "hi @package.json".length,
260 "hi @package.json there".length,
261 ),
262 ).toBe(true);
263 });
264
265 it("returns false when selection starts after the mention boundary whitespace", () => {
266 expect(
267 selectionTouchesMentionBoundary(
268 "hi @package.json there",
269 "hi @package.json ".length,
270 "hi @package.json there".length,
271 ),
272 ).toBe(false);
273 });
274
275 // regression: #2625 mentions with spaces are quoted; selection checks must span the quotes
276 it("returns true when selection includes whitespace after a quoted mention", () => {
277 expect(
278 selectionTouchesMentionBoundary(
279 'hi @"My File.md" there',
280 'hi @"My File.md"'.length,
281 'hi @"My File.md" there'.length,
282 ),
283 ).toBe(true);
284 });
285});