This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcommand-mentions.spec.ts
More file actions
358 lines (293 loc) · 11.7 KB
/
Copy pathcommand-mentions.spec.ts
File metadata and controls
358 lines (293 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { parseMentions } from "../core/mentions"
import { UrlContentFetcher } from "../services/browser/UrlContentFetcher"
import { getCommand } from "../services/command/commands"
// Mock the dependencies
vi.mock("../services/command/commands")
vi.mock("../services/browser/UrlContentFetcher")
const MockedUrlContentFetcher = vi.mocked(UrlContentFetcher)
const mockGetCommand = vi.mocked(getCommand)
describe("Command Mentions", () => {
let mockUrlContentFetcher: any
beforeEach(() => {
vi.clearAllMocks()
// Create a mock UrlContentFetcher instance
mockUrlContentFetcher = {
launchBrowser: vi.fn(),
urlToMarkdown: vi.fn(),
closeBrowser: vi.fn(),
}
MockedUrlContentFetcher.mockImplementation(() => mockUrlContentFetcher)
})
// Helper function to call parseMentions with required parameters
const callParseMentions = async (text: string) => {
return parseMentions(
text,
"/test/cwd", // cwd
mockUrlContentFetcher, // urlContentFetcher
undefined, // fileContextTracker
undefined, // rooIgnoreController
false, // showRooIgnoredFiles
true, // includeDiagnosticMessages
50, // maxDiagnosticMessages
)
}
describe("parseMentions with command support", () => {
it("should parse command mentions and include content", async () => {
const commandContent = "# Setup Environment\n\nRun the following commands:\n```bash\nnpm install\n```"
mockGetCommand.mockResolvedValue({
name: "setup",
content: commandContent,
source: "project",
filePath: "/project/.roo/commands/setup.md",
})
const input = "/setup Please help me set up the project"
const result = await callParseMentions(input)
expect(mockGetCommand).toHaveBeenCalledWith("/test/cwd", "setup")
expect(result.slashCommandHelp).toContain('<command name="setup">')
expect(result.slashCommandHelp).toContain(commandContent)
expect(result.slashCommandHelp).toContain("</command>")
expect(result.text).toContain("Please help me set up the project")
})
it("should handle multiple commands in message", async () => {
const setupContent = "# Setup Environment\n\nRun the following commands:\n```bash\nnpm install\n```"
const deployContent = "# Deploy Environment\n\nRun the following commands:\n```bash\nnpm run deploy\n```"
mockGetCommand
.mockResolvedValueOnce({
name: "setup",
content: setupContent,
source: "project",
filePath: "/project/.roo/commands/setup.md",
})
.mockResolvedValueOnce({
name: "deploy",
content: deployContent,
source: "project",
filePath: "/project/.roo/commands/deploy.md",
})
.mockResolvedValueOnce({
name: "setup",
content: setupContent,
source: "project",
filePath: "/project/.roo/commands/setup.md",
})
.mockResolvedValueOnce({
name: "deploy",
content: deployContent,
source: "project",
filePath: "/project/.roo/commands/deploy.md",
})
// Both commands should be recognized
const input = "/setup the project\nThen /deploy later"
const result = await callParseMentions(input)
expect(mockGetCommand).toHaveBeenCalledWith("/test/cwd", "setup")
expect(mockGetCommand).toHaveBeenCalledWith("/test/cwd", "deploy")
expect(mockGetCommand).toHaveBeenCalledTimes(2) // Each unique command called once (optimized)
expect(result.slashCommandHelp).toContain('<command name="setup">')
expect(result.slashCommandHelp).toContain("# Setup Environment")
expect(result.slashCommandHelp).toContain('<command name="deploy">')
expect(result.slashCommandHelp).toContain("# Deploy Environment")
})
it("should leave non-existent commands unchanged", async () => {
mockGetCommand.mockReset()
mockGetCommand.mockResolvedValue(undefined)
const input = "/nonexistent command"
const result = await callParseMentions(input)
expect(mockGetCommand).toHaveBeenCalledWith("/test/cwd", "nonexistent")
// The command should remain unchanged in the text
expect(result.text).toBe("/nonexistent command")
// Should not contain any command tags
expect(result.slashCommandHelp).toBeUndefined()
expect(result.text).not.toContain("Command 'nonexistent' not found")
})
it("should handle command loading errors during existence check", async () => {
mockGetCommand.mockReset()
mockGetCommand.mockRejectedValue(new Error("Failed to load command"))
const input = "/error-command test"
const result = await callParseMentions(input)
// When getCommand throws an error during existence check,
// the command is treated as non-existent and left unchanged
expect(result.text).toBe("/error-command test")
expect(result.slashCommandHelp).toBeUndefined()
})
it("should handle command loading errors during processing", async () => {
// With optimization, command is loaded once and cached
mockGetCommand.mockResolvedValue({
name: "error-command",
content: "# Error command",
source: "project",
filePath: "/project/.roo/commands/error-command.md",
})
const input = "/error-command test"
const result = await callParseMentions(input)
expect(result.slashCommandHelp).toContain('<command name="error-command">')
expect(result.slashCommandHelp).toContain("# Error command")
expect(result.slashCommandHelp).toContain("</command>")
})
it("should handle command names with hyphens and underscores at start", async () => {
mockGetCommand.mockResolvedValue({
name: "setup-dev",
content: "# Dev setup",
source: "project",
filePath: "/project/.roo/commands/setup-dev.md",
})
const input = "/setup-dev for the project"
const result = await callParseMentions(input)
expect(mockGetCommand).toHaveBeenCalledWith("/test/cwd", "setup-dev")
expect(result.slashCommandHelp).toContain('<command name="setup-dev">')
expect(result.slashCommandHelp).toContain("# Dev setup")
})
it("should preserve command content formatting", async () => {
const commandContent = `# Complex Command
## Step 1
Run this command:
\`\`\`bash
npm install
\`\`\`
## Step 2
- Check file1.js
- Update file2.ts
- Test everything
> **Note**: This is important!`
mockGetCommand.mockResolvedValue({
name: "complex",
content: commandContent,
source: "project",
filePath: "/project/.roo/commands/complex.md",
})
const input = "/complex command"
const result = await callParseMentions(input)
expect(result.slashCommandHelp).toContain('<command name="complex">')
expect(result.slashCommandHelp).toContain("# Complex Command")
expect(result.slashCommandHelp).toContain("```bash")
expect(result.slashCommandHelp).toContain("npm install")
expect(result.slashCommandHelp).toContain("- Check file1.js")
expect(result.slashCommandHelp).toContain("> **Note**: This is important!")
expect(result.slashCommandHelp).toContain("</command>")
})
it("should handle empty command content", async () => {
mockGetCommand.mockResolvedValue({
name: "empty",
content: "",
source: "project",
filePath: "/project/.roo/commands/empty.md",
})
const input = "/empty command"
const result = await callParseMentions(input)
expect(result.slashCommandHelp).toContain('<command name="empty">')
expect(result.slashCommandHelp).toContain("</command>")
// Should still include the command tags even with empty content
})
})
describe("command mention regex patterns", () => {
it("should match valid command mention patterns anywhere", () => {
const commandRegex = /\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
const validPatterns = ["/setup", "/build-prod", "/test_suite", "/my-command", "/command123"]
validPatterns.forEach((pattern) => {
const match = pattern.match(commandRegex)
expect(match).toBeTruthy()
expect(match![0]).toBe(pattern)
})
})
it("should match command patterns in middle of text", () => {
const commandRegex = /\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
const validPatterns = ["Please /setup", "Run /build now", "Use /deploy here"]
validPatterns.forEach((pattern) => {
const match = pattern.match(commandRegex)
expect(match).toBeTruthy()
expect(match![0]).toMatch(/^\/[a-zA-Z0-9_\.-]+$/)
})
})
it("should match commands at start of new lines", () => {
const commandRegex = /\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
const multilineText = "First line\n/setup the project\nAnother line\n/deploy when ready"
const matches = multilineText.match(commandRegex)
// Should match both commands now
expect(matches).toBeTruthy()
expect(matches).toHaveLength(2)
expect(matches![0]).toBe("/setup")
expect(matches![1]).toBe("/deploy")
})
it("should match multiple commands in message", () => {
const commandRegex = /(?:^|\s)\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
const validText = "/setup the project\nThen /deploy later"
const matches = validText.match(commandRegex)
expect(matches).toBeTruthy()
expect(matches).toHaveLength(2)
expect(matches![0]).toBe("/setup")
expect(matches![1]).toBe(" /deploy") // Note: includes leading space
})
it("should not match invalid command patterns", () => {
const commandRegex = /\/([a-zA-Z0-9_\.-]+)(?=\s|$)/g
const invalidPatterns = ["/ space", "/with space", "/with/slash", "//double", "/with@symbol"]
invalidPatterns.forEach((pattern) => {
const match = pattern.match(commandRegex)
if (match) {
// If it matches, it should not be the full invalid pattern
expect(match[0]).not.toBe(pattern)
}
})
})
})
describe("command mention text transformation", () => {
it("should transform existing command mentions at start of message", async () => {
mockGetCommand.mockResolvedValue({
name: "setup",
content: "# Setup instructions",
source: "project",
filePath: "/project/.roo/commands/setup.md",
})
const input = "/setup the project"
const result = await callParseMentions(input)
expect(result.text).toContain("Command 'setup' (see below for command content)")
})
it("should leave non-existent command mentions unchanged", async () => {
mockGetCommand.mockResolvedValue(undefined)
const input = "/nonexistent the project"
const result = await callParseMentions(input)
expect(result.text).toBe("/nonexistent the project")
})
it("should process multiple commands in message", async () => {
mockGetCommand
.mockResolvedValueOnce({
name: "setup",
content: "# Setup instructions",
source: "project",
filePath: "/project/.roo/commands/setup.md",
})
.mockResolvedValueOnce({
name: "deploy",
content: "# Deploy instructions",
source: "project",
filePath: "/project/.roo/commands/deploy.md",
})
const input = "/setup the project\nThen /deploy later"
const result = await callParseMentions(input)
expect(result.text).toContain("Command 'setup' (see below for command content)")
expect(result.text).toContain("Command 'deploy' (see below for command content)")
})
it("should match commands anywhere with proper word boundaries", async () => {
mockGetCommand.mockResolvedValue({
name: "build",
content: "# Build instructions",
source: "project",
filePath: "/project/.roo/commands/build.md",
})
// At the beginning - should match
let input = "/build the project"
let result = await callParseMentions(input)
expect(result.text).toContain("Command 'build'")
// After space - should match
input = "Please /build and test"
result = await callParseMentions(input)
expect(result.text).toContain("Command 'build'")
// At the end - should match
input = "Run the /build"
result = await callParseMentions(input)
expect(result.text).toContain("Command 'build'")
// At start of new line - should match
input = "Some text\n/build the project"
result = await callParseMentions(input)
expect(result.text).toContain("Command 'build'")
})
})
})