Elffuss BraidHub

kikocis/continue public

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