|
| 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 } from "vitest"; |
| 7 | +import { closeFixtureServer, startFixtureServer } from "./support/mcp-cli-harness"; |
| 8 | + |
| 9 | +// #7759: in-process coverage for the loopover_check_improvement_potential stdio tool. |
| 10 | +// Same #7764 entrypoint-guard pattern as sibling maintainer tools — import .ts, hold exported `server`, |
| 11 | +// connect InMemoryTransport so v8/Codecov attributes registerStdioTool. |
| 12 | +const MODULES = ["../../packages/loopover-mcp/bin/loopover-mcp.ts"] as const; |
| 13 | + |
| 14 | +type BinModule = { |
| 15 | + server: { connect: (transport: unknown) => Promise<void> }; |
| 16 | +}; |
| 17 | + |
| 18 | +let tempDir = ""; |
| 19 | +const capturedRequests: Array<{ url: string; method: string }> = []; |
| 20 | +const loaded = new Map<string, BinModule>(); |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + tempDir = mkdtempSync(join(tmpdir(), "loopover-improvement-potential-stdio-")); |
| 24 | + const apiUrl = await startFixtureServer({ |
| 25 | + onApiRequest: (request) => { |
| 26 | + if (request.url && request.url.includes("/lint/improvement-potential")) { |
| 27 | + capturedRequests.push({ url: request.url ?? "", method: request.method ?? "GET" }); |
| 28 | + } |
| 29 | + }, |
| 30 | + }); |
| 31 | + process.env.LOOPOVER_API_URL = apiUrl; |
| 32 | + process.env.LOOPOVER_API_TOKEN = "in-process-token"; |
| 33 | + process.env.LOOPOVER_API_TIMEOUT_MS = "2000"; |
| 34 | + process.env.LOOPOVER_CONFIG_DIR = tempDir; |
| 35 | + process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK = "1"; |
| 36 | + for (const specifier of MODULES) { |
| 37 | + loaded.set(specifier, (await import(specifier)) as unknown as BinModule); |
| 38 | + } |
| 39 | +}, 120_000); |
| 40 | + |
| 41 | +afterAll(async () => { |
| 42 | + await closeFixtureServer(); |
| 43 | + if (tempDir) rmSync(tempDir, { recursive: true, force: true }); |
| 44 | + delete process.env.LOOPOVER_API_URL; |
| 45 | + delete process.env.LOOPOVER_API_TOKEN; |
| 46 | + delete process.env.LOOPOVER_CONFIG_DIR; |
| 47 | + delete process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK; |
| 48 | +}); |
| 49 | + |
| 50 | +describe("bin loopover_check_improvement_potential stdio tool (in-process, #7759)", () => { |
| 51 | + it.each(MODULES)("registers and proxies POST /v1/lint/improvement-potential - %s", async (specifier) => { |
| 52 | + capturedRequests.length = 0; |
| 53 | + const mod = loaded.get(specifier)!; |
| 54 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 55 | + await mod.server.connect(serverTransport); |
| 56 | + const client = new Client({ name: "improvement-potential-stdio-test", version: "0.1.0" }, { capabilities: {} }); |
| 57 | + await client.connect(clientTransport); |
| 58 | + try { |
| 59 | + const { tools } = await client.listTools(); |
| 60 | + const tool = tools.find((entry) => entry.name === "loopover_check_improvement_potential"); |
| 61 | + expect(tool).toBeDefined(); |
| 62 | + expect(tool?.description).toMatch(/improvement/i); |
| 63 | + |
| 64 | + const result = await client.callTool({ |
| 65 | + name: "loopover_check_improvement_potential", |
| 66 | + arguments: { |
| 67 | + changedFiles: [{ path: "src/widget.ts", additions: 80, deletions: 2 }], |
| 68 | + testFiles: ["test/unit/widget.test.ts"], |
| 69 | + }, |
| 70 | + }); |
| 71 | + expect(capturedRequests.length).toBe(1); |
| 72 | + const captured = capturedRequests[0]!; |
| 73 | + expect(captured.url).toContain("/v1/lint/improvement-potential"); |
| 74 | + expect(captured.method).toBe("POST"); |
| 75 | + expect(result.isError).toBeFalsy(); |
| 76 | + const text = JSON.stringify(result); |
| 77 | + expect(text).toContain("improvementScore"); |
| 78 | + expect(text).toContain("minor"); |
| 79 | + expect(text).not.toMatch(/wallet|hotkey|reward|trust score/i); |
| 80 | + } finally { |
| 81 | + await client.close().catch(() => undefined); |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments