| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import { remote } from "./remote.js"; |
| 4 | |
| 5 | // Mock dependencies |
| 6 | vi.mock("../auth/workos.js"); |
| 7 | vi.mock("../env.js"); |
| 8 | vi.mock("../telemetry/telemetryService.js"); |
| 9 | vi.mock("../ui/index.js"); |
| 10 | vi.mock("../util/git.js"); |
| 11 | |
| 12 | const mockWorkos = vi.mocked(await import("../auth/workos.js")); |
| 13 | const mockEnv = vi.mocked(await import("../env.js")); |
| 14 | const mockGit = vi.mocked(await import("../util/git.js")); |
| 15 | const mockStartRemoteTUIChat = vi.mocked(await import("../ui/index.js")); |
| 16 | |
| 17 | // Mock fetch globally |
| 18 | const mockFetch = vi.fn(); |
| 19 | global.fetch = mockFetch as any; |
| 20 | |
| 21 | // Mock console methods |
| 22 | const mockConsoleInfo = vi.fn(); |
| 23 | const mockConsoleError = vi.fn(); |
| 24 | const mockConsoleLog = vi.fn(); |
| 25 | global.console = { |
| 26 | ...global.console, |
| 27 | info: mockConsoleInfo, |
| 28 | error: mockConsoleError, |
| 29 | log: mockConsoleLog, |
| 30 | }; |
| 31 | |
| 32 | describe("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 | agent: undefined, |
| 240 | config: undefined, |
| 241 | }); |
| 242 | }); |
| 243 | |
| 244 | it("should include config in request body when config option is provided", async () => { |
| 245 | const testConfig = "test-config-path"; |
| 246 | |
| 247 | await remote("test prompt", { config: testConfig }); |
| 248 | |
| 249 | expect(mockFetch).toHaveBeenCalledWith( |
| 250 | new URL("agents/devboxes", mockEnv.env.apiBase), |
| 251 | expect.objectContaining({ |
| 252 | method: "POST", |
| 253 | headers: { |
| 254 | "Content-Type": "application/json", |
| 255 | Authorization: "Bearer test-token", |
| 256 | }, |
| 257 | body: expect.stringContaining(`"config":"${testConfig}"`), |
| 258 | }), |
| 259 | ); |
| 260 | |
| 261 | expect(mockFetch).toHaveBeenCalledWith( |
| 262 | new URL("agents/devboxes", mockEnv.env.apiBase), |
| 263 | expect.objectContaining({ |
| 264 | body: expect.stringContaining(`"agent":"${testConfig}"`), |
| 265 | }), |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | it("should handle proper request body structure with config field", async () => { |
| 270 | const testConfig = "my-agent-config"; |
| 271 | const testIdempotencyKey = "test-with-config"; |
| 272 | |
| 273 | await remote("test prompt", { |
| 274 | config: testConfig, |
| 275 | idempotencyKey: testIdempotencyKey, |
| 276 | }); |
| 277 | |
| 278 | const fetchCall = mockFetch.mock.calls[0]; |
| 279 | const requestBody = JSON.parse(fetchCall[1].body); |
| 280 | |
| 281 | expect(requestBody).toEqual({ |
| 282 | repoUrl: "https://github.com/user/test-repo.git", |
| 283 | name: expect.stringMatching(/^devbox-\d+$/), |
| 284 | prompt: "test prompt", |
| 285 | idempotencyKey: testIdempotencyKey, |
| 286 | agent: testConfig, |
| 287 | config: testConfig, |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | it("should not include config in request body when config option is not provided", async () => { |
| 292 | await remote("test prompt", {}); |
| 293 | |
| 294 | const fetchCall = mockFetch.mock.calls[0]; |
| 295 | const requestBody = JSON.parse(fetchCall[1].body); |
| 296 | |
| 297 | expect(requestBody).toEqual({ |
| 298 | repoUrl: "https://github.com/user/test-repo.git", |
| 299 | name: expect.stringMatching(/^devbox-\d+$/), |
| 300 | prompt: "test prompt", |
| 301 | idempotencyKey: undefined, |
| 302 | agent: undefined, |
| 303 | config: undefined, |
| 304 | }); |
| 305 | }); |
| 306 | |
| 307 | describe("start mode (-s / --start flag)", () => { |
| 308 | it("should output JSON and exit without starting TUI when using --start with direct URL", async () => { |
| 309 | const testUrl = "ws://test-url.com"; |
| 310 | |
| 311 | await remote("test prompt", { url: testUrl, start: true }); |
| 312 | |
| 313 | // Should not start TUI |
| 314 | expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled(); |
| 315 | |
| 316 | // Should not make POST request |
| 317 | expect(mockFetch).not.toHaveBeenCalled(); |
| 318 | |
| 319 | // Should output JSON |
| 320 | expect(mockConsoleLog).toHaveBeenCalledWith( |
| 321 | JSON.stringify({ |
| 322 | status: "success", |
| 323 | message: "Remote environment connection details", |
| 324 | url: testUrl, |
| 325 | mode: "direct_url", |
| 326 | }), |
| 327 | ); |
| 328 | }); |
| 329 | |
| 330 | it("should create remote environment, output JSON, and exit without starting TUI when using --start", async () => { |
| 331 | await remote("test prompt", { start: true }); |
| 332 | |
| 333 | // Should make POST request to create environment |
| 334 | expect(mockFetch).toHaveBeenCalledWith( |
| 335 | new URL("agents/devboxes", mockEnv.env.apiBase), |
| 336 | expect.objectContaining({ |
| 337 | method: "POST", |
| 338 | headers: { |
| 339 | "Content-Type": "application/json", |
| 340 | Authorization: "Bearer test-token", |
| 341 | }, |
| 342 | }), |
| 343 | ); |
| 344 | |
| 345 | // Should not start TUI |
| 346 | expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled(); |
| 347 | |
| 348 | // Should output JSON with environment details |
| 349 | const consoleLogCall = mockConsoleLog.mock.calls[0][0]; |
| 350 | const outputJson = JSON.parse(consoleLogCall); |
| 351 | |
| 352 | expect(outputJson).toEqual({ |
| 353 | status: "success", |
| 354 | message: "Remote development environment created successfully", |
| 355 | url: "https://test.example.com/agents/test-agent-id", |
| 356 | containerUrl: "ws://test-url.com", |
| 357 | containerPort: 8080, |
| 358 | name: expect.stringMatching(/^devbox-\d+$/), |
| 359 | mode: "new_environment", |
| 360 | }); |
| 361 | }); |
| 362 | |
| 363 | it("should work with start mode and idempotency key", async () => { |
| 364 | const testIdempotencyKey = "start-mode-key"; |
| 365 | |
| 366 | await remote("test prompt", { |
| 367 | start: true, |
| 368 | idempotencyKey: testIdempotencyKey, |
| 369 | }); |
| 370 | |
| 371 | // Should include idempotency key in request |
| 372 | const fetchCall = mockFetch.mock.calls[0]; |
| 373 | const requestBody = JSON.parse(fetchCall[1].body); |
| 374 | expect(requestBody.idempotencyKey).toBe(testIdempotencyKey); |
| 375 | |
| 376 | // Should not start TUI |
| 377 | expect(mockStartRemoteTUIChat.startRemoteTUIChat).not.toHaveBeenCalled(); |
| 378 | |
| 379 | // Should output JSON |
| 380 | expect(mockConsoleLog).toHaveBeenCalledWith( |
| 381 | expect.stringContaining('"status":"success"'), |
| 382 | ); |
| 383 | }); |
| 384 | }); |
| 385 | }); |