Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
test: pick distinct free ports for the companion and webhook pairs (#7583)
Milind Sonif78b3fefb2b0
diff --git a/companion/test/tailscale-opt-in.test.ts b/companion/test/tailscale-opt-in.test.tsindex 0230762da3..9b41445f99 100644--- a/companion/test/tailscale-opt-in.test.ts+++ b/companion/test/tailscale-opt-in.test.ts@@ -1,6 +1,6 @@import { spawn } from "node:child_process";import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";-import { createServer } from "node:net";+import { createServer, type AddressInfo, type Server } from "node:net";import { tmpdir } from "node:os";import { dirname, join } from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";@@ -15,21 +15,30 @@ afterEach(() => {for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });});-const freePort = (): Promise<number> =>- new Promise((resolve) => {- const server = createServer();- server.listen(0, "127.0.0.1", () => {- const { port } = server.address() as { port: number };- server.close(() => resolve(port));- });- });+/** Distinct free ports: every listener stays open until all are bound, so the OS never hands the same one back twice. */+async function freePorts(count: number): Promise<number[]> {+ const servers: Server[] = [];+ try {+ for (let i = 0; i < count; i++) {+ const server = createServer();+ servers.push(server);+ await new Promise<void>((resolve, reject) => {+ server.once("error", reject);+ server.listen(0, "127.0.0.1", () => resolve());+ });+ }+ return servers.map((server) => (server.address() as AddressInfo).port);+ } finally {+ await Promise.all(servers.map((server) => new Promise<void>((resolve) => server.close(() => resolve()))));+ }+}/** Starts a real sidecar until it says it is up, and returns every program it tried to run meanwhile. */async function startSidecar(env: Record<string, string>): Promise<{ programs: string[][]; out: string }> {const home = mkdtempSync(join(tmpdir(), "omb-companion-tailscale-"));dirs.push(home);const record = join(home, "exec.jsonl");- const [companionPort, controlPort] = [await freePort(), await freePort()];+ const [companionPort, controlPort] = await freePorts(2);const child = spawn(process.execPath, ["--import", RECORDER, ENTRY], {env: {PATH: process.env.PATH ?? "",diff --git a/scripts/smoke-login-handoff.cjs b/scripts/smoke-login-handoff.cjsindex 01c1da2780..2b1c37964b 100644--- a/scripts/smoke-login-handoff.cjs+++ b/scripts/smoke-login-handoff.cjs@@ -43,11 +43,14 @@ async function until(check) {app.whenReady().then(async () => {const { createLoginHandoffClient } = await import(pathToFileURL(join(root, "electron/cloud-logins.mjs")).href);const port = await freePort();+ // Picked after the first listener closed, so the OS can hand the same port back.+ let webhookPort = await freePort();+ while (webhookPort === port) webhookPort = await freePort();writeFileSync(join(home, "config.json"), JSON.stringify({ instances: {claude: { driver: "claudeAgent", config: { cli: join(root, "server/testing/fake-claude-cli.ts") } },} }));harness = utilityProcess.fork(join(root, "dist-server/index.js"), [], { cwd: root, stdio: "pipe", env: {- HOME: home, USERPROFILE: home, OMB_DATA_DIR: home, PATH: "", OMB_PORT: String(port), OMB_WEBHOOK_PORT: String(await freePort()),+ HOME: home, USERPROFILE: home, OMB_DATA_DIR: home, PATH: "", OMB_PORT: String(port), OMB_WEBHOOK_PORT: String(webhookPort),OMB_DESKTOP_PARENT: "1", ...(process.env.SystemRoot ? { SystemRoot: process.env.SystemRoot } : {}),} });harness.stdout.on("data", (chunk) => { logs += chunk; });diff --git a/scripts/verify-ios-local-vm.ts b/scripts/verify-ios-local-vm.tsindex dcdcd120a8..373bc6fef9 100644--- a/scripts/verify-ios-local-vm.ts+++ b/scripts/verify-ios-local-vm.ts@@ -187,7 +187,9 @@ process.stdout.write(typeof result === 'string' ? result : JSON.stringify(resultconst harnessPort = new URL(fixture.info.url).port;const companionPort = await freePort();- const controlPort = await freePort();+ // Picked after the first listener closed, so the OS can hand the same port back; the sidecar refuses a shared one.+ let controlPort = await freePort();+ while (controlPort === companionPort) controlPort = await freePort();const controlToken = randomBytes(32).toString("base64url");const controlHeaders = { "x-openmausbot-control-token": controlToken };const companionHome = join(scratch, "companion-home");