const addToolMock = jest.fn(); const startMock = jest.fn(); const createdServers: Array<{ config: any; addTool: jest.Mock; start: jest.Mock }> = []; jest.mock('fastmcp', () => { return { __esModule: true, FastMCP: jest.fn().mockImplementation((config) => { const instance = { config, addTool: addToolMock, start: startMock }; createdServers.push(instance); return instance; }) }; }); let createMCPServer: typeof import('../ServerFactory')['createMCPServer']; let createHealthCheckTool: typeof import('../ServerFactory')['createHealthCheckTool']; let getFastMcpMock: () => jest.Mock; beforeAll(async () => { const factoryModule = await import('../ServerFactory'); createMCPServer = factoryModule.createMCPServer; createHealthCheckTool = factoryModule.createHealthCheckTool; const fastMcpModule = await import('fastmcp'); getFastMcpMock = () => fastMcpModule.FastMCP as unknown as jest.Mock; }); describe('createMCPServer', () => { beforeEach(() => { addToolMock.mockClear(); startMock.mockClear(); createdServers.length = 0; getFastMcpMock().mockClear(); }); it('creates a server with default instructions and registers tools and agents', async () => { const tools = [{ name: 'tool-a' }, { name: 'tool-b' }]; const agentRegister = jest.fn(); const onInitialize = jest.fn(); const server = await createMCPServer({ name: 'test-server', version: '1.2.3', tools, agents: [{ register: agentRegister }], onInitialize }); expect(getFastMcpMock()).toHaveBeenCalledWith({ name: 'test-server', version: '1.2.3', instructions: 'test-server MCP Server v1.2.3' }); expect(addToolMock).toHaveBeenCalledTimes(2); expect(addToolMock).toHaveBeenNthCalledWith(1, tools[0]); expect(addToolMock).toHaveBeenNthCalledWith(2, tools[1]); expect(agentRegister).toHaveBeenCalledWith(server); expect(onInitialize).toHaveBeenCalledWith(server); }); it('passes through explicit instructions and returns configured server', async () => { const server = await createMCPServer({ name: 'custom-server', version: '2.0.0', instructions: 'custom instructions' }); expect(getFastMcpMock()).toHaveBeenCalledWith({ name: 'custom-server', version: '2.0.0', instructions: 'custom instructions' }); expect(server).toEqual(createdServers[0]); }); }); describe('createHealthCheckTool', () => { beforeEach(() => { jest.useFakeTimers(); jest.setSystemTime(new Date('2024-01-01T00:00:00.000Z')); }); afterEach(() => { jest.useRealTimers(); }); it('returns a tool that reports health status with metadata', async () => { const tool = createHealthCheckTool('1.0.0', { region: 'us-central1' }) as any; expect(tool.name).toBe('health-check'); expect(tool.description).toContain('Check server health'); const result = await tool.execute(); expect(JSON.parse(result)).toEqual({ status: 'healthy', timestamp: '2024-01-01T00:00:00.000Z', version: '1.0.0', region: 'us-central1' }); }); });