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

composerTrigger.test.ts

93 lines2.8 KB

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