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.each([
107 "# Heading",
108 "issue#123",
109 "https://example.com/#123",
110 "(#123)",
111 "[issue#123",
112 "[#_fix",
113 "]#123",
114 ])("does not detect a pull request in %j", (text) => {
115 expect(detectComposerTrigger(text, text.length)).toBeNull();
116 });
117
118 it.each([
119 ["@src/(#1", "path", "src/(#1"],
120 ["$review(#1", "skill", "review(#1"],
121 ])("keeps explicit completion for %j", (token, kind, query) => {
122 const text = `Use ${token}`;
123 expect(detectComposerTrigger(text, text.length)).toEqual({
124 kind,
125 query,
126 rangeStart: 4,
127 rangeEnd: text.length,
128 });
129 });
130
131 it.each(["$", "€", "𑿝"])("detects %s skill prefixes and their source range", (prefix) => {
132 const text = `Use ${prefix}review`;
133 expect(detectComposerTrigger(text, text.length)).toEqual({
134 kind: "skill",
135 query: "review",
136 rangeStart: 4,
137 rangeEnd: text.length,
138 });
139 });
140
141 it.each([
142 "budget $9.99",
143 "only $.99",
144 "about €20",
145 "raise $10k",
146 "$1,000",
147 "£2e6",
148 "raise $10k.",
149 ])("does not open the skill menu for the amount in %j", (text) =>
150 expect(detectComposerTrigger(text, text.length)).toBeNull(),
151 );
152
153 it.each(["$", "$1password", "$2fa"])("still detects the skill prefix %j", (text) => {
154 expect(detectComposerTrigger(text, text.length)).toMatchObject({
155 kind: "skill",
156 query: text.slice(1),
157 });
158 });
159});
160
161describe("serializeComposerFileLink", () => {
162 it.each(["reports:final.md", "file:notes.md"])("disambiguates the colon filename %s", (path) => {
163 const link = serializeComposerFileLink(path);
164 expect(link).toBe(`[${path}](./${path})`);
165 expect(resolveMarkdownFileLinkTarget(`./${path}`, "/workspace")).toBe(`/workspace/${path}`);
166 });
167
168 it("encodes markdown-sensitive destination characters", () => {
169 expect(serializeComposerFileLink("docs/My File (draft).md")).toBe(
170 "[My File (draft).md](docs/My%20File%20%28draft%29.md)",
171 );
172 });
173
174 it("preserves literal HTML entities in the file label and destination", () => {
175 const path = "docs/R©.md";
176 const paragraph = unified().use(remarkParse).parse(serializeComposerFileLink(path)).children[0];
177 assert(paragraph?.type === "paragraph");
178 const link = paragraph.children[0];
179 assert(link?.type === "link");
180 expect(link.children).toMatchObject([{ type: "text", value: "R©.md" }]);
181 expect(resolveMarkdownFileLinkTarget(link.url, "/workspace")).toBe(`/workspace/${path}`);
182 });
183
184 it("supports windows paths", () => {
185 expect(serializeComposerFileLink("C:\\repo\\src\\index.ts")).toBe(
186 "[index.ts](C:%5Crepo%5Csrc%5Cindex.ts)",
187 );
188 });
189
190 it.each([
191 ["pkg/__init__.py", "__init__.py"],
192 ["notes/*draft*.md", "*draft*.md"],
193 ["notes/[TODO].md", "[TODO].md"],
194 ["notes/~~draft~~.md", "~~draft~~.md"],
195 ["notes/[TODO].md", "[TODO].md"],
196 ])("renders the mention of %j with its file name as the label", (path, basename) => {
197 expect(parsedLinkLabel(serializeComposerFileLink(path))).toBe(basename);
198 });
199
200 it("preserves paths that legitimately start with an at sign", () => {
201 expect(serializeComposerFileLink("@scope/package.json")).toBe(
202 "[package.json](@scope/package.json)",
203 );
204 });
205});
206
207describe("parseSideChatCommand", () => {
208 it("reads an optional prompt after /side or /btw", () => {
209 expect(parseSideChatCommand("/side")).toEqual({ prompt: "" });
210 expect(parseSideChatCommand(" /SIDE why does this fail?\nline two ")).toEqual({
211 prompt: "why does this fail?\nline two",
212 });
213 expect(parseSideChatCommand("/btw check @src/a.ts")).toEqual({ prompt: "check @src/a.ts" });
214 });
215
216 it("ignores other commands and inline mentions", () => {
217 expect(parseSideChatCommand("/sidebar")).toBeNull();
218 expect(parseSideChatCommand("/btwx")).toBeNull();
219 expect(parseSideChatCommand("ask /side later")).toBeNull();
220 });
221});