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

composer-editor-mentions.test.ts

232 lines7.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 selectionTouchesMentionBoundary,
5 splitPromptIntoComposerSegments,
6} from "./composer-editor-mentions";
7import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
8​
9describe("splitPromptIntoComposerSegments", () => {
10 it("splits mention tokens followed by whitespace into mention segments", () => {
11 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md please")).toEqual([
12 { type: "text", text: "Inspect " },
13 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
14 { type: "text", text: " please" },
15 ]);
16 });
17​
18 it("does not convert an incomplete trailing mention token", () => {
19 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md")).toEqual([
20 { type: "text", text: "Inspect @AGENTS.md" },
21 ]);
22 });
23​
24 it("keeps newlines around mention tokens", () => {
25 expect(splitPromptIntoComposerSegments("one\n@AGENTS.md \ntwo")).toEqual([
26 { type: "text", text: "one\n" },
27 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
28 { type: "text", text: " \ntwo" },
29 ]);
30 });
31​
32 it("splits quoted mention tokens containing whitespace", () => {
33 expect(splitPromptIntoComposerSegments('Inspect @"My File.md" please')).toEqual([
34 { type: "text", text: "Inspect " },
35 { type: "mention", path: "My File.md", source: '@"My File.md"' },
36 { type: "text", text: " please" },
37 ]);
38 });
39​
40 it("unescapes quoted mention token content", () => {
41 expect(splitPromptIntoComposerSegments('Inspect @"docs/My \\"File\\".md" please')).toEqual([
42 { type: "text", text: "Inspect " },
43 {
44 type: "mention",
45 path: 'docs/My "File".md',
46 source: '@"docs/My \\"File\\".md"',
47 },
48 { type: "text", text: " please" },
49 ]);
50 });
51​
52 it("splits generated markdown file links into mention segments", () => {
53 expect(
54 splitPromptIntoComposerSegments(
55 "Inspect [package.json](path/to/package.json) before continuing",
56 ),
57 ).toEqual([
58 { type: "text", text: "Inspect " },
59 {
60 type: "mention",
61 path: "path/to/package.json",
62 source: "[package.json](path/to/package.json)",
63 },
64 { type: "text", text: " before continuing" },
65 ]);
66 });
67​
68 it("does not turn normal web links into file mention segments", () => {
69 expect(
70 splitPromptIntoComposerSegments("Read [the docs](https://example.com/docs) first"),
71 ).toEqual([{ type: "text", text: "Read [the docs](https://example.com/docs) first" }]);
72 });
73​
74 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
75 "does not turn scoped package reference %s into file mention segments",
76 (reference) => {
77 const prompt = `Install ${reference} next`;
78 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
79 },
80 );
81​
82 it("keeps IME-composed text containing a scoped package reference as text", () => {
83 const prompt = "入力 @expo/ui を追加";
84 expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
85 });
86​
87 it("turns canonical scoped folder links into file mention segments", () => {
88 expect(splitPromptIntoComposerSegments("Inspect [sub](@scope/pkg/sub) next")).toEqual([
89 { type: "text", text: "Inspect " },
90 {
91 type: "mention",
92 path: "@scope/pkg/sub",
93 source: "[sub](@scope/pkg/sub)",
94 },
95 { type: "text", text: " next" },
96 ]);
97 });
98​
99 it("decodes reserved path characters from generated links", () => {
100 expect(
101 splitPromptIntoComposerSegments(
102 "Inspect [config#draft?.json](config%23draft%3F.json) before continuing",
103 ),
104 ).toEqual([
105 { type: "text", text: "Inspect " },
106 {
107 type: "mention",
108 path: "config#draft?.json",
109 source: "[config#draft?.json](config%23draft%3F.json)",
110 },
111 { type: "text", text: " before continuing" },
112 ]);
113 });
114​
115 it("splits skill tokens followed by whitespace into skill segments", () => {
116 expect(splitPromptIntoComposerSegments("Use $review-follow-up please")).toEqual([
117 { type: "text", text: "Use " },
118 { type: "skill", name: "review-follow-up" },
119 { type: "text", text: " please" },
120 ]);
121 });
122​
123 it("does not convert an incomplete trailing skill token", () => {
124 expect(splitPromptIntoComposerSegments("Use $review-follow-up")).toEqual([
125 { type: "text", text: "Use $review-follow-up" },
126 ]);
127 });
128​
129 it("keeps inline terminal context placeholders at their prompt positions", () => {
130 expect(
131 splitPromptIntoComposerSegments(
132 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md please`,
133 ),
134 ).toEqual([
135 { type: "text", text: "Inspect " },
136 { type: "terminal-context", context: null },
137 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
138 { type: "text", text: " please" },
139 ]);
140 });
141​
142 it("preserves consecutive terminal context placeholders without dropping positions", () => {
143 expect(
144 splitPromptIntoComposerSegments(
145 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
146 ),
147 ).toEqual([
148 { type: "terminal-context", context: null },
149 { type: "terminal-context", context: null },
150 { type: "text", text: "tail" },
151 ]);
152 });
153​
154 it("keeps skill parsing alongside mentions and terminal placeholders", () => {
155 expect(
156 splitPromptIntoComposerSegments(
157 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}$review-follow-up after @AGENTS.md `,
158 ),
159 ).toEqual([
160 { type: "text", text: "Inspect " },
161 { type: "terminal-context", context: null },
162 { type: "skill", name: "review-follow-up" },
163 { type: "text", text: " after " },
164 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
165 { type: "text", text: " " },
166 ]);
167 });
168});
169​
170describe("selectionTouchesMentionBoundary", () => {
171 it("returns true when selection includes the whitespace after a mention", () => {
172 expect(
173 selectionTouchesMentionBoundary(
174 "hi @package.json there",
175 "hi @package.json".length,
176 "hi @package.json there".length,
177 ),
178 ).toBe(true);
179 });
180​
181 it("returns true when selection includes the whitespace before a mention", () => {
182 expect(
183 selectionTouchesMentionBoundary(
184 "hi there @package.json later",
185 "hi there".length,
186 "hi there ".length,
187 ),
188 ).toBe(true);
189 });
190​
191 it("returns false when selection starts after the mention boundary whitespace", () => {
192 expect(
193 selectionTouchesMentionBoundary(
194 "hi @package.json there",
195 "hi @package.json ".length,
196 "hi @package.json there".length,
197 ),
198 ).toBe(false);
199 });
200​
201 it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
202 const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
203 expect(
204 selectionTouchesMentionBoundary(
205 prompt,
206 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
207 prompt.length,
208 ),
209 ).toBe(true);
210 });
211​
212 it("returns true when selection includes whitespace after a quoted mention", () => {
213 expect(
214 selectionTouchesMentionBoundary(
215 'hi @"My File.md" there',
216 'hi @"My File.md"'.length,
217 'hi @"My File.md" there'.length,
218 ),
219 ).toBe(true);
220 });
221​
222 it("returns true when selection includes whitespace after a markdown file link", () => {
223 const prompt = "hi [package.json](path/to/package.json) there";
224 expect(
225 selectionTouchesMentionBoundary(
226 prompt,
227 "hi [package.json](path/to/package.json)".length,
228 prompt.length,
229 ),
230 ).toBe(true);
231 });
232});