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

composerTrigger.test.ts

211 lines6.7 KB

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