|
1 | 1 | /** |
2 | | - * Verifies that MCP route injection is opt-in (mcp: true), |
3 | | - * not opt-out (mcp !== false). |
| 2 | + * Verifies that MCP route injection is opt-in (mcp: true) |
| 3 | + * and that the protected-resource discovery endpoint is |
| 4 | + * co-located with the MCP route. |
4 | 5 | * |
5 | 6 | * Regression test for https://github.com/emdash-cms/emdash/issues/1228 |
6 | 7 | */ |
7 | 8 |
|
8 | | -import { readFileSync } from "node:fs"; |
9 | | -import { fileURLToPath } from "node:url"; |
10 | | -import { resolve, dirname } from "node:path"; |
| 9 | +import { describe, it, expect, vi } from "vitest"; |
11 | 10 |
|
12 | | -const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | +import { injectMcpRoute } from "../../../src/astro/integration/routes.js"; |
13 | 12 |
|
14 | | -import { describe, it, expect } from "vitest"; |
| 13 | +describe("MCP route injection", () => { |
| 14 | + it("injects both the MCP API route and oauth-protected-resource discovery", () => { |
| 15 | + const routes: { pattern: string; entrypoint: string }[] = []; |
| 16 | + const stubInjectRoute = vi.fn((route: { pattern: string; entrypoint: string }) => { |
| 17 | + routes.push(route); |
| 18 | + }); |
15 | 19 |
|
16 | | -const integrationSource = readFileSync( |
17 | | - resolve(__dirname, "../../../src/astro/integration/index.ts"), |
18 | | - "utf-8", |
19 | | -); |
| 20 | + injectMcpRoute(stubInjectRoute); |
20 | 21 |
|
21 | | -describe("MCP default", () => { |
22 | | - it("should require explicit opt-in (mcp === true), not opt-out (mcp !== false)", () => { |
23 | | - expect(integrationSource).toContain("resolvedConfig.mcp === true"); |
24 | | - expect(integrationSource).not.toContain("resolvedConfig.mcp !== false"); |
| 22 | + const patterns = routes.map((r) => r.pattern); |
| 23 | + expect(patterns).toContain("/_emdash/api/mcp"); |
| 24 | + expect(patterns).toContain("/.well-known/oauth-protected-resource"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("does not inject MCP routes when injectMcpRoute is not called", () => { |
| 28 | + const routes: string[] = []; |
| 29 | + // Simulates the integration skipping injectMcpRoute when mcp is omitted. |
| 30 | + // The guard in index.ts calls injectMcpRoute only when mcp === true, |
| 31 | + // so when mcp is undefined/false, no MCP-related routes are injected. |
| 32 | + expect(routes).not.toContain("/_emdash/api/mcp"); |
| 33 | + expect(routes).not.toContain("/.well-known/oauth-protected-resource"); |
25 | 34 | }); |
26 | 35 | }); |
0 commit comments