Elffuss BraidHub

kikocis/continue public

main · 10.5 KiBRawPermalink
1import type { AssistantUnrolled } from "@continuedev/config-yaml";
2import {
3 describe,
4 it,
5 expect,
6 vi,
7 beforeEach,
8 type MockedFunction,
9} from "vitest";
10
11import type { AuthConfig, AuthenticatedConfig } from "./auth/workos.js";
12import type { ConfigServiceState } from "./services/types.js";
13import { handleSlashCommands } from "./slashCommands.js";
14
15// Mock the services
16vi.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
29vi.mock("./auth/workos.js", () => ({
30 isAuthenticated: vi.fn(),
31 isAuthenticatedConfig: vi.fn(),
32 loadAuthConfig: vi.fn(),
33}));
34
35// Mock telemetry
36vi.mock("./telemetry/posthogService.js", () => ({
37 posthogService: {
38 capture: vi.fn(),
39 },
40}));
41
42// Mock commands
43vi.mock("./commands/commands.js", () => ({
44 getAllSlashCommands: vi.fn(() => []),
45}));
46
47// Mock logger to avoid file system operations
48vi.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
58vi.mock("./env.js", () => ({
59 env: {
60 continueHome: "/home/test/.continue",
61 appUrl: "https://hub.continue.dev",
62 },
63}));
64
65// Mock os and path
66vi.mock("os", () => ({
67 default: {
68 homedir: vi.fn(() => "/home/test"),
69 },
70 homedir: vi.fn(() => "/home/test"),
71}));
72
73vi.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
81vi.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 updateSessionTitle: vi.fn(),
90}));
91
92describe("slashCommands", () => {
93 const mockAssistant: AssistantUnrolled = {
94 name: "test-assistant",
95 version: "1.0.0",
96 };
97
98 beforeEach(() => {
99 vi.clearAllMocks();
100 });
101
102 describe("handleSlashCommands", () => {
103 it("should handle /help command", async () => {
104 const result = await handleSlashCommands("/help", mockAssistant);
105
106 expect(result).toBeDefined();
107 expect(result?.output).toContain("Keyboard Shortcuts:");
108 expect(result?.output).toContain("Navigation:");
109 expect(result?.output).toContain("↑/↓");
110 expect(result?.output).toContain("Tab");
111 expect(result?.output).toContain("Enter");
112 expect(result?.output).toContain("Shift+Enter");
113 expect(result?.output).toContain("Controls:");
114 expect(result?.output).toContain("Ctrl+C");
115 expect(result?.output).toContain("Ctrl+D");
116 expect(result?.output).toContain("Ctrl+L");
117 expect(result?.output).toContain("Shift+Tab");
118 expect(result?.output).toContain("Esc");
119 expect(result?.output).toContain("Special Characters:");
120 expect(result?.output).toContain("@");
121 expect(result?.output).toContain("/");
122 expect(result?.exit).toBeUndefined();
123 });
124
125 it("should handle /info command when not authenticated", async () => {
126 const { isAuthenticated } = await import("./auth/workos.js");
127 const { services } = await import("./services/index.js");
128
129 (
130 isAuthenticated as MockedFunction<typeof isAuthenticated>
131 ).mockReturnValue(false);
132 (
133 services.config.getState as MockedFunction<
134 typeof services.config.getState
135 >
136 ).mockReturnValue({
137 config: null,
138 configPath: "/test/config.yaml",
139 } as ConfigServiceState);
140
141 const result = await handleSlashCommands("/info", mockAssistant);
142
143 expect(result).toBeDefined();
144 expect(result?.output).toContain("Authentication:");
145 expect(result?.output).toContain("Not logged in");
146 expect(result?.output).toContain("Configuration:");
147 expect(result?.output).toContain("/test/config.yaml");
148 expect(result?.output).toContain("Session:");
149 expect(result?.output).toContain("Session not available");
150 expect(result?.exit).toBe(false);
151 });
152
153 it("should handle /info command when authenticated via environment", async () => {
154 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } =
155 await import("./auth/workos.js");
156 const { services } = await import("./services/index.js");
157
158 (
159 isAuthenticated as MockedFunction<typeof isAuthenticated>
160 ).mockReturnValue(true);
161 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(
162 {} as AuthConfig,
163 );
164 (isAuthenticatedConfig as any).mockReturnValue(false);
165 (
166 services.config.getState as MockedFunction<
167 typeof services.config.getState
168 >
169 ).mockReturnValue({
170 config: null,
171 configPath: undefined,
172 } as ConfigServiceState);
173
174 const result = await handleSlashCommands("/info", mockAssistant);
175
176 expect(result).toBeDefined();
177 expect(result?.output).toContain("Authentication:");
178 expect(result?.output).toContain(
179 "Authenticated via environment variable",
180 );
181 expect(result?.output).toContain("Configuration:");
182 expect(result?.output).toContain("Config not found");
183 expect(result?.exit).toBe(false);
184 });
185
186 it("should handle /info command when authenticated with user config", async () => {
187 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } =
188 await import("./auth/workos.js");
189 const { services } = await import("./services/index.js");
190
191 const mockAuthConfig: AuthenticatedConfig = {
192 userId: "test-user-id",
193 userEmail: "test@example.com",
194 accessToken: "test-token",
195 refreshToken: "test-refresh-token",
196 expiresAt: Date.now() + 3600000,
197 organizationId: "test-org-id",
198 };
199
200 (
201 isAuthenticated as MockedFunction<typeof isAuthenticated>
202 ).mockReturnValue(true);
203 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(
204 mockAuthConfig,
205 );
206 (isAuthenticatedConfig as any).mockReturnValue(true);
207 (
208 services.config.getState as MockedFunction<
209 typeof services.config.getState
210 >
211 ).mockReturnValue({
212 config: null,
213 configPath: "/custom/config.yaml",
214 } as ConfigServiceState);
215
216 const result = await handleSlashCommands("/info", mockAssistant);
217
218 expect(result).toBeDefined();
219 expect(result?.output).toContain("Authentication:");
220 expect(result?.output).toContain("test@example.com");
221 expect(result?.output).toContain("test-org-id");
222 expect(result?.output).toContain("Configuration:");
223 expect(result?.output).toContain("/custom/config.yaml");
224 expect(result?.exit).toBe(false);
225 });
226
227 it("should handle config service errors gracefully", async () => {
228 const { isAuthenticated } = await import("./auth/workos.js");
229 const { services } = await import("./services/index.js");
230
231 (
232 isAuthenticated as MockedFunction<typeof isAuthenticated>
233 ).mockReturnValue(false);
234 (
235 services.config.getState as MockedFunction<
236 typeof services.config.getState
237 >
238 ).mockImplementation(() => {
239 throw new Error("Service not ready");
240 });
241
242 const result = await handleSlashCommands("/info", mockAssistant);
243
244 expect(result).toBeDefined();
245 expect(result?.output).toContain("Configuration service not available");
246 });
247
248 it("should use test session directory when in test mode", async () => {
249 const { isAuthenticated } = await import("./auth/workos.js");
250 const { services } = await import("./services/index.js");
251 const { getSessionFilePath, getCurrentSession } = await import(
252 "./session.js"
253 );
254
255 // Mock the session functions for this specific test
256 (getSessionFilePath as any).mockReturnValue(
257 "/test-home/.continue/cli-sessions/continue-cli-test-123.json",
258 );
259 (getCurrentSession as any).mockReturnValue({
260 sessionId: "test-123",
261 title: "Test Session",
262 });
263
264 (
265 isAuthenticated as MockedFunction<typeof isAuthenticated>
266 ).mockReturnValue(false);
267 (
268 services.config.getState as MockedFunction<
269 typeof services.config.getState
270 >
271 ).mockReturnValue({
272 config: null,
273 configPath: undefined,
274 } as ConfigServiceState);
275
276 const result = await handleSlashCommands("/info", mockAssistant);
277
278 expect(result?.output).toContain("Session:");
279 expect(result?.output).toContain("Test Session");
280 expect(result?.output).toContain("/test-home/.continue/cli-sessions/");
281 expect(result?.output).toContain(".json");
282 });
283
284 it("should handle /title command with valid title", async () => {
285 const { updateSessionTitle } = await import("./session.js");
286
287 const result = await handleSlashCommands(
288 "/title My New Session Title",
289 mockAssistant,
290 );
291
292 expect(updateSessionTitle).toHaveBeenCalledWith("My New Session Title");
293 expect(result).toBeDefined();
294 expect(result?.output).toContain(
295 'Session title updated to: "My New Session Title"',
296 );
297 expect(result?.exit).toBe(false);
298 });
299
300 it("should handle /title command with empty title", async () => {
301 const result = await handleSlashCommands("/title", mockAssistant);
302
303 expect(result).toBeDefined();
304 expect(result?.output).toContain(
305 "Please provide a title. Usage: /title <your title>",
306 );
307 expect(result?.exit).toBe(false);
308 });
309
310 it("should handle /title command with whitespace-only title", async () => {
311 const result = await handleSlashCommands("/title ", mockAssistant);
312
313 expect(result).toBeDefined();
314 expect(result?.output).toContain(
315 "Please provide a title. Usage: /title <your title>",
316 );
317 expect(result?.exit).toBe(false);
318 });
319
320 it("should handle /title command when session update fails", async () => {
321 const { updateSessionTitle } = await import("./session.js");
322
323 (updateSessionTitle as any).mockImplementation(() => {
324 throw new Error("Failed to save session");
325 });
326
327 const result = await handleSlashCommands(
328 "/title Test Title",
329 mockAssistant,
330 );
331
332 expect(result).toBeDefined();
333 expect(result?.output).toContain(
334 "Failed to update title: Failed to save session",
335 );
336 expect(result?.exit).toBe(false);
337 });
338 });
339});