|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { declineSubscriptions } from "./subscriptions"; |
| 4 | + |
| 5 | +const rpc = (method: string, id: unknown = 1, headers?: HeadersInit) => |
| 6 | + new Request("http://localhost/api/mcp", { |
| 7 | + method: "POST", |
| 8 | + headers: { "content-type": "application/json", ...headers }, |
| 9 | + body: JSON.stringify({ jsonrpc: "2.0", id, method, params: {} }), |
| 10 | + }); |
| 11 | + |
| 12 | +const capabilities = { |
| 13 | + logging: {}, |
| 14 | + tools: { listChanged: true }, |
| 15 | + prompts: { listChanged: true }, |
| 16 | + resources: { listChanged: true, subscribe: true }, |
| 17 | +}; |
| 18 | + |
| 19 | +const discoverResult = (id: unknown) => ({ |
| 20 | + jsonrpc: "2.0", |
| 21 | + id, |
| 22 | + result: { supportedVersions: ["2026-07-28"], capabilities }, |
| 23 | +}); |
| 24 | + |
| 25 | +const offCapabilities = { |
| 26 | + logging: {}, |
| 27 | + tools: { listChanged: false }, |
| 28 | + prompts: { listChanged: false }, |
| 29 | + resources: { listChanged: false, subscribe: false }, |
| 30 | +}; |
| 31 | + |
| 32 | +describe("declineSubscriptions", () => { |
| 33 | + it("answers subscriptions/listen with -32601 over 404 without opening a stream", async () => { |
| 34 | + const post = vi.fn(); |
| 35 | + const res = await declineSubscriptions(post)( |
| 36 | + rpc("subscriptions/listen", "listen:0"), |
| 37 | + ); |
| 38 | + expect(post).not.toHaveBeenCalled(); |
| 39 | + expect(res.status).toBe(404); |
| 40 | + expect(res.headers.get("content-type")).toContain("application/json"); |
| 41 | + expect(await res.json()).toEqual({ |
| 42 | + jsonrpc: "2.0", |
| 43 | + id: "listen:0", |
| 44 | + error: { |
| 45 | + code: -32601, |
| 46 | + message: "Method not found: subscriptions/listen", |
| 47 | + }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("recognises listen from the Mcp-Method header", async () => { |
| 52 | + const post = vi.fn(); |
| 53 | + const res = await declineSubscriptions(post)( |
| 54 | + new Request("http://localhost/api/mcp", { |
| 55 | + method: "POST", |
| 56 | + headers: { "mcp-method": "subscriptions/listen" }, |
| 57 | + body: "not json", |
| 58 | + }), |
| 59 | + ); |
| 60 | + expect(post).not.toHaveBeenCalled(); |
| 61 | + expect(res.status).toBe(404); |
| 62 | + }); |
| 63 | + |
| 64 | + it("turns the change-notification flags off in a JSON discover result", async () => { |
| 65 | + const post = vi.fn(async () => Response.json(discoverResult(7))); |
| 66 | + const res = await declineSubscriptions(post)(rpc("server/discover", 7)); |
| 67 | + expect(res.status).toBe(200); |
| 68 | + const body = (await res.json()) as { |
| 69 | + result: { capabilities: unknown; supportedVersions: string[] }; |
| 70 | + }; |
| 71 | + expect(body.result.capabilities).toEqual(offCapabilities); |
| 72 | + expect(body.result.supportedVersions).toEqual(["2026-07-28"]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("turns the flags off in an SSE discover result", async () => { |
| 76 | + const post = vi.fn( |
| 77 | + async () => |
| 78 | + new Response( |
| 79 | + `event: message\ndata: ${JSON.stringify(discoverResult(8))}\n\n`, |
| 80 | + { headers: { "content-type": "text/event-stream" } }, |
| 81 | + ), |
| 82 | + ); |
| 83 | + const res = await declineSubscriptions(post)(rpc("server/discover", 8)); |
| 84 | + const text = await res.text(); |
| 85 | + expect(text.startsWith("event: message\ndata: ")).toBe(true); |
| 86 | + const frame = text.split("\n").find((line) => line.startsWith("data:")); |
| 87 | + const body = JSON.parse(frame!.slice("data:".length)) as { |
| 88 | + result: { capabilities: unknown }; |
| 89 | + }; |
| 90 | + expect(body.result.capabilities).toEqual(offCapabilities); |
| 91 | + }); |
| 92 | + |
| 93 | + it("passes every other method through untouched, with its arguments", async () => { |
| 94 | + const response = new Response("ok"); |
| 95 | + const post = vi.fn(async (_request: Request, _context: { n: number }) => |
| 96 | + Promise.resolve(response), |
| 97 | + ); |
| 98 | + const request = rpc("tools/call"); |
| 99 | + const context = { n: 1 }; |
| 100 | + const res = await declineSubscriptions(post)(request, context); |
| 101 | + expect(res).toBe(response); |
| 102 | + expect(post).toHaveBeenCalledWith(request, context); |
| 103 | + // The wrapper reads a clone, so the handler can still read the body. |
| 104 | + expect(await request.json()).toMatchObject({ method: "tools/call" }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("leaves a failed discover response as it is", async () => { |
| 108 | + const response = new Response("nope", { status: 401 }); |
| 109 | + const post = vi.fn(async () => response); |
| 110 | + const res = await declineSubscriptions(post)(rpc("server/discover")); |
| 111 | + expect(res).toBe(response); |
| 112 | + }); |
| 113 | +}); |
0 commit comments