|
| 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 | +// #7763: in-process coverage for the loopover_watch_issues stdio tool. Same #7764 entrypoint-guard pattern as |
| 10 | +// mcp-cli-repo-focus-manifest -- import the .ts, hold the exported `server`, connect an InMemoryTransport so |
| 11 | +// v8/Codecov attributes the registerStdioTool block + the shared watchIssuesRequest helper (a subprocess spawn |
| 12 | +// can't be instrumented). Drives all three actions (list=GET, watch=POST, unwatch=DELETE) end to end. |
| 13 | +const MODULES = ["../../packages/loopover-mcp/bin/loopover-mcp.ts"] as const; |
| 14 | + |
| 15 | +type BinModule = { |
| 16 | + server: { connect: (transport: unknown) => Promise<void> }; |
| 17 | + watchCli: (args: string[]) => Promise<void>; |
| 18 | +}; |
| 19 | + |
| 20 | +async function captureStdout(fn: () => Promise<void>): Promise<string> { |
| 21 | + const chunks: string[] = []; |
| 22 | + const spy = vi.spyOn(process.stdout, "write").mockImplementation((chunk: string | Uint8Array): boolean => { |
| 23 | + chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8")); |
| 24 | + return true; |
| 25 | + }); |
| 26 | + try { |
| 27 | + await fn(); |
| 28 | + } finally { |
| 29 | + spy.mockRestore(); |
| 30 | + } |
| 31 | + return chunks.join(""); |
| 32 | +} |
| 33 | + |
| 34 | +let tempDir = ""; |
| 35 | +const watchGets: Array<{ method: string; url: string }> = []; |
| 36 | +const watchWrites: Array<{ method: string; body: { repoFullName?: string; labels?: string[] } }> = []; |
| 37 | +const loaded = new Map<string, BinModule>(); |
| 38 | + |
| 39 | +beforeAll(async () => { |
| 40 | + tempDir = mkdtempSync(join(tmpdir(), "loopover-watch-issues-")); |
| 41 | + const apiUrl = await startFixtureServer({ |
| 42 | + onApiRequest: (r) => { |
| 43 | + if (r.method === "GET" && r.url && r.url.includes("/watches")) watchGets.push({ method: r.method ?? "", url: r.url ?? "" }); |
| 44 | + }, |
| 45 | + onWatchRequest: (req) => watchWrites.push(req), |
| 46 | + }); |
| 47 | + process.env.LOOPOVER_API_URL = apiUrl; |
| 48 | + process.env.LOOPOVER_API_TOKEN = "in-process-token"; |
| 49 | + process.env.LOOPOVER_API_TIMEOUT_MS = "2000"; |
| 50 | + process.env.LOOPOVER_CONFIG_DIR = tempDir; |
| 51 | + process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK = "1"; |
| 52 | + for (const specifier of MODULES) { |
| 53 | + loaded.set(specifier, (await import(specifier)) as unknown as BinModule); |
| 54 | + } |
| 55 | +}, 120_000); |
| 56 | + |
| 57 | +afterAll(async () => { |
| 58 | + await closeFixtureServer(); |
| 59 | + if (tempDir) rmSync(tempDir, { recursive: true, force: true }); |
| 60 | + delete process.env.LOOPOVER_API_URL; |
| 61 | + delete process.env.LOOPOVER_API_TOKEN; |
| 62 | + delete process.env.LOOPOVER_CONFIG_DIR; |
| 63 | + delete process.env.LOOPOVER_SKIP_NPM_VERSION_CHECK; |
| 64 | +}); |
| 65 | + |
| 66 | +async function connectClient(specifier: (typeof MODULES)[number], name: string) { |
| 67 | + const mod = loaded.get(specifier)!; |
| 68 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 69 | + await mod.server.connect(serverTransport); |
| 70 | + const client = new Client({ name, version: "0.1.0" }, { capabilities: {} }); |
| 71 | + await client.connect(clientTransport); |
| 72 | + return client; |
| 73 | +} |
| 74 | + |
| 75 | +describe("bin loopover_watch_issues stdio tool (in-process, #7763)", () => { |
| 76 | + it.each(MODULES)("proxies list=GET, watch=POST (with/without labels), unwatch=DELETE — %s", async (specifier) => { |
| 77 | + watchGets.length = 0; |
| 78 | + watchWrites.length = 0; |
| 79 | + const client = await connectClient(specifier, "watch-issues-test"); |
| 80 | + try { |
| 81 | + const tool = (await client.listTools()).tools.find((entry) => entry.name === "loopover_watch_issues"); |
| 82 | + expect(tool).toBeDefined(); |
| 83 | + expect(tool?.description).toMatch(/watch repos|grabbable/i); |
| 84 | + |
| 85 | + const list = await client.callTool({ name: "loopover_watch_issues", arguments: { login: "octocat", action: "list" } }); |
| 86 | + expect(list.isError).toBeFalsy(); |
| 87 | + expect(watchGets.at(-1)).toEqual({ method: "GET", url: "/v1/contributors/octocat/watches" }); |
| 88 | + expect(JSON.stringify(list)).toContain("watching"); |
| 89 | + |
| 90 | + const watch = await client.callTool({ |
| 91 | + name: "loopover_watch_issues", |
| 92 | + arguments: { login: "octocat", action: "watch", repoFullName: "acme/widgets", labels: ["bug"] }, |
| 93 | + }); |
| 94 | + expect(watch.isError).toBeFalsy(); |
| 95 | + expect(watchWrites.at(-1)).toEqual({ method: "POST", body: { repoFullName: "acme/widgets", labels: ["bug"] } }); |
| 96 | + |
| 97 | + // No labels -> the shared helper omits the labels key entirely. |
| 98 | + await client.callTool({ name: "loopover_watch_issues", arguments: { login: "octocat", action: "watch", repoFullName: "acme/gadgets" } }); |
| 99 | + expect(watchWrites.at(-1)).toEqual({ method: "POST", body: { repoFullName: "acme/gadgets" } }); |
| 100 | + |
| 101 | + const unwatch = await client.callTool({ |
| 102 | + name: "loopover_watch_issues", |
| 103 | + arguments: { login: "octocat", action: "unwatch", repoFullName: "acme/widgets" }, |
| 104 | + }); |
| 105 | + expect(unwatch.isError).toBeFalsy(); |
| 106 | + expect(watchWrites.at(-1)).toEqual({ method: "DELETE", body: { repoFullName: "acme/widgets" } }); |
| 107 | + } finally { |
| 108 | + await client.close().catch(() => undefined); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it.each(MODULES)("errors (no request) when watch/unwatch is missing repoFullName — %s", async (specifier) => { |
| 113 | + watchWrites.length = 0; |
| 114 | + const client = await connectClient(specifier, "watch-issues-guard"); |
| 115 | + try { |
| 116 | + const result = await client.callTool({ name: "loopover_watch_issues", arguments: { login: "octocat", action: "watch" } }); |
| 117 | + expect(result.isError).toBe(true); |
| 118 | + expect(JSON.stringify(result.content)).toMatch(/requires repoFullName/i); |
| 119 | + expect(watchWrites).toEqual([]); |
| 120 | + } finally { |
| 121 | + await client.close().catch(() => undefined); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it.each(MODULES)("errors when no login can be resolved from arg/session/env — %s", async (specifier) => { |
| 126 | + const savedLogin = process.env.LOOPOVER_LOGIN; |
| 127 | + const savedGh = process.env.GITHUB_LOGIN; |
| 128 | + delete process.env.LOOPOVER_LOGIN; |
| 129 | + delete process.env.GITHUB_LOGIN; |
| 130 | + const client = await connectClient(specifier, "watch-issues-nologin"); |
| 131 | + try { |
| 132 | + const result = await client.callTool({ name: "loopover_watch_issues", arguments: { action: "list" } }); |
| 133 | + expect(result.isError).toBe(true); |
| 134 | + expect(JSON.stringify(result.content)).toMatch(/No GitHub login|LOOPOVER_LOGIN/i); |
| 135 | + } finally { |
| 136 | + await client.close().catch(() => undefined); |
| 137 | + if (savedLogin !== undefined) process.env.LOOPOVER_LOGIN = savedLogin; |
| 138 | + if (savedGh !== undefined) process.env.GITHUB_LOGIN = savedGh; |
| 139 | + } |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +// The `watch` CLI now routes through the same watchIssuesRequest helper. Drive it in-process (a subprocess |
| 144 | +// spawn -- mcp-cli-watch.test.ts -- can't be v8-instrumented) so those shared call sites get real coverage. |
| 145 | +describe("bin watch CLI reuses watchIssuesRequest (in-process, #7763)", () => { |
| 146 | + it.each(MODULES)("list=GET, add=POST {repoFullName,labels}, remove=DELETE via the shared helper — %s", async (specifier) => { |
| 147 | + watchGets.length = 0; |
| 148 | + watchWrites.length = 0; |
| 149 | + const mod = loaded.get(specifier)!; |
| 150 | + |
| 151 | + const listOut = await captureStdout(() => mod.watchCli(["list", "--login", "octocat"])); |
| 152 | + expect(listOut).toMatch(/Watching \d+ repo\(s\) for octocat/); |
| 153 | + expect(watchGets.at(-1)).toEqual({ method: "GET", url: "/v1/contributors/octocat/watches" }); |
| 154 | + |
| 155 | + await captureStdout(() => mod.watchCli(["add", "acme/widgets", "--labels", "bug,feature", "--login", "octocat"])); |
| 156 | + expect(watchWrites.at(-1)).toEqual({ method: "POST", body: { repoFullName: "acme/widgets", labels: ["bug", "feature"] } }); |
| 157 | + |
| 158 | + await captureStdout(() => mod.watchCli(["remove", "acme/widgets", "--login", "octocat"])); |
| 159 | + expect(watchWrites.at(-1)).toEqual({ method: "DELETE", body: { repoFullName: "acme/widgets" } }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments