|
| 1 | +import { CodexMCPClient } from '../codex'; |
| 2 | +import { getDefaultServerConfig } from '../../defaults'; |
| 3 | + |
| 4 | +jest.mock('node:child_process', () => ({ |
| 5 | + execSync: jest.fn(), |
| 6 | + spawnSync: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock('../../defaults', () => ({ |
| 10 | + getDefaultServerConfig: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('../../../../utils/analytics', () => ({ |
| 14 | + analytics: { |
| 15 | + captureException: jest.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +describe('CodexMCPClient', () => { |
| 20 | + const { execSync, spawnSync } = require('node:child_process'); |
| 21 | + const analytics = require('../../../../utils/analytics').analytics; |
| 22 | + const getDefaultServerConfigMock = getDefaultServerConfig as jest.Mock; |
| 23 | + |
| 24 | + const spawnSyncMock = spawnSync as jest.Mock; |
| 25 | + const execSyncMock = execSync as jest.Mock; |
| 26 | + |
| 27 | + const mockConfig = { |
| 28 | + command: 'npx', |
| 29 | + args: ['-y', 'mcp-remote@latest', 'https://example.com'], |
| 30 | + env: { |
| 31 | + POSTHOG_AUTH_HEADER: 'Bearer phx_example', |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + getDefaultServerConfigMock.mockReturnValue(mockConfig); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('isClientSupported', () => { |
| 41 | + it('returns true when codex binary is available', async () => { |
| 42 | + execSyncMock.mockReturnValue(undefined); |
| 43 | + |
| 44 | + const client = new CodexMCPClient(); |
| 45 | + await expect(client.isClientSupported()).resolves.toBe(true); |
| 46 | + expect(execSyncMock).toHaveBeenCalledWith('codex --version', { |
| 47 | + stdio: 'ignore', |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns false when codex binary is missing', async () => { |
| 52 | + execSyncMock.mockImplementation(() => { |
| 53 | + throw new Error('not found'); |
| 54 | + }); |
| 55 | + |
| 56 | + const client = new CodexMCPClient(); |
| 57 | + await expect(client.isClientSupported()).resolves.toBe(false); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('isServerInstalled', () => { |
| 62 | + it('returns true when posthog server exists', async () => { |
| 63 | + spawnSyncMock.mockReturnValue({ |
| 64 | + status: 0, |
| 65 | + stdout: JSON.stringify([{ name: 'posthog' }, { name: 'other' }]), |
| 66 | + }); |
| 67 | + |
| 68 | + const client = new CodexMCPClient(); |
| 69 | + await expect(client.isServerInstalled()).resolves.toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns false when command fails', async () => { |
| 73 | + spawnSyncMock.mockReturnValue({ status: 1, stdout: '' }); |
| 74 | + |
| 75 | + const client = new CodexMCPClient(); |
| 76 | + await expect(client.isServerInstalled()).resolves.toBe(false); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('addServer', () => { |
| 81 | + it('invokes codex mcp add with expected arguments', async () => { |
| 82 | + spawnSyncMock.mockReturnValue({ status: 0 }); |
| 83 | + |
| 84 | + const client = new CodexMCPClient(); |
| 85 | + const result = await client.addServer('phx_example'); |
| 86 | + |
| 87 | + expect(result).toEqual({ success: true }); |
| 88 | + expect(spawnSyncMock).toHaveBeenCalledWith( |
| 89 | + 'codex', |
| 90 | + [ |
| 91 | + 'mcp', |
| 92 | + 'add', |
| 93 | + 'posthog', |
| 94 | + '--env', |
| 95 | + 'POSTHOG_AUTH_HEADER=Bearer phx_example', |
| 96 | + '--', |
| 97 | + 'npx', |
| 98 | + '-y', |
| 99 | + 'mcp-remote@latest', |
| 100 | + 'https://example.com', |
| 101 | + ], |
| 102 | + { stdio: 'ignore' }, |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns false and captures exception on failure', async () => { |
| 107 | + spawnSyncMock.mockReturnValue({ status: 1 }); |
| 108 | + |
| 109 | + const client = new CodexMCPClient(); |
| 110 | + const result = await client.addServer('phx_example'); |
| 111 | + |
| 112 | + expect(result).toEqual({ success: false }); |
| 113 | + expect(analytics.captureException).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('removeServer', () => { |
| 118 | + it('invokes codex mcp remove and returns success', async () => { |
| 119 | + spawnSyncMock.mockReturnValue({ status: 0 }); |
| 120 | + |
| 121 | + const client = new CodexMCPClient(); |
| 122 | + const result = await client.removeServer(); |
| 123 | + |
| 124 | + expect(result).toEqual({ success: true }); |
| 125 | + expect(spawnSyncMock).toHaveBeenCalledWith( |
| 126 | + 'codex', |
| 127 | + ['mcp', 'remove', 'posthog'], |
| 128 | + { |
| 129 | + stdio: 'ignore', |
| 130 | + }, |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it('returns false and captures exception on failure', async () => { |
| 135 | + spawnSyncMock.mockReturnValue({ status: 1 }); |
| 136 | + |
| 137 | + const client = new CodexMCPClient(); |
| 138 | + const result = await client.removeServer(); |
| 139 | + |
| 140 | + expect(result).toEqual({ success: false }); |
| 141 | + expect(analytics.captureException).toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments