Elffuss BraidHub

kikocis/continue public

main · 13.7 KiBRawPermalink
1import { beforeEach, describe, expect, it, vi } from "vitest";
2
3import { remote } from "./remote.js";
4
5// Mock dependencies
6vi.mock("../auth/workos.js");
7vi.mock("../env.js");
8vi.mock("../telemetry/telemetryService.js");
9vi.mock("../ui/index.js");
10vi.mock("../util/git.js");
11
12const mockWorkos = vi.mocked(await import("../auth/workos.js"));
13const mockEnv = vi.mocked(await import("../env.js"));
14const mockGit = vi.mocked(await import("../util/git.js"));
15const mockStartRemoteTUIChat = vi.mocked(await import("../ui/index.js"));
16
17// Mock fetch globally
18const mockFetch = vi.fn();
19global.fetch = mockFetch as any;
20
21// Mock console methods
22const mockConsoleInfo = vi.fn();
23const mockConsoleError = vi.fn();
24const mockConsoleLog = vi.fn();
25global.console = {
26 ...global.console,
27 info: mockConsoleInfo,
28 error: mockConsoleError,
29 log: mockConsoleLog,
30};
31
32describe("remote command", () => {
33 beforeEach(() => {
34 vi.clearAllMocks();
35
36 // Clear GitHub Actions environment variables
37 delete process.env.GITHUB_ACTIONS;
38 delete process.env.GITHUB_REPOSITORY;
39 delete process.env.GITHUB_SERVER_URL;
40
41 // Setup default mocks
42 mockWorkos.loadAuthConfig.mockReturnValue({
43 userId: "test-user-id",
44 userEmail: "test@example.com",
45 accessToken: "test-token",
46 refreshToken: "test-refresh-token",
47 expiresAt: Date.now() + 3600000,
48 organizationId: null,
49 });
50
51 mockWorkos.isAuthenticatedConfig.mockReturnValue(true);
52 mockWorkos.getAccessToken.mockReturnValue("test-token");
53
54 mockEnv.env = {
55 apiBase: "https://api.example.com",
56 workOsClientId: "test-client-id",
57 appUrl: "https://test.example.com",
58 continueHome: "/tmp/test-continue-home",
59 };
60
61 mockGit.getRepoUrl.mockReturnValue("https://github.com/user/test-repo.git");
62
63 mockFetch.mockResolvedValue({
64 ok: true,
65 json: async () => ({
66 id: "test-agent-id",
67 url: "ws://test-url.com",
68 port: 8080,
69 }),
70 });
71
72 mockStartRemoteTUIChat.startRemoteTUIChat.mockResolvedValue({} as any);
73 });
74
75 it("should include idempotency key in request body when provided", async () => {
76 const testIdempotencyKey = "test-idempotency-key-123";
77
78 await remote("test prompt", { idempotencyKey: testIdempotencyKey });
79
80 expect(mockFetch).toHaveBeenCalledWith(
81 new URL("agents", mockEnv.env.apiBase),
82 expect.objectContaining({
83 method: "POST",
84 headers: {
85 "Content-Type": "application/json",
86 Authorization: "Bearer test-token",
87 },
88 body: expect.stringContaining(
89 `"idempotencyKey":"${testIdempotencyKey}"`,
90 ),
91 }),
92 );
93 });
94
95 it("should not include idempotency key in request body when not provided", async () => {
96 await remote("test prompt", {});
97
98 expect(mockFetch).toHaveBeenCalledWith(
99 new URL("agents", mockEnv.env.apiBase),
100 expect.objectContaining({
101 method: "POST",
102 headers: {
103 "Content-Type": "application/json",
104 Authorization: "Bearer test-token",
105 },
106 body: expect.not.stringContaining("idempotencyKey"),
107 }),
108 );
109 });
110
111 it("should work with direct URL connection and idempotency key", async () => {
112 const testUrl = "ws://direct-url.com";
113 const testIdempotencyKey = "direct-key-789";
114
115 await remote("test prompt", {
116 url: testUrl,
117 idempotencyKey: testIdempotencyKey,
118 });
119
120 // Should not make POST request when connecting directly to URL
121 expect(mockFetch).not.toHaveBeenCalled();
122
123 // Should connect directly to the provided URL
124 expect(mockStartRemoteTUIChat.startRemoteTUIChat).toHaveBeenCalledWith(
125 testUrl,
126 "test prompt",
127 );
128 });
129
130 it("should ignore --start flag when --url is provided without --start", async () => {
131 const testUrl = "ws://direct-url.com";
132
133 await remote("test prompt", { url: testUrl });
134
135 // Should not make POST request when connecting directly to URL
136 expect(mockFetch).not.toHaveBeenCalled();
137
138 // Should connect directly to the provided URL and start TUI
139 expect(mockStartRemoteTUIChat.startRemoteTUIChat).toHaveBeenCalledWith(
140 testUrl,
141 "test prompt",
142 );
143
144 // Should not output JSON
145 expect(mockConsoleLog).not.toHaveBeenCalled();
146 });
147
148 it("should fetch tunnel and connect when id option is provided", async () => {
149 const agentId = "agent-789";
150
151 mockFetch
152 .mockResolvedValueOnce({
153 ok: true,
154 json: async () => ({ url: "ws://tunnel-url.com", port: 9090 }),
155 })
156 .mockResolvedValue({
157 ok: true,
158 json: async () => ({
159 id: "test-agent-id",
160 url: "ws://test-url.com",
161 port: 8080,
162 }),
163 });
164
165 await remote("test prompt", { id: agentId });
166
167 expect(mockFetch).toHaveBeenCalledWith(
168 new URL(`agents/${agentId}/tunnel`, mockEnv.env.apiBase),
169 expect.objectContaining({
170 method: "POST",
171 headers: {
172 "Content-Type": "application/json",
173 Authorization: "Bearer test-token",
174 },
175 }),
176 );
177
178 expect(mockStartRemoteTUIChat.startRemoteTUIChat).toHaveBeenCalledWith(
179 "ws://tunnel-url.com",
180 "test prompt",
181 );
182 });
183
184 it("should output JSON without starting TUI when using --id with --start", async () => {
185 const agentId = "agent-456";
186 const tunnelResponse = { url: "ws://existing-tunnel.com", port: 7070 };
187
188 mockFetch.mockResolvedValueOnce({
189 ok: true,
190 json: async () => tunnelResponse,
191 });
192
193 await remote("test prompt", { id: agentId, start: true });
194
195 expect(mockFetch).toHaveBeenCalledWith(
196 new URL(`agents/${agentId}/tunnel`, mockEnv.env.apiBase),
197 expect.objectContaining({
198 method: "POST",
199 headers: {
200 "Content-Type": "application/json",
201 Authorization: "Bearer test-token",
202 },
203 }),
204 );
205
206 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
207 expect(mockConsoleLog).toHaveBeenCalledWith(
208 JSON.stringify({
209 status: "success",
210 message: "Remote agent tunnel connection details",
211 url: tunnelResponse.url,
212 containerPort: tunnelResponse.port,
213 agentId,
214 mode: "existing_agent",
215 }),
216 );
217 });
218
219 it("should handle proper request body structure with all fields", async () => {
220 const testIdempotencyKey = "structured-test-key";
221
222 await remote("test prompt", { idempotencyKey: testIdempotencyKey });
223
224 const fetchCall = mockFetch.mock.calls[0];
225 const requestBody = JSON.parse(fetchCall[1].body);
226
227 expect(requestBody).toEqual({
228 repoUrl: "https://github.com/user/test-repo.git",
229 name: expect.stringMatching(/^devbox-\d+$/),
230 prompt: "test prompt",
231 idempotencyKey: testIdempotencyKey,
232 });
233 });
234
235 it("should call getRepoUrl to get the current repository URL", async () => {
236 await remote("test prompt", {});
237
238 expect(mockGit.getRepoUrl).toHaveBeenCalled();
239 });
240
241 it("should work correctly in GitHub Actions environment", async () => {
242 // Simulate GitHub Actions environment
243 process.env.GITHUB_ACTIONS = "true";
244 process.env.GITHUB_REPOSITORY = "myorg/myrepo";
245
246 // Mock getRepoUrl to return the GitHub Actions URL
247 mockGit.getRepoUrl.mockReturnValue("https://github.com/myorg/myrepo.git");
248
249 await remote("test prompt", { idempotencyKey: "github-actions-key" });
250
251 const fetchCall = mockFetch.mock.calls[0];
252 const requestBody = JSON.parse(fetchCall[1].body);
253
254 expect(requestBody.repoUrl).toBe("https://github.com/myorg/myrepo.git");
255 expect(requestBody.idempotencyKey).toBe("github-actions-key");
256 });
257
258 it("should include branchName in request body when branch option is provided", async () => {
259 const testBranch = "feature/new-feature";
260
261 await remote("test prompt", { branch: testBranch });
262
263 expect(mockFetch).toHaveBeenCalledWith(
264 new URL("agents", mockEnv.env.apiBase),
265 expect.objectContaining({
266 method: "POST",
267 headers: {
268 "Content-Type": "application/json",
269 Authorization: "Bearer test-token",
270 },
271 body: expect.stringContaining(`"branchName":"${testBranch}"`),
272 }),
273 );
274 });
275
276 it("should not include branchName in request body when branch option is not provided", async () => {
277 await remote("test prompt", {});
278
279 expect(mockFetch).toHaveBeenCalledWith(
280 new URL("agents", mockEnv.env.apiBase),
281 expect.objectContaining({
282 method: "POST",
283 headers: {
284 "Content-Type": "application/json",
285 Authorization: "Bearer test-token",
286 },
287 body: expect.not.stringContaining("branchName"),
288 }),
289 );
290 });
291
292 it("should handle proper request body structure with branch field", async () => {
293 const testBranch = "main";
294 const testIdempotencyKey = "test-with-branch";
295
296 await remote("test prompt", {
297 branch: testBranch,
298 idempotencyKey: testIdempotencyKey,
299 });
300
301 const fetchCall = mockFetch.mock.calls[0];
302 const requestBody = JSON.parse(fetchCall[1].body);
303
304 expect(requestBody).toEqual({
305 repoUrl: "https://github.com/user/test-repo.git",
306 name: expect.stringMatching(/^devbox-\d+$/),
307 prompt: "test prompt",
308 idempotencyKey: testIdempotencyKey,
309 branchName: testBranch,
310 agent: undefined,
311 config: undefined,
312 });
313 });
314
315 it("should include config in request body when config option is provided", async () => {
316 const testConfig = "test-config-path";
317
318 await remote("test prompt", { config: testConfig });
319
320 expect(mockFetch).toHaveBeenCalledWith(
321 new URL("agents", mockEnv.env.apiBase),
322 expect.objectContaining({
323 method: "POST",
324 headers: {
325 "Content-Type": "application/json",
326 Authorization: "Bearer test-token",
327 },
328 body: expect.stringContaining(`"config":"${testConfig}"`),
329 }),
330 );
331
332 expect(mockFetch).toHaveBeenCalledWith(
333 new URL("agents", mockEnv.env.apiBase),
334 expect.objectContaining({
335 body: expect.stringContaining(`"agent":"${testConfig}"`),
336 }),
337 );
338 });
339
340 it("should handle proper request body structure with config field", async () => {
341 const testConfig = "my-agent-config";
342 const testIdempotencyKey = "test-with-config";
343
344 await remote("test prompt", {
345 config: testConfig,
346 idempotencyKey: testIdempotencyKey,
347 });
348
349 const fetchCall = mockFetch.mock.calls[0];
350 const requestBody = JSON.parse(fetchCall[1].body);
351
352 expect(requestBody).toEqual({
353 repoUrl: "https://github.com/user/test-repo.git",
354 name: expect.stringMatching(/^devbox-\d+$/),
355 prompt: "test prompt",
356 idempotencyKey: testIdempotencyKey,
357 agent: testConfig,
358 config: testConfig,
359 });
360 });
361
362 it("should not include config in request body when config option is not provided", async () => {
363 await remote("test prompt", {});
364
365 const fetchCall = mockFetch.mock.calls[0];
366 const requestBody = JSON.parse(fetchCall[1].body);
367
368 expect(requestBody).toEqual({
369 repoUrl: "https://github.com/user/test-repo.git",
370 name: expect.stringMatching(/^devbox-\d+$/),
371 prompt: "test prompt",
372 agent: undefined,
373 config: undefined,
374 });
375 expect(requestBody).not.toHaveProperty("idempotencyKey");
376 });
377
378 describe("start mode (-s / --start flag)", () => {
379 it("should output JSON and exit without starting TUI when using --start with direct URL", async () => {
380 const testUrl = "ws://test-url.com";
381
382 await remote("test prompt", { url: testUrl, start: true });
383
384 // Should not start TUI
385 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
386
387 // Should not make POST request
388 expect(mockFetch).not.toHaveBeenCalled();
389
390 // Should output JSON
391 expect(mockConsoleLog).toHaveBeenCalledWith(
392 JSON.stringify({
393 status: "success",
394 message: "Remote environment connection details",
395 url: testUrl,
396 mode: "direct_url",
397 }),
398 );
399 });
400
401 it("should create remote environment, output JSON, and exit without starting TUI when using --start", async () => {
402 await remote("test prompt", { start: true });
403
404 // Should make POST request to create environment
405 expect(mockFetch).toHaveBeenCalledWith(
406 new URL("agents", mockEnv.env.apiBase),
407 expect.objectContaining({
408 method: "POST",
409 headers: {
410 "Content-Type": "application/json",
411 Authorization: "Bearer test-token",
412 },
413 }),
414 );
415
416 // Should not start TUI
417 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
418
419 // Should output JSON with environment details
420 const consoleLogCall = mockConsoleLog.mock.calls[0][0];
421 const outputJson = JSON.parse(consoleLogCall);
422
423 expect(outputJson).toEqual({
424 status: "success",
425 message: "Remote development environment created successfully",
426 url: "https://test.example.com/agents/test-agent-id",
427 containerUrl: "ws://test-url.com",
428 containerPort: 8080,
429 name: expect.stringMatching(/^devbox-\d+$/),
430 mode: "new_environment",
431 });
432 });
433
434 it("should work with start mode and idempotency key", async () => {
435 const testIdempotencyKey = "start-mode-key";
436
437 await remote("test prompt", {
438 start: true,
439 idempotencyKey: testIdempotencyKey,
440 });
441
442 // Should include idempotency key in request
443 const fetchCall = mockFetch.mock.calls[0];
444 const requestBody = JSON.parse(fetchCall[1].body);
445 expect(requestBody.idempotencyKey).toBe(testIdempotencyKey);
446
447 // Should not start TUI
448 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
449
450 // Should output JSON
451 expect(mockConsoleLog).toHaveBeenCalledWith(
452 expect.stringContaining('"status":"success"'),
453 );
454 });
455 });
456});