| 1 | // Import after mocking |
| 2 | import { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 3 | import { afterEach, beforeEach, describe, expect, vi, test } from "vitest"; |
| 4 | |
| 5 | import * as workos from "./auth/workos.js"; |
| 6 | import * as commands from "./commands/commands.js"; |
| 7 | import { reloadService, SERVICE_NAMES, services } from "./services/index.js"; |
| 8 | import type { AuthServiceState } from "./services/types.js"; |
| 9 | import { handleSlashCommands } from "./slashCommands.js"; |
| 10 | |
| 11 | // The imports are already mocked via vitest.setup.ts, so we can use them directly |
| 12 | |
| 13 | // Mock console to avoid output during tests |
| 14 | const originalConsole = console; |
| 15 | const mockConsole = { |
| 16 | info: vi.fn(), |
| 17 | error: vi.fn(), |
| 18 | log: vi.fn(), |
| 19 | }; |
| 20 | |
| 21 | describe("handleSlashCommands", () => { |
| 22 | const mockAssistant: AssistantUnrolled = { |
| 23 | name: "test-assistant", |
| 24 | version: "1.0.0", |
| 25 | prompts: [{ name: "test", prompt: "Test prompt: " }], |
| 26 | }; |
| 27 | |
| 28 | beforeEach(() => { |
| 29 | Object.assign(console, mockConsole); |
| 30 | vi.clearAllMocks(); |
| 31 | |
| 32 | // Since the mock already returns the required value, we don't need to set it |
| 33 | // Just verify it returns what we expect |
| 34 | expect(commands.getAllSlashCommands(mockAssistant)).toEqual( |
| 35 | expect.arrayContaining([ |
| 36 | expect.objectContaining({ name: "help" }), |
| 37 | expect.objectContaining({ name: "login" }), |
| 38 | ]), |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | afterEach(() => { |
| 43 | Object.assign(console, originalConsole); |
| 44 | }); |
| 45 | |
| 46 | describe("Automatic Service Cascade Reloading", () => { |
| 47 | test.skip("login command should trigger automatic cascade reload via auth service", async () => { |
| 48 | const newAuthState: AuthServiceState = { |
| 49 | authConfig: { userEmail: "test@example.com" } as any, |
| 50 | isAuthenticated: true, |
| 51 | }; |
| 52 | |
| 53 | (services.auth.login as any).mockResolvedValue(newAuthState); |
| 54 | (workos.isAuthenticatedConfig as any).mockReturnValue(true); |
| 55 | (reloadService as any).mockResolvedValue(undefined); |
| 56 | |
| 57 | const result = await handleSlashCommands("/login", mockAssistant); |
| 58 | |
| 59 | expect(result).not.toBeNull(); |
| 60 | expect(result?.output).toContain( |
| 61 | "Login successful! All services updated automatically.", |
| 62 | ); |
| 63 | |
| 64 | // Verify auth service login was called |
| 65 | expect(services.auth.login).toHaveBeenCalledTimes(1); |
| 66 | |
| 67 | // Verify automatic cascade reload was triggered - only auth service reload needed |
| 68 | expect(reloadService).toHaveBeenCalledTimes(1); |
| 69 | expect(reloadService).toHaveBeenCalledWith(SERVICE_NAMES.AUTH); |
| 70 | |
| 71 | // Verify manual service reloads are NOT called |
| 72 | expect(reloadService).not.toHaveBeenCalledWith(SERVICE_NAMES.API_CLIENT); |
| 73 | expect(reloadService).not.toHaveBeenCalledWith(SERVICE_NAMES.CONFIG); |
| 74 | }); |
| 75 | |
| 76 | test.skip("login failure should not trigger any reloads", async () => { |
| 77 | const loginError = new Error("Login failed"); |
| 78 | (services.auth.login as any).mockRejectedValue(loginError); |
| 79 | |
| 80 | const result = await handleSlashCommands("/login", mockAssistant); |
| 81 | |
| 82 | expect(result).not.toBeNull(); |
| 83 | expect(result?.output).toContain("Login failed: Login failed"); |
| 84 | |
| 85 | // Verify NO reload was triggered on failure |
| 86 | expect(reloadService).not.toHaveBeenCalled(); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | describe("Other Commands (unchanged behavior)", () => { |
| 91 | test("help command should work normally", async () => { |
| 92 | const result = await handleSlashCommands("/help", mockAssistant); |
| 93 | |
| 94 | expect(result).not.toBeNull(); |
| 95 | expect(result?.output).toContain("Slash commands:"); |
| 96 | expect(result?.output).toContain("/help: Show help"); |
| 97 | expect(result?.output).toContain("/login: Authenticate"); |
| 98 | }); |
| 99 | |
| 100 | test.skip("logout command should exit", async () => { |
| 101 | const logoutState: AuthServiceState = { |
| 102 | authConfig: null, |
| 103 | isAuthenticated: false, |
| 104 | }; |
| 105 | (services.auth.logout as any).mockResolvedValue(logoutState); |
| 106 | |
| 107 | const result = await handleSlashCommands("/logout", mockAssistant); |
| 108 | |
| 109 | expect(result).not.toBeNull(); |
| 110 | expect(result?.exit).toBe(true); |
| 111 | expect(result?.output).toContain("Logged out successfully"); |
| 112 | }); |
| 113 | |
| 114 | test("clear command should work normally", async () => { |
| 115 | const result = await handleSlashCommands("/clear", mockAssistant); |
| 116 | |
| 117 | expect(result).not.toBeNull(); |
| 118 | expect(result?.clear).toBe(true); |
| 119 | expect(result?.output).toBe("Chat history cleared"); |
| 120 | }); |
| 121 | |
| 122 | test("compact command should work normally", async () => { |
| 123 | const result = await handleSlashCommands("/compact", mockAssistant); |
| 124 | |
| 125 | expect(result).not.toBeNull(); |
| 126 | expect(result?.compact).toBe(true); |
| 127 | }); |
| 128 | |
| 129 | test("resume command should open session selector", async () => { |
| 130 | const result = await handleSlashCommands("/resume", mockAssistant); |
| 131 | |
| 132 | expect(result).not.toBeNull(); |
| 133 | expect(result?.openSessionSelector).toBe(true); |
| 134 | }); |
| 135 | |
| 136 | test("non-slash input should return null", async () => { |
| 137 | const result = await handleSlashCommands("regular input", mockAssistant); |
| 138 | expect(result).toBeNull(); |
| 139 | }); |
| 140 | |
| 141 | test("unknown slash command should return error", async () => { |
| 142 | const result = await handleSlashCommands("/unknown", mockAssistant); |
| 143 | |
| 144 | expect(result).not.toBeNull(); |
| 145 | expect(result?.output).toBe("Unknown command: unknown"); |
| 146 | }); |
| 147 | |
| 148 | test("/org command should no longer exist (now merged into /config)", async () => { |
| 149 | const result = await handleSlashCommands("/org list", mockAssistant); |
| 150 | |
| 151 | expect(result).not.toBeNull(); |
| 152 | expect(result?.output).toBe("Unknown command: org"); |
| 153 | }); |
| 154 | }); |
| 155 | }); |