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

composerTrigger.test.ts

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