| 1 | import type { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | describe, |
| 4 | it, |
| 5 | expect, |
| 6 | vi, |
| 7 | beforeEach, |
| 8 | type MockedFunction, |
| 9 | } from "vitest"; |
| 10 | |
| 11 | import type { AuthConfig, AuthenticatedConfig } from "./auth/workos.js"; |
| 12 | import type { ConfigServiceState } from "./services/types.js"; |
| 13 | import { handleSlashCommands } from "./slashCommands.js"; |
| 14 | |
| 15 | // Mock the services |
| 16 | vi.mock("./services/index.js", () => ({ |
| 17 | services: { |
| 18 | config: { |
| 19 | getState: vi.fn(), |
| 20 | }, |
| 21 | }, |
| 22 | reloadService: vi.fn(), |
| 23 | SERVICE_NAMES: { |
| 24 | AUTH: "AUTH", |
| 25 | }, |
| 26 | })); |
| 27 | |
| 28 | // Mock auth functions |
| 29 | vi.mock("./auth/workos.js", () => ({ |
| 30 | isAuthenticated: vi.fn(), |
| 31 | isAuthenticatedConfig: vi.fn(), |
| 32 | loadAuthConfig: vi.fn(), |
| 33 | })); |
| 34 | |
| 35 | // Mock telemetry |
| 36 | vi.mock("./telemetry/posthogService.js", () => ({ |
| 37 | posthogService: { |
| 38 | capture: vi.fn(), |
| 39 | }, |
| 40 | })); |
| 41 | |
| 42 | // Mock commands |
| 43 | vi.mock("./commands/commands.js", () => ({ |
| 44 | getAllSlashCommands: vi.fn(() => []), |
| 45 | })); |
| 46 | |
| 47 | // Mock logger to avoid file system operations |
| 48 | vi.mock("./util/logger.js", () => ({ |
| 49 | logger: { |
| 50 | debug: vi.fn(), |
| 51 | info: vi.fn(), |
| 52 | warn: vi.fn(), |
| 53 | error: vi.fn(), |
| 54 | }, |
| 55 | })); |
| 56 | |
| 57 | // Mock env.js to avoid file operations |
| 58 | vi.mock("./env.js", () => ({ |
| 59 | env: { |
| 60 | continueHome: "/home/test/.continue", |
| 61 | appUrl: "https://hub.continue.dev", |
| 62 | }, |
| 63 | })); |
| 64 | |
| 65 | // Mock os and path |
| 66 | vi.mock("os", () => ({ |
| 67 | default: { |
| 68 | homedir: vi.fn(() => "/home/test"), |
| 69 | }, |
| 70 | homedir: vi.fn(() => "/home/test"), |
| 71 | })); |
| 72 | |
| 73 | vi.mock("path", () => ({ |
| 74 | default: { |
| 75 | join: vi.fn((...parts) => parts.join("/")), |
| 76 | }, |
| 77 | join: vi.fn((...parts) => parts.join("/")), |
| 78 | })); |
| 79 | |
| 80 | // Mock session functions |
| 81 | vi.mock("./session.js", () => ({ |
| 82 | getSessionFilePath: vi.fn( |
| 83 | () => "/home/test/.continue/cli-sessions/continue-cli-pid-12345.json", |
| 84 | ), |
| 85 | hasSession: vi.fn(() => false), |
| 86 | getCurrentSession: vi.fn(() => { |
| 87 | throw new Error("Session not available"); |
| 88 | }), |
| 89 | })); |
| 90 | |
| 91 | describe("slashCommands", () => { |
| 92 | const mockAssistant: AssistantUnrolled = { |
| 93 | name: "test-assistant", |
| 94 | version: "1.0.0", |
| 95 | }; |
| 96 | |
| 97 | beforeEach(() => { |
| 98 | vi.clearAllMocks(); |
| 99 | }); |
| 100 | |
| 101 | describe("handleSlashCommands", () => { |
| 102 | it("should handle /help command", async () => { |
| 103 | const result = await handleSlashCommands("/help", mockAssistant); |
| 104 | |
| 105 | expect(result).toBeDefined(); |
| 106 | expect(result?.output).toContain("Keyboard Shortcuts:"); |
| 107 | expect(result?.output).toContain("Navigation:"); |
| 108 | expect(result?.output).toContain("↑/↓"); |
| 109 | expect(result?.output).toContain("Tab"); |
| 110 | expect(result?.output).toContain("Enter"); |
| 111 | expect(result?.output).toContain("Shift+Enter"); |
| 112 | expect(result?.output).toContain("Controls:"); |
| 113 | expect(result?.output).toContain("Ctrl+C"); |
| 114 | expect(result?.output).toContain("Ctrl+D"); |
| 115 | expect(result?.output).toContain("Ctrl+L"); |
| 116 | expect(result?.output).toContain("Shift+Tab"); |
| 117 | expect(result?.output).toContain("Esc"); |
| 118 | expect(result?.output).toContain("Special Characters:"); |
| 119 | expect(result?.output).toContain("@"); |
| 120 | expect(result?.output).toContain("/"); |
| 121 | expect(result?.exit).toBeUndefined(); |
| 122 | }); |
| 123 | |
| 124 | it("should handle /info command when not authenticated", async () => { |
| 125 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 126 | const { services } = await import("./services/index.js"); |
| 127 | |
| 128 | ( |
| 129 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 130 | ).mockReturnValue(false); |
| 131 | ( |
| 132 | services.config.getState as MockedFunction< |
| 133 | typeof services.config.getState |
| 134 | > |
| 135 | ).mockReturnValue({ |
| 136 | config: null, |
| 137 | configPath: "/test/config.yaml", |
| 138 | } as ConfigServiceState); |
| 139 | |
| 140 | const result = await handleSlashCommands("/info", mockAssistant); |
| 141 | |
| 142 | expect(result).toBeDefined(); |
| 143 | expect(result?.output).toContain("Authentication:"); |
| 144 | expect(result?.output).toContain("Not logged in"); |
| 145 | expect(result?.output).toContain("Configuration:"); |
| 146 | expect(result?.output).toContain("/test/config.yaml"); |
| 147 | expect(result?.output).toContain("Session:"); |
| 148 | expect(result?.output).toContain("Session not available"); |
| 149 | expect(result?.exit).toBe(false); |
| 150 | }); |
| 151 | |
| 152 | it("should handle /info command when authenticated via environment", async () => { |
| 153 | const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = |
| 154 | await import("./auth/workos.js"); |
| 155 | const { services } = await import("./services/index.js"); |
| 156 | |
| 157 | ( |
| 158 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 159 | ).mockReturnValue(true); |
| 160 | (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( |
| 161 | {} as AuthConfig, |
| 162 | ); |
| 163 | (isAuthenticatedConfig as any).mockReturnValue(false); |
| 164 | ( |
| 165 | services.config.getState as MockedFunction< |
| 166 | typeof services.config.getState |
| 167 | > |
| 168 | ).mockReturnValue({ |
| 169 | config: null, |
| 170 | configPath: undefined, |
| 171 | } as ConfigServiceState); |
| 172 | |
| 173 | const result = await handleSlashCommands("/info", mockAssistant); |
| 174 | |
| 175 | expect(result).toBeDefined(); |
| 176 | expect(result?.output).toContain("Authentication:"); |
| 177 | expect(result?.output).toContain( |
| 178 | "Authenticated via environment variable", |
| 179 | ); |
| 180 | expect(result?.output).toContain("Configuration:"); |
| 181 | expect(result?.output).toContain("Config not found"); |
| 182 | expect(result?.exit).toBe(false); |
| 183 | }); |
| 184 | |
| 185 | it("should handle /info command when authenticated with user config", async () => { |
| 186 | const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = |
| 187 | await import("./auth/workos.js"); |
| 188 | const { services } = await import("./services/index.js"); |
| 189 | |
| 190 | const mockAuthConfig: AuthenticatedConfig = { |
| 191 | userId: "test-user-id", |
| 192 | userEmail: "test@example.com", |
| 193 | accessToken: "test-token", |
| 194 | refreshToken: "test-refresh-token", |
| 195 | expiresAt: Date.now() + 3600000, |
| 196 | organizationId: "test-org-id", |
| 197 | }; |
| 198 | |
| 199 | ( |
| 200 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 201 | ).mockReturnValue(true); |
| 202 | (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( |
| 203 | mockAuthConfig, |
| 204 | ); |
| 205 | (isAuthenticatedConfig as any).mockReturnValue(true); |
| 206 | ( |
| 207 | services.config.getState as MockedFunction< |
| 208 | typeof services.config.getState |
| 209 | > |
| 210 | ).mockReturnValue({ |
| 211 | config: null, |
| 212 | configPath: "/custom/config.yaml", |
| 213 | } as ConfigServiceState); |
| 214 | |
| 215 | const result = await handleSlashCommands("/info", mockAssistant); |
| 216 | |
| 217 | expect(result).toBeDefined(); |
| 218 | expect(result?.output).toContain("Authentication:"); |
| 219 | expect(result?.output).toContain("test@example.com"); |
| 220 | expect(result?.output).toContain("test-org-id"); |
| 221 | expect(result?.output).toContain("Configuration:"); |
| 222 | expect(result?.output).toContain("/custom/config.yaml"); |
| 223 | expect(result?.exit).toBe(false); |
| 224 | }); |
| 225 | |
| 226 | it("should handle config service errors gracefully", async () => { |
| 227 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 228 | const { services } = await import("./services/index.js"); |
| 229 | |
| 230 | ( |
| 231 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 232 | ).mockReturnValue(false); |
| 233 | ( |
| 234 | services.config.getState as MockedFunction< |
| 235 | typeof services.config.getState |
| 236 | > |
| 237 | ).mockImplementation(() => { |
| 238 | throw new Error("Service not ready"); |
| 239 | }); |
| 240 | |
| 241 | const result = await handleSlashCommands("/info", mockAssistant); |
| 242 | |
| 243 | expect(result).toBeDefined(); |
| 244 | expect(result?.output).toContain("Configuration service not available"); |
| 245 | }); |
| 246 | |
| 247 | it("should use test session directory when in test mode", async () => { |
| 248 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 249 | const { services } = await import("./services/index.js"); |
| 250 | const { getSessionFilePath, getCurrentSession } = await import( |
| 251 | "./session.js" |
| 252 | ); |
| 253 | |
| 254 | // Mock the session functions for this specific test |
| 255 | (getSessionFilePath as any).mockReturnValue( |
| 256 | "/test-home/.continue/cli-sessions/continue-cli-test-123.json", |
| 257 | ); |
| 258 | (getCurrentSession as any).mockReturnValue({ |
| 259 | sessionId: "test-123", |
| 260 | title: "Test Session", |
| 261 | }); |
| 262 | |
| 263 | ( |
| 264 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 265 | ).mockReturnValue(false); |
| 266 | ( |
| 267 | services.config.getState as MockedFunction< |
| 268 | typeof services.config.getState |
| 269 | > |
| 270 | ).mockReturnValue({ |
| 271 | config: null, |
| 272 | configPath: undefined, |
| 273 | } as ConfigServiceState); |
| 274 | |
| 275 | const result = await handleSlashCommands("/info", mockAssistant); |
| 276 | |
| 277 | expect(result?.output).toContain("Session:"); |
| 278 | expect(result?.output).toContain("Test Session"); |
| 279 | expect(result?.output).toContain("/test-home/.continue/cli-sessions/"); |
| 280 | expect(result?.output).toContain(".json"); |
| 281 | }); |
| 282 | }); |
| 283 | }); |