From 30f5c4af4a288585e207c677b7c9d1278086f7fa Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Fri, 2 Jan 2026 15:20:26 -0800 Subject: [PATCH] fix(core): mock powershell output in shell-utils test (#15831) --- packages/core/src/utils/shell-utils.test.ts | 44 ++++++++++++--------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/core/src/utils/shell-utils.test.ts b/packages/core/src/utils/shell-utils.test.ts index 4efbedc363..443e7d9182 100644 --- a/packages/core/src/utils/shell-utils.test.ts +++ b/packages/core/src/utils/shell-utils.test.ts @@ -69,6 +69,24 @@ afterEach(() => { vi.clearAllMocks(); }); +const mockPowerShellResult = ( + commands: Array<{ name: string; text: string }>, + hasRedirection: boolean, +) => { + mockSpawnSync.mockReturnValue({ + stdout: Buffer.from( + JSON.stringify({ + success: true, + commands, + hasRedirection, + }), + ), + stderr: Buffer.from(''), + status: 0, + error: undefined, + }); +}; + describe('getCommandRoots', () => { it('should return a single command', () => { expect(getCommandRoots('ls -l')).toEqual(['ls']); @@ -197,6 +215,14 @@ describeWindowsOnly('PowerShell integration', () => { }); it('should return command roots using PowerShell AST output', () => { + mockPowerShellResult( + [ + { name: 'Get-ChildItem', text: 'Get-ChildItem' }, + { name: 'Select-Object', text: 'Select-Object Name' }, + ], + false, + ); + const roots = getCommandRoots('Get-ChildItem | Select-Object Name'); expect(roots.length).toBeGreaterThan(0); expect(roots).toContain('Get-ChildItem'); @@ -378,24 +404,6 @@ describe('hasRedirection (PowerShell via mock)', () => { process.env['ComSpec'] = 'powershell.exe'; }); - const mockPowerShellResult = ( - commands: Array<{ name: string; text: string }>, - hasRedirection: boolean, - ) => { - mockSpawnSync.mockReturnValue({ - stdout: Buffer.from( - JSON.stringify({ - success: true, - commands, - hasRedirection, - }), - ), - stderr: Buffer.from(''), - status: 0, - error: undefined, - }); - }; - it('should return true when PowerShell parser detects redirection', () => { mockPowerShellResult([{ name: 'echo', text: 'echo hello' }], true); expect(hasRedirection('echo hello > file.txt')).toBe(true);