Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 collectComposerInlineTokens as collectWithoutMath,
5 type CollectComposerInlineTokensOptions,
6 composerFileLinksToPlainText,
7 composerMathRanges,
8 restoreTrailingTokenSpace,
9} from "./composerInlineTokens.ts";
10import { markdownMathRanges } from "./markdownMath.ts";
11import { serializeComposerFileLink } from "./composerTrigger.ts";
12
13const collectComposerInlineTokens = (
14 text: string,
15 options: CollectComposerInlineTokensOptions = {},
16) => collectWithoutMath(text, { findMath: markdownMathRanges, ...options });
17
18describe("collectComposerInlineTokens", () => {
19 it("collects file links, mentions, and skills with source ranges", () => {
20 const text = "Use $ui and inspect [Chat.tsx](src/Chat.tsx) with @AGENTS.md please";
21
22 expect(collectComposerInlineTokens(text)).toEqual([
23 {
24 type: "skill",
25 value: "ui",
26 source: "$ui",
27 start: 4,
28 end: 7,
29 },
30 {
31 type: "mention",
32 value: "src/Chat.tsx",
33 source: "[Chat.tsx](src/Chat.tsx)",
34 start: 20,
35 end: 44,
36 },
37 {
38 type: "mention",
39 value: "AGENTS.md",
40 source: "@AGENTS.md",
41 start: 50,
42 end: 60,
43 },
44 ]);
45 });
46
47 it.each(["$", "€", "𑿝"])("collects %s skill names that begin with a digit", (prefix) => {
48 expect(collectComposerInlineTokens(`Use ${prefix}2spec next`)).toEqual([
49 {
50 type: "skill",
51 value: "2spec",
52 source: `${prefix}2spec`,
53 start: 4,
54 end: 9 + prefix.length,
55 },
56 ]);
57 });
58
59 it("leaves digits-only dollar amounts and compact monetary expressions as text", () => {
60 expect(collectComposerInlineTokens("I'll pay $20 tomorrow")).toEqual([]);
61 expect(collectComposerInlineTokens("Budget is $1_000 total")).toEqual([]);
62 expect(collectComposerInlineTokens("Budget is $20k tomorrow")).toEqual([]);
63 expect(collectComposerInlineTokens("Cost is $100M total")).toEqual([]);
64 expect(collectComposerInlineTokens("Limit is $1e6 here")).toEqual([]);
65 });
66
67 it("does not convert incomplete trailing tokens", () => {
68 expect(collectComposerInlineTokens("Use $ui")).toEqual([]);
69 expect(collectComposerInlineTokens("Inspect @AGENTS.md")).toEqual([]);
70 });
71
72 it("preserves a confirmed pill when only its trailing delimiter is removed", () => {
73 const withDelimiter = "[package.json](package.json) ";
74 const confirmed = collectComposerInlineTokens(withDelimiter);
75
76 expect(
77 collectComposerInlineTokens(withDelimiter.trimEnd(), { preserveTrailingFrom: confirmed }),
78 ).toEqual([
79 {
80 type: "mention",
81 value: "package.json",
82 source: "[package.json](package.json)",
83 start: 0,
84 end: 28,
85 },
86 ]);
87 });
88
89 it("does not preserve a pill after its source is edited", () => {
90 const confirmed = collectComposerInlineTokens("[package.json](package.json) ");
91
92 expect(
93 collectComposerInlineTokens("[package.json](package-json)", {
94 preserveTrailingFrom: confirmed,
95 }),
96 ).toEqual([]);
97 });
98
99 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
100 "keeps scoped package reference %s as plain text",
101 (reference) => {
102 expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
103 },
104 );
105
106 it.each(["@namespace.foo(bar)", "@écrire(option)", "@e\u0301crire(option)"])(
107 "keeps decorator call %s as plain text",
108 (reference) => {
109 expect(collectComposerInlineTokens(`${reference}\nclass Example {}`)).toEqual([]);
110 },
111 );
112
113 it.each(['@"foo(bar)"', "[foo(bar)](foo%28bar%29)"])(
114 "keeps an explicit decorator-shaped file reference as a mention: %s",
115 (reference) => {
116 expect(collectComposerInlineTokens(`${reference} `)).toEqual([
117 {
118 type: "mention",
119 value: "foo(bar)",
120 source: reference,
121 start: 0,
122 end: reference.length,
123 },
124 ]);
125 },
126 );
127
128 it("keeps bare file mentions before whitespace-separated parentheses", () => {
129 expect(collectComposerInlineTokens("Inspect @README.md (notes)")).toEqual([
130 {
131 type: "mention",
132 value: "README.md",
133 source: "@README.md",
134 start: 8,
135 end: 18,
136 },
137 ]);
138 });
139
140 it("keeps canonical file links for scoped paths as mentions", () => {
141 expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
142 {
143 type: "mention",
144 value: "@scope/pkg/sub",
145 source: "[sub](@scope/pkg/sub)",
146 start: 8,
147 end: 29,
148 },
149 ]);
150 });
151
152 it("allows ambiguous scoped paths through explicit quoted mentions", () => {
153 expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
154 {
155 type: "mention",
156 value: "expo/ui",
157 source: '@"expo/ui"',
158 start: 8,
159 end: 18,
160 },
161 ]);
162 });
163
164 it.each([
165 [String.raw`@"C:\My Files\notes.md"`, String.raw`C:\My Files\notes.md`],
166 [String.raw`@"C:\\My Files\\notes.md"`, String.raw`C:\My Files\notes.md`],
167 [String.raw`@"docs/My \"draft\".md"`, 'docs/My "draft".md'],
168 ])("decodes quoted file mention %s without losing path characters", (source, value) => {
169 expect(collectComposerInlineTokens(`${source} `)).toEqual([
170 {
171 type: "mention",
172 value,
173 source,
174 start: 0,
175 end: source.length,
176 },
177 ]);
178 });
179
180 it("still collects a file link whose label is at the length cap", () => {
181 const label = `${"a".repeat(508)}.tsx`;
182 const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
183
184 expect(tokens).toHaveLength(1);
185 expect(tokens[0]?.value).toBe(`src/${label}`);
186 });
187
188 it("leaves a file link past the label cap as plain text", () => {
189 const label = `${"a".repeat(509)}.tsx`;
190 expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
191 });
192
193 it("collects a file link after an unclosed bracket", () => {
194 expect(collectComposerInlineTokens("See [WIP and [app.ts](src/app.ts) please")).toEqual([
195 {
196 type: "mention",
197 value: "src/app.ts",
198 source: "[app.ts](src/app.ts)",
199 start: 13,
200 end: 33,
201 },
202 ]);
203 });
204
205 it("leaves a context reference link alone", () => {
206 expect(
207 collectComposerInlineTokens("see [checkout.png](t3-context://v1/image/ctx_abc) ok"),
208 ).toEqual([]);
209 expect(collectComposerInlineTokens("see  ok")).toEqual(
210 [],
211 );
212 });
213
214 it("keeps skill and mention sources inside TeX out of the context chip list", () => {
215 const source = "\\[\\text{use $browser and @README.md here}\\] then $real-skill ";
216 expect(collectComposerInlineTokens(source).map((token) => token.source)).toEqual([
217 "$real-skill",
218 ]);
219 });
220
221 it.each([
222 "app/routes/posts.$postId.tsx",
223 "src/routes/_chat.$environmentId.$threadId.tsx",
224 "pages/posts/[id].tsx",
225 "app/blog/[...slug].tsx",
226 "notes/[TODO].md",
227 "notes/~~draft~~.md",
228 "notes/[TODO].md",
229 `notes/${"[".repeat(120)}].md`,
230 ])("keeps a file link whose name looks like TeX as one mention: %s", (path) => {
231 const link = serializeComposerFileLink(path);
232 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
233 { type: "mention", value: path, source: link },
234 ]);
235 });
236
237 it("still reads legacy backslash-escaped bracket file links as mentions", () => {
238 const link = String.raw`[\[TODO\].md](notes/%5BTODO%5D.md)`;
239 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
240 { type: "mention", value: "notes/[TODO].md", source: link },
241 ]);
242 });
243
244 // regression: #41223 the composer's `./` is not part of the mention's path
245 it.each([
246 "C#/Program.cs",
247 "docs/R&D/notes.md",
248 "docs/Bob's notes.md",
249 "docs/a,b.ts",
250 "docs/100%.md",
251 "docs/why?.md",
252 "icon@2x.png",
253 "reports:final.md",
254 "file:notes.md",
255 ])("reads a link to the punctuated file %s as that file's path", (path) => {
256 const link = serializeComposerFileLink(path);
257 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
258 { type: "mention", value: path, source: link },
259 ]);
260 });
261
262 it("keeps external links out of file chips even with matching labels", () => {
263 for (const link of [
264 "[docs](https://example.com/docs)",
265 "[reports:final.md](reports:final.md)",
266 "[guide.md](//example.com/docs/guide.md)",
267 ]) {
268 expect(collectComposerInlineTokens(`${link} `)).toEqual([]);
269 expect(composerFileLinksToPlainText(`${link} `)).toBe(`${link} `);
270 }
271 });
272
273 it("keeps a UNC path, whose leading slashes are encoded, as a file chip", () => {
274 const path = "\\\\server\\docs\\guide.md";
275 const link = serializeComposerFileLink(path);
276 expect(collectComposerInlineTokens(`${link} `)).toMatchObject([
277 { type: "mention", value: path, source: link },
278 ]);
279 });
280
281 // regression: #41223 a `./` the author wrote keeps its meaning, even where the parser rejects the rest
282 it.each([
283 ["[scratch](./%2Fscratch)", ".//scratch"],
284 ["[a.ts](./src/a.ts)", "./src/a.ts"],
285 ])("keeps the authored link %s as its own path", (link, value) => {
286 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
287 { type: "mention", value, source: link },
288 ]);
289 });
290
291 it.each(["docs/🧪 @abc final.md", "docs/use $ui kit.md"])(
292 "does not emit nested tokens inside a file link label: %s",
293 (path) => {
294 const link = serializeComposerFileLink(path);
295 expect(collectComposerInlineTokens(`${link} remainder`)).toEqual([
296 {
297 type: "mention",
298 value: path,
299 source: link,
300 start: 0,
301 end: link.length,
302 },
303 ]);
304 },
305 );
306
307 it("keeps only the preserved trailing file link when its label contains a bare mention", () => {
308 const path = "docs/🧪 @abc final.md";
309 const link = serializeComposerFileLink(path);
310 const confirmed = collectComposerInlineTokens(`${link} `);
311
312 expect(collectComposerInlineTokens(link, { preserveTrailingFrom: confirmed })).toEqual([
313 {
314 type: "mention",
315 value: path,
316 source: link,
317 start: 0,
318 end: link.length,
319 },
320 ]);
321 });
322
323 it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
324 "preserves a confirmed TeX-like pill when its trailing delimiter is removed: %s",
325 (path) => {
326 const link = serializeComposerFileLink(path);
327 const confirmed = collectComposerInlineTokens(`fix ${link} `);
328
329 expect(confirmed).toMatchObject([{ type: "mention", value: path, source: link }]);
330 expect(
331 collectComposerInlineTokens(`fix ${link}`, { preserveTrailingFrom: confirmed }),
332 ).toEqual(confirmed);
333 expect(collectComposerInlineTokens(`fix ${link}`)).toEqual([]);
334 },
335 );
336
337 it.each([
338 "before $x + @README.md$ after",
339 "before \\(x + @README.md\\) after",
340 "\\[x + @README.md\\] after",
341 "\\[\\text{use @README.md}\\] then go ",
342 ])("keeps a mention that ends a formula inside the formula: %s", (source) => {
343 expect(collectComposerInlineTokens(source)).toEqual([]);
344 });
345
346 it.each([
347 [`${serializeComposerFileLink("app/routes/posts.$postId.tsx")} sets $x_i$`, ["$x_i$"]],
348 ["[Fix $x$ parsing](t3-context://v1/thread/thread_1) sets $x_i$", ["$x_i$"]],
349 ["Use $ui with [posts.$b.tsx](t3-context://v1/file/ctx_1) ", []],
350 ])("ignores TeX delimiters inside file links and context references: %s", (text, formulas) => {
351 expect(composerMathRanges(text, markdownMathRanges).map((formula) => formula.source)).toEqual(
352 formulas,
353 );
354 });
355});
356
357describe("composerFileLinksToPlainText", () => {
358 it("shows file mentions by name and leaves other links as written", () => {
359 const mentions = [
360 "src/foo.ts",
361 ".gitignore",
362 "pkg/__init__.py",
363 "docs/My File (draft).md",
364 "notes/[TODO].md",
365 "notes/~~draft~~.md",
366 "notes/[TODO].md",
367 ]
368 .map(serializeComposerFileLink)
369 .join(" ");
370 expect(composerFileLinksToPlainText(`Review ${mentions}`)).toBe(
371 "Review foo.ts .gitignore __init__.py My File (draft).md [TODO].md ~~draft~~.md [TODO].md",
372 );
373 const links = "See [docs](https://example.com) and [the config](src/config.ts)";
374 expect(composerFileLinksToPlainText(links)).toBe(links);
375 expect(composerFileLinksToPlainText("See [WIP and [app.ts](src/app.ts) please")).toBe(
376 "See [WIP and app.ts please",
377 );
378 });
379});
380
381describe("restoreTrailingTokenSpace", () => {
382 const collect = (text: string) => collectComposerInlineTokens(text);
383
384 it("gives a sent prompt's trailing skill or mention back the space that makes it a chip", () => {
385 expect(restoreTrailingTokenSpace("use $ui-taste $ui-ux-pro-max", collect)).toBe(
386 "use $ui-taste $ui-ux-pro-max ",
387 );
388 expect(restoreTrailingTokenSpace("look at @AGENTS.md", collect)).toBe("look at @AGENTS.md ");
389 });
390
391 it("leaves prose, prices, and formulas as sent", () => {
392 expect(restoreTrailingTokenSpace("use $ui-taste please", collect)).toBe("use $ui-taste please");
393 expect(restoreTrailingTokenSpace("it costs $5", collect)).toBe("it costs $5");
394 expect(restoreTrailingTokenSpace("solve $x + y$", collect)).toBe("solve $x + y$");
395 const chipsAtEnd = (text: string) => [{ end: text.trimEnd().length }];
396 expect(restoreTrailingTokenSpace("already a chip", chipsAtEnd)).toBe("already a chip");
397 });
398});