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

composerTrigger.test.ts

113 lines3.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 detectComposerTrigger,
5 parseSideChatCommand,
6 serializeComposerFileLink,
7} from "./composerTrigger.ts";
8​
9describe("detectComposerTrigger", () => {
10 it.each([
11 ["Run /tmp/build.sh", "Run /tmp/build.sh".length],
12 ["/etc/hosts", "/etc/hosts".length],
13 ["Use /etc/hosts now", "Use /et".length],
14 ])("keeps the path in %j out of the slash menu", (text, cursor) => {
15 expect(detectComposerTrigger(text, cursor)).toBeNull();
16 });
17​
18 it("keeps a slash in the line-leading model picker query", () => {
19 expect(detectComposerTrigger("/model openai/gpt", 17)).toMatchObject({
20 kind: "slash-model",
21 query: "openai/gpt",
22 });
23 });
24​
25 it.each(["/Model opus", "/MODEL opus"])(
26 "opens the model picker for capitalized %j the same as /model",
27 (text) => {
28 expect(detectComposerTrigger(text, text.length)).toMatchObject({
29 kind: "slash-model",
30 query: "opus",
31 });
32 },
33 );
34​
35 it.each(["Use /", "Use /review"])("detects the inline slash token in %j", (text) => {
36 expect(detectComposerTrigger(text, text.length)).toEqual({
37 kind: "slash-command",
38 query: text.slice(5),
39 rangeStart: 4,
40 rangeEnd: text.length,
41 });
42 });
43​
44 it.each(["Use/review", "https://example.com/review", "Use /review "])(
45 "does not trigger for %j",
46 (text) => expect(detectComposerTrigger(text, text.length)).toBeNull(),
47 );
48​
49 it.each(["$", "€", "𑿝"])("detects %s skill prefixes and their source range", (prefix) => {
50 const text = `Use ${prefix}review`;
51 expect(detectComposerTrigger(text, text.length)).toEqual({
52 kind: "skill",
53 query: "review",
54 rangeStart: 4,
55 rangeEnd: text.length,
56 });
57 });
58​
59 it.each([
60 "budget $9.99",
61 "only $.99",
62 "about €20",
63 "raise $10k",
64 "$1,000",
65 "£2e6",
66 "raise $10k.",
67 ])("does not open the skill menu for the amount in %j", (text) =>
68 expect(detectComposerTrigger(text, text.length)).toBeNull(),
69 );
70​
71 it.each(["$", "$1password", "$2fa"])("still detects the skill prefix %j", (text) => {
72 expect(detectComposerTrigger(text, text.length)).toMatchObject({
73 kind: "skill",
74 query: text.slice(1),
75 });
76 });
77});
78​
79describe("serializeComposerFileLink", () => {
80 it("encodes markdown-sensitive destination characters", () => {
81 expect(serializeComposerFileLink("docs/My File (draft).md")).toBe(
82 "[My File (draft).md](docs/My%20File%20%28draft%29.md)",
83 );
84 });
85​
86 it("supports windows paths", () => {
87 expect(serializeComposerFileLink("C:\\repo\\src\\index.ts")).toBe(
88 "[index.ts](C:%5Crepo%5Csrc%5Cindex.ts)",
89 );
90 });
91​
92 it("preserves paths that legitimately start with an at sign", () => {
93 expect(serializeComposerFileLink("@scope/package.json")).toBe(
94 "[package.json](@scope/package.json)",
95 );
96 });
97});
98​
99describe("parseSideChatCommand", () => {
100 it("reads an optional prompt after /side or /btw", () => {
101 expect(parseSideChatCommand("/side")).toEqual({ prompt: "" });
102 expect(parseSideChatCommand(" /SIDE why does this fail?\nline two ")).toEqual({
103 prompt: "why does this fail?\nline two",
104 });
105 expect(parseSideChatCommand("/btw check @src/a.ts")).toEqual({ prompt: "check @src/a.ts" });
106 });
107​
108 it("ignores other commands and inline mentions", () => {
109 expect(parseSideChatCommand("/sidebar")).toBeNull();
110 expect(parseSideChatCommand("/btwx")).toBeNull();
111 expect(parseSideChatCommand("ask /side later")).toBeNull();
112 });
113});