|
| 1 | +// npx vitest run src/utils/__tests__/slashCommandDetection.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 4 | +import { detectSlashCommands, getFirstSlashCommand } from "../slashCommandDetection" |
| 5 | + |
| 6 | +// Mock the modes module |
| 7 | +vi.mock("../../shared/modes", () => ({ |
| 8 | + modes: [ |
| 9 | + { slug: "code", name: "Code" }, |
| 10 | + { slug: "architect", name: "Architect" }, |
| 11 | + { slug: "debug", name: "Debug" }, |
| 12 | + { slug: "ask", name: "Ask" }, |
| 13 | + ], |
| 14 | + getModeBySlug: vi.fn((slug: string) => { |
| 15 | + const modeMap: Record<string, any> = { |
| 16 | + code: { slug: "code", name: "Code" }, |
| 17 | + architect: { slug: "architect", name: "Architect" }, |
| 18 | + debug: { slug: "debug", name: "Debug" }, |
| 19 | + ask: { slug: "ask", name: "Ask" }, |
| 20 | + } |
| 21 | + return modeMap[slug] |
| 22 | + }), |
| 23 | +})) |
| 24 | + |
| 25 | +describe("slashCommandDetection", () => { |
| 26 | + describe("detectSlashCommands", () => { |
| 27 | + it("should detect mode switch commands", () => { |
| 28 | + const text = "Let me switch to /code mode to implement this" |
| 29 | + const commands = detectSlashCommands(text) |
| 30 | + |
| 31 | + expect(commands).toHaveLength(1) |
| 32 | + expect(commands[0]).toEqual({ |
| 33 | + fullCommand: "/code", |
| 34 | + commandName: "code", |
| 35 | + type: "mode_switch", |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + it("should detect custom commands", () => { |
| 40 | + const text = "I'll use /deploy to deploy this application" |
| 41 | + const commands = detectSlashCommands(text) |
| 42 | + |
| 43 | + expect(commands).toHaveLength(1) |
| 44 | + expect(commands[0]).toEqual({ |
| 45 | + fullCommand: "/deploy", |
| 46 | + commandName: "deploy", |
| 47 | + type: "custom", |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it("should detect multiple commands in one text", () => { |
| 52 | + const text = "First /architect the solution, then /code it, and finally /deploy" |
| 53 | + const commands = detectSlashCommands(text) |
| 54 | + |
| 55 | + expect(commands).toHaveLength(3) |
| 56 | + expect(commands[0]).toEqual({ |
| 57 | + fullCommand: "/architect", |
| 58 | + commandName: "architect", |
| 59 | + type: "mode_switch", |
| 60 | + }) |
| 61 | + expect(commands[1]).toEqual({ |
| 62 | + fullCommand: "/code", |
| 63 | + commandName: "code", |
| 64 | + type: "mode_switch", |
| 65 | + }) |
| 66 | + expect(commands[2]).toEqual({ |
| 67 | + fullCommand: "/deploy", |
| 68 | + commandName: "deploy", |
| 69 | + type: "custom", |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it("should detect commands at the beginning of text", () => { |
| 74 | + const text = "/debug this issue please" |
| 75 | + const commands = detectSlashCommands(text) |
| 76 | + |
| 77 | + expect(commands).toHaveLength(1) |
| 78 | + expect(commands[0]).toEqual({ |
| 79 | + fullCommand: "/debug", |
| 80 | + commandName: "debug", |
| 81 | + type: "mode_switch", |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it("should detect commands at the beginning of new lines", () => { |
| 86 | + const text = "First do this:\n/architect the solution\nThen:\n/code the implementation" |
| 87 | + const commands = detectSlashCommands(text) |
| 88 | + |
| 89 | + expect(commands).toHaveLength(2) |
| 90 | + expect(commands[0]).toEqual({ |
| 91 | + fullCommand: "/architect", |
| 92 | + commandName: "architect", |
| 93 | + type: "mode_switch", |
| 94 | + }) |
| 95 | + expect(commands[1]).toEqual({ |
| 96 | + fullCommand: "/code", |
| 97 | + commandName: "code", |
| 98 | + type: "mode_switch", |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it("should handle commands with hyphens and underscores", () => { |
| 103 | + const text = "Use /my-custom_command to do this" |
| 104 | + const commands = detectSlashCommands(text) |
| 105 | + |
| 106 | + expect(commands).toHaveLength(1) |
| 107 | + expect(commands[0]).toEqual({ |
| 108 | + fullCommand: "/my-custom_command", |
| 109 | + commandName: "my-custom_command", |
| 110 | + type: "custom", |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it("should not detect invalid slash patterns", () => { |
| 115 | + const text = "This is a file path /home/user/file.txt and a URL https://example.com/path" |
| 116 | + const commands = detectSlashCommands(text) |
| 117 | + |
| 118 | + expect(commands).toHaveLength(0) |
| 119 | + }) |
| 120 | + |
| 121 | + it("should not detect slash commands in the middle of words", () => { |
| 122 | + const text = "The file://path and http://example.com don't contain commands" |
| 123 | + const commands = detectSlashCommands(text) |
| 124 | + |
| 125 | + expect(commands).toHaveLength(0) |
| 126 | + }) |
| 127 | + |
| 128 | + it("should handle empty or null input", () => { |
| 129 | + expect(detectSlashCommands("")).toEqual([]) |
| 130 | + expect(detectSlashCommands(null as any)).toEqual([]) |
| 131 | + expect(detectSlashCommands(undefined as any)).toEqual([]) |
| 132 | + }) |
| 133 | + |
| 134 | + it("should handle text with no commands", () => { |
| 135 | + const text = "This is just regular text with no slash commands" |
| 136 | + const commands = detectSlashCommands(text) |
| 137 | + |
| 138 | + expect(commands).toEqual([]) |
| 139 | + }) |
| 140 | + |
| 141 | + it("should detect commands that start with numbers after the slash", () => { |
| 142 | + const text = "Use /2fa to enable two-factor authentication" |
| 143 | + const commands = detectSlashCommands(text) |
| 144 | + |
| 145 | + expect(commands).toHaveLength(0) // Should not match as it starts with a number |
| 146 | + }) |
| 147 | + |
| 148 | + it("should detect commands that contain numbers", () => { |
| 149 | + const text = "Use /deploy2prod to deploy to production" |
| 150 | + const commands = detectSlashCommands(text) |
| 151 | + |
| 152 | + expect(commands).toHaveLength(1) |
| 153 | + expect(commands[0]).toEqual({ |
| 154 | + fullCommand: "/deploy2prod", |
| 155 | + commandName: "deploy2prod", |
| 156 | + type: "custom", |
| 157 | + }) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + describe("getFirstSlashCommand", () => { |
| 162 | + it("should return the first command when multiple exist", () => { |
| 163 | + const text = "First /architect then /code then /deploy" |
| 164 | + const command = getFirstSlashCommand(text) |
| 165 | + |
| 166 | + expect(command).toEqual({ |
| 167 | + fullCommand: "/architect", |
| 168 | + commandName: "architect", |
| 169 | + type: "mode_switch", |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + it("should return null when no commands exist", () => { |
| 174 | + const text = "No commands here" |
| 175 | + const command = getFirstSlashCommand(text) |
| 176 | + |
| 177 | + expect(command).toBeNull() |
| 178 | + }) |
| 179 | + |
| 180 | + it("should return the single command when only one exists", () => { |
| 181 | + const text = "Please /debug this issue" |
| 182 | + const command = getFirstSlashCommand(text) |
| 183 | + |
| 184 | + expect(command).toEqual({ |
| 185 | + fullCommand: "/debug", |
| 186 | + commandName: "debug", |
| 187 | + type: "mode_switch", |
| 188 | + }) |
| 189 | + }) |
| 190 | + }) |
| 191 | +}) |
0 commit comments