|
| 1 | +import { execFile } from 'child_process'; |
| 2 | +import type { MockedFunction } from 'vitest'; |
| 3 | + |
| 4 | +import { TerminalFocusManager, TerminalType } from '../../terminal/TerminalFocusManager.js'; |
| 5 | +import { getProcessTty } from '../../utils/process.js'; |
| 6 | + |
| 7 | +vi.mock('child_process', () => ({ |
| 8 | + execFile: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('../../utils/process.js', async () => { |
| 12 | + const actual = await vi.importActual<typeof import('../../utils/process.js')>('../../utils/process.js'); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + getProcessTty: vi.fn(), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +type ExecFileCb = (err: Error | null, result?: { stdout: string; stderr: string }) => void; |
| 20 | +const mockedExecFile = execFile as unknown as MockedFunction< |
| 21 | + (cmd: string, args: string[], cb: ExecFileCb) => void |
| 22 | +>; |
| 23 | +const mockedGetProcessTty = getProcessTty as MockedFunction<typeof getProcessTty>; |
| 24 | + |
| 25 | +function setExecFileHandler(handler: (cmd: string, args: string[]) => string | Error) { |
| 26 | + mockedExecFile.mockImplementation((cmd, args, cb) => { |
| 27 | + const result = handler(cmd, args); |
| 28 | + if (result instanceof Error) cb(result); |
| 29 | + else cb(null, { stdout: result, stderr: '' }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +describe('TerminalFocusManager', () => { |
| 34 | + beforeEach(() => { |
| 35 | + mockedExecFile.mockReset(); |
| 36 | + mockedGetProcessTty.mockReset(); |
| 37 | + mockedGetProcessTty.mockReturnValue('ttys000'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('finds iTerm2 when the process is listed by full app binary path', async () => { |
| 41 | + setExecFileHandler((cmd, args) => { |
| 42 | + if (cmd === 'tmux') return new Error('tmux not running'); |
| 43 | + if (cmd === 'pgrep') return new Error('pgrep did not match GUI app'); |
| 44 | + if (cmd === 'ps' && args.join(' ') === '-Axo comm') { |
| 45 | + return '/Applications/iTerm.app/Contents/MacOS/iTerm2\n'; |
| 46 | + } |
| 47 | + if (cmd === 'osascript') return 'found\n'; |
| 48 | + return ''; |
| 49 | + }); |
| 50 | + |
| 51 | + const location = await new TerminalFocusManager().findTerminal(123); |
| 52 | + |
| 53 | + expect(location).toEqual({ |
| 54 | + type: TerminalType.ITERM2, |
| 55 | + identifier: '/dev/ttys000', |
| 56 | + tty: '/dev/ttys000', |
| 57 | + }); |
| 58 | + expect(mockedExecFile).not.toHaveBeenCalledWith( |
| 59 | + 'pgrep', |
| 60 | + expect.any(Array), |
| 61 | + expect.any(Function), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('finds Terminal.app when the process is listed by app bundle path', async () => { |
| 66 | + setExecFileHandler((cmd, args) => { |
| 67 | + if (cmd === 'tmux') return new Error('tmux not running'); |
| 68 | + if (cmd === 'pgrep') return new Error('pgrep did not match GUI app'); |
| 69 | + if (cmd === 'ps' && args.join(' ') === '-Axo comm') { |
| 70 | + return '/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal\n'; |
| 71 | + } |
| 72 | + if (cmd === 'osascript') { |
| 73 | + const script = args[1] ?? ''; |
| 74 | + return script.includes('tell application "Terminal"') ? 'found\n' : ''; |
| 75 | + } |
| 76 | + return ''; |
| 77 | + }); |
| 78 | + |
| 79 | + const location = await new TerminalFocusManager().findTerminal(123); |
| 80 | + |
| 81 | + expect(location).toEqual({ |
| 82 | + type: TerminalType.TERMINAL_APP, |
| 83 | + identifier: '/dev/ttys000', |
| 84 | + tty: '/dev/ttys000', |
| 85 | + }); |
| 86 | + expect(mockedExecFile).not.toHaveBeenCalledWith( |
| 87 | + 'pgrep', |
| 88 | + expect.any(Array), |
| 89 | + expect.any(Function), |
| 90 | + ); |
| 91 | + }); |
| 92 | +}); |
0 commit comments