|
| 1 | +import { McpError } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import { getAvailableModulesTool } from '../tool.getAvailableModules'; |
| 3 | +import { getLocalModulesMap } from '../utils.getLocalModulesMap'; |
| 4 | + |
| 5 | +// Mock dependencies |
| 6 | +jest.mock('../utils.getLocalModulesMap'); |
| 7 | +jest.mock('../utils.moduleResolver'); // Mock the module resolver to avoid import.meta issues |
| 8 | +jest.mock('../server.caching', () => ({ |
| 9 | + memo: jest.fn(fn => fn) |
| 10 | +})); |
| 11 | + |
| 12 | +const mockGetLocalModulesMap = getLocalModulesMap as jest.MockedFunction<typeof getLocalModulesMap>; |
| 13 | + |
| 14 | +describe('getAvailableModulesTool', () => { |
| 15 | + beforeEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should have a consistent return structure', () => { |
| 20 | + const tool = getAvailableModulesTool(); |
| 21 | + |
| 22 | + expect(tool).toMatchSnapshot('structure'); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('getAvailableModulesTool, callback', () => { |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return modules list separated by semicolons', async () => { |
| 32 | + const mockModulesMap = { |
| 33 | + Button: '/path/to/button', |
| 34 | + Card: '/path/to/card', |
| 35 | + Table: '/path/to/table' |
| 36 | + }; |
| 37 | + |
| 38 | + mockGetLocalModulesMap.mockResolvedValue(mockModulesMap); |
| 39 | + |
| 40 | + const [, , callback] = getAvailableModulesTool(); |
| 41 | + const result = await callback({}); |
| 42 | + |
| 43 | + expect(mockGetLocalModulesMap).toHaveBeenCalledWith('@patternfly/react-core'); |
| 44 | + expect(result).toMatchSnapshot(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return empty string when modules map is empty', async () => { |
| 48 | + mockGetLocalModulesMap.mockResolvedValue({}); |
| 49 | + |
| 50 | + const [, , callback] = getAvailableModulesTool(); |
| 51 | + const result = await callback({}); |
| 52 | + |
| 53 | + expect(result).toMatchSnapshot(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle modules with special characters in names', async () => { |
| 57 | + const mockModulesMap = { |
| 58 | + 'Button-Group': '/path/to/button-group', |
| 59 | + 'Data.Table': '/path/to/data-table', |
| 60 | + Nav_Item: '/path/to/nav-item' |
| 61 | + }; |
| 62 | + |
| 63 | + mockGetLocalModulesMap.mockResolvedValue(mockModulesMap); |
| 64 | + |
| 65 | + const [, , callback] = getAvailableModulesTool(); |
| 66 | + const result = await callback({}); |
| 67 | + |
| 68 | + expect(result).toMatchSnapshot(); |
| 69 | + }); |
| 70 | + |
| 71 | + it.each([ |
| 72 | + { |
| 73 | + description: 'with Error object', |
| 74 | + error: new Error('Package not found') |
| 75 | + }, |
| 76 | + { |
| 77 | + description: 'with string error', |
| 78 | + error: 'String error message' |
| 79 | + }, |
| 80 | + { |
| 81 | + description: 'with null error', |
| 82 | + error: null |
| 83 | + } |
| 84 | + ])('should handle getLocalModulesMap errors, $description', async ({ error }) => { |
| 85 | + mockGetLocalModulesMap.mockRejectedValue(error); |
| 86 | + |
| 87 | + const [, , callback] = getAvailableModulesTool(); |
| 88 | + |
| 89 | + await expect(callback({})).rejects.toThrow(McpError); |
| 90 | + await expect(callback({})).rejects.toThrow('Failed to retrieve available modules'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should always call with @patternfly/react-core package', async () => { |
| 94 | + mockGetLocalModulesMap.mockResolvedValue({ Component: '/path' }); |
| 95 | + |
| 96 | + const [, , callback] = getAvailableModulesTool(); |
| 97 | + |
| 98 | + await callback({}); |
| 99 | + |
| 100 | + expect(mockGetLocalModulesMap).toHaveBeenCalledWith('@patternfly/react-core'); |
| 101 | + expect(mockGetLocalModulesMap).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | +}); |
0 commit comments