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

composerInlineTokens.test.ts

232 lines7.5 KB

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