|
| 1 | +import { mkdtemp, readFile, rm } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import nodePath from "node:path"; |
| 4 | +import type { IAgentRuntime, UUID } from "@elizaos/core"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + createHandler, |
| 8 | + ensureWorkspace, |
| 9 | + loadConfig, |
| 10 | +} from "../../../cloud-services/coding-remote-runner/src/index.ts"; |
| 11 | +import { E2BRemoteCapabilityRouterService } from "./e2b-capability-router.ts"; |
| 12 | + |
| 13 | +const REMOTE_RUNNER_URL = "https://coding-remote-runner.test"; |
| 14 | +const REMOTE_RUNNER_TOKEN = "sat-token"; |
| 15 | + |
| 16 | +let workspaceRoot = ""; |
| 17 | +let originalFetch: typeof fetch; |
| 18 | + |
| 19 | +function replaceGlobalFetch(fetchImpl: typeof fetch): void { |
| 20 | + Object.defineProperty(globalThis, "fetch", { |
| 21 | + configurable: true, |
| 22 | + writable: true, |
| 23 | + value: fetchImpl, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(async () => { |
| 28 | + workspaceRoot = await mkdtemp( |
| 29 | + nodePath.join(tmpdir(), "agent-remote-runner-"), |
| 30 | + ); |
| 31 | + originalFetch = globalThis.fetch; |
| 32 | +}); |
| 33 | + |
| 34 | +afterEach(async () => { |
| 35 | + replaceGlobalFetch(originalFetch); |
| 36 | + await rm(workspaceRoot, { recursive: true, force: true }); |
| 37 | +}); |
| 38 | + |
| 39 | +function makeRuntime(): IAgentRuntime { |
| 40 | + const runtime: Partial<IAgentRuntime> = { |
| 41 | + agentId: "11111111-1111-1111-1111-111111111111" as UUID, |
| 42 | + character: { name: "Remote runner Proof" }, |
| 43 | + getSetting: () => null, |
| 44 | + getService: () => null, |
| 45 | + }; |
| 46 | + return runtime as IAgentRuntime; |
| 47 | +} |
| 48 | + |
| 49 | +async function installCodingRemoteRunnerFetch(): Promise<void> { |
| 50 | + const config = loadConfig({ |
| 51 | + ELIZA_CODING_WORKSPACE: workspaceRoot, |
| 52 | + ELIZA_REMOTE_RUNNER_HTTP_TOKEN: REMOTE_RUNNER_TOKEN, |
| 53 | + }); |
| 54 | + await ensureWorkspace(config); |
| 55 | + const handler = createHandler(config); |
| 56 | + const fetchMock: typeof fetch = Object.assign( |
| 57 | + async ( |
| 58 | + input: Parameters<typeof fetch>[0], |
| 59 | + init?: Parameters<typeof fetch>[1], |
| 60 | + ): Promise<Response> => { |
| 61 | + const request = new Request(input, init); |
| 62 | + const url = new URL(request.url); |
| 63 | + if (url.origin !== REMOTE_RUNNER_URL) { |
| 64 | + return originalFetch(input, init); |
| 65 | + } |
| 66 | + return handler(request); |
| 67 | + }, |
| 68 | + { preconnect: originalFetch.preconnect }, |
| 69 | + ); |
| 70 | + replaceGlobalFetch(fetchMock); |
| 71 | +} |
| 72 | + |
| 73 | +describe("E2B remote runner router with the Coding remote runner HTTP runner", () => { |
| 74 | + it("runs coding commands through the remote runner workspace instead of the caller host", async () => { |
| 75 | + await installCodingRemoteRunnerFetch(); |
| 76 | + const service = new E2BRemoteCapabilityRouterService(makeRuntime(), { |
| 77 | + enabled: true, |
| 78 | + provider: "home", |
| 79 | + remoteHttpBaseUrl: REMOTE_RUNNER_URL, |
| 80 | + remoteHttpToken: REMOTE_RUNNER_TOKEN, |
| 81 | + agentRunners: ["codex", "claude-code", "opencode"], |
| 82 | + workdir: "/workspace", |
| 83 | + hostWorkspaceRoot: workspaceRoot, |
| 84 | + timeoutMs: 30_000, |
| 85 | + requestTimeoutMs: 10_000, |
| 86 | + keepAlive: true, |
| 87 | + allowInternetAccess: false, |
| 88 | + envs: {}, |
| 89 | + metadata: {}, |
| 90 | + }); |
| 91 | + |
| 92 | + const result = await service.pty.runCommand({ |
| 93 | + command: "sh", |
| 94 | + args: ["-lc", "printf remote-coded > mobile-proof.txt"], |
| 95 | + cwd: "/workspace", |
| 96 | + timeoutMs: 10_000, |
| 97 | + }); |
| 98 | + const read = await service.fs.readText({ path: "mobile-proof.txt" }); |
| 99 | + |
| 100 | + expect(result.exitCode).toBe(0); |
| 101 | + expect(result.timedOut).toBe(false); |
| 102 | + expect(read).toMatchObject({ |
| 103 | + path: "/workspace/mobile-proof.txt", |
| 104 | + text: "remote-coded", |
| 105 | + truncated: false, |
| 106 | + }); |
| 107 | + await expect( |
| 108 | + readFile(nodePath.join(workspaceRoot, "mobile-proof.txt"), "utf8"), |
| 109 | + ).resolves.toBe("remote-coded"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments