| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import { createMocks } from 'node-mocks-http'
- import { beforeEach, describe, expect, it, vi } from 'vitest'
- import handler from '../../../../pages/api/mcp/index'
- import { mswServer } from '@/tests/lib/msw'
- // Mock the MCP SDK and Briven MCP server to avoid Hono/node-mocks-http compatibility issues.
- //
- // Starting with MCP SDK v1.25.x (required by @supabase/mcp-server-supabase@0.6.2), the SDK
- // uses @hono/node-server for converting between Node.js HTTP and Web Standard APIs. This is
- // incompatible with the node-mocks-http library used in these tests.
- //
- // Since these tests focus on query parameter validation in the API handler rather than
- // testing the MCP transport implementation, we mock both packages to avoid hitting the
- // incompatible Hono code paths.
- vi.mock('@modelcontextprotocol/sdk/server/streamableHttp.js', () => ({
- StreamableHTTPServerTransport: vi.fn().mockImplementation(function () {
- return { handleRequest: vi.fn().mockResolvedValue(undefined) }
- }),
- }))
- vi.mock('@supabase/mcp-server-supabase', () => ({
- createBrivenMcpServer: vi.fn().mockReturnValue({
- connect: vi.fn().mockResolvedValue(undefined),
- }),
- }))
- describe('/api/mcp', () => {
- beforeEach(() => {
- // Disable MSW for these tests
- mswServer.close()
- // Clear mock state between tests
- vi.clearAllMocks()
- })
- describe('Method handling', async () => {
- it('should handle POST requests', async () => {
- const { req, res } = createMocks({
- method: 'POST',
- query: {},
- body: {},
- })
- await handler(req, res)
- expect(res._getStatusCode()).not.toBe(405)
- })
- it('should return 405 for non-POST methods', async () => {
- const { req, res } = createMocks({
- method: 'GET',
- })
- await handler(req, res)
- expect(res._getStatusCode()).toBe(405)
- expect(JSON.parse(res._getData())).toEqual({
- error: { message: 'Method GET Not Allowed' },
- })
- expect(res._getHeaders()).toEqual({ allow: ['POST'], 'content-type': 'application/json' })
- })
- })
- describe('Query validation', async () => {
- it('should accept valid feature groups', async () => {
- const { req, res } = createMocks({
- method: 'POST',
- query: { features: 'docs,database' },
- body: {},
- })
- await handler(req, res)
- expect(res._getStatusCode()).not.toBe(400)
- })
- it('should reject invalid feature groups', async () => {
- const { req, res } = createMocks({
- method: 'POST',
- query: { features: 'invalid,unknown' },
- body: {},
- })
- await handler(req, res)
- expect(res._getStatusCode()).toBe(400)
- expect(JSON.parse(res._getData())).toHaveProperty('error')
- })
- it('should work without features parameter', async () => {
- const { req, res } = createMocks({
- method: 'POST',
- query: {},
- body: {},
- })
- await handler(req, res)
- expect(res._getStatusCode()).not.toBe(400)
- })
- })
- })
|