File size: 4,703 Bytes
40e575e |
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 |
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { act, renderHook } from '@testing-library/react';
import { vi } from 'vitest';
import { useShellCommandProcessor } from './shellCommandProcessor';
import { Config, GeminiClient } from '@google/gemini-cli-core';
import * as fs from 'fs';
import EventEmitter from 'events';
// Mock dependencies
vi.mock('child_process');
vi.mock('fs');
vi.mock('os', () => ({
default: {
platform: () => 'linux',
tmpdir: () => '/tmp',
},
platform: () => 'linux',
tmpdir: () => '/tmp',
}));
vi.mock('@google/gemini-cli-core');
vi.mock('../utils/textUtils.js', () => ({
isBinary: vi.fn(),
}));
describe('useShellCommandProcessor', () => {
let spawnEmitter: EventEmitter;
let addItemToHistoryMock: vi.Mock;
let setPendingHistoryItemMock: vi.Mock;
let onExecMock: vi.Mock;
let onDebugMessageMock: vi.Mock;
let configMock: Config;
let geminiClientMock: GeminiClient;
beforeEach(async () => {
const { spawn } = await import('child_process');
spawnEmitter = new EventEmitter();
spawnEmitter.stdout = new EventEmitter();
spawnEmitter.stderr = new EventEmitter();
(spawn as vi.Mock).mockReturnValue(spawnEmitter);
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
vi.spyOn(fs, 'readFileSync').mockReturnValue('');
vi.spyOn(fs, 'unlinkSync').mockReturnValue(undefined);
addItemToHistoryMock = vi.fn();
setPendingHistoryItemMock = vi.fn();
onExecMock = vi.fn();
onDebugMessageMock = vi.fn();
configMock = {
getTargetDir: () => '/test/dir',
} as unknown as Config;
geminiClientMock = {
addHistory: vi.fn(),
} as unknown as GeminiClient;
});
afterEach(() => {
vi.restoreAllMocks();
});
const renderProcessorHook = () =>
renderHook(() =>
useShellCommandProcessor(
addItemToHistoryMock,
setPendingHistoryItemMock,
onExecMock,
onDebugMessageMock,
configMock,
geminiClientMock,
),
);
it('should execute a command and update history on success', async () => {
const { result } = renderProcessorHook();
const abortController = new AbortController();
act(() => {
result.current.handleShellCommand('ls -l', abortController.signal);
});
expect(onExecMock).toHaveBeenCalledTimes(1);
const execPromise = onExecMock.mock.calls[0][0];
// Simulate stdout
act(() => {
spawnEmitter.stdout.emit('data', Buffer.from('file1.txt\nfile2.txt'));
});
// Simulate process exit
act(() => {
spawnEmitter.emit('exit', 0, null);
});
await act(async () => {
await execPromise;
});
expect(addItemToHistoryMock).toHaveBeenCalledTimes(2);
expect(addItemToHistoryMock.mock.calls[1][0]).toEqual({
type: 'info',
text: 'file1.txt\nfile2.txt',
});
expect(geminiClientMock.addHistory).toHaveBeenCalledTimes(1);
});
it('should handle binary output', async () => {
const { result } = renderProcessorHook();
const abortController = new AbortController();
const { isBinary } = await import('../utils/textUtils.js');
(isBinary as vi.Mock).mockReturnValue(true);
act(() => {
result.current.handleShellCommand(
'cat myimage.png',
abortController.signal,
);
});
expect(onExecMock).toHaveBeenCalledTimes(1);
const execPromise = onExecMock.mock.calls[0][0];
act(() => {
spawnEmitter.stdout.emit('data', Buffer.from([0x89, 0x50, 0x4e, 0x47]));
});
act(() => {
spawnEmitter.emit('exit', 0, null);
});
await act(async () => {
await execPromise;
});
expect(addItemToHistoryMock).toHaveBeenCalledTimes(2);
expect(addItemToHistoryMock.mock.calls[1][0]).toEqual({
type: 'info',
text: '[Command produced binary output, which is not shown.]',
});
});
it('should handle command failure', async () => {
const { result } = renderProcessorHook();
const abortController = new AbortController();
act(() => {
result.current.handleShellCommand(
'a-bad-command',
abortController.signal,
);
});
const execPromise = onExecMock.mock.calls[0][0];
act(() => {
spawnEmitter.stderr.emit('data', Buffer.from('command not found'));
});
act(() => {
spawnEmitter.emit('exit', 127, null);
});
await act(async () => {
await execPromise;
});
expect(addItemToHistoryMock).toHaveBeenCalledTimes(2);
expect(addItemToHistoryMock.mock.calls[1][0]).toEqual({
type: 'error',
text: 'Command exited with code 127.\ncommand not found',
});
});
});
|