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

composerTrigger.test.ts

138 lines4.4 KB

1import remarkParse from "remark-parse";
2import { unified } from "unified";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 detectComposerTrigger,
7 parseSideChatCommand,
8 serializeComposerFileLink,
9} from "./composerTrigger.ts";
10​
11interface MarkdownNode {
12 readonly type: string;
13 readonly value?: string;
14 readonly children?: ReadonlyArray<MarkdownNode>;
16​
17function 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);
25​
26describe("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(["$", "€", "𑿝"])("detects %s skill prefixes and their source range", (prefix) => {
68 const text = `Use ${prefix}review`;
69 expect(detectComposerTrigger(text, text.length)).toEqual({
70 kind: "skill",
71 query: "review",
72 rangeStart: 4,
73 rangeEnd: text.length,
74 });
75 });
76​
77 it.each([
78 "budget $9.99",
79 "only $.99",
80 "about €20",
81 "raise $10k",
82 "$1,000",
83 "£2e6",
84 "raise $10k.",
85 ])("does not open the skill menu for the amount in %j", (text) =>
86 expect(detectComposerTrigger(text, text.length)).toBeNull(),
87 );
88​
89 it.each(["$", "$1password", "$2fa"])("still detects the skill prefix %j", (text) => {
90 expect(detectComposerTrigger(text, text.length)).toMatchObject({
91 kind: "skill",
92 query: text.slice(1),
93 });
94 });
95});
96​
97describe("serializeComposerFileLink", () => {
98 it("encodes markdown-sensitive destination characters", () => {
99 expect(serializeComposerFileLink("docs/My File (draft).md")).toBe(
100 "[My File (draft).md](docs/My%20File%20%28draft%29.md)",
101 );
102 });
103​
104 it("supports windows paths", () => {
105 expect(serializeComposerFileLink("C:\\repo\\src\\index.ts")).toBe(
106 "[index.ts](C:%5Crepo%5Csrc%5Cindex.ts)",
107 );
108 });
109​
110 it.each([
111 ["pkg/__init__.py", "__init__.py"],
112 ["notes/*draft*.md", "*draft*.md"],
113 ])("renders the mention of %j with its file name as the label", (path, basename) => {
114 expect(parsedLinkLabel(serializeComposerFileLink(path))).toBe(basename);
115 });
116​
117 it("preserves paths that legitimately start with an at sign", () => {
118 expect(serializeComposerFileLink("@scope/package.json")).toBe(
119 "[package.json](@scope/package.json)",
120 );
121 });
122});
123​
124describe("parseSideChatCommand", () => {
125 it("reads an optional prompt after /side or /btw", () => {
126 expect(parseSideChatCommand("/side")).toEqual({ prompt: "" });
127 expect(parseSideChatCommand(" /SIDE why does this fail?\nline two ")).toEqual({
128 prompt: "why does this fail?\nline two",
129 });
130 expect(parseSideChatCommand("/btw check @src/a.ts")).toEqual({ prompt: "check @src/a.ts" });
131 });
132​
133 it("ignores other commands and inline mentions", () => {
134 expect(parseSideChatCommand("/sidebar")).toBeNull();
135 expect(parseSideChatCommand("/btwx")).toBeNull();
136 expect(parseSideChatCommand("ask /side later")).toBeNull();
137 });
138});