| 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 | })); |
| 87 | |
| 88 | describe("slashCommands", () => { |
| 89 | const mockAssistant: AssistantUnrolled = { |
| 90 | name: "test-assistant", |
| 91 | version: "1.0.0", |
| 92 | }; |
| 93 | |
| 94 | beforeEach(() => { |
| 95 | vi.clearAllMocks(); |
| 96 | }); |
| 97 | |
| 98 | describe("handleSlashCommands", () => { |
| 99 | it("should handle /info command when not authenticated", async () => { |
| 100 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 101 | const { services } = await import("./services/index.js"); |
| 102 | |
| 103 | ( |
| 104 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 105 | ).mockReturnValue(false); |
| 106 | ( |
| 107 | services.config.getState as MockedFunction< |
| 108 | typeof services.config.getState |
| 109 | > |
| 110 | ).mockReturnValue({ |
| 111 | config: null, |
| 112 | configPath: "/test/config.yaml", |
| 113 | } as ConfigServiceState); |
| 114 | |
| 115 | const result = await handleSlashCommands("/info", mockAssistant); |
| 116 | |
| 117 | expect(result).toBeDefined(); |
| 118 | expect(result?.output).toContain("Authentication:"); |
| 119 | expect(result?.output).toContain("Not logged in"); |
| 120 | expect(result?.output).toContain("Configuration:"); |
| 121 | expect(result?.output).toContain("/test/config.yaml"); |
| 122 | expect(result?.output).toContain("Session History:"); |
| 123 | expect(result?.output).toContain(".json"); |
| 124 | expect(result?.exit).toBe(false); |
| 125 | }); |
| 126 | |
| 127 | it("should handle /info command when authenticated via environment", async () => { |
| 128 | const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = |
| 129 | await import("./auth/workos.js"); |
| 130 | const { services } = await import("./services/index.js"); |
| 131 | |
| 132 | ( |
| 133 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 134 | ).mockReturnValue(true); |
| 135 | (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( |
| 136 | {} as AuthConfig, |
| 137 | ); |
| 138 | (isAuthenticatedConfig as any).mockReturnValue(false); |
| 139 | ( |
| 140 | services.config.getState as MockedFunction< |
| 141 | typeof services.config.getState |
| 142 | > |
| 143 | ).mockReturnValue({ |
| 144 | config: null, |
| 145 | configPath: undefined, |
| 146 | } as ConfigServiceState); |
| 147 | |
| 148 | const result = await handleSlashCommands("/info", mockAssistant); |
| 149 | |
| 150 | expect(result).toBeDefined(); |
| 151 | expect(result?.output).toContain("Authentication:"); |
| 152 | expect(result?.output).toContain( |
| 153 | "Authenticated via environment variable", |
| 154 | ); |
| 155 | expect(result?.output).toContain("Configuration:"); |
| 156 | expect(result?.output).toContain("Config not found"); |
| 157 | expect(result?.exit).toBe(false); |
| 158 | }); |
| 159 | |
| 160 | it("should handle /info command when authenticated with user config", async () => { |
| 161 | const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = |
| 162 | await import("./auth/workos.js"); |
| 163 | const { services } = await import("./services/index.js"); |
| 164 | |
| 165 | const mockAuthConfig: AuthenticatedConfig = { |
| 166 | userId: "test-user-id", |
| 167 | userEmail: "test@example.com", |
| 168 | accessToken: "test-token", |
| 169 | refreshToken: "test-refresh-token", |
| 170 | expiresAt: Date.now() + 3600000, |
| 171 | organizationId: "test-org-id", |
| 172 | }; |
| 173 | |
| 174 | ( |
| 175 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 176 | ).mockReturnValue(true); |
| 177 | (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( |
| 178 | mockAuthConfig, |
| 179 | ); |
| 180 | (isAuthenticatedConfig as any).mockReturnValue(true); |
| 181 | ( |
| 182 | services.config.getState as MockedFunction< |
| 183 | typeof services.config.getState |
| 184 | > |
| 185 | ).mockReturnValue({ |
| 186 | config: null, |
| 187 | configPath: "/custom/config.yaml", |
| 188 | } as ConfigServiceState); |
| 189 | |
| 190 | const result = await handleSlashCommands("/info", mockAssistant); |
| 191 | |
| 192 | expect(result).toBeDefined(); |
| 193 | expect(result?.output).toContain("Authentication:"); |
| 194 | expect(result?.output).toContain("test@example.com"); |
| 195 | expect(result?.output).toContain("(no org)"); |
| 196 | expect(result?.output).toContain("Configuration:"); |
| 197 | expect(result?.output).toContain("/custom/config.yaml"); |
| 198 | expect(result?.exit).toBe(false); |
| 199 | }); |
| 200 | |
| 201 | it("should handle config service errors gracefully", async () => { |
| 202 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 203 | const { services } = await import("./services/index.js"); |
| 204 | |
| 205 | ( |
| 206 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 207 | ).mockReturnValue(false); |
| 208 | ( |
| 209 | services.config.getState as MockedFunction< |
| 210 | typeof services.config.getState |
| 211 | > |
| 212 | ).mockImplementation(() => { |
| 213 | throw new Error("Service not ready"); |
| 214 | }); |
| 215 | |
| 216 | const result = await handleSlashCommands("/info", mockAssistant); |
| 217 | |
| 218 | expect(result).toBeDefined(); |
| 219 | expect(result?.output).toContain("Configuration service not available"); |
| 220 | }); |
| 221 | |
| 222 | it("should use test session directory when in test mode", async () => { |
| 223 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 224 | const { services } = await import("./services/index.js"); |
| 225 | const { getSessionFilePath } = await import("./session.js"); |
| 226 | |
| 227 | // Mock the session path for this specific test |
| 228 | (getSessionFilePath as any).mockReturnValue( |
| 229 | "/test-home/.continue/cli-sessions/continue-cli-test-123.json", |
| 230 | ); |
| 231 | |
| 232 | ( |
| 233 | isAuthenticated as MockedFunction<typeof isAuthenticated> |
| 234 | ).mockReturnValue(false); |
| 235 | ( |
| 236 | services.config.getState as MockedFunction< |
| 237 | typeof services.config.getState |
| 238 | > |
| 239 | ).mockReturnValue({ |
| 240 | config: null, |
| 241 | configPath: undefined, |
| 242 | } as ConfigServiceState); |
| 243 | |
| 244 | const result = await handleSlashCommands("/info", mockAssistant); |
| 245 | |
| 246 | expect(result?.output).toContain("/test-home/.continue/cli-sessions/"); |
| 247 | expect(result?.output).toContain(".json"); |
| 248 | }); |
| 249 | }); |
| 250 | }); |