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

composerInlineTokens.test.ts

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