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

composer-editor-mentions.test.ts

207 lines6.8 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@src/index.ts \ntwo")).toEqual([
26 { type: "text", text: "one\n" },
27 { type: "mention", path: "src/index.ts", source: "@src/index.ts" },
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("decodes reserved path characters from generated links", () => {
75 expect(
76 splitPromptIntoComposerSegments(
77 "Inspect [config#draft?.json](config%23draft%3F.json) before continuing",
78 ),
79 ).toEqual([
80 { type: "text", text: "Inspect " },
81 {
82 type: "mention",
83 path: "config#draft?.json",
84 source: "[config#draft?.json](config%23draft%3F.json)",
85 },
86 { type: "text", text: " before continuing" },
87 ]);
88 });
89​
90 it("splits skill tokens followed by whitespace into skill segments", () => {
91 expect(splitPromptIntoComposerSegments("Use $review-follow-up please")).toEqual([
92 { type: "text", text: "Use " },
93 { type: "skill", name: "review-follow-up" },
94 { type: "text", text: " please" },
95 ]);
96 });
97​
98 it("does not convert an incomplete trailing skill token", () => {
99 expect(splitPromptIntoComposerSegments("Use $review-follow-up")).toEqual([
100 { type: "text", text: "Use $review-follow-up" },
101 ]);
102 });
103​
104 it("keeps inline terminal context placeholders at their prompt positions", () => {
105 expect(
106 splitPromptIntoComposerSegments(
107 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md please`,
108 ),
109 ).toEqual([
110 { type: "text", text: "Inspect " },
111 { type: "terminal-context", context: null },
112 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
113 { type: "text", text: " please" },
114 ]);
115 });
116​
117 it("preserves consecutive terminal context placeholders without dropping positions", () => {
118 expect(
119 splitPromptIntoComposerSegments(
120 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
121 ),
122 ).toEqual([
123 { type: "terminal-context", context: null },
124 { type: "terminal-context", context: null },
125 { type: "text", text: "tail" },
126 ]);
127 });
128​
129 it("keeps skill parsing alongside mentions and terminal placeholders", () => {
130 expect(
131 splitPromptIntoComposerSegments(
132 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}$review-follow-up after @AGENTS.md `,
133 ),
134 ).toEqual([
135 { type: "text", text: "Inspect " },
136 { type: "terminal-context", context: null },
137 { type: "skill", name: "review-follow-up" },
138 { type: "text", text: " after " },
139 { type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
140 { type: "text", text: " " },
141 ]);
142 });
143});
144​
145describe("selectionTouchesMentionBoundary", () => {
146 it("returns true when selection includes the whitespace after a mention", () => {
147 expect(
148 selectionTouchesMentionBoundary(
149 "hi @package.json there",
150 "hi @package.json".length,
151 "hi @package.json there".length,
152 ),
153 ).toBe(true);
154 });
155​
156 it("returns true when selection includes the whitespace before a mention", () => {
157 expect(
158 selectionTouchesMentionBoundary(
159 "hi there @package.json later",
160 "hi there".length,
161 "hi there ".length,
162 ),
163 ).toBe(true);
164 });
165​
166 it("returns false when selection starts after the mention boundary whitespace", () => {
167 expect(
168 selectionTouchesMentionBoundary(
169 "hi @package.json there",
170 "hi @package.json ".length,
171 "hi @package.json there".length,
172 ),
173 ).toBe(false);
174 });
175​
176 it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
177 const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
178 expect(
179 selectionTouchesMentionBoundary(
180 prompt,
181 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
182 prompt.length,
183 ),
184 ).toBe(true);
185 });
186​
187 it("returns true when selection includes whitespace after a quoted mention", () => {
188 expect(
189 selectionTouchesMentionBoundary(
190 'hi @"My File.md" there',
191 'hi @"My File.md"'.length,
192 'hi @"My File.md" there'.length,
193 ),
194 ).toBe(true);
195 });
196​
197 it("returns true when selection includes whitespace after a markdown file link", () => {
198 const prompt = "hi [package.json](path/to/package.json) there";
199 expect(
200 selectionTouchesMentionBoundary(
201 prompt,
202 "hi [package.json](path/to/package.json)".length,
203 prompt.length,
204 ),
205 ).toBe(true);
206 });
207});