import { describe, expect, it } from "vitest"; import { buildOriginAllowlist, countMcpRequestOperations } from "./transport.js"; const ALLOWED = ["https://staging-app.hivegrid.de", "https://staging-web.hivegrid.de"]; describe("buildOriginAllowlist", () => { it("skips the origin allowlist for native clients without an Origin header", () => { // Claude Code/Desktop, Cursor and the MCP Inspector CLI send no Origin; // passing allowedOrigins anyway would make the SDK reject them with // "Invalid Origin header: undefined" after successful OAuth. expect(buildOriginAllowlist(undefined, ALLOWED)).toEqual({}); }); it("enforces the allowlist when a browser context sends an Origin header", () => { expect(buildOriginAllowlist("https://staging-app.hivegrid.de", ALLOWED)).toEqual({ allowedOrigins: ALLOWED }); // A disallowed origin is still passed through — the SDK rejects it. expect(buildOriginAllowlist("https://evil.example", ALLOWED)).toEqual({ allowedOrigins: ALLOWED }); }); }); describe("countMcpRequestOperations", () => { it("counts a single JSON-RPC object as one operation", () => { expect(countMcpRequestOperations({ jsonrpc: "2.0", id: 1, method: "tools/call" })).toBe(1); }); it("counts every method-bearing message in a batch so the rate limiter charges per operation", () => { const batch = Array.from({ length: 12 }, (_, index) => ({ jsonrpc: "2.0", id: index, method: "tools/call" })); expect(countMcpRequestOperations(batch)).toBe(12); }); it("ignores non-request array elements but never returns less than one", () => { expect(countMcpRequestOperations([{ jsonrpc: "2.0", id: 1, result: {} }, 42, null])).toBe(1); }); it("treats malformed or empty bodies as a single operation", () => { expect(countMcpRequestOperations(undefined)).toBe(1); expect(countMcpRequestOperations("not-json")).toBe(1); }); });