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

composerInlineTokens.test.ts

291 lines9.7 KB

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("collects a file link after an unclosed bracket", () => {
177 expect(collectComposerInlineTokens("See [WIP and [app.ts](src/app.ts) please")).toEqual([
178 {
179 type: "mention",
180 value: "src/app.ts",
181 source: "[app.ts](src/app.ts)",
182 start: 13,
183 end: 33,
184 },
185 ]);
186 });
187​
188 it("leaves a context reference link alone", () => {
189 expect(
190 collectComposerInlineTokens("see [checkout.png](t3-context://v1/image/ctx_abc) ok"),
191 ).toEqual([]);
192 expect(collectComposerInlineTokens("see ![ctx_abc](t3-context://v1/image/ctx_abc) ok")).toEqual(
193 [],
194 );
195 });
196​
197 it("keeps skill and mention sources inside TeX out of the context chip list", () => {
198 const source = "\\[\\text{use $browser and @README.md here}\\] then $real-skill ";
199 expect(collectComposerInlineTokens(source).map((token) => token.source)).toEqual([
200 "$real-skill",
201 ]);
202 });
203​
204 it.each([
205 "app/routes/posts.$postId.tsx",
206 "src/routes/_chat.$environmentId.$threadId.tsx",
207 "pages/posts/[id].tsx",
208 "app/blog/[...slug].tsx",
209 ])("keeps a file link whose name looks like TeX as one mention: %s", (path) => {
210 const link = serializeComposerFileLink(path);
211 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
212 { type: "mention", value: path, source: link },
213 ]);
214 });
215​
216 // regression: #41223 the composer's `./` is not part of the mention's path
217 it.each([
218 "C#/Program.cs",
219 "docs/R&D/notes.md",
220 "docs/Bob's notes.md",
221 "docs/a,b.ts",
222 "docs/100%.md",
223 "docs/why?.md",
224 "icon@2x.png",
225 ])("reads a link to the punctuated file %s as that file's path", (path) => {
226 const link = serializeComposerFileLink(path);
227 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
228 { type: "mention", value: path, source: link },
229 ]);
230 });
231​
232 // regression: #41223 a `./` the author wrote keeps its meaning, even where the parser rejects the rest
233 it.each([
234 ["[draft:notes.md](./draft:notes.md)", "./draft:notes.md"],
235 ["[scratch](./%2Fscratch)", ".//scratch"],
236 ["[a.ts](./src/a.ts)", "./src/a.ts"],
237 ])("keeps the authored link %s as its own path", (link, value) => {
238 expect(collectComposerInlineTokens(`fix ${link} please`)).toMatchObject([
239 { type: "mention", value, source: link },
240 ]);
241 });
242​
243 it.each(["app/routes/posts.$postId.tsx", "pages/posts/[id].tsx"])(
244 "preserves a confirmed TeX-like pill when its trailing delimiter is removed: %s",
245 (path) => {
246 const link = serializeComposerFileLink(path);
247 const confirmed = collectComposerInlineTokens(`fix ${link} `);
248​
249 expect(confirmed).toMatchObject([{ type: "mention", value: path, source: link }]);
250 expect(
251 collectComposerInlineTokens(`fix ${link}`, { preserveTrailingFrom: confirmed }),
252 ).toEqual(confirmed);
253 expect(collectComposerInlineTokens(`fix ${link}`)).toEqual([]);
254 },
255 );
256​
257 it.each([
258 "before $x + @README.md$ after",
259 "before \\(x + @README.md\\) after",
260 "\\[x + @README.md\\] after",
261 "\\[\\text{use @README.md}\\] then go ",
262 ])("keeps a mention that ends a formula inside the formula: %s", (source) => {
263 expect(collectComposerInlineTokens(source)).toEqual([]);
264 });
265​
266 it.each([
267 [`${serializeComposerFileLink("app/routes/posts.$postId.tsx")} sets $x_i$`, ["$x_i$"]],
268 ["[Fix $x$ parsing](t3-context://v1/thread/thread_1) sets $x_i$", ["$x_i$"]],
269 ["Use $ui with [posts.$b.tsx](t3-context://v1/file/ctx_1) ", []],
270 ])("ignores TeX delimiters inside file links and context references: %s", (text, formulas) => {
271 expect(composerMathRanges(text, markdownMathRanges).map((formula) => formula.source)).toEqual(
272 formulas,
273 );
274 });
275});
276​
277describe("composerFileLinksToPlainText", () => {
278 it("shows file mentions by name and leaves other links as written", () => {
279 const mentions = ["src/foo.ts", ".gitignore", "pkg/__init__.py", "docs/My File (draft).md"]
280 .map(serializeComposerFileLink)
281 .join(" ");
282 expect(composerFileLinksToPlainText(`Review ${mentions}`)).toBe(
283 "Review foo.ts .gitignore __init__.py My File (draft).md",
284 );
285 const links = "See [docs](https://example.com) and [the config](src/config.ts)";
286 expect(composerFileLinksToPlainText(links)).toBe(links);
287 expect(composerFileLinksToPlainText("See [WIP and [app.ts](src/app.ts) please")).toBe(
288 "See [WIP and app.ts please",
289 );
290 });
291});