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} from "./composerInlineTokens.ts";
9import { markdownMathRanges } from "./markdownMath.ts";
10import { serializeComposerFileLink } from "./composerTrigger.ts";
11
12const collectComposerInlineTokens = (
13 text: string,
14 options: CollectComposerInlineTokensOptions = {},
15) => collectWithoutMath(text, { findMath: markdownMathRanges, ...options });
16
17describe("collectComposerInlineTokens", () => {
18 it("collects file links, mentions, and skills with source ranges", () => {
19 const text = "Use $ui and inspect [Chat.tsx](src/Chat.tsx) with @AGENTS.md please";
20
21 expect(collectComposerInlineTokens(text)).toEqual([
22 {
23 type: "skill",
24 value: "ui",
25 source: "$ui",
26 start: 4,
27 end: 7,
28 },
29 {
30 type: "mention",
31 value: "src/Chat.tsx",
32 source: "[Chat.tsx](src/Chat.tsx)",
33 start: 20,
34 end: 44,
35 },
36 {
37 type: "mention",
38 value: "AGENTS.md",
39 source: "@AGENTS.md",
40 start: 50,
41 end: 60,
42 },
43 ]);
44 });
45
46 it.each(["$", "€", "𑿝"])("collects %s skill names that begin with a digit", (prefix) => {
47 expect(collectComposerInlineTokens(`Use ${prefix}2spec next`)).toEqual([
48 {
49 type: "skill",
50 value: "2spec",
51 source: `${prefix}2spec`,
52 start: 4,
53 end: 9 + prefix.length,
54 },
55 ]);
56 });
57
58 it("leaves digits-only dollar amounts and compact monetary expressions as text", () => {
59 expect(collectComposerInlineTokens("I'll pay $20 tomorrow")).toEqual([]);
60 expect(collectComposerInlineTokens("Budget is $1_000 total")).toEqual([]);
61 expect(collectComposerInlineTokens("Budget is $20k tomorrow")).toEqual([]);
62 expect(collectComposerInlineTokens("Cost is $100M total")).toEqual([]);
63 expect(collectComposerInlineTokens("Limit is $1e6 here")).toEqual([]);
64 });
65
66 it("does not convert incomplete trailing tokens", () => {
67 expect(collectComposerInlineTokens("Use $ui")).toEqual([]);
68 expect(collectComposerInlineTokens("Inspect @AGENTS.md")).toEqual([]);
69 });
70
71 it("preserves a confirmed pill when only its trailing delimiter is removed", () => {
72 const withDelimiter = "[package.json](package.json) ";
73 const confirmed = collectComposerInlineTokens(withDelimiter);
74
75 expect(
76 collectComposerInlineTokens(withDelimiter.trimEnd(), { preserveTrailingFrom: confirmed }),
77 ).toEqual([
78 {
79 type: "mention",
80 value: "package.json",
81 source: "[package.json](package.json)",
82 start: 0,
83 end: 28,
84 },
85 ]);
86 });
87
88 it("does not preserve a pill after its source is edited", () => {
89 const confirmed = collectComposerInlineTokens("[package.json](package.json) ");
90
91 expect(
92 collectComposerInlineTokens("[package.json](package-json)", {
93 preserveTrailingFrom: confirmed,
94 }),
95 ).toEqual([]);
96 });
97
98 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
99 "keeps scoped package reference %s as plain text",
100 (reference) => {
101 expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
102 },
103 );
104
105 it.each(["@namespace.foo(bar)", "@écrire(option)", "@e\u0301crire(option)"])(
106 "keeps decorator call %s as plain text",
107 (reference) => {
108 expect(collectComposerInlineTokens(`${reference}\nclass Example {}`)).toEqual([]);
109 },
110 );
111
112 it.each(['@"foo(bar)"', "[foo(bar)](foo%28bar%29)"])(
113 "keeps an explicit decorator-shaped file reference as a mention: %s",
114 (reference) => {
115 expect(collectComposerInlineTokens(`${reference} `)).toEqual([
116 {
117 type: "mention",
118 value: "foo(bar)",
119 source: reference,
120 start: 0,
121 end: reference.length,
122 },
123 ]);
124 },
125 );
126
127 it("keeps bare file mentions before whitespace-separated parentheses", () => {
128 expect(collectComposerInlineTokens("Inspect @README.md (notes)")).toEqual([
129 {
130 type: "mention",
131 value: "README.md",
132 source: "@README.md",
133 start: 8,
134 end: 18,
135 },
136 ]);
137 });
138
139 it("keeps canonical file links for scoped paths as mentions", () => {
140 expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
141 {
142 type: "mention",
143 value: "@scope/pkg/sub",
144 source: "[sub](@scope/pkg/sub)",
145 start: 8,
146 end: 29,
147 },
148 ]);
149 });
150
151 it("allows ambiguous scoped paths through explicit quoted mentions", () => {
152 expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
153 {
154 type: "mention",
155 value: "expo/ui",
156 source: '@"expo/ui"',
157 start: 8,
158 end: 18,
159 },
160 ]);
161 });
162
163 it("still collects a file link whose label is at the length cap", () => {
164 const label = `${"a".repeat(508)}.tsx`;
165 const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
166
167 expect(tokens).toHaveLength(1);
168 expect(tokens[0]?.value).toBe(`src/${label}`);
169 });
170
171 it("leaves a file link past the label cap as plain text", () => {
172 const label = `${"a".repeat(509)}.tsx`;
173 expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
174 });
175
176 it("leaves a context reference link alone", () => {
177 expect(
178 collectComposerInlineTokens("see [checkout.png](t3-context://v1/image/ctx_abc) ok"),
179 ).toEqual([]);
180 expect(collectComposerInlineTokens("see  ok")).toEqual(
181 [],
182 );
183 });
184
185 it("keeps skill and mention sources inside TeX out of the context chip list", () => {
186 const source = "\\[\\text{use $browser and @README.md here}\\] then $real-skill ";
187 expect(collectComposerInlineTokens(source).map((token) => token.source)).toEqual([
188 "$real-skill",
189 ]);
190 });
191
192 it.each([
193 "app/routes/posts.$postId.tsx",
194 "src/routes/_chat.$environmentId.$threadId.tsx",
195 "pages/posts/[id].tsx",
196 "app/blog/[...slug].tsx",
197 ])("keeps a file link whose name looks like TeX as one mention: %s", (path) => {
198 const link = serializeComposerFileLink(path);
199 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
200 { type: "mention", value: path, source: link },
201 ]);
202 });
203
204 // regression: #41223 the composer's `./` is not part of the mention's path
205 it.each([
206 "C#/Program.cs",
207 "docs/R&D/notes.md",
208 "docs/Bob's notes.md",
209 "docs/a,b.ts",
210 "docs/100%.md",
211 "docs/why?.md",
212 "icon@2x.png",
213 ])("reads a link to the punctuated file %s as that file's path", (path) => {
214 const link = serializeComposerFileLink(path);
215 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
216 { type: "mention", value: path, source: link },
217 ]);
218 });
219
220 // regression: #41223 a `./` the author wrote keeps its meaning, even where the parser rejects the rest
221 it.each([
222 ["[draft:notes.md](./draft:notes.md)", "./draft:notes.md"],
223 ["[scratch](./%2Fscratch)", ".//scratch"],
224 ["[a.ts](./src/a.ts)", "./src/a.ts"],
225 ])("keeps the authored link %s as its own path", (link, value) => {
226 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
227 { type: "mention", value, source: link },
228 ]);
229 });
230
231 it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
232 "preserves a confirmed TeX-like pill when its trailing delimiter is removed: %s",
233 (path) => {
234 const link = serializeComposerFileLink(path);
235 const confirmed = collectComposerInlineTokens(`fix ${link} `);
236
237 expect(confirmed).toMatchObject([{ type: "mention", value: path, source: link }]);
238 expect(
239 collectComposerInlineTokens(`fix ${link}`, { preserveTrailingFrom: confirmed }),
240 ).toEqual(confirmed);
241 expect(collectComposerInlineTokens(`fix ${link}`)).toEqual([]);
242 },
243 );
244
245 it.each([
246 "before $x + @README.md$ after",
247 "before \\(x + @README.md\\) after",
248 "\\[x + @README.md\\] after",
249 "\\[\\text{use @README.md}\\] then go ",
250 ])("keeps a mention that ends a formula inside the formula: %s", (source) => {
251 expect(collectComposerInlineTokens(source)).toEqual([]);
252 });
253
254 it("still finds a formula after a mention that starts the line", () => {
255 const link = serializeComposerFileLink("app/routes/posts.$postId.tsx");
256 expect(
257 composerMathRanges(`${link} sets $x_i$`, markdownMathRanges).map((formula) => formula.source),
258 ).toEqual(["$x_i$"]);
259 });
260});
261
262describe("composerFileLinksToPlainText", () => {
263 it("shows file mentions by name and leaves other links as written", () => {
264 const mentions = ["src/foo.ts", ".gitignore", "pkg/__init__.py", "docs/My File (draft).md"]
265 .map(serializeComposerFileLink)
266 .join(" ");
267 expect(composerFileLinksToPlainText(`Review ${mentions}`)).toBe(
268 "Review foo.ts .gitignore __init__.py My File (draft).md",
269 );
270 const links = "See [docs](https://example.com) and [the config](src/config.ts)";
271 expect(composerFileLinksToPlainText(links)).toBe(links);
272 });
273});