|
| 1 | +import { beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { NOOP_TELEMETRY, type Telemetry } from "../telemetry/index.js"; |
| 3 | +import { withMockedModule } from "../../tests/helpers/mock-module.js"; |
| 4 | + |
| 5 | +let connectCalls = 0; |
| 6 | +let failOnCall = -1; |
| 7 | +const connectError = new Error("spawn ENOENT"); |
| 8 | + |
| 9 | +await withMockedModule( |
| 10 | + import.meta.resolve("@modelcontextprotocol/sdk/client/index.js"), |
| 11 | + (real: typeof import("@modelcontextprotocol/sdk/client/index.js")) => ({ |
| 12 | + ...real, |
| 13 | + Client: class { |
| 14 | + async connect(): Promise<void> { |
| 15 | + connectCalls += 1; |
| 16 | + if (connectCalls === failOnCall) throw connectError; |
| 17 | + } |
| 18 | + async listTools(): Promise<{ tools: [] }> { |
| 19 | + return { tools: [] }; |
| 20 | + } |
| 21 | + async close(): Promise<void> { |
| 22 | + return undefined; |
| 23 | + } |
| 24 | + }, |
| 25 | + }), |
| 26 | +); |
| 27 | + |
| 28 | +const { connectMCPServers } = await import("./client.js"); |
| 29 | + |
| 30 | +function recordingTelemetry(events: Record<string, unknown>[]): Telemetry { |
| 31 | + return { |
| 32 | + enabled: true, |
| 33 | + installationId: "test", |
| 34 | + capture: (event, properties) => { |
| 35 | + if (event === "mcp_connect") events.push({ ...(properties ?? {}) }); |
| 36 | + }, |
| 37 | + captureIntentional: () => false, |
| 38 | + flush: async () => undefined, |
| 39 | + discard: () => undefined, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("connectMCPServers telemetry", () => { |
| 44 | + beforeEach(() => { |
| 45 | + connectCalls = 0; |
| 46 | + failOnCall = -1; |
| 47 | + }); |
| 48 | + |
| 49 | + test("a mixed multi-config run emits exactly one mcp_connect per config", async () => { |
| 50 | + failOnCall = 2; |
| 51 | + const events: Record<string, unknown>[] = []; |
| 52 | + const warnings: string[] = []; |
| 53 | + const clients = await connectMCPServers( |
| 54 | + [ |
| 55 | + { name: "a", type: "stdio", command: "test-server-a" }, |
| 56 | + { name: "b", type: "stdio", command: "test-server-b" }, |
| 57 | + ], |
| 58 | + (message) => warnings.push(message), |
| 59 | + { telemetry: recordingTelemetry(events) }, |
| 60 | + ); |
| 61 | + try { |
| 62 | + expect(clients).toHaveLength(1); |
| 63 | + expect(warnings).toHaveLength(1); |
| 64 | + expect(events).toHaveLength(2); |
| 65 | + expect(events.map((event) => event.transport)).toEqual([ |
| 66 | + "stdio", |
| 67 | + "stdio", |
| 68 | + ]); |
| 69 | + expect(events.map((event) => event.result)).toEqual(["ok", "fail"]); |
| 70 | + } finally { |
| 71 | + for (const client of clients) await client.close(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test("omitted telemetry stays silent on the dropping default", async () => { |
| 76 | + // The omitted path routes capture calls into NOOP_TELEMETRY, whose |
| 77 | + // capture drops everything — the gate lives in the Telemetry handle, |
| 78 | + // not at the call site. This keeper pins that contract: the default |
| 79 | + // handle is disabled, and connecting without telemetry resolves |
| 80 | + // normally with nothing to flush or send. |
| 81 | + expect(NOOP_TELEMETRY.enabled).toBe(false); |
| 82 | + const warnings: string[] = []; |
| 83 | + const clients = await connectMCPServers( |
| 84 | + [{ name: "a", type: "stdio", command: "test-server-a" }], |
| 85 | + (message) => warnings.push(message), |
| 86 | + ); |
| 87 | + try { |
| 88 | + expect(clients).toHaveLength(1); |
| 89 | + expect(warnings).toHaveLength(0); |
| 90 | + } finally { |
| 91 | + for (const client of clients) await client.close(); |
| 92 | + } |
| 93 | + }); |
| 94 | +}); |
0 commit comments