|
| 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 | +// #7756: in-process coverage for the loopover_get_repo_onboarding_pack stdio tool. |
| 10 | +// Same #7764 entrypoint-guard pattern as mcp-cli-repo-focus-manifest — import the .ts, hold the exported |
| 11 | +// `server`, connect an InMemoryTransport so v8/Codecov attributes the registerStdioTool block (a subprocess |
| 12 | +// spawn cannot be instrumented). Drives GET {repoBase}/onboarding-pack/preview end to end, covering both |
| 13 | +// sides of the `refresh === true` query ternary. |
| 14 | +const MODULES = ["../../packages/loopover-mcp/bin/loopover-mcp.ts"] as const; |
| 15 | + |
| 16 | +type BinModule = { |
| 17 | + server: { connect: (transport: unknown) => Promise<void> }; |
| 18 | +}; |
| 19 | + |
| 20 | +let tempDir = ""; |
| 21 | +const capturedRequests: Array<{ url: string; method: string }> = []; |
| 22 | +const loaded = new Map<string, BinModule>(); |
| 23 | + |
| 24 | +beforeAll(async () => { |
| 25 | + tempDir = mkdtempSync(join(tmpdir(), "loopover-repo-onboarding-pack-")); |
| 26 | + const apiUrl = await startFixtureServer({ |
| 27 | + onApiRequest: (request) => { |
| 28 | + if (request.url && request.url.includes("/onboarding-pack/preview")) { |
| 29 | + capturedRequests.push({ url: request.url ?? "", method: request.method ?? "GET" }); |
| 30 | + } |
| 31 | + }, |
| 32 | + }); |
| 33 | + process.env.LOOPOVER_API_URL = apiUrl; |
| 34 | + process.env.LOOPOVER_API_TOKEN = "in-process-token"; |
| 35 | + process.env.LOOPOVER_API_TIMEOUT_MS = "2000"; |
| 36 | + process.env.LOOPOVER_CONFIG_DIR = tempDir; |
| 37 | + process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK = "1"; |
| 38 | + for (const specifier of MODULES) { |
| 39 | + loaded.set(specifier, (await import(specifier)) as unknown as BinModule); |
| 40 | + } |
| 41 | +}, 120_000); |
| 42 | + |
| 43 | +afterAll(async () => { |
| 44 | + await closeFixtureServer(); |
| 45 | + if (tempDir) rmSync(tempDir, { recursive: true, force: true }); |
| 46 | + delete process.env.LOOPOVER_API_URL; |
| 47 | + delete process.env.LOOPOVER_API_TOKEN; |
| 48 | + delete process.env.LOOPOVER_CONFIG_DIR; |
| 49 | + delete process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK; |
| 50 | +}); |
| 51 | + |
| 52 | +describe("bin loopover_get_repo_onboarding_pack stdio tool (in-process, #7756)", () => { |
| 53 | + it.each(MODULES)("registers and proxies GET .../onboarding-pack/preview without a refresh query — %s", async (specifier) => { |
| 54 | + capturedRequests.length = 0; |
| 55 | + const mod = loaded.get(specifier)!; |
| 56 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 57 | + await mod.server.connect(serverTransport); |
| 58 | + const client = new Client({ name: "repo-onboarding-pack-test", version: "0.1.0" }, { capabilities: {} }); |
| 59 | + await client.connect(clientTransport); |
| 60 | + try { |
| 61 | + const { tools } = await client.listTools(); |
| 62 | + const tool = tools.find((entry) => entry.name === "loopover_get_repo_onboarding_pack"); |
| 63 | + expect(tool).toBeDefined(); |
| 64 | + expect(tool?.description).toMatch(/onboarding pack|preview-only|not published/i); |
| 65 | + |
| 66 | + // refresh omitted -> the else side of `refresh === true`: no query string appended. |
| 67 | + const result = await client.callTool({ |
| 68 | + name: "loopover_get_repo_onboarding_pack", |
| 69 | + arguments: { owner: "owner", repo: "repo" }, |
| 70 | + }); |
| 71 | + expect(capturedRequests.length).toBe(1); |
| 72 | + const captured = capturedRequests[0]!; |
| 73 | + expect(captured.url).toBe("/v1/repos/owner/repo/onboarding-pack/preview"); |
| 74 | + expect(captured.url).not.toContain("refresh"); |
| 75 | + expect(captured.method).toBe("GET"); |
| 76 | + expect(result.isError).toBeFalsy(); |
| 77 | + const text = JSON.stringify(result); |
| 78 | + expect(text).toContain("onboarding pack preview for owner/repo"); |
| 79 | + expect(text).toContain("preview-only, not published"); |
| 80 | + expect(text).toContain("Contributor onboarding"); |
| 81 | + } finally { |
| 82 | + await client.close().catch(() => undefined); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it.each(MODULES)("forwards ?refresh=true when refresh is true — %s", async (specifier) => { |
| 87 | + capturedRequests.length = 0; |
| 88 | + const mod = loaded.get(specifier)!; |
| 89 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 90 | + await mod.server.connect(serverTransport); |
| 91 | + const client = new Client({ name: "repo-onboarding-pack-refresh", version: "0.1.0" }, { capabilities: {} }); |
| 92 | + await client.connect(clientTransport); |
| 93 | + try { |
| 94 | + // refresh: true -> the true side of `refresh === true`: ?refresh=true appended. |
| 95 | + const result = await client.callTool({ |
| 96 | + name: "loopover_get_repo_onboarding_pack", |
| 97 | + arguments: { owner: "owner", repo: "repo", refresh: true }, |
| 98 | + }); |
| 99 | + expect(capturedRequests.length).toBe(1); |
| 100 | + const captured = capturedRequests[0]!; |
| 101 | + expect(captured.url).toBe("/v1/repos/owner/repo/onboarding-pack/preview?refresh=true"); |
| 102 | + expect(captured.method).toBe("GET"); |
| 103 | + expect(result.isError).toBeFalsy(); |
| 104 | + expect(JSON.stringify(result)).toContain("onboarding pack preview for owner/repo"); |
| 105 | + } finally { |
| 106 | + await client.close().catch(() => undefined); |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments