|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 4 | +import { LiteKernel, Plugin, PluginContext } from '@objectstack/core'; |
| 5 | +import { HonoServerPlugin } from '@objectstack/plugin-hono-server'; |
| 6 | +import type { IHttpServer } from '@objectstack/spec/contracts'; |
| 7 | + |
| 8 | +import { createDispatcherPlugin } from './dispatcher-plugin.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * End-to-end regression for #7649 — `POST /api/v1/mcp/skill` answered 405 with |
| 12 | + * the WRONG envelope. |
| 13 | + * |
| 14 | + * ## What was measured (QA run #7627) |
| 15 | + * |
| 16 | + * ``` |
| 17 | + * POST /api/v1/mcp/skill |
| 18 | + * → HTTP 405 |
| 19 | + * {"error":"Method Not Allowed","code":"METHOD_NOT_ALLOWED", |
| 20 | + * "message":"POST is not supported for /api/v1/mcp/skill. Allowed: GET.", |
| 21 | + * "method":"POST","path":"/api/v1/mcp/skill","allowed":["GET"]} |
| 22 | + * ``` |
| 23 | + * |
| 24 | + * …instead of the standard dispatcher envelope |
| 25 | + * `{success:false, error:{code, message, httpStatus}}` carrying the documented |
| 26 | + * message "Method not allowed — use GET". |
| 27 | + * |
| 28 | + * ## Why the defect was invisible to the existing tests |
| 29 | + * |
| 30 | + * The 405 branch is NOT missing. `handleMcpSkillRequest` has had one since |
| 31 | + * #3842 routed it through `buildApiError`, and |
| 32 | + * `http-dispatcher.mcp.test.ts` covers it — by calling |
| 33 | + * `dispatcher.handleMcpSkill('POST', …)` DIRECTLY. That call cannot observe the |
| 34 | + * defect, because the defect is one layer above the dispatcher: the plugin |
| 35 | + * mounted `${prefix}/mcp/skill` for GET only, so a POST matched no route at |
| 36 | + * all, Hono routed it to `notFound`, and the hono adapter's |
| 37 | + * `unmatchedResponse()` — which re-matches the path across verbs and answers |
| 38 | + * 405 with its own hand-rolled body — replied first. The domain's branch was |
| 39 | + * dead code on this adapter. |
| 40 | + * |
| 41 | + * That is exactly the class of bug `dispatcher-plugin.routes.test.ts` opens by |
| 42 | + * naming ("unit tests called the handlers directly, hiding it"), with one extra |
| 43 | + * turn of the screw: here the status was already RIGHT. Only the body differed, |
| 44 | + * so a test asserting `res.status === 405` passes in both worlds. Hence this |
| 45 | + * suite drives a REAL Hono server over real `fetch` and asserts the BODY. |
| 46 | + * |
| 47 | + * ## Shape of the suite |
| 48 | + * |
| 49 | + * `LiteKernel` (as in `auth-unknown-subpath.hono.integration.test.ts`): this is |
| 50 | + * about the HTTP mount seam, and a full `ObjectKernel` would demand a `data` |
| 51 | + * service no assertion here reads. The fake `mcp` service implements only |
| 52 | + * `renderSkill`, which is all `GET /mcp/skill` calls — enough for the happy-path |
| 53 | + * control that proves the fix did not disturb the method the route serves. |
| 54 | + */ |
| 55 | + |
| 56 | +/** The standard envelope's message for this branch — contract, not prose. */ |
| 57 | +const EXPECTED_MESSAGE = 'Method not allowed — use GET'; |
| 58 | +const SKILL_PATH = '/api/v1/mcp/skill'; |
| 59 | +const SKILL_MARKER = 'OBJECTSTACK_SKILL_FIXTURE'; |
| 60 | + |
| 61 | +/** An `mcp` service that can render the skill and nothing else. */ |
| 62 | +function fakeMcpPlugin(): Plugin { |
| 63 | + return { |
| 64 | + name: 'com.objectstack.test.fake-mcp-skill', |
| 65 | + version: '1.0.0', |
| 66 | + init: async (ctx: PluginContext) => { |
| 67 | + ctx.registerService('mcp', { |
| 68 | + renderSkill: (o: any) => |
| 69 | + `---\nname: objectstack\n---\n\n# ${SKILL_MARKER}\n\nMCP: ${o?.mcpUrl ?? '<YOUR_ENV_MCP_URL>'}\n`, |
| 70 | + }); |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +describe('POST /api/v1/mcp/skill answers the standard 405 envelope (integration, #7649)', () => { |
| 76 | + let kernel: LiteKernel; |
| 77 | + let baseUrl: string; |
| 78 | + const prevEnabled = process.env.OS_MCP_SERVER_ENABLED; |
| 79 | + |
| 80 | + beforeAll(async () => { |
| 81 | + // Default-on; set explicitly so a stray env var in the runner cannot |
| 82 | + // turn every assertion below into a 404 that still "passes" a laxer read. |
| 83 | + delete process.env.OS_MCP_SERVER_ENABLED; |
| 84 | + |
| 85 | + kernel = new LiteKernel(); |
| 86 | + kernel.use(fakeMcpPlugin()); |
| 87 | + // port 0 → OS-assigned free port; resolved via getPort() after listening. |
| 88 | + kernel.use(new HonoServerPlugin({ port: 0, cors: false })); |
| 89 | + kernel.use(createDispatcherPlugin({ prefix: '/api/v1', securityHeaders: false, requireAuth: false })); |
| 90 | + |
| 91 | + await kernel.bootstrap(); |
| 92 | + |
| 93 | + const httpServer = kernel.getService<IHttpServer>('http.server'); |
| 94 | + baseUrl = `http://127.0.0.1:${httpServer.getPort!()}`; |
| 95 | + }, 30_000); |
| 96 | + |
| 97 | + afterAll(async () => { |
| 98 | + if (prevEnabled === undefined) delete process.env.OS_MCP_SERVER_ENABLED; |
| 99 | + else process.env.OS_MCP_SERVER_ENABLED = prevEnabled; |
| 100 | + if (kernel) { |
| 101 | + await Promise.race([ |
| 102 | + kernel.shutdown(), |
| 103 | + new Promise<void>((resolve) => setTimeout(resolve, 10_000)), |
| 104 | + ]); |
| 105 | + } |
| 106 | + }, 30_000); |
| 107 | + |
| 108 | + // ── ① the defect ──────────────────────────────────────────────────────── |
| 109 | + it('POST returns {success:false, error:{code, message, httpStatus}} — not the adapter\'s hand-rolled body', async () => { |
| 110 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'POST' }); |
| 111 | + const body = await res.json(); |
| 112 | + |
| 113 | + expect(res.status).toBe(405); |
| 114 | + // The envelope, field by field — the whole defect is that these differ, |
| 115 | + // so the status assertion above proves nothing on its own. |
| 116 | + expect(body.success).toBe(false); |
| 117 | + expect(body.error).toBeTypeOf('object'); |
| 118 | + expect(body.error.code).toBe('METHOD_NOT_ALLOWED'); |
| 119 | + expect(body.error.message).toBe(EXPECTED_MESSAGE); |
| 120 | + expect(body.error.httpStatus).toBe(405); |
| 121 | + }); |
| 122 | + |
| 123 | + it('POST does not answer with `unmatchedResponse()`\'s shape', async () => { |
| 124 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'POST' }); |
| 125 | + const body = await res.json(); |
| 126 | + |
| 127 | + // The four keys that identify the adapter's unmatched-route answer. |
| 128 | + // `error` as a STRING is the tell — the standard envelope nests an |
| 129 | + // object there, so this assertion cannot be satisfied by both shapes. |
| 130 | + expect(typeof body.error).not.toBe('string'); |
| 131 | + expect(body).not.toHaveProperty('method'); |
| 132 | + expect(body).not.toHaveProperty('path'); |
| 133 | + expect(body).not.toHaveProperty('allowed'); |
| 134 | + }); |
| 135 | + |
| 136 | + // The `Allow` header CHANGES with this fix, which is worth stating exactly |
| 137 | + // rather than filing under "unchanged". Before, the adapter derived it from |
| 138 | + // its own route table and Hono registers HEAD implicitly alongside every |
| 139 | + // GET, so the hint read `GET, HEAD`. Now the domain branch's own literal |
| 140 | + // answers, and it says `GET` — matching the message next to it ("use GET") |
| 141 | + // and the one verb this route actually serves. HEAD is still served; the |
| 142 | + // hint just no longer enumerates it. |
| 143 | + it('answers Allow: GET — the domain branch\'s literal, not the adapter\'s derived `GET, HEAD`', async () => { |
| 144 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'POST' }); |
| 145 | + expect(res.status).toBe(405); |
| 146 | + expect(res.headers.get('allow')).toBe('GET'); |
| 147 | + }); |
| 148 | + |
| 149 | + // DELETE is mounted for the same reason POST is — `/mcp` carries all three |
| 150 | + // verbs, and one of them answering a different 405 envelope than the other |
| 151 | + // is the drift this issue closes. |
| 152 | + it('DELETE gets the same standard envelope', async () => { |
| 153 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'DELETE' }); |
| 154 | + const body = await res.json(); |
| 155 | + |
| 156 | + expect(res.status).toBe(405); |
| 157 | + expect(body.success).toBe(false); |
| 158 | + expect(body.error.code).toBe('METHOD_NOT_ALLOWED'); |
| 159 | + expect(body.error.message).toBe(EXPECTED_MESSAGE); |
| 160 | + expect(body.error.httpStatus).toBe(405); |
| 161 | + }); |
| 162 | + |
| 163 | + // ── ② positive control: the happy path is untouched ───────────────────── |
| 164 | + it('GET still serves the SKILL.md as text/markdown, anonymously', async () => { |
| 165 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'GET' }); |
| 166 | + const text = await res.text(); |
| 167 | + |
| 168 | + expect(res.status).toBe(200); |
| 169 | + expect(res.headers.get('content-type')).toContain('text/markdown'); |
| 170 | + expect(res.headers.get('cache-control')).toBe('no-store'); |
| 171 | + expect(text).toContain(SKILL_MARKER); |
| 172 | + // Derived from the request host — the auth service is absent here. |
| 173 | + expect(text).toContain(`${baseUrl.replace('http://', 'http://')}/api/v1/mcp`); |
| 174 | + }); |
| 175 | + |
| 176 | + // A verb with no mount at all still falls to the adapter, and should: |
| 177 | + // `unmatchedResponse()` is the correct owner of a route that does not |
| 178 | + // exist under that verb. This pins the BOUNDARY of the fix rather than |
| 179 | + // claiming the adapter answer is wrong everywhere. |
| 180 | + it('PUT — unmounted — still falls through to the adapter (boundary, not a regression)', async () => { |
| 181 | + const res = await fetch(`${baseUrl}${SKILL_PATH}`, { method: 'PUT' }); |
| 182 | + expect(res.status).toBe(405); |
| 183 | + expect(await res.json()).toHaveProperty('allowed'); |
| 184 | + }); |
| 185 | +}); |
0 commit comments