Fix so paste timeout protection is much less invasive. (#9284)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Jacob Richman
2025-10-01 15:21:57 -07:00
committed by GitHub
parent 8174e1d5b8
commit 6553e64431
2 changed files with 125 additions and 39 deletions

View File

@@ -14,7 +14,7 @@ import { ApprovalMode } from '@google/gemini-cli-core';
import * as path from 'node:path';
import type { CommandContext, SlashCommand } from '../commands/types.js';
import { CommandKind } from '../commands/types.js';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import type { UseShellHistoryReturn } from '../hooks/useShellHistory.js';
import { useShellHistory } from '../hooks/useShellHistory.js';
import type { UseCommandCompletionReturn } from '../hooks/useCommandCompletion.js';
@@ -24,6 +24,7 @@ import { useInputHistory } from '../hooks/useInputHistory.js';
import type { UseReverseSearchCompletionReturn } from '../hooks/useReverseSearchCompletion.js';
import { useReverseSearchCompletion } from '../hooks/useReverseSearchCompletion.js';
import * as clipboardUtils from '../utils/clipboardUtils.js';
import { useKittyKeyboardProtocol } from '../hooks/useKittyKeyboardProtocol.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import stripAnsi from 'strip-ansi';
import chalk from 'chalk';
@@ -33,6 +34,7 @@ vi.mock('../hooks/useCommandCompletion.js');
vi.mock('../hooks/useInputHistory.js');
vi.mock('../hooks/useReverseSearchCompletion.js');
vi.mock('../utils/clipboardUtils.js');
vi.mock('../hooks/useKittyKeyboardProtocol.js');
const mockSlashCommands: SlashCommand[] = [
{
@@ -97,6 +99,7 @@ describe('InputPrompt', () => {
const mockedUseReverseSearchCompletion = vi.mocked(
useReverseSearchCompletion,
);
const mockedUseKittyKeyboardProtocol = vi.mocked(useKittyKeyboardProtocol);
beforeEach(() => {
vi.resetAllMocks();
@@ -194,6 +197,10 @@ describe('InputPrompt', () => {
mockReverseSearchCompletion,
);
mockedUseKittyKeyboardProtocol.mockReturnValue({
supported: false,
});
props = {
buffer: mockBuffer,
onSubmit: vi.fn(),
@@ -1531,56 +1538,100 @@ describe('InputPrompt', () => {
});
describe('paste auto-submission protection', () => {
it('should prevent auto-submission immediately after paste with newlines', async () => {
beforeEach(() => {
vi.useFakeTimers();
mockedUseKittyKeyboardProtocol.mockReturnValue({ supported: false });
});
afterEach(() => {
vi.useRealTimers();
});
it('should prevent auto-submission immediately after an unsafe paste', async () => {
// isTerminalPasteTrusted will be false due to beforeEach setup.
props.buffer.text = 'some command';
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await wait();
// First type some text manually
stdin.write('test command');
await wait();
await vi.runAllTimersAsync();
// Simulate a paste operation (this should set the paste protection)
stdin.write(`\x1b[200~\npasted content\x1b[201~`);
await wait();
stdin.write(`\x1b[200~pasted content\x1b[201~`);
await vi.runAllTimersAsync();
// Simulate an Enter key press immediately after paste
stdin.write('\r');
await wait();
await vi.runAllTimersAsync();
// Verify that onSubmit was NOT called due to recent paste protection
expect(props.onSubmit).not.toHaveBeenCalled();
// It should call newline() instead
expect(props.buffer.newline).toHaveBeenCalled();
unmount();
});
it('should allow submission after paste protection timeout', async () => {
// Set up buffer with text for submission
props.buffer.text = 'test command';
it('should allow submission after unsafe paste protection timeout', async () => {
// isTerminalPasteTrusted will be false due to beforeEach setup.
props.buffer.text = 'pasted text';
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await wait();
await vi.runAllTimersAsync();
// Simulate a paste operation (this sets the protection)
stdin.write(`\x1b[200~\npasted\x1b[201~`);
await wait();
act(() => {
stdin.write('\x1b[200~pasted text\x1b[201~');
});
await vi.runAllTimersAsync();
// Wait for the protection timeout to naturally expire
await new Promise((resolve) => setTimeout(resolve, 600));
// Advance timers past the protection timeout
await act(async () => {
await vi.advanceTimersByTimeAsync(50);
});
// Now Enter should work normally
stdin.write('\r');
await wait();
await vi.runAllTimersAsync();
// Verify that onSubmit was called after the timeout
expect(props.onSubmit).toHaveBeenCalledWith('test command');
expect(props.onSubmit).toHaveBeenCalledWith('pasted text');
expect(props.buffer.newline).not.toHaveBeenCalled();
unmount();
});
it.each([
{
name: 'kitty',
setup: () =>
mockedUseKittyKeyboardProtocol.mockReturnValue({ supported: true }),
},
])(
'should allow immediate submission for a trusted paste ($name)',
async ({ setup }) => {
setup();
props.buffer.text = 'pasted command';
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await vi.runAllTimersAsync();
// Simulate a paste operation
stdin.write('\x1b[200~some pasted stuff\x1b[201~');
await vi.runAllTimersAsync();
// Simulate an Enter key press immediately after paste
stdin.write('\r');
await vi.runAllTimersAsync();
// Verify that onSubmit was called
expect(props.onSubmit).toHaveBeenCalledWith('pasted command');
unmount();
},
);
it('should not interfere with normal Enter key submission when no recent paste', async () => {
// Set up buffer with text before rendering to ensure submission works
props.buffer.text = 'normal command';
@@ -1588,11 +1639,11 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await wait();
await vi.runAllTimersAsync();
// Press Enter without any recent paste
stdin.write('\r');
await wait();
await vi.runAllTimersAsync();
// Verify that onSubmit was called normally
expect(props.onSubmit).toHaveBeenCalledWith('normal command');