Elffuss BraidHub

kikocis/continue public

0e09f43481cb · 7.0 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 os and path
48vi.mock("os", () => ({
49 default: {
50 homedir: vi.fn(() => "/home/test"),
51 },
52}));
53
54vi.mock("path", () => ({
55 default: {
56 join: vi.fn((...parts) => parts.join("/")),
57 },
58 join: vi.fn((...parts) => parts.join("/")),
59}));
60
61// Mock session functions
62vi.mock("./session.js", () => ({
63 getSessionFilePath: vi.fn(
64 () => "/home/test/.continue/cli-sessions/continue-cli-pid-12345.json",
65 ),
66 hasSession: vi.fn(() => false),
67}));
68
69describe("slashCommands", () => {
70 const mockAssistant: AssistantUnrolled = {
71 name: "test-assistant",
72 version: "1.0.0",
73 };
74
75 beforeEach(() => {
76 vi.clearAllMocks();
77 });
78
79 describe("handleSlashCommands", () => {
80 it("should handle /info command when not authenticated", async () => {
81 const { isAuthenticated } = await import("./auth/workos.js");
82 const { services } = await import("./services/index.js");
83
84 (
85 isAuthenticated as MockedFunction<typeof isAuthenticated>
86 ).mockReturnValue(false);
87 (
88 services.config.getState as MockedFunction<
89 typeof services.config.getState
90 >
91 ).mockReturnValue({
92 config: null,
93 configPath: "/test/config.yaml",
94 } as ConfigServiceState);
95
96 const result = await handleSlashCommands("/info", mockAssistant);
97
98 expect(result).toBeDefined();
99 expect(result?.output).toContain("Authentication:");
100 expect(result?.output).toContain("Not logged in");
101 expect(result?.output).toContain("Configuration:");
102 expect(result?.output).toContain("/test/config.yaml");
103 expect(result?.output).toContain("Session History:");
104 expect(result?.output).toContain(".json");
105 expect(result?.exit).toBe(false);
106 });
107
108 it("should handle /info command when authenticated via environment", async () => {
109 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } =
110 await import("./auth/workos.js");
111 const { services } = await import("./services/index.js");
112
113 (
114 isAuthenticated as MockedFunction<typeof isAuthenticated>
115 ).mockReturnValue(true);
116 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(
117 {} as AuthConfig,
118 );
119 (isAuthenticatedConfig as any).mockReturnValue(false);
120 (
121 services.config.getState as MockedFunction<
122 typeof services.config.getState
123 >
124 ).mockReturnValue({
125 config: null,
126 configPath: undefined,
127 } as ConfigServiceState);
128
129 const result = await handleSlashCommands("/info", mockAssistant);
130
131 expect(result).toBeDefined();
132 expect(result?.output).toContain("Authentication:");
133 expect(result?.output).toContain(
134 "Authenticated via environment variable",
135 );
136 expect(result?.output).toContain("Configuration:");
137 expect(result?.output).toContain("Config not found");
138 expect(result?.exit).toBe(false);
139 });
140
141 it("should handle /info command when authenticated with user config", async () => {
142 const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } =
143 await import("./auth/workos.js");
144 const { services } = await import("./services/index.js");
145
146 const mockAuthConfig: AuthenticatedConfig = {
147 userId: "test-user-id",
148 userEmail: "test@example.com",
149 accessToken: "test-token",
150 refreshToken: "test-refresh-token",
151 expiresAt: Date.now() + 3600000,
152 organizationId: "test-org-id",
153 };
154
155 (
156 isAuthenticated as MockedFunction<typeof isAuthenticated>
157 ).mockReturnValue(true);
158 (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(
159 mockAuthConfig,
160 );
161 (isAuthenticatedConfig as any).mockReturnValue(true);
162 (
163 services.config.getState as MockedFunction<
164 typeof services.config.getState
165 >
166 ).mockReturnValue({
167 config: null,
168 configPath: "/custom/config.yaml",
169 } as ConfigServiceState);
170
171 const result = await handleSlashCommands("/info", mockAssistant);
172
173 expect(result).toBeDefined();
174 expect(result?.output).toContain("Authentication:");
175 expect(result?.output).toContain("test@example.com");
176 expect(result?.output).toContain("(no org)");
177 expect(result?.output).toContain("Configuration:");
178 expect(result?.output).toContain("/custom/config.yaml");
179 expect(result?.exit).toBe(false);
180 });
181
182 it("should handle config service errors gracefully", async () => {
183 const { isAuthenticated } = await import("./auth/workos.js");
184 const { services } = await import("./services/index.js");
185
186 (
187 isAuthenticated as MockedFunction<typeof isAuthenticated>
188 ).mockReturnValue(false);
189 (
190 services.config.getState as MockedFunction<
191 typeof services.config.getState
192 >
193 ).mockImplementation(() => {
194 throw new Error("Service not ready");
195 });
196
197 const result = await handleSlashCommands("/info", mockAssistant);
198
199 expect(result).toBeDefined();
200 expect(result?.output).toContain("Configuration service not available");
201 });
202
203 it("should use test session directory when in test mode", async () => {
204 const { isAuthenticated } = await import("./auth/workos.js");
205 const { services } = await import("./services/index.js");
206 const { getSessionFilePath } = await import("./session.js");
207
208 // Mock the session path for this specific test
209 (getSessionFilePath as any).mockReturnValue(
210 "/test-home/.continue/cli-sessions/continue-cli-test-123.json",
211 );
212
213 (
214 isAuthenticated as MockedFunction<typeof isAuthenticated>
215 ).mockReturnValue(false);
216 (
217 services.config.getState as MockedFunction<
218 typeof services.config.getState
219 >
220 ).mockReturnValue({
221 config: null,
222 configPath: undefined,
223 } as ConfigServiceState);
224
225 const result = await handleSlashCommands("/info", mockAssistant);
226
227 expect(result?.output).toContain("/test-home/.continue/cli-sessions/");
228 expect(result?.output).toContain(".json");
229 });
230 });
231});