|
| 1 | +import { handler } from './install' |
| 2 | +import { downloadManifest } from '../packages' |
| 3 | +import { readClientConfig } from '../config' |
| 4 | +import { logger } from '../logger' |
| 5 | +import { ArgumentsCamelCase } from 'yargs' |
| 6 | + |
| 7 | +// Create an interface matching the structure of yargs arguments object |
| 8 | +interface InstallArgv { |
| 9 | + source: string |
| 10 | + name?: string |
| 11 | + client?: string |
| 12 | +} |
| 13 | + |
| 14 | +// Mock dependencies |
| 15 | +jest.mock('../packages', () => ({ |
| 16 | + downloadManifest: jest.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +jest.mock('../config', () => ({ |
| 20 | + readClientConfig: jest.fn(), |
| 21 | + writeClientConfig: jest.fn(), |
| 22 | + getAvailableClients: jest.fn().mockReturnValue(['claude']), |
| 23 | + clientPaths: { claude: '/path/to/claude/config' }, |
| 24 | + createPlatformCommand: jest.fn().mockImplementation((command, args) => ({ command, args })), |
| 25 | +})) |
| 26 | + |
| 27 | +jest.mock('../logger', () => ({ |
| 28 | + logger: { |
| 29 | + error: jest.fn(), |
| 30 | + info: jest.fn(), |
| 31 | + success: jest.fn(), |
| 32 | + prompt: jest.fn(), |
| 33 | + }, |
| 34 | +})) |
| 35 | + |
| 36 | +// Create a helper function to generate a properly typed yargs args object |
| 37 | +function createMockArgs(args: Partial<InstallArgv>): ArgumentsCamelCase<InstallArgv> { |
| 38 | + return { |
| 39 | + ...args, |
| 40 | + _: [], |
| 41 | + $0: 'mcpbar', |
| 42 | + } as ArgumentsCamelCase<InstallArgv> |
| 43 | +} |
| 44 | + |
| 45 | +describe('Install Command', () => { |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks() |
| 48 | + |
| 49 | + // Set up default mock returns |
| 50 | + const mockClientConfig = { mcpServers: {} } |
| 51 | + ;(readClientConfig as jest.Mock).mockResolvedValue(mockClientConfig) |
| 52 | + |
| 53 | + // Setup for downloadManifest |
| 54 | + ;(downloadManifest as jest.Mock).mockResolvedValue({ |
| 55 | + name: 'test-server', |
| 56 | + description: 'Test server description', |
| 57 | + manifest: { |
| 58 | + name: 'test-server', |
| 59 | + inputs: [], |
| 60 | + server: { |
| 61 | + command: 'echo', |
| 62 | + args: ['hello'], |
| 63 | + env: {}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + buildConfig: jest.fn().mockReturnValue({ |
| 67 | + command: 'echo', |
| 68 | + args: ['hello'], |
| 69 | + env: {}, |
| 70 | + }), |
| 71 | + }) |
| 72 | + |
| 73 | + // Setup for prompt |
| 74 | + ;(logger.prompt as jest.Mock).mockResolvedValue(true) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should transform package alias to registry URL', async () => { |
| 78 | + // Call the handler with an alias |
| 79 | + await handler( |
| 80 | + createMockArgs({ |
| 81 | + source: 'vendor/package-name', |
| 82 | + client: 'claude', |
| 83 | + }), |
| 84 | + ) |
| 85 | + |
| 86 | + // Check if downloadManifest was called with the transformed URL |
| 87 | + expect(downloadManifest).toHaveBeenCalledWith('https://esm.sh/gh/in-fun/mcpbar/registry/vendor/package-name.json') |
| 88 | + expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should not transform URLs with protocols', async () => { |
| 92 | + // Call the handler with a URL |
| 93 | + await handler( |
| 94 | + createMockArgs({ |
| 95 | + source: 'https://example.com/manifest.json', |
| 96 | + client: 'claude', |
| 97 | + }), |
| 98 | + ) |
| 99 | + |
| 100 | + // Should pass through the URL unchanged |
| 101 | + expect(downloadManifest).toHaveBeenCalledWith('https://example.com/manifest.json') |
| 102 | + expect(logger.info).not.toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should not transform absolute file paths', async () => { |
| 106 | + // Call the handler with an absolute path |
| 107 | + await handler( |
| 108 | + createMockArgs({ |
| 109 | + source: '/path/to/manifest.json', |
| 110 | + client: 'claude', |
| 111 | + }), |
| 112 | + ) |
| 113 | + |
| 114 | + // Should pass through the path unchanged |
| 115 | + expect(downloadManifest).toHaveBeenCalledWith('/path/to/manifest.json') |
| 116 | + expect(logger.info).not.toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should not transform relative file paths', async () => { |
| 120 | + // Call the handler with a relative path |
| 121 | + await handler( |
| 122 | + createMockArgs({ |
| 123 | + source: './manifests/package.json', |
| 124 | + client: 'claude', |
| 125 | + }), |
| 126 | + ) |
| 127 | + |
| 128 | + // Should pass through the path unchanged |
| 129 | + expect(downloadManifest).toHaveBeenCalledWith('./manifests/package.json') |
| 130 | + expect(logger.info).not.toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle file:// protocol', async () => { |
| 134 | + // Call the handler with file protocol |
| 135 | + await handler( |
| 136 | + createMockArgs({ |
| 137 | + source: 'file:///path/to/manifest.json', |
| 138 | + client: 'claude', |
| 139 | + }), |
| 140 | + ) |
| 141 | + |
| 142 | + // Should pass through the URL unchanged |
| 143 | + expect(downloadManifest).toHaveBeenCalledWith('file:///path/to/manifest.json') |
| 144 | + expect(logger.info).not.toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 145 | + }) |
| 146 | + |
| 147 | + it('should handle other protocols', async () => { |
| 148 | + // Call the handler with data protocol |
| 149 | + await handler( |
| 150 | + createMockArgs({ |
| 151 | + source: 'data:application/json;base64,ewogICJuYW1lIjogInRlc3Qtc2VydmVyIgp9', |
| 152 | + client: 'claude', |
| 153 | + }), |
| 154 | + ) |
| 155 | + |
| 156 | + // Should pass through the URL unchanged |
| 157 | + expect(downloadManifest).toHaveBeenCalledWith('data:application/json;base64,ewogICJuYW1lIjogInRlc3Qtc2VydmVyIgp9') |
| 158 | + expect(logger.info).not.toHaveBeenCalledWith(expect.stringContaining('Using package alias')) |
| 159 | + }) |
| 160 | +}) |
0 commit comments