import { describe, expect, it } from "vitest"; import * as harness from "../src/index.js"; describe("swads MCP bridge", () => { it("performs MCP initialization, sends credentials only in headers and blocks non-allowlisted tools", async () => { const requests: Array<{ method: string; authorization: string | null }> = []; const fetcher: typeof fetch = async (_url, init) => { await Promise.resolve(); const headers = new Headers(init?.headers); if (init?.method === "GET") return new Response(null, { status: 405 }); if (typeof init?.body !== "string") throw new Error("Expected JSON-RPC body"); const message = JSON.parse(init.body) as { method: string; id?: number; params?: { name?: string } }; requests.push({ method: message.method, authorization: headers.get("authorization") }); if (message.id === undefined) return new Response(null, { status: 202 }); const result = message.method === "initialize" ? { protocolVersion: "2025-03-26", capabilities: { tools: {} }, serverInfo: { name: "test", version: "1" } } : { content: [{ type: "text", text: '{"metric":"spend","value":7}' }], structuredContent: { metric: "spend", value: 7 } }; return Response.json({ jsonrpc: "2.0", id: message.id, result }); }; const Constructor = Reflect.get(harness, "SwadsMcpClient"); expect(Constructor).toBeDefined(); const client = new Constructor({ token: "test-only-token", fetch: fetcher }); await expect(client.call("campaign_delete", {})).rejects.toThrow("not allowed"); expect(requests).toHaveLength(0); const result = await client.call("metrics_catalog", {}); expect(result.structuredContent).toEqual({ metric: "spend", value: 7 }); expect(requests.filter((r) => r.method === "tools/call")).toEqual([{ method: "tools/call", authorization: "Bearer test-only-token" }]); expect(JSON.stringify(result)).not.toContain("test-only-token"); await client.close(); }); it("fails closed without credentials", async () => { const Constructor = Reflect.get(harness, "SwadsMcpClient"); expect(Constructor).toBeDefined(); const client = new Constructor({ token: "" }); await expect(client.call("metrics_catalog", {})).rejects.toThrow("SWADS_MCP_TOKEN"); }); });