File size: 7,949 Bytes
52efc7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
describe,
it,
expect,
vi,
afterEach,
beforeEach,
type Mock,
} from 'vitest';
import { getIdeProcessInfo } from './process-utils.js';
import os from 'node:os';
const mockedExec = vi.hoisted(() => vi.fn());
vi.mock('node:util', () => ({
promisify: vi.fn().mockReturnValue(mockedExec),
}));
vi.mock('node:os', () => ({
default: {
platform: vi.fn(),
},
}));
describe('getIdeProcessInfo', () => {
beforeEach(() => {
Object.defineProperty(process, 'pid', { value: 1000, configurable: true });
mockedExec.mockReset();
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
describe('GEMINI_CLI_IDE_PID override', () => {
it('should use GEMINI_CLI_IDE_PID and fetch command on Unix', async () => {
(os.platform as Mock).mockReturnValue('linux');
vi.stubEnv('GEMINI_CLI_IDE_PID', '12345');
mockedExec.mockResolvedValueOnce({ stdout: '0 my-ide-command' }); // getProcessInfo result
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 12345, command: 'my-ide-command' });
expect(mockedExec).toHaveBeenCalledWith(
expect.stringContaining('ps -o ppid=,command= -p 12345'),
{ timeout: 3000 },
);
});
it('should use GEMINI_CLI_IDE_PID and fetch command on Windows', async () => {
(os.platform as Mock).mockReturnValue('win32');
vi.stubEnv('GEMINI_CLI_IDE_PID', '54321');
const processes = [
{
ProcessId: 54321,
ParentProcessId: 0,
Name: 'Code.exe',
CommandLine: 'C:\\Program Files\\VSCode\\Code.exe',
},
];
mockedExec.mockResolvedValueOnce({ stdout: JSON.stringify(processes) });
const result = await getIdeProcessInfo();
expect(result).toEqual({
pid: 54321,
command: 'C:\\Program Files\\VSCode\\Code.exe',
});
expect(mockedExec).toHaveBeenCalledWith(
expect.stringContaining(
'Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,Name,CommandLine',
),
expect.anything(),
);
});
});
describe('on Unix', () => {
it('should traverse up to find the shell and return grandparent process info', async () => {
(os.platform as Mock).mockReturnValue('linux');
// process (1000) -> shell (800) -> IDE (700)
mockedExec
.mockResolvedValueOnce({ stdout: '800 /bin/bash' }) // pid 1000 -> ppid 800 (shell)
.mockResolvedValueOnce({ stdout: '700 /usr/lib/vscode/code' }) // pid 800 -> ppid 700 (IDE)
.mockResolvedValueOnce({ stdout: '700 /usr/lib/vscode/code' }); // get command for pid 700
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 700, command: '/usr/lib/vscode/code' });
});
it('should return parent process info if grandparent lookup fails', async () => {
(os.platform as Mock).mockReturnValue('linux');
mockedExec
.mockResolvedValueOnce({ stdout: '800 /bin/bash' }) // pid 1000 -> ppid 800 (shell)
.mockRejectedValueOnce(new Error('ps failed')) // lookup for ppid of 800 fails
.mockResolvedValueOnce({ stdout: '800 /bin/bash' }); // get command for pid 800
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 800, command: '/bin/bash' });
});
it('should handle timeout when ps command hangs or rejects during getProcessInfo', async () => {
(os.platform as Mock).mockReturnValue('linux');
const timeoutError = new Error('exec timeout');
Object.assign(timeoutError, {
killed: true,
code: undefined,
signal: 'SIGTERM',
});
mockedExec.mockRejectedValue(timeoutError);
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 1000, command: '' });
});
});
describe('on Windows', () => {
it('should traverse up and find the great-grandchild of the root process', async () => {
(os.platform as Mock).mockReturnValue('win32');
// process (1000) -> powershell (900) -> code (800) -> wininit (700) -> root (0)
// Ancestors: [1000, 900, 800, 700]
// Target (great-grandchild of root): 900
const processes = [
{
ProcessId: 1000,
ParentProcessId: 900,
Name: 'node.exe',
CommandLine: 'node.exe',
},
{
ProcessId: 900,
ParentProcessId: 800,
Name: 'powershell.exe',
CommandLine: 'powershell.exe',
},
{
ProcessId: 800,
ParentProcessId: 700,
Name: 'code.exe',
CommandLine: 'code.exe',
},
{
ProcessId: 700,
ParentProcessId: 0,
Name: 'wininit.exe',
CommandLine: 'wininit.exe',
},
];
mockedExec.mockResolvedValueOnce({ stdout: JSON.stringify(processes) });
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 900, command: 'powershell.exe' });
expect(mockedExec).toHaveBeenCalledWith(
expect.stringContaining('Get-CimInstance Win32_Process'),
expect.anything(),
);
});
it('should handle short process chains', async () => {
(os.platform as Mock).mockReturnValue('win32');
// process (1000) -> root (0)
const processes = [
{
ProcessId: 1000,
ParentProcessId: 0,
Name: 'node.exe',
CommandLine: 'node.exe',
},
];
mockedExec.mockResolvedValueOnce({ stdout: JSON.stringify(processes) });
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 1000, command: 'node.exe' });
});
it('should handle PowerShell failure gracefully', async () => {
(os.platform as Mock).mockReturnValue('win32');
mockedExec.mockRejectedValueOnce(new Error('PowerShell failed'));
// Fallback to getProcessInfo for current PID
mockedExec.mockResolvedValueOnce({ stdout: '' }); // ps command fails on windows
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 1000, command: '' });
});
it('should handle malformed JSON output gracefully', async () => {
(os.platform as Mock).mockReturnValue('win32');
mockedExec.mockResolvedValueOnce({ stdout: '{"invalid":json}' });
// Fallback to getProcessInfo for current PID
mockedExec.mockResolvedValueOnce({ stdout: '' });
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 1000, command: '' });
});
it('should handle single process output from ConvertTo-Json', async () => {
(os.platform as Mock).mockReturnValue('win32');
const process = {
ProcessId: 1000,
ParentProcessId: 0,
Name: 'node.exe',
CommandLine: 'node.exe',
};
mockedExec.mockResolvedValueOnce({ stdout: JSON.stringify(process) });
const result = await getIdeProcessInfo();
expect(result).toEqual({ pid: 1000, command: 'node.exe' });
});
it('should handle missing process in map during traversal', async () => {
(os.platform as Mock).mockReturnValue('win32');
// process (1000) -> parent (900) -> missing (800)
const processes = [
{
ProcessId: 1000,
ParentProcessId: 900,
Name: 'node.exe',
CommandLine: 'node.exe',
},
{
ProcessId: 900,
ParentProcessId: 800,
Name: 'parent.exe',
CommandLine: 'parent.exe',
},
];
mockedExec.mockResolvedValueOnce({ stdout: JSON.stringify(processes) });
const result = await getIdeProcessInfo();
// Ancestors: [1000, 900]. Length < 3, returns last (900)
expect(result).toEqual({ pid: 900, command: 'parent.exe' });
});
});
});
|