Elffuss BraidHub

kikocis/continue public

1c83b0b1adf2 · 6.6 KiBRawPermalink
1import type { AssistantUnrolled } from "@continuedev/config-yaml";
2import { describe, it, expect, vi, beforeEach, type MockedFunction } from "vitest";
3
4import type { AuthConfig, AuthenticatedConfig } from "./auth/workos.js";
5import type { ConfigServiceState } from "./services/types.js";
6import { handleSlashCommands } from "./slashCommands.js";
7
8// Mock the services
9vi.mock("./services/index.js", () => ({
10 services: {
11 config: {
12 getState: vi.fn()
13 }
14 },
15 reloadService: vi.fn(),
16 SERVICE_NAMES: {
17 AUTH: 'AUTH'
18 }
19}));
20
21// Mock auth functions
22vi.mock("./auth/workos.js", () => ({
23 isAuthenticated: vi.fn(),
24 isAuthenticatedConfig: vi.fn(),
25 loadAuthConfig: vi.fn()
26}));
27
28// Mock telemetry
29vi.mock("./telemetry/posthogService.js", () => ({
30 posthogService: {
31 capture: vi.fn()
32 }
33}));
34
35// Mock commands
36vi.mock("./commands/commands.js", () => ({
37 getAllSlashCommands: vi.fn(() => [])
38}));
39
40// Mock os and path
41vi.mock("os", () => ({
42 default: {
43 homedir: vi.fn(() => "/home/test")
44 }
45}));
46
47vi.mock("path", () => ({
48 default: {
49 join: vi.fn((...parts) => parts.join("/"))
50 },
51 join: vi.fn((...parts) => parts.join("/"))
52}));
53
54// Mock session functions
55vi.mock("./session.js", () => ({
56 getSessionFilePath: vi.fn(() => "/home/test/.continue/cli-sessions/continue-cli-pid-12345.json"),
57 hasSession: vi.fn(() => false)
58}));
59
60describe("slashCommands", () => {
61 const mockAssistant: AssistantUnrolled = {
62 name: "test-assistant",
63 version: "1.0.0"
64 };
65
66 beforeEach(() => {
67 vi.clearAllMocks();
68 });
69
70 describe("handleSlashCommands", () => {
71 it("should handle /info command when not authenticated", async () => {
72 const { isAuthenticated } = await import("./auth/workos.js");
73 const { services } = await import("./services/index.js");
74
75 (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false);
76 (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({
77 config: null,
78 configPath: "/test/config.yaml"
79 } as ConfigServiceState);
80
81 const result = await handleSlashCommands("/info", mockAssistant);
82
83 expect(result).toBeDefined();
84 expect(result?.output).toContain("Authentication:");
85 expect(result?.output).toContain("Not logged in");
86 expect(result?.output).toContain("Configuration:");
87 expect(result?.output).toContain("/test/config.yaml");
88 expect(result?.output).toContain("Session History:");
89 expect(result?.output).toContain(".json");
90 expect(result?.exit).toBe(false);
91 });
92
93 it("should handle /info command when authenticated via environment", async () => {
94 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = await import("./auth/workos.js");
95 const { services } = await import("./services/index.js");
96
97 (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(true);
98 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue({} as AuthConfig);
99 (isAuthenticatedConfig as any).mockReturnValue(false);
100 (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({
101 config: null,
102 configPath: undefined
103 } as ConfigServiceState);
104
105 const result = await handleSlashCommands("/info", mockAssistant);
106
107 expect(result).toBeDefined();
108 expect(result?.output).toContain("Authentication:");
109 expect(result?.output).toContain("Authenticated via environment variable");
110 expect(result?.output).toContain("Configuration:");
111 expect(result?.output).toContain("Config not found");
112 expect(result?.exit).toBe(false);
113 });
114
115 it("should handle /info command when authenticated with user config", async () => {
116 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = await import("./auth/workos.js");
117 const { services } = await import("./services/index.js");
118
119 const mockAuthConfig: AuthenticatedConfig = {
120 userId: "test-user-id",
121 userEmail: "test@example.com",
122 accessToken: "test-token",
123 refreshToken: "test-refresh-token",
124 expiresAt: Date.now() + 3600000,
125 organizationId: "test-org-id"
126 };
127
128 (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(true);
129 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(mockAuthConfig);
130 (isAuthenticatedConfig as any).mockReturnValue(true);
131 (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({
132 config: null,
133 configPath: "/custom/config.yaml"
134 } as ConfigServiceState);
135
136 const result = await handleSlashCommands("/info", mockAssistant);
137
138 expect(result).toBeDefined();
139 expect(result?.output).toContain("Authentication:");
140 expect(result?.output).toContain("test@example.com");
141 expect(result?.output).toContain("(no org)");
142 expect(result?.output).toContain("Configuration:");
143 expect(result?.output).toContain("/custom/config.yaml");
144 expect(result?.exit).toBe(false);
145 });
146
147 it("should handle config service errors gracefully", async () => {
148 const { isAuthenticated } = await import("./auth/workos.js");
149 const { services } = await import("./services/index.js");
150
151 (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false);
152 (services.config.getState as MockedFunction<typeof services.config.getState>).mockImplementation(() => {
153 throw new Error("Service not ready");
154 });
155
156 const result = await handleSlashCommands("/info", mockAssistant);
157
158 expect(result).toBeDefined();
159 expect(result?.output).toContain("Configuration service not available");
160 });
161
162 it("should use test session directory when in test mode", async () => {
163 const { isAuthenticated } = await import("./auth/workos.js");
164 const { services } = await import("./services/index.js");
165 const { getSessionFilePath } = await import("./session.js");
166
167 // Mock the session path for this specific test
168 (getSessionFilePath as any).mockReturnValue("/test-home/.continue/cli-sessions/continue-cli-test-123.json");
169
170 (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false);
171 (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({
172 config: null,
173 configPath: undefined
174 } as ConfigServiceState);
175
176 const result = await handleSlashCommands("/info", mockAssistant);
177
178 expect(result?.output).toContain("/test-home/.continue/cli-sessions/");
179 expect(result?.output).toContain(".json");
180 });
181 });
182});