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

composer-logic.test.ts

140 lines4.6 KB

1import { describe, expect, it } from "vitest";
2​
3import {
4 detectComposerTrigger,
5 expandCollapsedComposerCursor,
6 isCollapsedCursorAdjacentToMention,
7 parseStandaloneComposerSlashCommand,
8 replaceTextRange,
9} from "./composer-logic";
10​
11describe("detectComposerTrigger", () => {
12 it("detects @path trigger at cursor", () => {
13 const text = "Please check @src/com";
14 const trigger = detectComposerTrigger(text, text.length);
15​
16 expect(trigger).toEqual({
17 kind: "path",
18 query: "src/com",
19 rangeStart: "Please check ".length,
20 rangeEnd: text.length,
21 });
22 });
23​
24 it("detects slash command token while typing command name", () => {
25 const text = "/mo";
26 const trigger = detectComposerTrigger(text, text.length);
27​
28 expect(trigger).toEqual({
29 kind: "slash-command",
30 query: "mo",
31 rangeStart: 0,
32 rangeEnd: text.length,
33 });
34 });
35​
36 it("detects slash model query after /model", () => {
37 const text = "/model spark";
38 const trigger = detectComposerTrigger(text, text.length);
39​
40 expect(trigger).toEqual({
41 kind: "slash-model",
42 query: "spark",
43 rangeStart: 0,
44 rangeEnd: text.length,
45 });
46 });
47​
48 it("detects non-model slash commands while typing", () => {
49 const text = "/pl";
50 const trigger = detectComposerTrigger(text, text.length);
51​
52 expect(trigger).toEqual({
53 kind: "slash-command",
54 query: "pl",
55 rangeStart: 0,
56 rangeEnd: text.length,
57 });
58 });
59});
60​
61describe("replaceTextRange", () => {
62 it("replaces a text range and returns new cursor", () => {
63 const replaced = replaceTextRange("hello @src", 6, 10, "");
64 expect(replaced).toEqual({
65 text: "hello ",
66 cursor: 6,
67 });
68 });
69});
70​
71describe("expandCollapsedComposerCursor", () => {
72 it("keeps cursor unchanged when no mention segment is present", () => {
73 expect(expandCollapsedComposerCursor("plain text", 5)).toBe(5);
74 });
75​
76 it("maps collapsed mention cursor to expanded text cursor", () => {
77 const text = "what's in my @AGENTS.md fsfdas";
78 const collapsedCursorAfterMention = "what's in my ".length + 2;
79 const expandedCursorAfterMention = "what's in my @AGENTS.md ".length;
80​
81 expect(expandCollapsedComposerCursor(text, collapsedCursorAfterMention)).toBe(
82 expandedCursorAfterMention,
83 );
84 });
85​
86 it("allows path trigger detection to close after selecting a mention", () => {
87 const text = "what's in my @AGENTS.md ";
88 const collapsedCursorAfterMention = "what's in my ".length + 2;
89 const expandedCursor = expandCollapsedComposerCursor(text, collapsedCursorAfterMention);
90​
91 expect(detectComposerTrigger(text, expandedCursor)).toBeNull();
92 });
93});
94​
95describe("isCollapsedCursorAdjacentToMention", () => {
96 it("returns false when no mention exists", () => {
97 expect(isCollapsedCursorAdjacentToMention("plain text", 6, "left")).toBe(false);
98 expect(isCollapsedCursorAdjacentToMention("plain text", 6, "right")).toBe(false);
99 });
100​
101 it("keeps @query typing non-adjacent while no mention pill exists", () => {
102 const text = "hello @pac";
103 expect(isCollapsedCursorAdjacentToMention(text, text.length, "left")).toBe(false);
104 expect(isCollapsedCursorAdjacentToMention(text, text.length, "right")).toBe(false);
105 });
106​
107 it("detects left adjacency only when cursor is directly after a mention", () => {
108 const text = "open @AGENTS.md next";
109 const mentionStart = "open ".length;
110 const mentionEnd = mentionStart + 1;
111​
112 expect(isCollapsedCursorAdjacentToMention(text, mentionEnd, "left")).toBe(true);
113 expect(isCollapsedCursorAdjacentToMention(text, mentionStart, "left")).toBe(false);
114 expect(isCollapsedCursorAdjacentToMention(text, mentionEnd + 1, "left")).toBe(false);
115 });
116​
117 it("detects right adjacency only when cursor is directly before a mention", () => {
118 const text = "open @AGENTS.md next";
119 const mentionStart = "open ".length;
120 const mentionEnd = mentionStart + 1;
121​
122 expect(isCollapsedCursorAdjacentToMention(text, mentionStart, "right")).toBe(true);
123 expect(isCollapsedCursorAdjacentToMention(text, mentionEnd, "right")).toBe(false);
124 expect(isCollapsedCursorAdjacentToMention(text, mentionStart - 1, "right")).toBe(false);
125 });
126});
127​
128describe("parseStandaloneComposerSlashCommand", () => {
129 it("parses standalone /plan command", () => {
130 expect(parseStandaloneComposerSlashCommand(" /plan ")).toBe("plan");
131 });
132​
133 it("parses standalone /default command", () => {
134 expect(parseStandaloneComposerSlashCommand("/default")).toBe("default");
135 });
136​
137 it("ignores slash commands with extra message text", () => {
138 expect(parseStandaloneComposerSlashCommand("/plan explain this")).toBeNull();
139 });
140});