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

composer-editor-mentions.test.ts

339 lines11.6 KB

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 { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
10​
11const citation: AssistantCitation = {
12 version: 1,
13 environmentId: EnvironmentId.make("remote/環境"),
14 threadId: ThreadId.make("thread-1"),
15 messageId: MessageId.make("message-1"),
16 text: 'Use @AGENTS.md, $review and "雪 ❄️" (carefully).',
17 start: 4,
18 end: 50,
19 prefix: "前: ",
20 suffix: " 後",
21};
22​
23describe("splitPromptIntoComposerSegments", () => {
24 it("splits mention tokens followed by whitespace into mention segments", () => {
25 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md please")).toEqual([
26 { type: "text", text: "Inspect " },
27 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
28 { type: "text", text: " please" },
29 ]);
30 });
31​
32 it("does not convert an incomplete trailing mention token", () => {
33 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md")).toEqual([
34 { type: "text", text: "Inspect @AGENTS.md" },
35 ]);
36 });
37​
38 it("keeps newlines around mention tokens", () => {
39 expect(splitPromptIntoComposerSegments("one\n@AGENTS.md \ntwo")).toEqual([
40 { type: "text", text: "one\n" },
41 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
42 { type: "text", text: " \ntwo" },
43 ]);
44 });
45​
46 it("splits quoted mention tokens containing whitespace", () => {
47 expect(splitPromptIntoComposerSegments('Inspect @"My File.md" please')).toEqual([
48 { type: "text", text: "Inspect " },
49 { type: "mention", path: "My File.md", source: '@"My File.md"' },
50 { type: "text", text: " please" },
51 ]);
52 });
53​
54 it("unescapes quoted mention token content", () => {
55 expect(splitPromptIntoComposerSegments('Inspect @"docs/My \\"File\\".md" please')).toEqual([
56 { type: "text", text: "Inspect " },
57 {
58 type: "mention",
59 path: 'docs/My "File".md',
60 source: '@"docs/My \\"File\\".md"',
61 },
62 { type: "text", text: " please" },
63 ]);
64 });
65​
66 it("splits generated markdown file links into mention segments", () => {
67 expect(
68 splitPromptIntoComposerSegments(
69 "Inspect [package.json](path/to/package.json) before continuing",
70 ),
71 ).toEqual([
72 { type: "text", text: "Inspect " },
73 {
74 type: "mention",
75 path: "path/to/package.json",
76 source: "[package.json](path/to/package.json)",
77 },
78 { type: "text", text: " before continuing" },
79 ]);
80 });
81​
82 it("does not turn normal web links into file mention segments", () => {
83 expect(
84 splitPromptIntoComposerSegments("Read [the docs](https://example.com/docs) first"),
85 ).toEqual([{ type: "text", text: "Read [the docs](https://example.com/docs) first" }]);
86 });
87​
88 it("keeps multiple assistant citations atomic next to punctuation and Unicode", () => {
89 const source = serializeAssistantCitation(citation);
90 const otherCitation = {
91 ...citation,
92 messageId: MessageId.make("message-2"),
93 text: "A second quote",
94 };
95 const otherSource = serializeAssistantCitation(otherCitation);
96​
97 expect(splitPromptIntoComposerSegments(`前(${source}),${otherSource}後`)).toEqual([
98 { type: "text", text: "前(" },
99 { type: "citation", citation, source },
100 { type: "text", text: ")," },
101 { type: "citation", citation: otherCitation, source: otherSource },
102 { type: "text", text: "後" },
103 ]);
104 });
105​
106 it("preserves exact citation source encoding for adjacent chips at the end of a prompt", () => {
107 const source = serializeAssistantCitation(citation).replaceAll("+", "%20");
108​
109 expect(splitPromptIntoComposerSegments(`${source}${source}`)).toEqual([
110 { type: "citation", citation, source },
111 { type: "citation", citation, source },
112 ]);
113 });
114​
115 it.each(["@", "@AGENTS.md"])(
116 "keeps a citation after the unfinished mention %s intact",
117 (prefix) => {
118 const source = serializeAssistantCitation(citation);
119​
120 expect(splitPromptIntoComposerSegments(`${prefix}${source}`)).toEqual([
121 { type: "text", text: prefix },
122 { type: "citation", citation, source },
123 ]);
124 },
125 );
126​
127 it("parses citations alongside file mentions, skills, and terminal contexts", () => {
128 const source = serializeAssistantCitation(citation);
129​
130 expect(
131 splitPromptIntoComposerSegments(
132 `@AGENTS.md ${source}\n$review ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${source}`,
133 ),
134 ).toEqual([
135 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
136 { type: "text", text: " " },
137 { type: "citation", citation, source },
138 { type: "text", text: "\n" },
139 { type: "skill", name: "review" },
140 { type: "text", text: " " },
141 { type: "terminal-context", context: null },
142 { type: "citation", citation, source },
143 ]);
144 });
145​
146 it("keeps malformed citation links as editable text", () => {
147 const prompt = "[Assistant quote](t3-citation://v1/env/thread/message?text=missing+metadata)";
148​
149 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
150 });
151​
152 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
153 "does not turn scoped package reference %s into file mention segments",
154 (reference) => {
155 const prompt = `Install ${reference} next`;
156 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
157 },
158 );
159​
160 it("keeps IME-composed text containing a scoped package reference as text", () => {
161 const prompt = "入力 @expo/ui を追加";
162 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
163 });
164​
165 it("turns canonical scoped folder links into file mention segments", () => {
166 expect(splitPromptIntoComposerSegments("Inspect [sub](@scope/pkg/sub) next")).toEqual([
167 { type: "text", text: "Inspect " },
168 {
169 type: "mention",
170 path: "@scope/pkg/sub",
171 source: "[sub](@scope/pkg/sub)",
172 },
173 { type: "text", text: " next" },
174 ]);
175 });
176​
177 it("decodes reserved path characters from generated links", () => {
178 expect(
179 splitPromptIntoComposerSegments(
180 "Inspect [config#draft?.json](config%23draft%3F.json) before continuing",
181 ),
182 ).toEqual([
183 { type: "text", text: "Inspect " },
184 {
185 type: "mention",
186 path: "config#draft?.json",
187 source: "[config#draft?.json](config%23draft%3F.json)",
188 },
189 { type: "text", text: " before continuing" },
190 ]);
191 });
192​
193 it("splits skill tokens followed by whitespace into skill segments", () => {
194 expect(splitPromptIntoComposerSegments("Use $review-follow-up please")).toEqual([
195 { type: "text", text: "Use " },
196 { type: "skill", name: "review-follow-up" },
197 { type: "text", text: " please" },
198 ]);
199 });
200​
201 it("splits digit-leading skill tokens into skill segments", () => {
202 expect(splitPromptIntoComposerSegments("Use $2spec please")).toEqual([
203 { type: "text", text: "Use " },
204 { type: "skill", name: "2spec" },
205 { type: "text", text: " please" },
206 ]);
207 });
208​
209 it("keeps digits-only dollar amounts and compact monetary expressions as text", () => {
210 expect(splitPromptIntoComposerSegments("I'll pay $20 tomorrow")).toEqual([
211 { type: "text", text: "I'll pay $20 tomorrow" },
212 ]);
213 expect(splitPromptIntoComposerSegments("Budget is $20k tomorrow")).toEqual([
214 { type: "text", text: "Budget is $20k tomorrow" },
215 ]);
216 expect(splitPromptIntoComposerSegments("Cost is $100M total")).toEqual([
217 { type: "text", text: "Cost is $100M total" },
218 ]);
219 expect(splitPromptIntoComposerSegments("Limit is $1e6 here")).toEqual([
220 { type: "text", text: "Limit is $1e6 here" },
221 ]);
222 });
223​
224 it("does not convert an incomplete trailing skill token", () => {
225 expect(splitPromptIntoComposerSegments("Use $review-follow-up")).toEqual([
226 { type: "text", text: "Use $review-follow-up" },
227 ]);
228 });
229​
230 it("keeps inline terminal context placeholders at their prompt positions", () => {
231 expect(
232 splitPromptIntoComposerSegments(
233 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md please`,
234 ),
235 ).toEqual([
236 { type: "text", text: "Inspect " },
237 { type: "terminal-context", context: null },
238 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
239 { type: "text", text: " please" },
240 ]);
241 });
242​
243 it("preserves consecutive terminal context placeholders without dropping positions", () => {
244 expect(
245 splitPromptIntoComposerSegments(
246 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
247 ),
248 ).toEqual([
249 { type: "terminal-context", context: null },
250 { type: "terminal-context", context: null },
251 { type: "text", text: "tail" },
252 ]);
253 });
254​
255 it("keeps skill parsing alongside mentions and terminal placeholders", () => {
256 expect(
257 splitPromptIntoComposerSegments(
258 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}$review-follow-up after @AGENTS.md `,
259 ),
260 ).toEqual([
261 { type: "text", text: "Inspect " },
262 { type: "terminal-context", context: null },
263 { type: "skill", name: "review-follow-up" },
264 { type: "text", text: " after " },
265 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
266 { type: "text", text: " " },
267 ]);
268 });
269});
270​
271describe("selectionTouchesMentionBoundary", () => {
272 it("does not treat text before a citation as an overlapping file mention", () => {
273 const prompt = `before @${serializeAssistantCitation(citation)}`;
274​
275 expect(selectionTouchesMentionBoundary(prompt, "before".length, "before ".length)).toBe(false);
276 });
277​
278 it("returns true when selection includes the whitespace after a mention", () => {
279 expect(
280 selectionTouchesMentionBoundary(
281 "hi @package.json there",
282 "hi @package.json".length,
283 "hi @package.json there".length,
284 ),
285 ).toBe(true);
286 });
287​
288 it("returns true when selection includes the whitespace before a mention", () => {
289 expect(
290 selectionTouchesMentionBoundary(
291 "hi there @package.json later",
292 "hi there".length,
293 "hi there ".length,
294 ),
295 ).toBe(true);
296 });
297​
298 it("returns false when selection starts after the mention boundary whitespace", () => {
299 expect(
300 selectionTouchesMentionBoundary(
301 "hi @package.json there",
302 "hi @package.json ".length,
303 "hi @package.json there".length,
304 ),
305 ).toBe(false);
306 });
307​
308 it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
309 const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
310 expect(
311 selectionTouchesMentionBoundary(
312 prompt,
313 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
314 prompt.length,
315 ),
316 ).toBe(true);
317 });
318​
319 it("returns true when selection includes whitespace after a quoted mention", () => {
320 expect(
321 selectionTouchesMentionBoundary(
322 'hi @"My File.md" there',
323 'hi @"My File.md"'.length,
324 'hi @"My File.md" there'.length,
325 ),
326 ).toBe(true);
327 });
328​
329 it("returns true when selection includes whitespace after a markdown file link", () => {
330 const prompt = "hi [package.json](path/to/package.json) there";
331 expect(
332 selectionTouchesMentionBoundary(
333 prompt,
334 "hi [package.json](path/to/package.json)".length,
335 prompt.length,
336 ),
337 ).toBe(true);
338 });
339});