| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { verifyMcp } from "./mcp-readiness.mjs"; |
| 4 | |
| 5 | test("MCP initializes, notifies, forwards negotiated headers and verifies action scopes", async () => { |
| 6 | const methods = []; |
| 7 | const fetcher = async (_url, options) => { |
| 8 | if (!options.body) |
| 9 | return Response.json({ tools: [{ name: "repository", actions: [{ name: "get", scope: "repo:read" }] }], scopes_supported: ["repo:read"] }); |
| 10 | const request = JSON.parse(options.body); |
| 11 | methods.push(request.method); |
| 12 | if (request.method === "initialize") |
| 13 | return Response.json({ id: 1, result: { protocolVersion: "2025-03-26" } }, { headers: { "mcp-session-id": "test-session" } }); |
| 14 | assert.equal(options.headers["MCP-Protocol-Version"], "2025-03-26"); |
| 15 | assert.equal(options.headers["Mcp-Session-Id"], "test-session"); |
| 16 | if (request.method === "notifications/initialized") { |
| 17 | assert.equal(request.id, undefined); |
| 18 | return new Response(null, { status: 202 }); |
| 19 | } |
| 20 | return Response.json({ id: 2, result: { tools: [{ name: "repository", inputSchema: { type: "object", properties: { action: { enum: ["get"] } } } }] } }); |
| 21 | }; |
| 22 | assert.deepEqual(await verifyMcp("test-token", fetcher), { tools: 1, actions: 1 }); |
| 23 | assert.deepEqual(methods, ["initialize", "notifications/initialized", "tools/list"]); |
| 24 | }); |
| 25 | |
| 26 | test("MCP authorization failures are reported without credential contents", async () => { |
| 27 | await assert.rejects( |
| 28 | verifyMcp("secret", async () => new Response(null, { status: 401 })), |
| 29 | /401/, |
| 30 | ); |
| 31 | }); |