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

composerInlineTokens.test.ts

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