|
| 1 | +const assert = require('node:assert/strict'); |
| 2 | +const { registerMcpProfile, createStubHandler } = require('#src/components/mcp/index'); |
| 3 | + |
| 4 | +function makeFakeFastify() { |
| 5 | + const calls = []; |
| 6 | + return { |
| 7 | + calls, |
| 8 | + post(path, handler) { |
| 9 | + calls.push({ path, handler }); |
| 10 | + }, |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +function makeFakeReply() { |
| 15 | + const reply = { |
| 16 | + statusCode: undefined, |
| 17 | + headers: {}, |
| 18 | + body: undefined, |
| 19 | + code(status) { |
| 20 | + this.statusCode = status; |
| 21 | + return this; |
| 22 | + }, |
| 23 | + header(name, value) { |
| 24 | + this.headers[name] = value; |
| 25 | + return this; |
| 26 | + }, |
| 27 | + send(payload) { |
| 28 | + this.body = payload; |
| 29 | + return this; |
| 30 | + }, |
| 31 | + }; |
| 32 | + return reply; |
| 33 | +} |
| 34 | + |
| 35 | +describe('components/mcp/index', () => { |
| 36 | + describe('registerMcpProfile', () => { |
| 37 | + it('does nothing when the profile is disabled', () => { |
| 38 | + const host = makeFakeFastify(); |
| 39 | + registerMcpProfile({ |
| 40 | + profile: 'operations', |
| 41 | + host, |
| 42 | + config: { mcp: { operations: { enabled: false } } }, |
| 43 | + }); |
| 44 | + assert.equal(host.calls.length, 0); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does nothing when the mcp config block is absent', () => { |
| 48 | + const host = makeFakeFastify(); |
| 49 | + registerMcpProfile({ profile: 'operations', host, config: {} }); |
| 50 | + assert.equal(host.calls.length, 0); |
| 51 | + }); |
| 52 | + |
| 53 | + it('registers POST /mcp when operations profile is enabled with defaults', () => { |
| 54 | + const host = makeFakeFastify(); |
| 55 | + registerMcpProfile({ |
| 56 | + profile: 'operations', |
| 57 | + host, |
| 58 | + config: { mcp: { operations: { enabled: true } } }, |
| 59 | + }); |
| 60 | + assert.equal(host.calls.length, 1); |
| 61 | + assert.equal(host.calls[0].path, '/mcp'); |
| 62 | + assert.equal(typeof host.calls[0].handler, 'function'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('honors a custom mountPath', () => { |
| 66 | + const host = makeFakeFastify(); |
| 67 | + registerMcpProfile({ |
| 68 | + profile: 'application', |
| 69 | + host, |
| 70 | + config: { mcp: { application: { enabled: true, mountPath: '/agent' } } }, |
| 71 | + }); |
| 72 | + assert.equal(host.calls.length, 1); |
| 73 | + assert.equal(host.calls[0].path, '/agent'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('stub handler', () => { |
| 78 | + it('returns 503 with mcp_not_implemented body for the operations profile', async () => { |
| 79 | + const handler = createStubHandler('operations'); |
| 80 | + const reply = makeFakeReply(); |
| 81 | + await handler({}, reply); |
| 82 | + assert.equal(reply.statusCode, 503); |
| 83 | + assert.equal(reply.headers['Retry-After'], '0'); |
| 84 | + assert.equal(reply.headers['Content-Type'], 'application/json'); |
| 85 | + assert.deepEqual(reply.body, { error: 'mcp_not_implemented', profile: 'operations' }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns 503 with mcp_not_implemented body for the application profile', async () => { |
| 89 | + const handler = createStubHandler('application'); |
| 90 | + const reply = makeFakeReply(); |
| 91 | + await handler({}, reply); |
| 92 | + assert.equal(reply.statusCode, 503); |
| 93 | + assert.deepEqual(reply.body, { error: 'mcp_not_implemented', profile: 'application' }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments