import { expect, it } from "bun:test"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { createServer } from "node:net"; const availablePort = () => new Promise((done, reject) => { const server = createServer(); server.once("error", reject); server.listen(0, "127.0.0.1", () => { const port = (server.address() as { port: number }).port; server.close((error) => error ? reject(error) : done(port)); }); }); it("passes the isolated connection path to OpenCode before the Agent writes it", async () => { const root = await mkdtemp(join(tmpdir(), "query-launcher-test-")); try { const ports = await Promise.all([availablePort(), availablePort()]); // Execute the real launcher up to its first dependency. No OpenCode, Agent, // provider or business service starts; the dependency records its inputs. const script = `import { mock } from "bun:test"; mock.module(${JSON.stringify(resolve("src/runtime/opencode2-server.ts"))}, () => ({ startOpencode2: async (options) => { console.log("LAUNCHER_ENV=" + JSON.stringify({ connection: options.env.TJWATER_AGENT_MCP_CONNECTION_PATH })); return { password: "TEST_ONLY", exited: Promise.resolve(0), close() {}, client: { server: { async info() { throw new Error("STOP_BEFORE_AGENT"); } } } }; } })); await import(${JSON.stringify(resolve("scripts/start-isolated-integration.ts"))});`; const child = Bun.spawn([process.execPath, "-e", script], { cwd: process.cwd(), env: { ...process.env, INTEGRATION_DATA_DIR: root, INTEGRATION_AGENT_PORT: String(ports[0]), INTEGRATION_OPENCODE_PORT: String(ports[1]), TJWATER_AGENT_MCP_CONNECTION_PATH: "/unrelated/connection.json", AGENT_INTERNAL_TOKEN: "TEST_ONLY" }, stdout: "pipe", stderr: "pipe" }); const [code, out, err] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); expect(code).toBe(1); expect(err).toContain("STOP_BEFORE_AGENT"); const record = out.split("\n").find((line) => line.startsWith("LAUNCHER_ENV=")); expect(record).toBeDefined(); expect(JSON.parse(record!.slice("LAUNCHER_ENV=".length)).connection).toBe(join(root, "mcp-query", "connection.json")); } finally { await rm(root, { recursive: true, force: true }); } });