| 1 | import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | |
| 3 | import * as workosModule from "./auth/workos.js"; |
| 4 | import { services } from "./services/index.js"; |
| 5 | import * as sessionModule from "./session.js"; |
| 6 | import { handleSlashCommands } from "./slashCommands.js"; |
| 7 | import { posthogService } from "./telemetry/posthogService.js"; |
| 8 | import * as versionModule from "./version.js"; |
| 9 | |
| 10 | // Mock all dependencies |
| 11 | vi.mock("./auth/workos.js"); |
| 12 | vi.mock("./version.js"); |
| 13 | vi.mock("./session.js"); |
| 14 | vi.mock("./telemetry/posthogService.js"); |
| 15 | |
| 16 | vi.mock("./services/index.js", async (importOriginal) => { |
| 17 | const actual = await importOriginal<typeof import("./services/index.js")>(); |
| 18 | return { |
| 19 | ...actual, |
| 20 | services: { |
| 21 | config: { |
| 22 | getState: vi.fn(), |
| 23 | }, |
| 24 | model: { |
| 25 | getModelInfo: vi.fn(), |
| 26 | }, |
| 27 | }, |
| 28 | }; |
| 29 | }); |
| 30 | |
| 31 | describe("handleSlashCommands - /info", () => { |
| 32 | const mockAssistant = { |
| 33 | prompts: [], |
| 34 | }; |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | vi.clearAllMocks(); |
| 38 | |
| 39 | // Mock process.cwd |
| 40 | vi.spyOn(process, "cwd").mockReturnValue("/test/working/directory"); |
| 41 | |
| 42 | // Mock posthog |
| 43 | vi.mocked(posthogService.capture).mockReturnValue(Promise.resolve()); |
| 44 | |
| 45 | // Mock version |
| 46 | vi.mocked(versionModule.getVersion).mockReturnValue("1.2.3"); |
| 47 | |
| 48 | // Mock session |
| 49 | vi.mocked(sessionModule.getSessionFilePath).mockReturnValue( |
| 50 | "/test/session.json", |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | it("should include version and working directory in output", async () => { |
| 55 | // Mock auth as not authenticated |
| 56 | vi.mocked(workosModule.isAuthenticated).mockReturnValue(false); |
| 57 | |
| 58 | // Mock config service |
| 59 | const mockConfigState = { |
| 60 | config: { name: "test-config", version: "1.0.0" } as any, |
| 61 | configPath: "/test/config.yaml", |
| 62 | }; |
| 63 | vi.mocked(services.config.getState).mockReturnValue(mockConfigState); |
| 64 | vi.mocked(services.model.getModelInfo).mockReturnValue({ |
| 65 | provider: "openai", |
| 66 | name: "gpt-4", |
| 67 | }); |
| 68 | |
| 69 | const result = await handleSlashCommands("/info", mockAssistant as any); |
| 70 | |
| 71 | expect(result).toBeDefined(); |
| 72 | expect(result?.output).toContain("CLI Information:"); |
| 73 | expect(result?.output).toContain("Version: 1.2.3"); |
| 74 | expect(result?.output).toContain( |
| 75 | "Working Directory: /test/working/directory", |
| 76 | ); |
| 77 | expect(result?.output).toContain("Configuration:"); |
| 78 | expect(result?.output).toContain("Model: gpt-4"); |
| 79 | expect(result?.exit).toBe(false); |
| 80 | }); |
| 81 | |
| 82 | it("should handle authenticated user info", async () => { |
| 83 | // Mock auth as authenticated |
| 84 | vi.mocked(workosModule.isAuthenticated).mockReturnValue(true); |
| 85 | vi.mocked(workosModule.loadAuthConfig).mockReturnValue({ |
| 86 | userEmail: "test@example.com", |
| 87 | userId: "test-user", |
| 88 | } as any); |
| 89 | vi.mocked(workosModule.isAuthenticatedConfig).mockReturnValue(true); |
| 90 | |
| 91 | // Mock config service |
| 92 | const mockConfigState = { |
| 93 | config: { name: "test-config", version: "1.0.0" } as any, |
| 94 | configPath: "/test/config.yaml", |
| 95 | }; |
| 96 | vi.mocked(services.config.getState).mockReturnValue(mockConfigState); |
| 97 | vi.mocked(services.model.getModelInfo).mockReturnValue({ |
| 98 | provider: "anthropic", |
| 99 | name: "claude-3-sonnet", |
| 100 | }); |
| 101 | |
| 102 | const result = await handleSlashCommands("/info", mockAssistant as any); |
| 103 | |
| 104 | expect(result?.output).toContain("Authentication:"); |
| 105 | expect(result?.output).toContain("Email: test@example.com"); |
| 106 | expect(result?.output).toContain("Model: claude-3-sonnet"); |
| 107 | }); |
| 108 | |
| 109 | it("should handle missing model info gracefully", async () => { |
| 110 | // Mock auth as not authenticated |
| 111 | vi.mocked(workosModule.isAuthenticated).mockReturnValue(false); |
| 112 | |
| 113 | // Mock config service with no model info |
| 114 | const mockConfigState = { |
| 115 | config: { name: "test-config", version: "1.0.0" } as any, |
| 116 | configPath: "/test/config.yaml", |
| 117 | }; |
| 118 | vi.mocked(services.config.getState).mockReturnValue(mockConfigState); |
| 119 | vi.mocked(services.model.getModelInfo).mockReturnValue(null); |
| 120 | |
| 121 | const result = await handleSlashCommands("/info", mockAssistant as any); |
| 122 | |
| 123 | expect(result?.output).toContain("Model: Not available"); |
| 124 | }); |
| 125 | |
| 126 | it("should handle config service error", async () => { |
| 127 | // Mock auth as not authenticated |
| 128 | vi.mocked(workosModule.isAuthenticated).mockReturnValue(false); |
| 129 | |
| 130 | // Mock config service throwing error |
| 131 | vi.mocked(services.config.getState).mockImplementation(() => { |
| 132 | throw new Error("Service not available"); |
| 133 | }); |
| 134 | |
| 135 | const result = await handleSlashCommands("/info", mockAssistant as any); |
| 136 | |
| 137 | expect(result?.output).toContain("Configuration service not available"); |
| 138 | }); |
| 139 | }); |