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

composer-editor-mentions.test.ts

316 lines10.7 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("does not convert an incomplete trailing skill token", () => {
202 expect(splitPromptIntoComposerSegments("Use $review-follow-up")).toEqual([
203 { type: "text", text: "Use $review-follow-up" },
204 ]);
205 });
206​
207 it("keeps inline terminal context placeholders at their prompt positions", () => {
208 expect(
209 splitPromptIntoComposerSegments(
210 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md please`,
211 ),
212 ).toEqual([
213 { type: "text", text: "Inspect " },
214 { type: "terminal-context", context: null },
215 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
216 { type: "text", text: " please" },
217 ]);
218 });
219​
220 it("preserves consecutive terminal context placeholders without dropping positions", () => {
221 expect(
222 splitPromptIntoComposerSegments(
223 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
224 ),
225 ).toEqual([
226 { type: "terminal-context", context: null },
227 { type: "terminal-context", context: null },
228 { type: "text", text: "tail" },
229 ]);
230 });
231​
232 it("keeps skill parsing alongside mentions and terminal placeholders", () => {
233 expect(
234 splitPromptIntoComposerSegments(
235 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}$review-follow-up after @AGENTS.md `,
236 ),
237 ).toEqual([
238 { type: "text", text: "Inspect " },
239 { type: "terminal-context", context: null },
240 { type: "skill", name: "review-follow-up" },
241 { type: "text", text: " after " },
242 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
243 { type: "text", text: " " },
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 true when selection includes the whitespace before a mention", () => {
266 expect(
267 selectionTouchesMentionBoundary(
268 "hi there @package.json later",
269 "hi there".length,
270 "hi there ".length,
271 ),
272 ).toBe(true);
273 });
274​
275 it("returns false when selection starts after the mention boundary whitespace", () => {
276 expect(
277 selectionTouchesMentionBoundary(
278 "hi @package.json there",
279 "hi @package.json ".length,
280 "hi @package.json there".length,
281 ),
282 ).toBe(false);
283 });
284​
285 it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
286 const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
287 expect(
288 selectionTouchesMentionBoundary(
289 prompt,
290 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
291 prompt.length,
292 ),
293 ).toBe(true);
294 });
295​
296 it("returns true when selection includes whitespace after a quoted mention", () => {
297 expect(
298 selectionTouchesMentionBoundary(
299 'hi @"My File.md" there',
300 'hi @"My File.md"'.length,
301 'hi @"My File.md" there'.length,
302 ),
303 ).toBe(true);
304 });
305​
306 it("returns true when selection includes whitespace after a markdown file link", () => {
307 const prompt = "hi [package.json](path/to/package.json) there";
308 expect(
309 selectionTouchesMentionBoundary(
310 prompt,
311 "hi [package.json](path/to/package.json)".length,
312 prompt.length,
313 ),
314 ).toBe(true);
315 });
316});