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

composerInlineTokens.test.ts

220 lines6.8 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { collectComposerInlineTokens } from "./composerInlineTokens.ts";
4​
5describe("collectComposerInlineTokens", () => {
6 it("collects file links, mentions, and skills with source ranges", () => {
7 const text = "Use $ui and inspect [Chat.tsx](src/Chat.tsx) with @AGENTS.md please";
8​
9 expect(collectComposerInlineTokens(text)).toEqual([
10 {
11 type: "skill",
12 value: "ui",
13 source: "$ui",
14 start: 4,
15 end: 7,
16 },
17 {
18 type: "mention",
19 value: "src/Chat.tsx",
20 source: "[Chat.tsx](src/Chat.tsx)",
21 start: 20,
22 end: 44,
23 },
24 {
25 type: "mention",
26 value: "AGENTS.md",
27 source: "@AGENTS.md",
28 start: 50,
29 end: 60,
30 },
31 ]);
32 });
33​
34 it.each(["$", "€", "£", "¥", "₹", "₩", "₿", "𑿝"])(
35 "collects %s skill names that begin with a digit",
36 (prefix) => {
37 expect(collectComposerInlineTokens(`Use ${prefix}2spec next`)).toEqual([
38 {
39 type: "skill",
40 value: "2spec",
41 source: `${prefix}2spec`,
42 start: 4,
43 end: 9 + prefix.length,
44 },
45 ]);
46 },
47 );
48​
49 it.each(["€", "£", "¥", "₹", "₩", "₿", "𑿝"])("leaves %s currency amounts as text", (prefix) => {
50 expect(
51 collectComposerInlineTokens(
52 `${prefix}20 ${prefix}1_000 ${prefix}20k ${prefix}100M ${prefix}1e6 `,
53 ),
54 ).toEqual([]);
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("keeps the delimiter after a token outside its source range", () => {
71 const text = "Inspect [package.json](package.json) next";
72​
73 expect(collectComposerInlineTokens(text)).toEqual([
74 {
75 type: "mention",
76 value: "package.json",
77 source: "[package.json](package.json)",
78 start: 8,
79 end: 36,
80 },
81 ]);
82 expect(text.slice(36)).toBe(" next");
83 });
84​
85 it("preserves a confirmed pill when only its trailing delimiter is removed", () => {
86 const withDelimiter = "[package.json](package.json) ";
87 const confirmed = collectComposerInlineTokens(withDelimiter);
88​
89 expect(
90 collectComposerInlineTokens(withDelimiter.trimEnd(), { preserveTrailingFrom: confirmed }),
91 ).toEqual([
92 {
93 type: "mention",
94 value: "package.json",
95 source: "[package.json](package.json)",
96 start: 0,
97 end: 28,
98 },
99 ]);
100 });
101​
102 it("does not preserve a pill after its source is edited", () => {
103 const confirmed = collectComposerInlineTokens("[package.json](package.json) ");
104​
105 expect(
106 collectComposerInlineTokens("[package.json](package-json)", {
107 preserveTrailingFrom: confirmed,
108 }),
109 ).toEqual([]);
110 });
111​
112 it("ignores normal web links", () => {
113 expect(collectComposerInlineTokens("Read [docs](https://example.com) first")).toEqual([]);
114 });
115​
116 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
117 "keeps scoped package reference %s as plain text",
118 (reference) => {
119 expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
120 },
121 );
122​
123 it("keeps scoped package references plain across incomplete input and IME whitespace", () => {
124 expect(collectComposerInlineTokens("Install @expo/ui")).toEqual([]);
125 expect(collectComposerInlineTokens("入力 @expo/ui を追加")).toEqual([]);
126 });
127​
128 it.each(["@namespace.foo(bar)", "@écrire(option)", "@e\u0301crire(option)"])(
129 "keeps decorator call %s as plain text",
130 (reference) => {
131 expect(collectComposerInlineTokens(`${reference}\nclass Example {}`)).toEqual([]);
132 },
133 );
134​
135 it.each(['@"foo(bar)"', "[foo(bar)](foo%28bar%29)"])(
136 "keeps an explicit decorator-shaped file reference as a mention: %s",
137 (reference) => {
138 expect(collectComposerInlineTokens(`${reference} `)).toEqual([
139 {
140 type: "mention",
141 value: "foo(bar)",
142 source: reference,
143 start: 0,
144 end: reference.length,
145 },
146 ]);
147 },
148 );
149​
150 it("keeps bare file mentions before whitespace-separated parentheses", () => {
151 expect(collectComposerInlineTokens("Inspect @README.md (notes)")).toEqual([
152 {
153 type: "mention",
154 value: "README.md",
155 source: "@README.md",
156 start: 8,
157 end: 18,
158 },
159 ]);
160 });
161​
162 it("keeps canonical file links for scoped paths as mentions", () => {
163 expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
164 {
165 type: "mention",
166 value: "@scope/pkg/sub",
167 source: "[sub](@scope/pkg/sub)",
168 start: 8,
169 end: 29,
170 },
171 ]);
172 });
173​
174 it("allows ambiguous scoped paths through explicit quoted mentions", () => {
175 expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
176 {
177 type: "mention",
178 value: "expo/ui",
179 source: '@"expo/ui"',
180 start: 8,
181 end: 18,
182 },
183 ]);
184 });
185​
186 it("still collects a file link whose label is at the length cap", () => {
187 const label = `${"a".repeat(508)}.tsx`;
188 const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
189​
190 expect(tokens).toHaveLength(1);
191 expect(tokens[0]?.value).toBe(`src/${label}`);
192 });
193​
194 it("leaves a file link past the label cap as plain text", () => {
195 const label = `${"a".repeat(509)}.tsx`;
196 expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
197 });
198​
199 it("leaves a context reference link alone", () => {
200 expect(
201 collectComposerInlineTokens("see [checkout.png](t3-context://v1/image/ctx_abc) ok"),
202 ).toEqual([]);
203 expect(collectComposerInlineTokens("see ![ctx_abc](t3-context://v1/image/ctx_abc) ok")).toEqual(
204 [],
205 );
206 });
207​
208 it("stays fast on unterminated bracket runs", () => {
209 const started = performance.now();
210 expect(collectComposerInlineTokens(" [[".repeat(40_000))).toEqual([]);
211 expect(performance.now() - started).toBeLessThan(1_000);
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});