|
| 1 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 2 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 7 | +import { closeFixtureServer, startFixtureServer } from "./support/mcp-cli-harness"; |
| 8 | + |
| 9 | +// (#7800) In-process coverage of the stdio loopover_get_gate_config_effective proxy. The bin ends with |
| 10 | +// `await server.connect(new StdioServerTransport())` at module scope, so we mock StdioServerTransport to hand |
| 11 | +// the imported module an in-memory transport we control, then drive its tool surface with a real MCP client. |
| 12 | +// This is the only way to instrument bin/loopover-mcp.ts's new lines -- subprocess spawn (the sibling |
| 13 | +// mcp-cli-pr-reviewability.test.ts) is functionally faithful but not coverage-instrumented. |
| 14 | +const holder = vi.hoisted(() => ({ serverTransport: undefined as any })); |
| 15 | +vi.mock("@modelcontextprotocol/sdk/server/stdio.js", () => ({ |
| 16 | + StdioServerTransport: class { |
| 17 | + constructor() { |
| 18 | + return holder.serverTransport as any; |
| 19 | + } |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +const FORBIDDEN_PUBLIC_TERMS = /wallet\s*[:=]\s*\S+|hotkey\s*[:=]\s*\S+|coldkey\s*[:=]\s*\S+|raw trust score is|your trust score|reward estimate is|estimated reward/i; |
| 24 | + |
| 25 | +let client: Client; |
| 26 | +let configDir: string; |
| 27 | +let capturedRequests: Array<{ url: string; method: string }>; |
| 28 | +const ENV_KEYS = ["LOOPOVER_CONFIG_DIR", "LOOPOVER_API_URL", "LOOPOVER_TOKEN", "LOOPOVER_API_TIMEOUT_MS"] as const; |
| 29 | +const savedEnv: Record<string, string | undefined> = {}; |
| 30 | + |
| 31 | +beforeAll(async () => { |
| 32 | + for (const key of ENV_KEYS) savedEnv[key] = process.env[key]; |
| 33 | + configDir = mkdtempSync(join(tmpdir(), "loopover-gate-config-effective-")); |
| 34 | + capturedRequests = []; |
| 35 | + const apiUrl = await startFixtureServer({ |
| 36 | + onApiRequest: (request) => { |
| 37 | + if (request.url && request.url.includes("/gate-config/effective")) { |
| 38 | + capturedRequests.push({ url: request.url ?? "", method: request.method ?? "GET" }); |
| 39 | + } |
| 40 | + }, |
| 41 | + }); |
| 42 | + process.env.LOOPOVER_CONFIG_DIR = configDir; |
| 43 | + process.env.LOOPOVER_API_URL = apiUrl; |
| 44 | + process.env.LOOPOVER_TOKEN = "session-token"; |
| 45 | + process.env.LOOPOVER_API_TIMEOUT_MS = "5000"; |
| 46 | + |
| 47 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 48 | + holder.serverTransport = serverTransport; |
| 49 | + |
| 50 | + // cliArgs[0] === undefined skips the module's `if (cliArgs[0] && cliArgs[0] !== "--stdio")` CLI-dispatch |
| 51 | + // guard (which would runCli + process.exit), so importing just registers the tools and connects our |
| 52 | + // in-memory transport instead of a real stdio one. |
| 53 | + const originalArgv = process.argv; |
| 54 | + process.argv = [process.execPath, "loopover-mcp"]; |
| 55 | + // Import the .ts source explicitly (not the .js): a committed/build-artifact .js on disk would otherwise be |
| 56 | + // resolved and instrumented under its .js path, so codecov/patch would map the new lines to the wrong file. |
| 57 | + // A non-literal specifier keeps tsc from rejecting the .ts extension (TS5097) while vitest still loads it. |
| 58 | + const binTsModule = "../../packages/loopover-mcp/bin/loopover-mcp.ts"; |
| 59 | + await import(/* @vite-ignore */ binTsModule); |
| 60 | + process.argv = originalArgv; |
| 61 | + |
| 62 | + client = new Client({ name: "gate-config-effective-test", version: "0.0.1" }); |
| 63 | + await client.connect(clientTransport); |
| 64 | +}); |
| 65 | + |
| 66 | +afterAll(async () => { |
| 67 | + await client?.close().catch(() => undefined); |
| 68 | + await closeFixtureServer(); |
| 69 | + if (configDir) rmSync(configDir, { recursive: true, force: true }); |
| 70 | + for (const key of ENV_KEYS) { |
| 71 | + if (savedEnv[key] === undefined) delete process.env[key]; |
| 72 | + else process.env[key] = savedEnv[key]; |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +describe("loopover_get_gate_config_effective stdio proxy (#7800)", () => { |
| 77 | + it("registers the tool in the stdio server tool list", async () => { |
| 78 | + const { tools } = await client.listTools(); |
| 79 | + const tool = tools.find((entry) => entry.name === "loopover_get_gate_config_effective"); |
| 80 | + expect(tool).toBeDefined(); |
| 81 | + expect(tool?.description).toMatch(/gate thresholds/i); |
| 82 | + }); |
| 83 | + |
| 84 | + it("proxies owner/repo to /gate-config/effective via apiGet and returns the payload", async () => { |
| 85 | + const result = await client.callTool({ |
| 86 | + name: "loopover_get_gate_config_effective", |
| 87 | + arguments: { owner: "owner", repo: "repo" }, |
| 88 | + }); |
| 89 | + expect(capturedRequests.length).toBe(1); |
| 90 | + const captured = capturedRequests[0]!; |
| 91 | + expect(captured.url).toContain("/v1/repos/owner/repo/gate-config/effective"); |
| 92 | + expect(captured.method).toBe("GET"); |
| 93 | + expect(result.isError).toBeFalsy(); |
| 94 | + const text = JSON.stringify(result); |
| 95 | + expect(text).not.toMatch(FORBIDDEN_PUBLIC_TERMS); |
| 96 | + expect(text).toContain("owner/repo"); |
| 97 | + expect(text).toContain("confidenceFloor"); |
| 98 | + expect(text).toContain("shadowPending"); |
| 99 | + }); |
| 100 | +}); |
0 commit comments