|
| 1 | +/** |
| 2 | + * Unit tests for CLI Resolution Utilities |
| 3 | + * |
| 4 | + * Tests the detection and resolution of CLI commands for different providers, |
| 5 | + * particularly the Cursor CLI which has two names: 'agent' (newer) and 'cursor-agent' (older). |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; |
| 9 | +import { execSync } from 'node:child_process'; |
| 10 | +import { |
| 11 | + commandExists, |
| 12 | + detectCursorCli, |
| 13 | + resolveCli, |
| 14 | + resetCursorCliCache, |
| 15 | + CLI_COMMAND_MAP, |
| 16 | +} from './cli-resolution.js'; |
| 17 | + |
| 18 | +// Mock child_process module |
| 19 | +vi.mock('node:child_process', () => ({ |
| 20 | + execSync: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +const mockedExecSync = vi.mocked(execSync); |
| 24 | + |
| 25 | +describe('CLI Resolution', () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + resetCursorCliCache(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + vi.restoreAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('commandExists', () => { |
| 36 | + it('returns true when command exists', () => { |
| 37 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 38 | + expect(commandExists('agent')).toBe(true); |
| 39 | + expect(mockedExecSync).toHaveBeenCalledWith('which agent', { stdio: 'ignore' }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns false when command does not exist', () => { |
| 43 | + mockedExecSync.mockImplementation(() => { |
| 44 | + throw new Error('Command not found'); |
| 45 | + }); |
| 46 | + expect(commandExists('nonexistent-cmd')).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('uses "where" on Windows', () => { |
| 50 | + const originalPlatform = process.platform; |
| 51 | + Object.defineProperty(process, 'platform', { value: 'win32' }); |
| 52 | + |
| 53 | + mockedExecSync.mockReturnValue(Buffer.from('C:\\Path\\agent.exe')); |
| 54 | + commandExists('agent'); |
| 55 | + expect(mockedExecSync).toHaveBeenCalledWith('where agent', { stdio: 'ignore' }); |
| 56 | + |
| 57 | + Object.defineProperty(process, 'platform', { value: originalPlatform }); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('detectCursorCli', () => { |
| 62 | + it('returns "agent" when agent command exists', () => { |
| 63 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 64 | + expect(detectCursorCli()).toBe('agent'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns "cursor-agent" when only cursor-agent exists', () => { |
| 68 | + mockedExecSync.mockImplementation((cmd: string) => { |
| 69 | + if (cmd === 'which agent') { |
| 70 | + throw new Error('not found'); |
| 71 | + } |
| 72 | + return Buffer.from('/usr/bin/cursor-agent'); |
| 73 | + }); |
| 74 | + expect(detectCursorCli()).toBe('cursor-agent'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns null when neither command exists', () => { |
| 78 | + mockedExecSync.mockImplementation(() => { |
| 79 | + throw new Error('not found'); |
| 80 | + }); |
| 81 | + expect(detectCursorCli()).toBeNull(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('caches the detected CLI', () => { |
| 85 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 86 | + |
| 87 | + // First call detects |
| 88 | + expect(detectCursorCli()).toBe('agent'); |
| 89 | + expect(mockedExecSync).toHaveBeenCalledTimes(1); |
| 90 | + |
| 91 | + // Second call uses cache |
| 92 | + expect(detectCursorCli()).toBe('agent'); |
| 93 | + expect(mockedExecSync).toHaveBeenCalledTimes(1); // Still 1 |
| 94 | + }); |
| 95 | + |
| 96 | + it('cache can be reset', () => { |
| 97 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 98 | + |
| 99 | + detectCursorCli(); |
| 100 | + expect(mockedExecSync).toHaveBeenCalledTimes(1); |
| 101 | + |
| 102 | + resetCursorCliCache(); |
| 103 | + |
| 104 | + detectCursorCli(); |
| 105 | + expect(mockedExecSync).toHaveBeenCalledTimes(2); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('resolveCli', () => { |
| 110 | + it('resolves "cursor" to detected CLI (agent)', () => { |
| 111 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 112 | + expect(resolveCli('cursor')).toBe('agent'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('resolves "cursor" to detected CLI (cursor-agent)', () => { |
| 116 | + mockedExecSync.mockImplementation((cmd: string) => { |
| 117 | + if (cmd === 'which agent') { |
| 118 | + throw new Error('not found'); |
| 119 | + } |
| 120 | + return Buffer.from('/usr/bin/cursor-agent'); |
| 121 | + }); |
| 122 | + expect(resolveCli('cursor')).toBe('cursor-agent'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('resolves "cursor-agent" input to detected CLI', () => { |
| 126 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 127 | + expect(resolveCli('cursor-agent')).toBe('agent'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('falls back to "agent" when cursor CLI not detected', () => { |
| 131 | + mockedExecSync.mockImplementation(() => { |
| 132 | + throw new Error('not found'); |
| 133 | + }); |
| 134 | + expect(resolveCli('cursor')).toBe('agent'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('is case insensitive for cursor', () => { |
| 138 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 139 | + expect(resolveCli('CURSOR')).toBe('agent'); |
| 140 | + expect(resolveCli('Cursor')).toBe('agent'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('resolves "google" to "gemini"', () => { |
| 144 | + expect(resolveCli('google')).toBe('gemini'); |
| 145 | + expect(resolveCli('Google')).toBe('gemini'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('returns other commands unchanged', () => { |
| 149 | + expect(resolveCli('claude')).toBe('claude'); |
| 150 | + expect(resolveCli('codex')).toBe('codex'); |
| 151 | + expect(resolveCli('gemini')).toBe('gemini'); |
| 152 | + expect(resolveCli('opencode')).toBe('opencode'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('preserves case for unknown commands', () => { |
| 156 | + expect(resolveCli('MyCustomCli')).toBe('MyCustomCli'); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('CLI_COMMAND_MAP', () => { |
| 161 | + it('maps cursor to agent', () => { |
| 162 | + expect(CLI_COMMAND_MAP['cursor']).toBe('agent'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('maps cursor-agent to agent', () => { |
| 166 | + expect(CLI_COMMAND_MAP['cursor-agent']).toBe('agent'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('maps google to gemini', () => { |
| 170 | + expect(CLI_COMMAND_MAP['google']).toBe('gemini'); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe('CLI Resolution Integration', () => { |
| 176 | + describe('Cursor CLI scenarios', () => { |
| 177 | + beforeEach(() => { |
| 178 | + vi.clearAllMocks(); |
| 179 | + resetCursorCliCache(); |
| 180 | + }); |
| 181 | + |
| 182 | + it('handles user with newer Cursor (agent available)', () => { |
| 183 | + // User has Cursor v0.50+ with "agent" CLI |
| 184 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 185 | + |
| 186 | + const resolved = resolveCli('cursor'); |
| 187 | + expect(resolved).toBe('agent'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('handles user with older Cursor (cursor-agent available)', () => { |
| 191 | + // User has older Cursor with "cursor-agent" CLI |
| 192 | + mockedExecSync.mockImplementation((cmd: string) => { |
| 193 | + if (cmd === 'which agent') { |
| 194 | + throw new Error('not found'); |
| 195 | + } |
| 196 | + return Buffer.from('/usr/bin/cursor-agent'); |
| 197 | + }); |
| 198 | + |
| 199 | + const resolved = resolveCli('cursor'); |
| 200 | + expect(resolved).toBe('cursor-agent'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('handles team spawn request with cursor CLI', () => { |
| 204 | + // Lead agent spawns worker with "cursor" - should resolve to available CLI |
| 205 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 206 | + |
| 207 | + // Simulate spawn request parsing |
| 208 | + const cli = 'cursor'; |
| 209 | + const cliParts = cli.split(' '); |
| 210 | + const rawCommand = cliParts[0]; |
| 211 | + const resolved = resolveCli(rawCommand); |
| 212 | + |
| 213 | + expect(resolved).toBe('agent'); |
| 214 | + }); |
| 215 | + |
| 216 | + it('handles explicit cursor-agent in spawn request', () => { |
| 217 | + // User explicitly requests cursor-agent - should still check availability |
| 218 | + mockedExecSync.mockReturnValue(Buffer.from('/usr/bin/agent')); |
| 219 | + |
| 220 | + const resolved = resolveCli('cursor-agent'); |
| 221 | + // Even when asking for cursor-agent, if agent is available, use agent |
| 222 | + expect(resolved).toBe('agent'); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments