server.test.js•3.19 kB
import { jest } from '@jest/globals';
const mockSetRequestHandler = jest.fn();
const mockConnect = jest.fn().mockResolvedValue({ success: true });
// Mock the Server class
jest.mock('@modelcontextprotocol/sdk/server/index.js', () => ({
Server: jest.fn().mockImplementation(() => ({
setRequestHandler: mockSetRequestHandler,
connect: mockConnect
}))
}));
// Mock StdioServerTransport
jest.mock('@modelcontextprotocol/sdk/server/stdio.js', () => ({
StdioServerTransport: jest.fn().mockImplementation(() => ({
connect: jest.fn().mockResolvedValue({ success: true })
}))
}));
// Mock express
const mockGet = jest.fn();
const mockOn = jest.fn();
const mockClose = jest.fn();
const mockListen = jest.fn().mockImplementation((port, callback) => {
if (callback)
callback();
return {
close: mockClose,
on: mockOn
};
});
jest.mock('express', () => {
return jest.fn().mockImplementation(() => ({
get: mockGet,
listen: mockListen
}));
});
// Mock axios
jest.mock('axios', () => {
return {
post: jest.fn().mockResolvedValue({ data: { access_token: 'mock-token', expires_in: 3600 } }),
default: jest.fn().mockImplementation(() => Promise.resolve({ data: {} }))
};
});
// Mock open
jest.mock('open', () => jest.fn());
// Mock net module (for isPortInUse)
jest.mock('net', () => {
const mockServer = {
once: jest.fn(function (event, handler) {
// When called with 'listening', simulate the server is free
if (event === 'listening') {
setTimeout(() => handler(), 0);
}
return this;
}),
listen: jest.fn().mockReturnThis(),
close: jest.fn()
};
const mockCreateServer = jest.fn().mockReturnValue(mockServer);
return { createServer: mockCreateServer };
});
describe('Spotify MCP Server', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should have request handlers for tool listing and execution', async () => {
// Import the server module dynamically
await import('../index.js');
// Check if setRequestHandler was called
expect(mockSetRequestHandler).toHaveBeenCalledTimes(2);
});
it('should connect to the transport when main is called', async () => {
await import('../index.js');
// We can't directly test main() as it's not exported, but
// we can check that the connect method was properly prepared
expect(mockConnect).not.toHaveBeenCalled(); // It's only called within main()
});
});
describe('MCP Tool Implementations', () => {
// Placeholder tests for tool implementations
it('should provide authentication functionality', () => {
// This would test the auth-spotify tool implementation
expect(true).toBe(true);
});
it('should provide search functionality', () => {
// This would test the search-spotify tool implementation
expect(true).toBe(true);
});
it('should provide playback control', () => {
// This would test play, pause, next, previous tools
expect(true).toBe(true);
});
});