Elffuss BraidHub

kikocis/continue public

1ff75182e19e · 9.9 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/devboxes", 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/devboxes", 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 handle proper request body structure with all fields", async () => {
149 const testIdempotencyKey = "structured-test-key";
150
151 await remote("test prompt", { idempotencyKey: testIdempotencyKey });
152
153 const fetchCall = mockFetch.mock.calls[0];
154 const requestBody = JSON.parse(fetchCall[1].body);
155
156 expect(requestBody).toEqual({
157 repoUrl: "https://github.com/user/test-repo.git",
158 name: expect.stringMatching(/^devbox-\d+$/),
159 prompt: "test prompt",
160 idempotencyKey: testIdempotencyKey,
161 });
162 });
163
164 it("should call getRepoUrl to get the current repository URL", async () => {
165 await remote("test prompt", {});
166
167 expect(mockGit.getRepoUrl).toHaveBeenCalled();
168 });
169
170 it("should work correctly in GitHub Actions environment", async () => {
171 // Simulate GitHub Actions environment
172 process.env.GITHUB_ACTIONS = "true";
173 process.env.GITHUB_REPOSITORY = "myorg/myrepo";
174
175 // Mock getRepoUrl to return the GitHub Actions URL
176 mockGit.getRepoUrl.mockReturnValue("https://github.com/myorg/myrepo.git");
177
178 await remote("test prompt", { idempotencyKey: "github-actions-key" });
179
180 const fetchCall = mockFetch.mock.calls[0];
181 const requestBody = JSON.parse(fetchCall[1].body);
182
183 expect(requestBody.repoUrl).toBe("https://github.com/myorg/myrepo.git");
184 expect(requestBody.idempotencyKey).toBe("github-actions-key");
185 });
186
187 it("should include branchName in request body when branch option is provided", async () => {
188 const testBranch = "feature/new-feature";
189
190 await remote("test prompt", { branch: testBranch });
191
192 expect(mockFetch).toHaveBeenCalledWith(
193 new URL("agents/devboxes", mockEnv.env.apiBase),
194 expect.objectContaining({
195 method: "POST",
196 headers: {
197 "Content-Type": "application/json",
198 Authorization: "Bearer test-token",
199 },
200 body: expect.stringContaining(`"branchName":"${testBranch}"`),
201 }),
202 );
203 });
204
205 it("should not include branchName in request body when branch option is not provided", async () => {
206 await remote("test prompt", {});
207
208 expect(mockFetch).toHaveBeenCalledWith(
209 new URL("agents/devboxes", mockEnv.env.apiBase),
210 expect.objectContaining({
211 method: "POST",
212 headers: {
213 "Content-Type": "application/json",
214 Authorization: "Bearer test-token",
215 },
216 body: expect.not.stringContaining("branchName"),
217 }),
218 );
219 });
220
221 it("should handle proper request body structure with branch field", async () => {
222 const testBranch = "main";
223 const testIdempotencyKey = "test-with-branch";
224
225 await remote("test prompt", {
226 branch: testBranch,
227 idempotencyKey: testIdempotencyKey,
228 });
229
230 const fetchCall = mockFetch.mock.calls[0];
231 const requestBody = JSON.parse(fetchCall[1].body);
232
233 expect(requestBody).toEqual({
234 repoUrl: "https://github.com/user/test-repo.git",
235 name: expect.stringMatching(/^devbox-\d+$/),
236 prompt: "test prompt",
237 idempotencyKey: testIdempotencyKey,
238 branchName: testBranch,
239 });
240 });
241
242 describe("start mode (-s / --start flag)", () => {
243 it("should output JSON and exit without starting TUI when using --start with direct URL", async () => {
244 const testUrl = "ws://test-url.com";
245
246 await remote("test prompt", { url: testUrl, start: true });
247
248 // Should not start TUI
249 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
250
251 // Should not make POST request
252 expect(mockFetch).not.toHaveBeenCalled();
253
254 // Should output JSON
255 expect(mockConsoleLog).toHaveBeenCalledWith(
256 JSON.stringify({
257 status: "success",
258 message: "Remote environment connection details",
259 url: testUrl,
260 mode: "direct_url",
261 }),
262 );
263 });
264
265 it("should create remote environment, output JSON, and exit without starting TUI when using --start", async () => {
266 await remote("test prompt", { start: true });
267
268 // Should make POST request to create environment
269 expect(mockFetch).toHaveBeenCalledWith(
270 new URL("agents/devboxes", mockEnv.env.apiBase),
271 expect.objectContaining({
272 method: "POST",
273 headers: {
274 "Content-Type": "application/json",
275 Authorization: "Bearer test-token",
276 },
277 }),
278 );
279
280 // Should not start TUI
281 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
282
283 // Should output JSON with environment details
284 const consoleLogCall = mockConsoleLog.mock.calls[0][0];
285 const outputJson = JSON.parse(consoleLogCall);
286
287 expect(outputJson).toEqual({
288 status: "success",
289 message: "Remote development environment created successfully",
290 url: "https://test.example.com/agents/test-agent-id",
291 containerUrl: "ws://test-url.com",
292 containerPort: 8080,
293 name: expect.stringMatching(/^devbox-\d+$/),
294 mode: "new_environment",
295 });
296 });
297
298 it("should work with start mode and idempotency key", async () => {
299 const testIdempotencyKey = "start-mode-key";
300
301 await remote("test prompt", {
302 start: true,
303 idempotencyKey: testIdempotencyKey,
304 });
305
306 // Should include idempotency key in request
307 const fetchCall = mockFetch.mock.calls[0];
308 const requestBody = JSON.parse(fetchCall[1].body);
309 expect(requestBody.idempotencyKey).toBe(testIdempotencyKey);
310
311 // Should not start TUI
312 expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled();
313
314 // Should output JSON
315 expect(mockConsoleLog).toHaveBeenCalledWith(
316 expect.stringContaining('"status":"success"'),
317 );
318 });
319 });
320});