import { afterEach, expect, it } from "bun:test"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { queryMcpConnection } from "../../.opencode/query-mcp.js"; import { writeQueryMcpConnection } from "../../src/runtime/query-mcp-connection.js"; const previous = { path: process.env.TJWATER_AGENT_MCP_CONNECTION_PATH, token: process.env.AGENT_INTERNAL_TOKEN }; const roots: string[] = []; afterEach(() => { for (const [key, value] of [["TJWATER_AGENT_MCP_CONNECTION_PATH", previous.path], ["AGENT_INTERNAL_TOKEN", previous.token]]) { if (value === undefined) delete process.env[key!]; else process.env[key!] = value; } for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); }); it("configures a pinned native MCP connection using the host-owned endpoint and private channel credential", () => { const root = mkdtempSync(join(tmpdir(), "query-connection-")); roots.push(root); process.env.AGENT_INTERNAL_TOKEN = "MCP_TEST_ONLY"; writeQueryMcpConnection(join(root, "sessions"), "http://127.0.0.1:12345/mcp"); expect(queryMcpConnection()).toMatchObject({ type: "remote", protocol: "2026-07-28", oauth: false, codemode: false, headers: { "x-agent-internal-token": "MCP_TEST_ONLY" } }); writeQueryMcpConnection(join(root, "sessions"), "http://127.0.0.1:12345/mcp", 3_665_000); expect(queryMcpConnection()?.timeout?.execution).toBe(3_665_000); writeQueryMcpConnection(join(root, "sessions"), "http://127.0.0.1:12345/mcp", Infinity); expect(() => queryMcpConnection()).toThrow("timeout"); writeQueryMcpConnection(join(root, "sessions"), "https://foreign.test/mcp"); expect(() => queryMcpConnection()).toThrow("loopback"); });