Revamp KeypressContext (#12746)

This commit is contained in:
Tommaso Sciortino
2025-11-09 08:45:04 -08:00
committed by GitHub
parent f649948713
commit 9e4ae214a8
16 changed files with 891 additions and 1660 deletions
+1 -2
View File
@@ -184,11 +184,10 @@ export async function startInteractiveUI(
// Create wrapper component to use hooks inside render
const AppWrapper = () => {
const kittyProtocolStatus = useKittyKeyboardProtocol();
useKittyKeyboardProtocol();
return (
<SettingsContext.Provider value={settings}>
<KeypressProvider
kittyProtocolEnabled={kittyProtocolStatus.enabled}
config={config}
debugKeystrokeLogging={settings.merged.general?.debugKeystrokeLogging}
>
+1 -3
View File
@@ -120,7 +120,6 @@ export const renderWithProviders = (
settings = mockSettings,
uiState: providedUiState,
width,
kittyProtocolEnabled = true,
mouseEventsEnabled = false,
config = configProxy as unknown as Config,
}: {
@@ -128,7 +127,6 @@ export const renderWithProviders = (
settings?: LoadedSettings;
uiState?: Partial<UIState>;
width?: number;
kittyProtocolEnabled?: boolean;
mouseEventsEnabled?: boolean;
config?: Config;
} = {},
@@ -166,7 +164,7 @@ export const renderWithProviders = (
<UIStateContext.Provider value={finalUiState}>
<VimModeProvider settings={settings}>
<ShellFocusContext.Provider value={shellFocus}>
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}>
<KeypressProvider>
<MouseProvider mouseEventsEnabled={mouseEventsEnabled}>
<ScrollProvider>
<Box
@@ -1297,7 +1297,6 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: true },
);
await act(async () => {
await vi.runAllTimersAsync();
@@ -1352,6 +1351,9 @@ describe('InputPrompt', () => {
});
describe('enhanced input UX - double ESC clear functionality', () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
it('should clear buffer on second ESC press', async () => {
const onEscapePromptChange = vi.fn();
props.onEscapePromptChange = onEscapePromptChange;
@@ -1359,22 +1361,40 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
stdin.write('\x1B');
await waitFor(() => {
expect(onEscapePromptChange).toHaveBeenCalledWith(false);
});
vi.advanceTimersByTime(100);
expect(onEscapePromptChange).toHaveBeenCalledWith(false);
});
await act(async () => {
stdin.write('\x1B');
await waitFor(() => {
expect(props.buffer.setText).toHaveBeenCalledWith('');
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled();
});
vi.advanceTimersByTime(100);
expect(props.buffer.setText).toHaveBeenCalledWith('');
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled();
});
unmount();
});
it('should clear buffer on double ESC', async () => {
const onEscapePromptChange = vi.fn();
props.onEscapePromptChange = onEscapePromptChange;
props.buffer.setText('text to clear');
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await act(async () => {
stdin.write('\x1B\x1B');
vi.advanceTimersByTime(100);
expect(props.buffer.setText).toHaveBeenCalledWith('');
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled();
});
unmount();
});
@@ -1386,7 +1406,6 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
@@ -1410,14 +1429,13 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
stdin.write('\x1B');
await waitFor(() =>
expect(props.setShellModeActive).toHaveBeenCalledWith(false),
);
vi.advanceTimersByTime(100);
expect(props.setShellModeActive).toHaveBeenCalledWith(false);
});
unmount();
});
@@ -1431,26 +1449,23 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
stdin.write('\x1B');
vi.advanceTimersByTime(100);
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled();
});
await waitFor(() =>
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled(),
);
unmount();
});
it('should not call onEscapePromptChange when not provided', async () => {
vi.useFakeTimers();
props.onEscapePromptChange = undefined;
props.buffer.setText('some text');
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
await vi.runAllTimersAsync();
@@ -1463,14 +1478,12 @@ describe('InputPrompt', () => {
await vi.runAllTimersAsync();
});
vi.useRealTimers();
unmount();
});
it('should not interfere with existing keyboard shortcuts', async () => {
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
);
await act(async () => {
@@ -1535,18 +1548,13 @@ describe('InputPrompt', () => {
});
it.each([
{ name: 'standard', kittyProtocolEnabled: false, escapeSequence: '\x1B' },
{
name: 'kitty',
kittyProtocolEnabled: true,
escapeSequence: '\u001b[27u',
},
{ name: 'standard', escapeSequence: '\x1B' },
{ name: 'kitty', escapeSequence: '\u001b[27u' },
])(
'resets reverse search state on Escape ($name)',
async ({ kittyProtocolEnabled, escapeSequence }) => {
async ({ escapeSequence }) => {
const { stdin, stdout, unmount } = renderWithProviders(
<InputPrompt {...props} />,
{ kittyProtocolEnabled },
);
await act(async () => {
@@ -234,7 +234,7 @@ const renderDialog = (
},
) =>
render(
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<SettingsDialog
settings={settings}
onSelect={onSelect}
@@ -679,7 +679,7 @@ describe('SettingsDialog', () => {
const { stdin, unmount } = render(
<VimModeProvider settings={settings}>
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider>
</VimModeProvider>,
@@ -1062,7 +1062,7 @@ describe('SettingsDialog', () => {
const onSelect = vi.fn();
const { stdin, unmount, rerender } = render(
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider>,
);
@@ -1087,7 +1087,7 @@ describe('SettingsDialog', () => {
{},
);
rerender(
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider>,
);
@@ -78,7 +78,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings();
const { lastFrame } = render(
<SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<ThemeDialog {...baseProps} settings={settings} />
</KeypressProvider>
</SettingsContext.Provider>,
@@ -91,7 +91,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings();
const { lastFrame, stdin } = render(
<SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<ThemeDialog {...baseProps} settings={settings} />
</KeypressProvider>
</SettingsContext.Provider>,
@@ -113,7 +113,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings();
const { stdin } = render(
<SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<ThemeDialog
{...baseProps}
onCancel={mockOnCancel}
@@ -78,7 +78,7 @@ const TestComponent = ({
return (
<MouseProvider mouseEventsEnabled={false}>
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<ScrollProvider>
<Box flexDirection="column" width={80} height={24} padding={1}>
<Box flexGrow={1} borderStyle="round" borderColor="cyan">
@@ -9,17 +9,12 @@ import { act } from 'react';
import { renderHook } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js';
import type { Mock } from 'vitest';
import { vi } from 'vitest';
import { vi, afterAll, beforeAll } from 'vitest';
import type { Key } from './KeypressContext.js';
import {
KeypressProvider,
useKeypressContext,
DRAG_COMPLETION_TIMEOUT_MS,
KITTY_SEQUENCE_TIMEOUT_MS,
// CSI_END_O,
// SS3_END,
SINGLE_QUOTE,
DOUBLE_QUOTE,
ESC_TIMEOUT,
} from './KeypressContext.js';
import { useStdin } from 'ink';
import { EventEmitter } from 'node:events';
@@ -53,12 +48,10 @@ class MockStdin extends EventEmitter {
}
// Helper function to setup keypress test with standard configuration
const setupKeypressTest = (kittyProtocolEnabled = true) => {
const setupKeypressTest = () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}>
{children}
</KeypressProvider>
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
@@ -67,22 +60,17 @@ const setupKeypressTest = (kittyProtocolEnabled = true) => {
return { result, keyHandler };
};
describe('KeypressContext - Kitty Protocol', () => {
describe('KeypressContext', () => {
let stdin: MockStdin;
const mockSetRawMode = vi.fn();
const wrapper = ({
children,
kittyProtocolEnabled = true,
}: {
children: React.ReactNode;
kittyProtocolEnabled?: boolean;
}) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled ?? false}>
{children}
</KeypressProvider>
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider>{children}</KeypressProvider>
);
beforeAll(() => vi.useFakeTimers());
afterAll(() => vi.useRealTimers());
beforeEach(() => {
vi.clearAllMocks();
stdin = new MockStdin();
@@ -103,16 +91,13 @@ describe('KeypressContext - Kitty Protocol', () => {
sequence: '\x1b[57414u',
},
])('should recognize $name in kitty protocol', async ({ sequence }) => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => {
stdin.write(sequence);
});
act(() => stdin.write(sequence));
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'return',
kittyProtocol: true,
ctrl: false,
meta: false,
shift: false,
@@ -139,42 +124,23 @@ describe('KeypressContext - Kitty Protocol', () => {
])(
'should handle numpad enter with $modifier modifier',
async ({ sequence, expected }) => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => stdin.write(sequence));
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'return',
kittyProtocol: true,
...expected,
}),
);
},
);
it('should not process kitty sequences when kitty protocol is disabled', async () => {
const { keyHandler } = setupKeypressTest(false);
// Send kitty protocol sequence for numpad enter
act(() => {
stdin.write(`\x1b[57414u`);
});
// When kitty protocol is disabled, the sequence should be passed through
// as individual keypresses, not recognized as a single enter key
expect(keyHandler).not.toHaveBeenCalledWith(
expect.objectContaining({
name: 'return',
kittyProtocol: true,
}),
);
});
});
describe('Escape key handling', () => {
it('should recognize escape key (keycode 27) in kitty protocol', async () => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
// Send kitty protocol sequence for escape: ESC[27u
act(() => {
@@ -184,19 +150,41 @@ describe('KeypressContext - Kitty Protocol', () => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'escape',
kittyProtocol: true,
}),
);
});
it('should handle lone Escape key (keycode 27) with timeout when kitty protocol is enabled', async () => {
// Use real timers for this test to avoid issues with stream/buffer timing
vi.useRealTimers();
it('should handle double Escape', async () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={true}>
{children}
</KeypressProvider>
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => {
stdin.write('\x1b');
vi.advanceTimersByTime(10);
stdin.write('\x1b');
expect(keyHandler).not.toHaveBeenCalled();
vi.advanceTimersByTime(ESC_TIMEOUT);
expect(keyHandler).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: 'escape', meta: true }),
);
expect(keyHandler).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: 'escape', meta: true }),
);
});
});
it('should handle lone Escape key (keycode 27) with timeout when kitty protocol is enabled', async () => {
// Use real timers for this test to avoid issues with stream/buffer timing
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
@@ -204,23 +192,19 @@ describe('KeypressContext - Kitty Protocol', () => {
// Send just ESC
act(() => {
stdin.write('\x1b');
// Should be buffered initially
expect(keyHandler).not.toHaveBeenCalled();
vi.advanceTimersByTime(ESC_TIMEOUT + 10);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'escape',
meta: true,
}),
);
});
// Should be buffered initially
expect(keyHandler).not.toHaveBeenCalled();
// Wait for timeout
await waitFor(
() => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'escape',
meta: true,
}),
);
},
{ timeout: 500 },
);
});
});
@@ -254,7 +238,7 @@ describe('KeypressContext - Kitty Protocol', () => {
])(
'should recognize $name in kitty protocol',
async ({ sequence, expected }) => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => {
stdin.write(sequence);
@@ -263,7 +247,6 @@ describe('KeypressContext - Kitty Protocol', () => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
...expected,
kittyProtocol: true,
}),
);
},
@@ -341,10 +324,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={false}
>
<KeypressProvider debugKeystrokeLogging={false}>
{children}
</KeypressProvider>
);
@@ -368,10 +348,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
<KeypressProvider debugKeystrokeLogging={true}>
{children}
</KeypressProvider>
);
@@ -384,76 +361,7 @@ describe('KeypressContext - Kitty Protocol', () => {
act(() => stdin.write('\x1b[27u'));
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer accumulating:',
expect.stringContaining('"\\u001b[27u"'),
);
const parsedCall = consoleLogSpy.mock.calls.find(
(args) =>
typeof args[0] === 'string' &&
args[0].includes('[DEBUG] Sequence parsed successfully'),
);
expect(parsedCall).toBeTruthy();
expect(parsedCall?.[1]).toEqual(expect.stringContaining('\\u001b[27u'));
});
it('should log kitty buffer overflow when debugKeystrokeLogging is true', async () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
{children}
</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
// Send a long sequence starting with a valid kitty prefix to trigger overflow
const longSequence = '\x1b[1;' + '1'.repeat(100);
act(() => stdin.write(longSequence));
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer overflow, clearing:',
expect.any(String),
);
});
it('should log kitty buffer clear on Ctrl+C when debugKeystrokeLogging is true', async () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
{children}
</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(INCOMPLETE_KITTY_SEQUENCE));
// Send Ctrl+C
act(() => stdin.write('\x03'));
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer cleared on Ctrl+C:',
INCOMPLETE_KITTY_SEQUENCE,
);
// Verify Ctrl+C was handled
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'c',
ctrl: true,
}),
`[DEBUG] Raw StdIn: ${JSON.stringify('\x1b[27u')}`,
);
});
@@ -461,10 +369,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
<KeypressProvider debugKeystrokeLogging={true}>
{children}
</KeypressProvider>
);
@@ -478,14 +383,7 @@ describe('KeypressContext - Kitty Protocol', () => {
// Verify debug logging for accumulation
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer accumulating:',
JSON.stringify(INCOMPLETE_KITTY_SEQUENCE),
);
// Verify warning for char codes
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Input sequence buffer has content:',
JSON.stringify(INCOMPLETE_KITTY_SEQUENCE),
`[DEBUG] Raw StdIn: ${JSON.stringify(INCOMPLETE_KITTY_SEQUENCE)}`,
);
});
});
@@ -554,7 +452,7 @@ describe('KeypressContext - Kitty Protocol', () => {
describe('Double-tap and batching', () => {
it('should emit two delete events for double-tap CSI[3~', async () => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => stdin.write(`\x1b[3~`));
act(() => stdin.write(`\x1b[3~`));
@@ -570,7 +468,7 @@ describe('KeypressContext - Kitty Protocol', () => {
});
it('should parse two concatenated tilde-coded sequences in one chunk', async () => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => stdin.write(`\x1b[3~\x1b[5~`));
@@ -581,145 +479,6 @@ describe('KeypressContext - Kitty Protocol', () => {
expect.objectContaining({ name: 'pageup' }),
);
});
it('should ignore incomplete CSI then parse the next complete sequence', async () => {
const { keyHandler } = setupKeypressTest(true);
// Incomplete ESC sequence then a complete Delete
act(() => {
// Provide an incomplete ESC sequence chunk with a real ESC character
stdin.write('\x1b[1;');
});
act(() => stdin.write(`\x1b[3~`));
expect(keyHandler).toHaveBeenCalledTimes(1);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ name: 'delete' }),
);
});
});
});
describe('Drag and Drop Handling', () => {
let stdin: MockStdin;
const mockSetRawMode = vi.fn();
const wrapper = ({
children,
kittyProtocolEnabled = true,
}: {
children: React.ReactNode;
kittyProtocolEnabled?: boolean;
}) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}>
{children}
</KeypressProvider>
);
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
stdin = new MockStdin();
(useStdin as Mock).mockReturnValue({
stdin,
setRawMode: mockSetRawMode,
});
});
afterEach(() => {
vi.useRealTimers();
});
describe('drag start by quotes', () => {
it.each([
{ name: 'single quote', quote: SINGLE_QUOTE },
{ name: 'double quote', quote: DOUBLE_QUOTE },
])(
'should start collecting when $name arrives and not broadcast immediately',
async ({ quote }) => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(quote));
expect(keyHandler).not.toHaveBeenCalled();
},
);
});
describe('drag collection and completion', () => {
it.each([
{
name: 'collect single character inputs during drag mode',
characters: ['a'],
expectedText: 'a',
},
{
name: 'collect multiple characters and complete on timeout',
characters: ['p', 'a', 't', 'h'],
expectedText: 'path',
},
])('should $name', async ({ characters, expectedText }) => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(SINGLE_QUOTE));
characters.forEach((char) => {
act(() => stdin.write(char));
});
expect(keyHandler).not.toHaveBeenCalled();
act(() => {
vi.advanceTimersByTime(DRAG_COMPLETION_TIMEOUT_MS + 10);
});
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: '',
paste: true,
sequence: `${SINGLE_QUOTE}${expectedText}`,
}),
);
});
});
});
describe('Kitty Sequence Parsing', () => {
let stdin: MockStdin;
const mockSetRawMode = vi.fn();
const wrapper = ({
children,
kittyProtocolEnabled = true,
}: {
children: React.ReactNode;
kittyProtocolEnabled?: boolean;
}) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}>
{children}
</KeypressProvider>
);
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
stdin = new MockStdin();
(useStdin as Mock).mockReturnValue({
stdin,
setRawMode: mockSetRawMode,
});
});
afterEach(() => {
vi.useRealTimers();
});
describe('Cross-terminal Alt key handling (simulating macOS)', () => {
@@ -765,7 +524,6 @@ describe('Kitty Sequence Parsing', () => {
meta: true,
shift: false,
paste: false,
kittyProtocol: true,
},
};
} else if (terminal === 'MacTerminal') {
@@ -806,20 +564,10 @@ describe('Kitty Sequence Parsing', () => {
),
)(
'should handle Alt+$key in $terminal',
({
chunk,
expected,
kitty = true,
}: {
chunk: string;
expected: Partial<Key>;
kitty?: boolean;
}) => {
({ chunk, expected }: { chunk: string; expected: Partial<Key> }) => {
const keyHandler = vi.fn();
const testWrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={kitty}>
{children}
</KeypressProvider>
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), {
wrapper: testWrapper,
@@ -836,16 +584,8 @@ describe('Kitty Sequence Parsing', () => {
});
describe('Backslash key handling', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should treat backslash as a regular keystroke', () => {
const { keyHandler } = setupKeypressTest(true);
const { keyHandler } = setupKeypressTest();
act(() => stdin.write('\\'));
@@ -875,7 +615,7 @@ describe('Kitty Sequence Parsing', () => {
expect(keyHandler).not.toHaveBeenCalled();
// Advance time just before timeout
act(() => vi.advanceTimersByTime(KITTY_SEQUENCE_TIMEOUT_MS - 5));
act(() => vi.advanceTimersByTime(ESC_TIMEOUT - 5));
// Still shouldn't broadcast
expect(keyHandler).not.toHaveBeenCalled();
@@ -886,7 +626,7 @@ describe('Kitty Sequence Parsing', () => {
// Should now broadcast the incomplete sequence as regular input
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: '',
name: 'undefined',
sequence: INCOMPLETE_KITTY_SEQUENCE,
paste: false,
}),
@@ -926,7 +666,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({
name: 'a',
ctrl: true,
kittyProtocol: true,
}),
);
});
@@ -947,7 +686,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({
name: 'a',
ctrl: true,
kittyProtocol: true,
}),
);
expect(keyHandler).toHaveBeenNthCalledWith(
@@ -955,31 +693,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({
name: 'b',
ctrl: true,
kittyProtocol: true,
}),
);
});
it('should clear kitty buffer and timeout on Ctrl+C', async () => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(INCOMPLETE_KITTY_SEQUENCE));
// Press Ctrl+C
act(() => stdin.write('\x03'));
// Advance past timeout
act(() => vi.advanceTimersByTime(KITTY_SEQUENCE_TIMEOUT_MS + 10));
// Should only have received Ctrl+C, not the incomplete sequence
expect(keyHandler).toHaveBeenCalledTimes(1);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'c',
ctrl: true,
}),
);
});
@@ -1000,7 +713,6 @@ describe('Kitty Sequence Parsing', () => {
1,
expect.objectContaining({
name: 'return',
kittyProtocol: true,
}),
);
expect(keyHandler).toHaveBeenNthCalledWith(
@@ -1011,61 +723,31 @@ describe('Kitty Sequence Parsing', () => {
);
});
it('should not buffer sequences when kitty protocol is disabled', async () => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), {
wrapper: ({ children }) =>
wrapper({ children, kittyProtocolEnabled: false }),
});
it.each([1, ESC_TIMEOUT - 1])(
'should handle sequences arriving character by character with %s ms delay',
async (delay) => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => result.current.subscribe(keyHandler));
// Send what would be a kitty sequence
act(() => stdin.write('\x1b[13u'));
// Send kitty sequence character by character
for (const char of '\x1b[27u') {
act(() => stdin.write(char));
// Advance time but not enough to timeout
vi.advanceTimersByTime(delay);
}
// Should pass through without parsing
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
sequence: '\x1b[13u',
}),
);
expect(keyHandler).not.toHaveBeenCalledWith(
expect.objectContaining({
name: 'return',
kittyProtocol: true,
}),
);
});
it('should handle sequences arriving character by character', async () => {
vi.useRealTimers(); // Required for correct buffering timing.
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => {
result.current.subscribe(keyHandler);
});
// Send kitty sequence character by character
const sequence = '\x1b[27u'; // Escape key
for (const char of sequence) {
act(() => {
stdin.emit('data', Buffer.from(char));
// Should parse once complete
await waitFor(() => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'escape',
}),
);
});
await new Promise((resolve) => setImmediate(resolve));
}
// Should parse once complete
await waitFor(() => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'escape',
kittyProtocol: true,
}),
);
});
});
},
);
it('should reset timeout when new input arrives', async () => {
const keyHandler = vi.fn();
@@ -1095,108 +777,10 @@ describe('Kitty Sequence Parsing', () => {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'a',
kittyProtocol: true,
}),
);
});
it('should flush incomplete kitty sequence on FOCUS_IN event', async () => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(INCOMPLETE_KITTY_SEQUENCE));
// Incomplete sequence should be buffered, not broadcast
expect(keyHandler).not.toHaveBeenCalled();
// Send FOCUS_IN event
act(() => stdin.write('\x1b[I'));
// The buffered sequence should be flushed
expect(keyHandler).toHaveBeenCalledTimes(1);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: '',
sequence: INCOMPLETE_KITTY_SEQUENCE,
paste: false,
}),
);
});
it('should flush incomplete kitty sequence on FOCUS_OUT event', async () => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(INCOMPLETE_KITTY_SEQUENCE));
// Incomplete sequence should be buffered, not broadcast
expect(keyHandler).not.toHaveBeenCalled();
// Send FOCUS_OUT event
act(() => stdin.write('\x1b[O'));
// The buffered sequence should be flushed
expect(keyHandler).toHaveBeenCalledTimes(1);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: '',
sequence: INCOMPLETE_KITTY_SEQUENCE,
paste: false,
}),
);
});
it('should flush incomplete kitty sequence on paste event', async () => {
vi.useFakeTimers();
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(INCOMPLETE_KITTY_SEQUENCE));
// Incomplete sequence should be buffered, not broadcast
expect(keyHandler).not.toHaveBeenCalled();
// Send paste start sequence
act(() => stdin.write(`\x1b[200~`));
// The buffered sequence should be flushed
expect(keyHandler).toHaveBeenCalledTimes(1);
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: '',
sequence: INCOMPLETE_KITTY_SEQUENCE,
paste: false,
}),
);
// Now send some paste content and end paste to make sure paste still works
const pastedText = 'hello';
const PASTE_MODE_SUFFIX = `\x1b[201~`;
act(() => {
stdin.write(pastedText);
stdin.write(PASTE_MODE_SUFFIX);
});
act(() => vi.runAllTimers());
// The paste event should be broadcast
expect(keyHandler).toHaveBeenCalledTimes(2);
expect(keyHandler).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
paste: true,
sequence: pastedText,
}),
);
vi.useRealTimers();
});
describe('SGR Mouse Handling', () => {
it('should ignore SGR mouse sequences', async () => {
const keyHandler = vi.fn();
@@ -1248,16 +832,13 @@ describe('Kitty Sequence Parsing', () => {
// Space is 32. 32+0=32 (button 0), 32+33=65 ('A', col 33), 32+34=66 ('B', row 34)
const x11Seq = '\x1b[M AB';
act(() => {
stdin.write(x11Seq);
});
act(() => stdin.write(x11Seq));
// Should not broadcast as keystrokes
expect(keyHandler).not.toHaveBeenCalled();
});
it('should not flush slow SGR mouse sequences as garbage', async () => {
vi.useFakeTimers();
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
@@ -1267,15 +848,13 @@ describe('Kitty Sequence Parsing', () => {
act(() => stdin.write('\x1b[<'));
// Advance time past the normal kitty timeout (50ms)
act(() => vi.advanceTimersByTime(KITTY_SEQUENCE_TIMEOUT_MS + 10));
act(() => vi.advanceTimersByTime(ESC_TIMEOUT + 10));
// Send the rest
act(() => stdin.write('0;37;25M'));
// Should NOT have flushed the prefix as garbage, and should have consumed the whole thing
expect(keyHandler).not.toHaveBeenCalled();
vi.useRealTimers();
});
it('should ignore specific SGR mouse sequence sandwiched between keystrokes', async () => {
@@ -1303,61 +882,44 @@ describe('Kitty Sequence Parsing', () => {
});
describe('Ignored Sequences', () => {
describe.each([true, false])(
'with kittyProtocolEnabled = %s',
(kittyEnabled) => {
it.each([
{ name: 'Focus In', sequence: '\x1b[I' },
{ name: 'Focus Out', sequence: '\x1b[O' },
{ name: 'SGR Mouse Release', sequence: '\u001b[<0;44;18m' },
{ name: 'something mouse', sequence: '\u001b[<0;53;19M' },
{ name: 'another mouse', sequence: '\u001b[<0;29;19m' },
])('should ignore $name sequence', async ({ sequence }) => {
vi.useFakeTimers();
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={kittyEnabled}>
{children}
</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), {
wrapper,
});
act(() => result.current.subscribe(keyHandler));
for (const char of sequence) {
act(() => {
stdin.write(char);
});
await act(async () => {
vi.advanceTimersByTime(0);
});
}
act(() => {
stdin.write('HI');
});
expect(keyHandler).toHaveBeenCalledTimes(2);
expect(keyHandler).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: 'h', sequence: 'H', shift: true }),
);
expect(keyHandler).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: 'i', sequence: 'I', shift: true }),
);
vi.useRealTimers();
});
},
);
it('should handle F12 when kittyProtocolEnabled is false', async () => {
it.each([
{ name: 'Focus In', sequence: '\x1b[I' },
{ name: 'Focus Out', sequence: '\x1b[O' },
{ name: 'SGR Mouse Release', sequence: '\u001b[<0;44;18m' },
{ name: 'something mouse', sequence: '\u001b[<0;53;19M' },
{ name: 'another mouse', sequence: '\u001b[<0;29;19m' },
])('should ignore $name sequence', async ({ sequence }) => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={false}>
{children}
</KeypressProvider>
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), {
wrapper,
});
act(() => result.current.subscribe(keyHandler));
for (const char of sequence) {
act(() => stdin.write(char));
act(() => vi.advanceTimersByTime(0));
}
act(() => stdin.write('HI'));
expect(keyHandler).toHaveBeenCalledTimes(2);
expect(keyHandler).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ name: 'h', sequence: 'H', shift: true }),
);
expect(keyHandler).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ name: 'i', sequence: 'I', shift: true }),
);
});
it('should handle F12', async () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider>{children}</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
@@ -1371,4 +933,27 @@ describe('Kitty Sequence Parsing', () => {
);
});
});
describe('Individual Character Input', () => {
it.each([
'abc', // ASCII character
'你好', // Chinese characters
'こんにちは', // Japanese characters
'안녕하세요', // Korean characters
'A你B好C', // Mixed characters
])('should correctly handle string "%s"', async (inputString) => {
const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler));
act(() => stdin.write(inputString));
expect(keyHandler).toHaveBeenCalledTimes(inputString.length);
for (const char of inputString) {
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ sequence: char }),
);
}
});
});
});
File diff suppressed because it is too large Load Diff
+10 -10
View File
@@ -55,7 +55,7 @@ describe('useFocus', () => {
return null;
}
const { unmount } = render(
<KeypressProvider kittyProtocolEnabled={false}>
<KeypressProvider>
<TestComponent />
</KeypressProvider>,
);
@@ -84,7 +84,7 @@ describe('useFocus', () => {
// Simulate focus-out event
act(() => {
stdin.emit('data', Buffer.from('\x1b[O'));
stdin.emit('data', '\x1b[O');
});
// State should now be unfocused
@@ -96,13 +96,13 @@ describe('useFocus', () => {
// Simulate focus-out to set initial state to false
act(() => {
stdin.emit('data', Buffer.from('\x1b[O'));
stdin.emit('data', '\x1b[O');
});
expect(result.current).toBe(false);
// Simulate focus-in event
act(() => {
stdin.emit('data', Buffer.from('\x1b[I'));
stdin.emit('data', '\x1b[I');
});
// State should now be focused
@@ -128,22 +128,22 @@ describe('useFocus', () => {
const { result } = renderFocusHook();
act(() => {
stdin.emit('data', Buffer.from('\x1b[O'));
stdin.emit('data', '\x1b[O');
});
expect(result.current).toBe(false);
act(() => {
stdin.emit('data', Buffer.from('\x1b[O'));
stdin.emit('data', '\x1b[O');
});
expect(result.current).toBe(false);
act(() => {
stdin.emit('data', Buffer.from('\x1b[I'));
stdin.emit('data', '\x1b[I');
});
expect(result.current).toBe(true);
act(() => {
stdin.emit('data', Buffer.from('\x1b[I'));
stdin.emit('data', '\x1b[I');
});
expect(result.current).toBe(true);
});
@@ -153,13 +153,13 @@ describe('useFocus', () => {
// Simulate focus-out event
act(() => {
stdin.emit('data', Buffer.from('\x1b[O'));
stdin.emit('data', '\x1b[O');
});
expect(result.current).toBe(false);
// Simulate a keypress
act(() => {
stdin.emit('data', Buffer.from('a'));
stdin.emit('data', 'a');
});
expect(result.current).toBe(true);
});
+12 -19
View File
@@ -38,7 +38,7 @@ class MockStdin extends EventEmitter {
}
}
describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
describe(`useKeypress with useKitty=%s`, () => {
let stdin: MockStdin;
const mockSetRawMode = vi.fn();
const onKeypress = vi.fn();
@@ -50,7 +50,7 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
return null;
}
return render(
<KeypressProvider kittyProtocolEnabled={useKitty}>
<KeypressProvider>
<TestComponent />
</KeypressProvider>,
);
@@ -196,20 +196,13 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
stdin.write('do');
});
if (useKitty) {
vi.advanceTimersByTime(60); // wait for kitty timeout
expect(onKeypress).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({ sequence: '\x1B[200do' }),
);
} else {
expect(onKeypress).toHaveBeenCalledWith(
expect.objectContaining({ sequence: '\x1B[200d' }),
);
expect(onKeypress).toHaveBeenCalledWith(
expect.objectContaining({ sequence: 'o' }),
);
expect(onKeypress).toHaveBeenCalledTimes(2);
}
expect(onKeypress).toHaveBeenCalledWith(
expect.objectContaining({ sequence: '\x1B[200d' }),
);
expect(onKeypress).toHaveBeenCalledWith(
expect.objectContaining({ sequence: 'o' }),
);
expect(onKeypress).toHaveBeenCalledTimes(2);
});
it('should handle back to back pastes', () => {
@@ -249,11 +242,11 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
const pasteText = 'pasted';
await act(async () => {
stdin.write(PASTE_START.slice(0, 3));
vi.advanceTimersByTime(50);
vi.advanceTimersByTime(40);
stdin.write(PASTE_START.slice(3) + pasteText.slice(0, 3));
vi.advanceTimersByTime(50);
vi.advanceTimersByTime(40);
stdin.write(pasteText.slice(3) + PASTE_END.slice(0, 3));
vi.advanceTimersByTime(50);
vi.advanceTimersByTime(40);
stdin.write(PASTE_END.slice(3));
});
expect(onKeypress).toHaveBeenCalledWith(
@@ -1,87 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Terminal Platform Constants
*
* This file contains terminal-related constants used throughout the application,
* specifically for handling keyboard inputs and terminal protocols.
*/
/**
* Kitty keyboard protocol sequences for enhanced keyboard input.
* @see https://sw.kovidgoyal.net/kitty/keyboard-protocol/
*/
export const KITTY_CTRL_C = '[99;5u';
/**
* Kitty keyboard protocol keycodes
*/
export const KITTY_KEYCODE_ENTER = 13;
export const KITTY_KEYCODE_NUMPAD_ENTER = 57414;
export const KITTY_KEYCODE_TAB = 9;
export const KITTY_KEYCODE_BACKSPACE = 127;
/**
* Kitty modifier decoding constants
*
* In Kitty/Ghostty, the modifier parameter is encoded as (1 + bitmask).
* Some terminals also set bit 7 (i.e., add 128) when reporting event types.
*/
export const KITTY_MODIFIER_BASE = 1; // Base value per spec before bitmask decode
export const KITTY_MODIFIER_EVENT_TYPES_OFFSET = 128; // Added when event types are included
/**
* Modifier bit flags for Kitty/Xterm-style parameters.
*
* Per spec, the modifiers parameter encodes (1 + bitmask) where:
* - 1: no modifiers
* - bit 0 (1): Shift
* - bit 1 (2): Alt/Option (reported as "alt" in spec; we map to meta)
* - bit 2 (4): Ctrl
*
* Some terminals add 128 to the entire modifiers field when reporting event types.
* See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#modifiers
*/
export const MODIFIER_SHIFT_BIT = 1;
export const MODIFIER_ALT_BIT = 2;
export const MODIFIER_CTRL_BIT = 4;
/**
* Timing constants for terminal interactions
*/
export const CTRL_EXIT_PROMPT_DURATION_MS = 1000;
/**
* VS Code terminal integration constants
*/
export const VSCODE_SHIFT_ENTER_SEQUENCE = '\\\r\n';
/**
* Backslash + Enter detection window in milliseconds.
* Used to detect Shift+Enter pattern where backslash
* is followed by Enter within this timeframe.
*/
export const BACKSLASH_ENTER_DETECTION_WINDOW_MS = 5;
/**
* Maximum expected length of a Kitty keyboard protocol sequence.
* Format: ESC [ <keycode> ; <modifiers> u/~
* Example: \x1b[13;2u (Shift+Enter) = 8 chars
* Longest reasonable: \x1b[127;15~ = 11 chars (Del with all modifiers)
* We use 12 to provide a small buffer.
*/
// Increased to accommodate parameterized forms and occasional colon subfields
// while still being small enough to avoid pathological buffering.
export const MAX_KITTY_SEQUENCE_LENGTH = 32;
/**
* Character codes for common escape sequences
*/
export const CHAR_CODE_ESC = 27;
export const CHAR_CODE_LEFT_BRACKET = 91;
export const CHAR_CODE_1 = 49;
export const CHAR_CODE_2 = 50;
+3 -1
View File
@@ -29,9 +29,11 @@ import * as path from 'node:path';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { isKittyProtocolEnabled } from './kittyProtocolDetector.js';
import { VSCODE_SHIFT_ENTER_SEQUENCE } from './platformConstants.js';
import { debugLogger } from '@google/gemini-cli-core';
export const VSCODE_SHIFT_ENTER_SEQUENCE = '\\\r\n';
const execAsync = promisify(exec);
/**