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 it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
205 "preserves a confirmed TeX-like pill when its trailing delimiter is removed: %s",
206 (path) => {
207 const link = serializeComposerFileLink(path);
208 const confirmed = collectComposerInlineTokens(`fix ${link} `);
209
210 expect(confirmed).toMatchObject([{ type: "mention", value: path, source: link }]);
211 expect(
212 collectComposerInlineTokens(`fix ${link}`, { preserveTrailingFrom: confirmed }),
213 ).toEqual(confirmed);
214 expect(collectComposerInlineTokens(`fix ${link}`)).toEqual([]);
215 },
216 );
217
218 it.each([
219 "before $x + @README.md$ after",
220 "before \\(x + @README.md\\) after",
221 "\\[x + @README.md\\] after",
222 "\\[\\text{use @README.md}\\] then go ",
223 ])("keeps a mention that ends a formula inside the formula: %s", (source) => {
224 expect(collectComposerInlineTokens(source)).toEqual([]);
225 });
226
227 it("still finds a formula after a mention that starts the line", () => {
228 const link = serializeComposerFileLink("app/routes/posts.$postId.tsx");
229 expect(
230 composerMathRanges(`${link} sets $x_i$`, markdownMathRanges).map((formula) => formula.source),
231 ).toEqual(["$x_i$"]);
232 });
233});
234
235describe("composerFileLinksToPlainText", () => {
236 it("shows file mentions by name and leaves other links as written", () => {
237 const mentions = ["src/foo.ts", ".gitignore", "pkg/__init__.py", "docs/My File (draft).md"]
238 .map(serializeComposerFileLink)
239 .join(" ");
240 expect(composerFileLinksToPlainText(`Review ${mentions}`)).toBe(
241 "Review foo.ts .gitignore __init__.py My File (draft).md",
242 );
243 const links = "See [docs](https://example.com) and [the config](src/config.ts)";
244 expect(composerFileLinksToPlainText(links)).toBe(links);
245 });
246});