The best way to control Claude Code, Codex and more from any computer or phone

main
ts 182 lines 5.57 KB
Raw
1 import remarkParse from "remark-parse";
2 import { unified } from "unified";
3 import { describe, expect, it } from "vite-plus/test";
4
5 import {
6 detectComposerTrigger,
7 parseSideChatCommand,
8 serializeComposerFileLink,
9 } from "./composerTrigger.ts";
10
11 interface MarkdownNode {
12 readonly type: string;
13 readonly value?: string;
14 readonly children?: ReadonlyArray<MarkdownNode>;
15 }
16
17 function parsedLinkLabel(markdown: string): string | undefined {
18 const textOf = (node: MarkdownNode): string =>
19 node.value ?? (node.children ?? []).map(textOf).join("");
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).parse(markdown) as MarkdownNode);
23 return link && textOf(link);
24 }
25
26 describe("detectComposerTrigger", () => {
27 it.each([
28 ["Run /tmp/build.sh", "Run /tmp/build.sh".length],
29 ["/etc/hosts", "/etc/hosts".length],
30 ["Use /etc/hosts now", "Use /et".length],
31 ["cmd /foo\\bar", "cmd /foo\\bar".length],
32 ["/Windows\\System32", "/Windows\\System32".length],
33 ])("keeps the path in %j out of the slash menu", (text, cursor) => {
34 expect(detectComposerTrigger(text, cursor)).toBeNull();
35 });
36
37 it.each([
38 ["/model", 0],
39 ["Question\n/MODEL", 9],
40 ])("offers typed %j as a slash command", (text, rangeStart) => {
41 expect(detectComposerTrigger(text, text.length)).toEqual({
42 kind: "slash-command",
43 query: text.slice(rangeStart + 1),
44 rangeStart,
45 rangeEnd: text.length,
46 });
47 });
48
49 it("does not reopen a menu after /model arguments", () => {
50 expect(detectComposerTrigger("/Model opus", "/Model opus".length)).toBeNull();
51 });
52
53 it.each(["Use /", "Use /review"])("detects the inline slash token in %j", (text) => {
54 expect(detectComposerTrigger(text, text.length)).toEqual({
55 kind: "slash-command",
56 query: text.slice(5),
57 rangeStart: 4,
58 rangeEnd: text.length,
59 });
60 });
61
62 it.each(["Use/review", "https://example.com/review", "Use /review "])(
63 "does not trigger for %j",
64 (text) => expect(detectComposerTrigger(text, text.length)).toBeNull(),
65 );
66
67 it.each([
68 ["", "", ""],
69 ["Compare ", "8737", ""],
70 ["Compare (", "", ")"],
71 ["Compare(", "fix-name", ")"],
72 ["((", "fix_name", "))"],
73 ["[", "8737", "]"],
74 ["{", "8737", "}"],
75 ["【", "修正", "】"],
76 ])("keeps brackets outside the PR range after %j", (prefix, query, suffix) => {
77 const text = `${prefix}#${query}`;
78 expect(detectComposerTrigger(`${text}${suffix}`, text.length)).toEqual({
79 kind: "pull-request",
80 query,
81 rangeStart: prefix.length,
82 rangeEnd: text.length,
83 });
84 });
85
86 it.each([
87 "# Heading",
88 "issue#123",
89 "https://example.com/#123",
90 "(#123)",
91 "[issue#123",
92 "[#_fix",
93 "]#123",
94 ])("does not detect a pull request in %j", (text) => {
95 expect(detectComposerTrigger(text, text.length)).toBeNull();
96 });
97
98 it.each([
99 ["@src/(#1", "path", "src/(#1"],
100 ["$review(#1", "skill", "review(#1"],
101 ])("keeps explicit completion for %j", (token, kind, query) => {
102 const text = `Use ${token}`;
103 expect(detectComposerTrigger(text, text.length)).toEqual({
104 kind,
105 query,
106 rangeStart: 4,
107 rangeEnd: text.length,
108 });
109 });
110
111 it.each(["$", "€", "𑿝"])("detects %s skill prefixes and their source range", (prefix) => {
112 const text = `Use ${prefix}review`;
113 expect(detectComposerTrigger(text, text.length)).toEqual({
114 kind: "skill",
115 query: "review",
116 rangeStart: 4,
117 rangeEnd: text.length,
118 });
119 });
120
121 it.each([
122 "budget $9.99",
123 "only $.99",
124 "about €20",
125 "raise $10k",
126 "$1,000",
127 "£2e6",
128 "raise $10k.",
129 ])("does not open the skill menu for the amount in %j", (text) =>
130 expect(detectComposerTrigger(text, text.length)).toBeNull(),
131 );
132
133 it.each(["$", "$1password", "$2fa"])("still detects the skill prefix %j", (text) => {
134 expect(detectComposerTrigger(text, text.length)).toMatchObject({
135 kind: "skill",
136 query: text.slice(1),
137 });
138 });
139 });
140
141 describe("serializeComposerFileLink", () => {
142 it("encodes markdown-sensitive destination characters", () => {
143 expect(serializeComposerFileLink("docs/My File (draft).md")).toBe(
144 "[My File (draft).md](docs/My%20File%20%28draft%29.md)",
145 );
146 });
147
148 it("supports windows paths", () => {
149 expect(serializeComposerFileLink("C:\\repo\\src\\index.ts")).toBe(
150 "[index.ts](C:%5Crepo%5Csrc%5Cindex.ts)",
151 );
152 });
153
154 it.each([
155 ["pkg/__init__.py", "__init__.py"],
156 ["notes/*draft*.md", "*draft*.md"],
157 ])("renders the mention of %j with its file name as the label", (path, basename) => {
158 expect(parsedLinkLabel(serializeComposerFileLink(path))).toBe(basename);
159 });
160
161 it("preserves paths that legitimately start with an at sign", () => {
162 expect(serializeComposerFileLink("@scope/package.json")).toBe(
163 "[package.json](@scope/package.json)",
164 );
165 });
166 });
167
168 describe("parseSideChatCommand", () => {
169 it("reads an optional prompt after /side or /btw", () => {
170 expect(parseSideChatCommand("/side")).toEqual({ prompt: "" });
171 expect(parseSideChatCommand(" /SIDE why does this fail?\nline two ")).toEqual({
172 prompt: "why does this fail?\nline two",
173 });
174 expect(parseSideChatCommand("/btw check @src/a.ts")).toEqual({ prompt: "check @src/a.ts" });
175 });
176
177 it("ignores other commands and inline mentions", () => {
178 expect(parseSideChatCommand("/sidebar")).toBeNull();
179 expect(parseSideChatCommand("/btwx")).toBeNull();
180 expect(parseSideChatCommand("ask /side later")).toBeNull();
181 });
182 });