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