Open History to see the last commit
1import remarkGfm from "remark-gfm";
2import remarkParse from "remark-parse";
3import { unified } from "unified";
4import { assert, describe, expect, it } from "vite-plus/test";
5
6import {
7 detectComposerTrigger,
8 parseSideChatCommand,
9 serializeComposerFileLink,
10} from "./composerTrigger.ts";
11import { resolveMarkdownFileLinkTarget } from "./markdownLinks.ts";
12
13interface MarkdownNode {
14 readonly type: string;
15 readonly value?: string;
16 readonly children?: ReadonlyArray<MarkdownNode>;
17}
18
19function parsedLinkLabel(markdown: string): string | undefined {
20 const find = (node: MarkdownNode): MarkdownNode | undefined =>
21 node.type === "link" ? node : node.children?.map(find).find(Boolean);
22 const link = find(unified().use(remarkParse).use(remarkGfm).parse(markdown) as MarkdownNode);
23 const children = link?.children ?? [];
24 return children.length === 1 && children[0]?.type === "text" ? children[0].value : undefined;
25}
26
27describe("detectComposerTrigger", () => {
28 it.each(["\u3000", "\u00a0"])("detects completion after Unicode space %j", (space) => {
29 for (const [token, kind, query] of [
30 ["@src", "path", "src"],
31 ["#123", "pull-request", "123"],
32 ["$review", "skill", "review"],
33 ["/model", "slash-command", "model"],
34 ]) {
35 const prefix = `日本語${space}`;
36 const text = `${prefix}${token}`;
37 expect(detectComposerTrigger(text, text.length)).toEqual({
38 kind,
39 query,
40 rangeStart: prefix.length,
41 rangeEnd: text.length,
42 });
43 }
44 });
45
46 it.each([
47 ["Run /tmp/build.sh", "Run /tmp/build.sh".length],
48 ["/etc/hosts", "/etc/hosts".length],
49 ["Use /etc/hosts now", "Use /et".length],
50 ["cmd /foo\\bar", "cmd /foo\\bar".length],
51 ["/Windows\\System32", "/Windows\\System32".length],
52 ])("keeps the path in %j out of the slash menu", (text, cursor) => {
53 expect(detectComposerTrigger(text, cursor)).toBeNull();
54 });
55
56 it.each([
57 ["/model", 0],
58 ["Question\n/MODEL", 9],
59 ])("offers typed %j as a slash command", (text, rangeStart) => {
60 expect(detectComposerTrigger(text, text.length)).toEqual({
61 kind: "slash-command",
62 query: text.slice(rangeStart + 1),
63 rangeStart,
64 rangeEnd: text.length,
65 });
66 });
67
68 it("does not reopen a menu after /model arguments", () => {
69 expect(detectComposerTrigger("/Model opus", "/Model opus".length)).toBeNull();
70 });
71
72 it.each(["Use /", "Use /review"])("detects the inline slash token in %j", (text) => {
73 expect(detectComposerTrigger(text, text.length)).toEqual({
74 kind: "slash-command",
75 query: text.slice(5),
76 rangeStart: 4,
77 rangeEnd: text.length,
78 });
79 });
80
81 it.each(["Use/review", "https://example.com/review", "Use /review "])(
82 "does not trigger for %j",
83 (text) => expect(detectComposerTrigger(text, text.length)).toBeNull(),
84 );
85
86 it.each([
87 ["", "", ""],
88 ["Compare ", "8737", ""],
89 ["Compare (", "", ")"],
90 ["Compare(", "fix-name", ")"],
91 ["((", "fix_name", "))"],
92 ["[", "8737", "]"],
93 ["{", "8737", "}"],
94 ["【", "修正", "】"],
95 ])("keeps brackets outside the PR range after %j", (prefix, query, suffix) => {
96 const text = `${prefix}#${query}`;
97 expect(detectComposerTrigger(`${text}${suffix}`, text.length)).toEqual({
98 kind: "pull-request",
99 query,
100 rangeStart: prefix.length,
101 rangeEnd: text.length,
102 });
103 });
104
105 it("keeps pull-request search active for titles with combining marks", () => {
106 expect(detectComposerTrigger("#cafe\u0301", 6)).toEqual({
107 kind: "pull-request",
108 query: "cafe\u0301",
109 rangeStart: 0,
110 rangeEnd: 6,
111 });
112 expect(detectComposerTrigger("#नमस्ते", "#नमस्ते".length)).toEqual({
113 kind: "pull-request",
114 query: "नमस्ते",
115 rangeStart: 0,
116 rangeEnd: 7,
117 });
118 });
119
120 it.each([
121 "# Heading",
122 "#\u0301",
123 "issue#123",
124 "https://example.com/#123",
125 "(#123)",
126 "[issue#123",
127 "[#_fix",
128 "]#123",
129 ])("does not detect a pull request in %j", (text) => {
130 expect(detectComposerTrigger(text, text.length)).toBeNull();
131 });
132
133 it.each([
134 ["@src/(#1", "path", "src/(#1"],
135 ["$review(#1", "skill", "review(#1"],
136 ])("keeps explicit completion for %j", (token, kind, query) => {
137 const text = `Use ${token}`;
138 expect(detectComposerTrigger(text, text.length)).toEqual({
139 kind,
140 query,
141 rangeStart: 4,
142 rangeEnd: text.length,
143 });
144 });
145
146 it.each(["$", "€", "𑿝"])("detects %s skill prefixes and their source range", (prefix) => {
147 const text = `Use ${prefix}review`;
148 expect(detectComposerTrigger(text, text.length)).toEqual({
149 kind: "skill",
150 query: "review",
151 rangeStart: 4,
152 rangeEnd: text.length,
153 });
154 });
155
156 it.each([
157 "budget $9.99",
158 "only $.99",
159 "about €20",
160 "raise $10k",
161 "$1,000",
162 "£2e6",
163 "raise $10k.",
164 ])("does not open the skill menu for the amount in %j", (text) =>
165 expect(detectComposerTrigger(text, text.length)).toBeNull(),
166 );
167
168 it.each(["$", "$1password", "$2fa"])("still detects the skill prefix %j", (text) => {
169 expect(detectComposerTrigger(text, text.length)).toMatchObject({
170 kind: "skill",
171 query: text.slice(1),
172 });
173 });
174});
175
176describe("serializeComposerFileLink", () => {
177 it.each(["reports:final.md", "file:notes.md"])("disambiguates the colon filename %s", (path) => {
178 const link = serializeComposerFileLink(path);
179 expect(link).toBe(`[${path}](./${path})`);
180 expect(resolveMarkdownFileLinkTarget(`./${path}`, "/workspace")).toBe(`/workspace/${path}`);
181 });
182
183 it("encodes markdown-sensitive destination characters", () => {
184 expect(serializeComposerFileLink("docs/My File (draft).md")).toBe(
185 "[My File (draft).md](docs/My%20File%20%28draft%29.md)",
186 );
187 });
188
189 it("preserves literal HTML entities in the file label and destination", () => {
190 const path = "docs/R©.md";
191 const paragraph = unified().use(remarkParse).parse(serializeComposerFileLink(path)).children[0];
192 assert(paragraph?.type === "paragraph");
193 const link = paragraph.children[0];
194 assert(link?.type === "link");
195 expect(link.children).toMatchObject([{ type: "text", value: "R©.md" }]);
196 expect(resolveMarkdownFileLinkTarget(link.url, "/workspace")).toBe(`/workspace/${path}`);
197 });
198
199 it("supports windows paths", () => {
200 expect(serializeComposerFileLink("C:\\repo\\src\\index.ts")).toBe(
201 "[index.ts](C:%5Crepo%5Csrc%5Cindex.ts)",
202 );
203 });
204
205 it.each([
206 ["pkg/__init__.py", "__init__.py"],
207 ["notes/*draft*.md", "*draft*.md"],
208 ["notes/[TODO].md", "[TODO].md"],
209 ["notes/~~draft~~.md", "~~draft~~.md"],
210 ["notes/[TODO].md", "[TODO].md"],
211 ["src/a` b `.txt", "a` b `.txt"],
212 ["src/a<T>.ts", "a<T>.ts"],
213 ["src/a<x@y.com>b.ts", "a<x@y.com>b.ts"],
214 ])("renders the mention of %j with its file name as the label", (path, basename) => {
215 expect(parsedLinkLabel(serializeComposerFileLink(path))).toBe(basename);
216 });
217
218 it("preserves paths that legitimately start with an at sign", () => {
219 expect(serializeComposerFileLink("@scope/package.json")).toBe(
220 "[package.json](@scope/package.json)",
221 );
222 });
223});
224
225describe("parseSideChatCommand", () => {
226 it("reads an optional prompt after /side or /btw", () => {
227 expect(parseSideChatCommand("/side")).toEqual({ prompt: "" });
228 expect(parseSideChatCommand(" /SIDE why does this fail?\nline two ")).toEqual({
229 prompt: "why does this fail?\nline two",
230 });
231 expect(parseSideChatCommand("/btw check @src/a.ts")).toEqual({ prompt: "check @src/a.ts" });
232 });
233
234 it("ignores other commands and inline mentions", () => {
235 expect(parseSideChatCommand("/sidebar")).toBeNull();
236 expect(parseSideChatCommand("/btwx")).toBeNull();
237 expect(parseSideChatCommand("ask /side later")).toBeNull();
238 });
239});