|
| 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 | +// #7760: in-process coverage for the stdio loopover_get_contributor_profile tool AND the exported |
| 10 | +// contributorProfileCli, both in packages/loopover-mcp/bin/loopover-mcp.ts. The bin is otherwise only exercised |
| 11 | +// via subprocess spawn (the sibling mcp-cli-contributor-profile.test.ts), which v8 cannot instrument -- the |
| 12 | +// isProcessEntrypoint guard is what lets a test import the module without it hijacking argv / binding stdin, so |
| 13 | +// the shared getContributorProfile call + the new stdio handler get real Codecov-measured coverage. Same shape |
| 14 | +// as mcp-cli-plan-issues.test.ts / mcp-cli-activation-preview.test.ts. Only the committed .ts source is imported. |
| 15 | +const MODULES = ["../../packages/loopover-mcp/bin/loopover-mcp.ts"] as const; |
| 16 | + |
| 17 | +type BinModule = { |
| 18 | + contributorProfileCli: (options: { login?: string; json?: boolean }) => Promise<void>; |
| 19 | + server: { connect: (transport: unknown) => Promise<void> }; |
| 20 | +}; |
| 21 | + |
| 22 | +let tempDir = ""; |
| 23 | +const capturedRequests: Array<{ url: string; method: string }> = []; |
| 24 | +const loaded = new Map<string, BinModule>(); |
| 25 | + |
| 26 | +beforeAll(async () => { |
| 27 | + tempDir = mkdtempSync(join(tmpdir(), "loopover-contributor-profile-inprocess-")); |
| 28 | + const apiUrl = await startFixtureServer({ |
| 29 | + onApiRequest: (request) => { |
| 30 | + if (request.url && request.url.includes("/profile")) { |
| 31 | + capturedRequests.push({ url: request.url ?? "", method: request.method ?? "GET" }); |
| 32 | + } |
| 33 | + }, |
| 34 | + }); |
| 35 | + // The bin reads LOOPOVER_API_URL at module load, so set the env BEFORE importing (hence the dynamic import). |
| 36 | + process.env.LOOPOVER_API_URL = apiUrl; |
| 37 | + process.env.LOOPOVER_API_TOKEN = "in-process-token"; |
| 38 | + process.env.LOOPOVER_API_TIMEOUT_MS = "2000"; |
| 39 | + process.env.LOOPOVER_CONFIG_DIR = tempDir; |
| 40 | + process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK = "1"; |
| 41 | + for (const specifier of MODULES) { |
| 42 | + loaded.set(specifier, (await import(specifier)) as unknown as BinModule); |
| 43 | + } |
| 44 | +}, 120_000); |
| 45 | + |
| 46 | +afterAll(async () => { |
| 47 | + await closeFixtureServer(); |
| 48 | + if (tempDir) rmSync(tempDir, { recursive: true, force: true }); |
| 49 | + delete process.env.LOOPOVER_API_URL; |
| 50 | + delete process.env.LOOPOVER_API_TOKEN; |
| 51 | + delete process.env.LOOPOVER_API_TIMEOUT_MS; |
| 52 | + delete process.env.LOOPOVER_CONFIG_DIR; |
| 53 | + delete process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK; |
| 54 | +}); |
| 55 | + |
| 56 | +async function captureStdout(fn: () => Promise<void>): Promise<string> { |
| 57 | + const chunks: string[] = []; |
| 58 | + const spy = vi.spyOn(process.stdout, "write").mockImplementation((chunk: string | Uint8Array): boolean => { |
| 59 | + chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8")); |
| 60 | + return true; |
| 61 | + }); |
| 62 | + try { |
| 63 | + await fn(); |
| 64 | + } finally { |
| 65 | + spy.mockRestore(); |
| 66 | + } |
| 67 | + return chunks.join(""); |
| 68 | +} |
| 69 | + |
| 70 | +describe("bin loopover_get_contributor_profile stdio tool (in-process, #7760)", () => { |
| 71 | + it.each(MODULES)("registers and proxies GET /v1/contributors/:login/profile — %s", async (specifier) => { |
| 72 | + capturedRequests.length = 0; |
| 73 | + const mod = loaded.get(specifier)!; |
| 74 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 75 | + await mod.server.connect(serverTransport); |
| 76 | + const client = new Client({ name: "contributor-profile-test", version: "0.1.0" }, { capabilities: {} }); |
| 77 | + await client.connect(clientTransport); |
| 78 | + try { |
| 79 | + const { tools } = await client.listTools(); |
| 80 | + const tool = tools.find((entry) => entry.name === "loopover_get_contributor_profile"); |
| 81 | + expect(tool).toBeDefined(); |
| 82 | + expect(tool?.description).toMatch(/contributor profile/i); |
| 83 | + |
| 84 | + const result = await client.callTool({ |
| 85 | + name: "loopover_get_contributor_profile", |
| 86 | + arguments: { login: "octocat" }, |
| 87 | + }); |
| 88 | + expect(capturedRequests.length).toBe(1); |
| 89 | + const captured = capturedRequests[0]!; |
| 90 | + expect(captured.url).toContain("/v1/contributors/octocat/profile"); |
| 91 | + expect(captured.method).toBe("GET"); |
| 92 | + expect(result.isError).toBeFalsy(); |
| 93 | + // structuredContent is the raw API payload; the summary line is the remote tool's fixed sentence. |
| 94 | + expect(result.structuredContent).toMatchObject({ login: "octocat" }); |
| 95 | + const text = JSON.stringify(result); |
| 96 | + expect(text).toContain("LoopOver contributor profile for octocat."); |
| 97 | + expect(text).toContain("3 registered repos; 12 merged PRs; strongest in review-tooling."); |
| 98 | + } finally { |
| 99 | + await client.close().catch(() => undefined); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it.each(MODULES)("url-encodes the login in the proxied path — %s", async (specifier) => { |
| 104 | + capturedRequests.length = 0; |
| 105 | + const mod = loaded.get(specifier)!; |
| 106 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 107 | + await mod.server.connect(serverTransport); |
| 108 | + const client = new Client({ name: "contributor-profile-encode-test", version: "0.1.0" }, { capabilities: {} }); |
| 109 | + await client.connect(clientTransport); |
| 110 | + try { |
| 111 | + await client.callTool({ name: "loopover_get_contributor_profile", arguments: { login: "a b/c" } }); |
| 112 | + expect(capturedRequests.at(-1)!.url).toContain("/v1/contributors/a%20b%2Fc/profile"); |
| 113 | + } finally { |
| 114 | + await client.close().catch(() => undefined); |
| 115 | + } |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe("bin contributor-profile CLI (in-process, #7760)", () => { |
| 120 | + it.each(MODULES)("shares getContributorProfile with the stdio tool: prints the header + API summary — %s", async (specifier) => { |
| 121 | + capturedRequests.length = 0; |
| 122 | + const mod = loaded.get(specifier)!; |
| 123 | + const out = await captureStdout(() => mod.contributorProfileCli({ login: "octocat" })); |
| 124 | + expect(capturedRequests.at(-1)!.url).toBe("/v1/contributors/octocat/profile"); |
| 125 | + expect(out).toMatch(/LoopOver contributor profile for octocat\./); |
| 126 | + expect(out).toContain("3 registered repos; 12 merged PRs; strongest in review-tooling."); |
| 127 | + }); |
| 128 | + |
| 129 | + it.each(MODULES)("--json re-serializes the same payload the shared call returned — %s", async (specifier) => { |
| 130 | + const mod = loaded.get(specifier)!; |
| 131 | + const out = await captureStdout(() => mod.contributorProfileCli({ login: "octocat", json: true })); |
| 132 | + const payload = JSON.parse(out) as { login: string; summary: string }; |
| 133 | + expect(payload).toMatchObject({ login: "octocat", summary: "3 registered repos; 12 merged PRs; strongest in review-tooling." }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments