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 // Create wrapper component to use hooks inside render
const AppWrapper = () => { const AppWrapper = () => {
const kittyProtocolStatus = useKittyKeyboardProtocol(); useKittyKeyboardProtocol();
return ( return (
<SettingsContext.Provider value={settings}> <SettingsContext.Provider value={settings}>
<KeypressProvider <KeypressProvider
kittyProtocolEnabled={kittyProtocolStatus.enabled}
config={config} config={config}
debugKeystrokeLogging={settings.merged.general?.debugKeystrokeLogging} debugKeystrokeLogging={settings.merged.general?.debugKeystrokeLogging}
> >
+1 -3
View File
@@ -120,7 +120,6 @@ export const renderWithProviders = (
settings = mockSettings, settings = mockSettings,
uiState: providedUiState, uiState: providedUiState,
width, width,
kittyProtocolEnabled = true,
mouseEventsEnabled = false, mouseEventsEnabled = false,
config = configProxy as unknown as Config, config = configProxy as unknown as Config,
}: { }: {
@@ -128,7 +127,6 @@ export const renderWithProviders = (
settings?: LoadedSettings; settings?: LoadedSettings;
uiState?: Partial<UIState>; uiState?: Partial<UIState>;
width?: number; width?: number;
kittyProtocolEnabled?: boolean;
mouseEventsEnabled?: boolean; mouseEventsEnabled?: boolean;
config?: Config; config?: Config;
} = {}, } = {},
@@ -166,7 +164,7 @@ export const renderWithProviders = (
<UIStateContext.Provider value={finalUiState}> <UIStateContext.Provider value={finalUiState}>
<VimModeProvider settings={settings}> <VimModeProvider settings={settings}>
<ShellFocusContext.Provider value={shellFocus}> <ShellFocusContext.Provider value={shellFocus}>
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}> <KeypressProvider>
<MouseProvider mouseEventsEnabled={mouseEventsEnabled}> <MouseProvider mouseEventsEnabled={mouseEventsEnabled}>
<ScrollProvider> <ScrollProvider>
<Box <Box
@@ -1297,7 +1297,6 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: true },
); );
await act(async () => { await act(async () => {
await vi.runAllTimersAsync(); await vi.runAllTimersAsync();
@@ -1352,6 +1351,9 @@ describe('InputPrompt', () => {
}); });
describe('enhanced input UX - double ESC clear functionality', () => { describe('enhanced input UX - double ESC clear functionality', () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
it('should clear buffer on second ESC press', async () => { it('should clear buffer on second ESC press', async () => {
const onEscapePromptChange = vi.fn(); const onEscapePromptChange = vi.fn();
props.onEscapePromptChange = onEscapePromptChange; props.onEscapePromptChange = onEscapePromptChange;
@@ -1359,22 +1361,40 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
stdin.write('\x1B'); stdin.write('\x1B');
await waitFor(() => { vi.advanceTimersByTime(100);
expect(onEscapePromptChange).toHaveBeenCalledWith(false);
}); expect(onEscapePromptChange).toHaveBeenCalledWith(false);
}); });
await act(async () => { await act(async () => {
stdin.write('\x1B'); stdin.write('\x1B');
await waitFor(() => { vi.advanceTimersByTime(100);
expect(props.buffer.setText).toHaveBeenCalledWith('');
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled(); 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(); unmount();
}); });
@@ -1386,7 +1406,6 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
@@ -1410,14 +1429,13 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
stdin.write('\x1B'); stdin.write('\x1B');
await waitFor(() => vi.advanceTimersByTime(100);
expect(props.setShellModeActive).toHaveBeenCalledWith(false),
); expect(props.setShellModeActive).toHaveBeenCalledWith(false);
}); });
unmount(); unmount();
}); });
@@ -1431,26 +1449,23 @@ describe('InputPrompt', () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
stdin.write('\x1B'); stdin.write('\x1B');
vi.advanceTimersByTime(100);
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled();
}); });
await waitFor(() =>
expect(mockCommandCompletion.resetCompletionState).toHaveBeenCalled(),
);
unmount(); unmount();
}); });
it('should not call onEscapePromptChange when not provided', async () => { it('should not call onEscapePromptChange when not provided', async () => {
vi.useFakeTimers();
props.onEscapePromptChange = undefined; props.onEscapePromptChange = undefined;
props.buffer.setText('some text'); props.buffer.setText('some text');
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
await vi.runAllTimersAsync(); await vi.runAllTimersAsync();
@@ -1463,14 +1478,12 @@ describe('InputPrompt', () => {
await vi.runAllTimersAsync(); await vi.runAllTimersAsync();
}); });
vi.useRealTimers();
unmount(); unmount();
}); });
it('should not interfere with existing keyboard shortcuts', async () => { it('should not interfere with existing keyboard shortcuts', async () => {
const { stdin, unmount } = renderWithProviders( const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled: false },
); );
await act(async () => { await act(async () => {
@@ -1535,18 +1548,13 @@ describe('InputPrompt', () => {
}); });
it.each([ it.each([
{ name: 'standard', kittyProtocolEnabled: false, escapeSequence: '\x1B' }, { name: 'standard', escapeSequence: '\x1B' },
{ { name: 'kitty', escapeSequence: '\u001b[27u' },
name: 'kitty',
kittyProtocolEnabled: true,
escapeSequence: '\u001b[27u',
},
])( ])(
'resets reverse search state on Escape ($name)', 'resets reverse search state on Escape ($name)',
async ({ kittyProtocolEnabled, escapeSequence }) => { async ({ escapeSequence }) => {
const { stdin, stdout, unmount } = renderWithProviders( const { stdin, stdout, unmount } = renderWithProviders(
<InputPrompt {...props} />, <InputPrompt {...props} />,
{ kittyProtocolEnabled },
); );
await act(async () => { await act(async () => {
@@ -234,7 +234,7 @@ const renderDialog = (
}, },
) => ) =>
render( render(
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<SettingsDialog <SettingsDialog
settings={settings} settings={settings}
onSelect={onSelect} onSelect={onSelect}
@@ -679,7 +679,7 @@ describe('SettingsDialog', () => {
const { stdin, unmount } = render( const { stdin, unmount } = render(
<VimModeProvider settings={settings}> <VimModeProvider settings={settings}>
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} /> <SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider> </KeypressProvider>
</VimModeProvider>, </VimModeProvider>,
@@ -1062,7 +1062,7 @@ describe('SettingsDialog', () => {
const onSelect = vi.fn(); const onSelect = vi.fn();
const { stdin, unmount, rerender } = render( const { stdin, unmount, rerender } = render(
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} /> <SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider>, </KeypressProvider>,
); );
@@ -1087,7 +1087,7 @@ describe('SettingsDialog', () => {
{}, {},
); );
rerender( rerender(
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<SettingsDialog settings={settings} onSelect={onSelect} /> <SettingsDialog settings={settings} onSelect={onSelect} />
</KeypressProvider>, </KeypressProvider>,
); );
@@ -78,7 +78,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings(); const settings = createMockSettings();
const { lastFrame } = render( const { lastFrame } = render(
<SettingsContext.Provider value={settings}> <SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<ThemeDialog {...baseProps} settings={settings} /> <ThemeDialog {...baseProps} settings={settings} />
</KeypressProvider> </KeypressProvider>
</SettingsContext.Provider>, </SettingsContext.Provider>,
@@ -91,7 +91,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings(); const settings = createMockSettings();
const { lastFrame, stdin } = render( const { lastFrame, stdin } = render(
<SettingsContext.Provider value={settings}> <SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<ThemeDialog {...baseProps} settings={settings} /> <ThemeDialog {...baseProps} settings={settings} />
</KeypressProvider> </KeypressProvider>
</SettingsContext.Provider>, </SettingsContext.Provider>,
@@ -113,7 +113,7 @@ describe('ThemeDialog Snapshots', () => {
const settings = createMockSettings(); const settings = createMockSettings();
const { stdin } = render( const { stdin } = render(
<SettingsContext.Provider value={settings}> <SettingsContext.Provider value={settings}>
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<ThemeDialog <ThemeDialog
{...baseProps} {...baseProps}
onCancel={mockOnCancel} onCancel={mockOnCancel}
@@ -78,7 +78,7 @@ const TestComponent = ({
return ( return (
<MouseProvider mouseEventsEnabled={false}> <MouseProvider mouseEventsEnabled={false}>
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<ScrollProvider> <ScrollProvider>
<Box flexDirection="column" width={80} height={24} padding={1}> <Box flexDirection="column" width={80} height={24} padding={1}>
<Box flexGrow={1} borderStyle="round" borderColor="cyan"> <Box flexGrow={1} borderStyle="round" borderColor="cyan">
@@ -9,17 +9,12 @@ import { act } from 'react';
import { renderHook } from '../../test-utils/render.js'; import { renderHook } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js'; import { waitFor } from '../../test-utils/async.js';
import type { Mock } from 'vitest'; import type { Mock } from 'vitest';
import { vi } from 'vitest'; import { vi, afterAll, beforeAll } from 'vitest';
import type { Key } from './KeypressContext.js'; import type { Key } from './KeypressContext.js';
import { import {
KeypressProvider, KeypressProvider,
useKeypressContext, useKeypressContext,
DRAG_COMPLETION_TIMEOUT_MS, ESC_TIMEOUT,
KITTY_SEQUENCE_TIMEOUT_MS,
// CSI_END_O,
// SS3_END,
SINGLE_QUOTE,
DOUBLE_QUOTE,
} from './KeypressContext.js'; } from './KeypressContext.js';
import { useStdin } from 'ink'; import { useStdin } from 'ink';
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';
@@ -53,12 +48,10 @@ class MockStdin extends EventEmitter {
} }
// Helper function to setup keypress test with standard configuration // Helper function to setup keypress test with standard configuration
const setupKeypressTest = (kittyProtocolEnabled = true) => { const setupKeypressTest = () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled}> <KeypressProvider>{children}</KeypressProvider>
{children}
</KeypressProvider>
); );
const { result } = renderHook(() => useKeypressContext(), { wrapper }); const { result } = renderHook(() => useKeypressContext(), { wrapper });
@@ -67,22 +60,17 @@ const setupKeypressTest = (kittyProtocolEnabled = true) => {
return { result, keyHandler }; return { result, keyHandler };
}; };
describe('KeypressContext - Kitty Protocol', () => { describe('KeypressContext', () => {
let stdin: MockStdin; let stdin: MockStdin;
const mockSetRawMode = vi.fn(); const mockSetRawMode = vi.fn();
const wrapper = ({ const wrapper = ({ children }: { children: React.ReactNode }) => (
children, <KeypressProvider>{children}</KeypressProvider>
kittyProtocolEnabled = true,
}: {
children: React.ReactNode;
kittyProtocolEnabled?: boolean;
}) => (
<KeypressProvider kittyProtocolEnabled={kittyProtocolEnabled ?? false}>
{children}
</KeypressProvider>
); );
beforeAll(() => vi.useFakeTimers());
afterAll(() => vi.useRealTimers());
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
stdin = new MockStdin(); stdin = new MockStdin();
@@ -103,16 +91,13 @@ describe('KeypressContext - Kitty Protocol', () => {
sequence: '\x1b[57414u', sequence: '\x1b[57414u',
}, },
])('should recognize $name in kitty protocol', async ({ sequence }) => { ])('should recognize $name in kitty protocol', async ({ sequence }) => {
const { keyHandler } = setupKeypressTest(true); const { keyHandler } = setupKeypressTest();
act(() => { act(() => stdin.write(sequence));
stdin.write(sequence);
});
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
name: 'return', name: 'return',
kittyProtocol: true,
ctrl: false, ctrl: false,
meta: false, meta: false,
shift: false, shift: false,
@@ -139,42 +124,23 @@ describe('KeypressContext - Kitty Protocol', () => {
])( ])(
'should handle numpad enter with $modifier modifier', 'should handle numpad enter with $modifier modifier',
async ({ sequence, expected }) => { async ({ sequence, expected }) => {
const { keyHandler } = setupKeypressTest(true); const { keyHandler } = setupKeypressTest();
act(() => stdin.write(sequence)); act(() => stdin.write(sequence));
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
name: 'return', name: 'return',
kittyProtocol: true,
...expected, ...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', () => { describe('Escape key handling', () => {
it('should recognize escape key (keycode 27) in kitty protocol', async () => { 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 // Send kitty protocol sequence for escape: ESC[27u
act(() => { act(() => {
@@ -184,19 +150,41 @@ describe('KeypressContext - Kitty Protocol', () => {
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
name: 'escape', name: 'escape',
kittyProtocol: true,
}), }),
); );
}); });
it('should handle lone Escape key (keycode 27) with timeout when kitty protocol is enabled', async () => { it('should handle double Escape', async () => {
// Use real timers for this test to avoid issues with stream/buffer timing
vi.useRealTimers();
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={true}> <KeypressProvider>{children}</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 }); const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler)); act(() => result.current.subscribe(keyHandler));
@@ -204,23 +192,19 @@ describe('KeypressContext - Kitty Protocol', () => {
// Send just ESC // Send just ESC
act(() => { act(() => {
stdin.write('\x1b'); 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', 'should recognize $name in kitty protocol',
async ({ sequence, expected }) => { async ({ sequence, expected }) => {
const { keyHandler } = setupKeypressTest(true); const { keyHandler } = setupKeypressTest();
act(() => { act(() => {
stdin.write(sequence); stdin.write(sequence);
@@ -263,7 +247,6 @@ describe('KeypressContext - Kitty Protocol', () => {
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
...expected, ...expected,
kittyProtocol: true,
}), }),
); );
}, },
@@ -341,10 +324,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider <KeypressProvider debugKeystrokeLogging={false}>
kittyProtocolEnabled={true}
debugKeystrokeLogging={false}
>
{children} {children}
</KeypressProvider> </KeypressProvider>
); );
@@ -368,10 +348,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider <KeypressProvider debugKeystrokeLogging={true}>
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
{children} {children}
</KeypressProvider> </KeypressProvider>
); );
@@ -384,76 +361,7 @@ describe('KeypressContext - Kitty Protocol', () => {
act(() => stdin.write('\x1b[27u')); act(() => stdin.write('\x1b[27u'));
expect(consoleLogSpy).toHaveBeenCalledWith( expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer accumulating:', `[DEBUG] Raw StdIn: ${JSON.stringify('\x1b[27u')}`,
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,
}),
); );
}); });
@@ -461,10 +369,7 @@ describe('KeypressContext - Kitty Protocol', () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider <KeypressProvider debugKeystrokeLogging={true}>
kittyProtocolEnabled={true}
debugKeystrokeLogging={true}
>
{children} {children}
</KeypressProvider> </KeypressProvider>
); );
@@ -478,14 +383,7 @@ describe('KeypressContext - Kitty Protocol', () => {
// Verify debug logging for accumulation // Verify debug logging for accumulation
expect(consoleLogSpy).toHaveBeenCalledWith( expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Input buffer accumulating:', `[DEBUG] Raw StdIn: ${JSON.stringify(INCOMPLETE_KITTY_SEQUENCE)}`,
JSON.stringify(INCOMPLETE_KITTY_SEQUENCE),
);
// Verify warning for char codes
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Input sequence buffer has content:',
JSON.stringify(INCOMPLETE_KITTY_SEQUENCE),
); );
}); });
}); });
@@ -554,7 +452,7 @@ describe('KeypressContext - Kitty Protocol', () => {
describe('Double-tap and batching', () => { describe('Double-tap and batching', () => {
it('should emit two delete events for double-tap CSI[3~', async () => { 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~`));
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 () => { 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~`)); act(() => stdin.write(`\x1b[3~\x1b[5~`));
@@ -581,145 +479,6 @@ describe('KeypressContext - Kitty Protocol', () => {
expect.objectContaining({ name: 'pageup' }), 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)', () => { describe('Cross-terminal Alt key handling (simulating macOS)', () => {
@@ -765,7 +524,6 @@ describe('Kitty Sequence Parsing', () => {
meta: true, meta: true,
shift: false, shift: false,
paste: false, paste: false,
kittyProtocol: true,
}, },
}; };
} else if (terminal === 'MacTerminal') { } else if (terminal === 'MacTerminal') {
@@ -806,20 +564,10 @@ describe('Kitty Sequence Parsing', () => {
), ),
)( )(
'should handle Alt+$key in $terminal', 'should handle Alt+$key in $terminal',
({ ({ chunk, expected }: { chunk: string; expected: Partial<Key> }) => {
chunk,
expected,
kitty = true,
}: {
chunk: string;
expected: Partial<Key>;
kitty?: boolean;
}) => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const testWrapper = ({ children }: { children: React.ReactNode }) => ( const testWrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={kitty}> <KeypressProvider>{children}</KeypressProvider>
{children}
</KeypressProvider>
); );
const { result } = renderHook(() => useKeypressContext(), { const { result } = renderHook(() => useKeypressContext(), {
wrapper: testWrapper, wrapper: testWrapper,
@@ -836,16 +584,8 @@ describe('Kitty Sequence Parsing', () => {
}); });
describe('Backslash key handling', () => { describe('Backslash key handling', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should treat backslash as a regular keystroke', () => { it('should treat backslash as a regular keystroke', () => {
const { keyHandler } = setupKeypressTest(true); const { keyHandler } = setupKeypressTest();
act(() => stdin.write('\\')); act(() => stdin.write('\\'));
@@ -875,7 +615,7 @@ describe('Kitty Sequence Parsing', () => {
expect(keyHandler).not.toHaveBeenCalled(); expect(keyHandler).not.toHaveBeenCalled();
// Advance time just before timeout // Advance time just before timeout
act(() => vi.advanceTimersByTime(KITTY_SEQUENCE_TIMEOUT_MS - 5)); act(() => vi.advanceTimersByTime(ESC_TIMEOUT - 5));
// Still shouldn't broadcast // Still shouldn't broadcast
expect(keyHandler).not.toHaveBeenCalled(); expect(keyHandler).not.toHaveBeenCalled();
@@ -886,7 +626,7 @@ describe('Kitty Sequence Parsing', () => {
// Should now broadcast the incomplete sequence as regular input // Should now broadcast the incomplete sequence as regular input
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
name: '', name: 'undefined',
sequence: INCOMPLETE_KITTY_SEQUENCE, sequence: INCOMPLETE_KITTY_SEQUENCE,
paste: false, paste: false,
}), }),
@@ -926,7 +666,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({ expect.objectContaining({
name: 'a', name: 'a',
ctrl: true, ctrl: true,
kittyProtocol: true,
}), }),
); );
}); });
@@ -947,7 +686,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({ expect.objectContaining({
name: 'a', name: 'a',
ctrl: true, ctrl: true,
kittyProtocol: true,
}), }),
); );
expect(keyHandler).toHaveBeenNthCalledWith( expect(keyHandler).toHaveBeenNthCalledWith(
@@ -955,31 +693,6 @@ describe('Kitty Sequence Parsing', () => {
expect.objectContaining({ expect.objectContaining({
name: 'b', name: 'b',
ctrl: true, 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, 1,
expect.objectContaining({ expect.objectContaining({
name: 'return', name: 'return',
kittyProtocol: true,
}), }),
); );
expect(keyHandler).toHaveBeenNthCalledWith( expect(keyHandler).toHaveBeenNthCalledWith(
@@ -1011,61 +723,31 @@ describe('Kitty Sequence Parsing', () => {
); );
}); });
it('should not buffer sequences when kitty protocol is disabled', async () => { it.each([1, ESC_TIMEOUT - 1])(
const keyHandler = vi.fn(); 'should handle sequences arriving character by character with %s ms delay',
const { result } = renderHook(() => useKeypressContext(), { async (delay) => {
wrapper: ({ children }) => const keyHandler = vi.fn();
wrapper({ children, kittyProtocolEnabled: false }), const { result } = renderHook(() => useKeypressContext(), { wrapper });
});
act(() => result.current.subscribe(keyHandler)); act(() => result.current.subscribe(keyHandler));
// Send what would be a kitty sequence // Send kitty sequence character by character
act(() => stdin.write('\x1b[13u')); 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 // Should parse once complete
expect(keyHandler).toHaveBeenCalledWith( await waitFor(() => {
expect.objectContaining({ expect(keyHandler).toHaveBeenCalledWith(
sequence: '\x1b[13u', expect.objectContaining({
}), name: 'escape',
); }),
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));
}); });
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 () => { it('should reset timeout when new input arrives', async () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
@@ -1095,108 +777,10 @@ describe('Kitty Sequence Parsing', () => {
expect(keyHandler).toHaveBeenCalledWith( expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
name: 'a', 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', () => { describe('SGR Mouse Handling', () => {
it('should ignore SGR mouse sequences', async () => { it('should ignore SGR mouse sequences', async () => {
const keyHandler = vi.fn(); 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) // 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'; const x11Seq = '\x1b[M AB';
act(() => { act(() => stdin.write(x11Seq));
stdin.write(x11Seq);
});
// Should not broadcast as keystrokes // Should not broadcast as keystrokes
expect(keyHandler).not.toHaveBeenCalled(); expect(keyHandler).not.toHaveBeenCalled();
}); });
it('should not flush slow SGR mouse sequences as garbage', async () => { it('should not flush slow SGR mouse sequences as garbage', async () => {
vi.useFakeTimers();
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const { result } = renderHook(() => useKeypressContext(), { wrapper }); const { result } = renderHook(() => useKeypressContext(), { wrapper });
@@ -1267,15 +848,13 @@ describe('Kitty Sequence Parsing', () => {
act(() => stdin.write('\x1b[<')); act(() => stdin.write('\x1b[<'));
// Advance time past the normal kitty timeout (50ms) // 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 // Send the rest
act(() => stdin.write('0;37;25M')); act(() => stdin.write('0;37;25M'));
// Should NOT have flushed the prefix as garbage, and should have consumed the whole thing // Should NOT have flushed the prefix as garbage, and should have consumed the whole thing
expect(keyHandler).not.toHaveBeenCalled(); expect(keyHandler).not.toHaveBeenCalled();
vi.useRealTimers();
}); });
it('should ignore specific SGR mouse sequence sandwiched between keystrokes', async () => { it('should ignore specific SGR mouse sequence sandwiched between keystrokes', async () => {
@@ -1303,61 +882,44 @@ describe('Kitty Sequence Parsing', () => {
}); });
describe('Ignored Sequences', () => { describe('Ignored Sequences', () => {
describe.each([true, false])( it.each([
'with kittyProtocolEnabled = %s', { name: 'Focus In', sequence: '\x1b[I' },
(kittyEnabled) => { { name: 'Focus Out', sequence: '\x1b[O' },
it.each([ { name: 'SGR Mouse Release', sequence: '\u001b[<0;44;18m' },
{ name: 'Focus In', sequence: '\x1b[I' }, { name: 'something mouse', sequence: '\u001b[<0;53;19M' },
{ name: 'Focus Out', sequence: '\x1b[O' }, { name: 'another mouse', sequence: '\u001b[<0;29;19m' },
{ name: 'SGR Mouse Release', sequence: '\u001b[<0;44;18m' }, ])('should ignore $name sequence', async ({ sequence }) => {
{ 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 () => {
const keyHandler = vi.fn(); const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => ( const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>{children}</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 }); const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => result.current.subscribe(keyHandler)); 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; return null;
} }
const { unmount } = render( const { unmount } = render(
<KeypressProvider kittyProtocolEnabled={false}> <KeypressProvider>
<TestComponent /> <TestComponent />
</KeypressProvider>, </KeypressProvider>,
); );
@@ -84,7 +84,7 @@ describe('useFocus', () => {
// Simulate focus-out event // Simulate focus-out event
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[O')); stdin.emit('data', '\x1b[O');
}); });
// State should now be unfocused // State should now be unfocused
@@ -96,13 +96,13 @@ describe('useFocus', () => {
// Simulate focus-out to set initial state to false // Simulate focus-out to set initial state to false
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[O')); stdin.emit('data', '\x1b[O');
}); });
expect(result.current).toBe(false); expect(result.current).toBe(false);
// Simulate focus-in event // Simulate focus-in event
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[I')); stdin.emit('data', '\x1b[I');
}); });
// State should now be focused // State should now be focused
@@ -128,22 +128,22 @@ describe('useFocus', () => {
const { result } = renderFocusHook(); const { result } = renderFocusHook();
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[O')); stdin.emit('data', '\x1b[O');
}); });
expect(result.current).toBe(false); expect(result.current).toBe(false);
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[O')); stdin.emit('data', '\x1b[O');
}); });
expect(result.current).toBe(false); expect(result.current).toBe(false);
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[I')); stdin.emit('data', '\x1b[I');
}); });
expect(result.current).toBe(true); expect(result.current).toBe(true);
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[I')); stdin.emit('data', '\x1b[I');
}); });
expect(result.current).toBe(true); expect(result.current).toBe(true);
}); });
@@ -153,13 +153,13 @@ describe('useFocus', () => {
// Simulate focus-out event // Simulate focus-out event
act(() => { act(() => {
stdin.emit('data', Buffer.from('\x1b[O')); stdin.emit('data', '\x1b[O');
}); });
expect(result.current).toBe(false); expect(result.current).toBe(false);
// Simulate a keypress // Simulate a keypress
act(() => { act(() => {
stdin.emit('data', Buffer.from('a')); stdin.emit('data', 'a');
}); });
expect(result.current).toBe(true); 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; let stdin: MockStdin;
const mockSetRawMode = vi.fn(); const mockSetRawMode = vi.fn();
const onKeypress = vi.fn(); const onKeypress = vi.fn();
@@ -50,7 +50,7 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
return null; return null;
} }
return render( return render(
<KeypressProvider kittyProtocolEnabled={useKitty}> <KeypressProvider>
<TestComponent /> <TestComponent />
</KeypressProvider>, </KeypressProvider>,
); );
@@ -196,20 +196,13 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
stdin.write('do'); stdin.write('do');
}); });
if (useKitty) { expect(onKeypress).toHaveBeenCalledWith(
vi.advanceTimersByTime(60); // wait for kitty timeout expect.objectContaining({ sequence: '\x1B[200d' }),
expect(onKeypress).toHaveBeenCalledExactlyOnceWith( );
expect.objectContaining({ sequence: '\x1B[200do' }), expect(onKeypress).toHaveBeenCalledWith(
); expect.objectContaining({ sequence: 'o' }),
} else { );
expect(onKeypress).toHaveBeenCalledWith( expect(onKeypress).toHaveBeenCalledTimes(2);
expect.objectContaining({ sequence: '\x1B[200d' }),
);
expect(onKeypress).toHaveBeenCalledWith(
expect.objectContaining({ sequence: 'o' }),
);
expect(onKeypress).toHaveBeenCalledTimes(2);
}
}); });
it('should handle back to back pastes', () => { it('should handle back to back pastes', () => {
@@ -249,11 +242,11 @@ describe.each([true, false])(`useKeypress with useKitty=%s`, (useKitty) => {
const pasteText = 'pasted'; const pasteText = 'pasted';
await act(async () => { await act(async () => {
stdin.write(PASTE_START.slice(0, 3)); stdin.write(PASTE_START.slice(0, 3));
vi.advanceTimersByTime(50); vi.advanceTimersByTime(40);
stdin.write(PASTE_START.slice(3) + pasteText.slice(0, 3)); 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)); stdin.write(pasteText.slice(3) + PASTE_END.slice(0, 3));
vi.advanceTimersByTime(50); vi.advanceTimersByTime(40);
stdin.write(PASTE_END.slice(3)); stdin.write(PASTE_END.slice(3));
}); });
expect(onKeypress).toHaveBeenCalledWith( 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 { exec } from 'node:child_process';
import { promisify } from 'node:util'; import { promisify } from 'node:util';
import { isKittyProtocolEnabled } from './kittyProtocolDetector.js'; import { isKittyProtocolEnabled } from './kittyProtocolDetector.js';
import { VSCODE_SHIFT_ENTER_SEQUENCE } from './platformConstants.js';
import { debugLogger } from '@google/gemini-cli-core'; import { debugLogger } from '@google/gemini-cli-core';
export const VSCODE_SHIFT_ENTER_SEQUENCE = '\\\r\n';
const execAsync = promisify(exec); const execAsync = promisify(exec);
/** /**
@@ -18,7 +18,6 @@ import type {
MalformedJsonResponseEvent, MalformedJsonResponseEvent,
IdeConnectionEvent, IdeConnectionEvent,
ConversationFinishedEvent, ConversationFinishedEvent,
KittySequenceOverflowEvent,
ChatCompressionEvent, ChatCompressionEvent,
FileOperationEvent, FileOperationEvent,
InvalidChunkEvent, InvalidChunkEvent,
@@ -847,20 +846,6 @@ export class ClearcutLogger {
this.flushIfNeeded(); this.flushIfNeeded();
} }
logKittySequenceOverflowEvent(event: KittySequenceOverflowEvent): void {
const data: EventValue[] = [
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_KITTY_SEQUENCE_LENGTH,
value: event.sequence_length.toString(),
},
];
this.enqueueLogEvent(
this.createLogEvent(EventNames.KITTY_SEQUENCE_OVERFLOW, data),
);
this.flushIfNeeded();
}
logEndSessionEvent(): void { logEndSessionEvent(): void {
// Flush immediately on session end. // Flush immediately on session end.
this.enqueueLogEvent(this.createLogEvent(EventNames.END_SESSION, [])); this.enqueueLogEvent(this.createLogEvent(EventNames.END_SESSION, []));
-2
View File
@@ -38,7 +38,6 @@ export {
logFlashFallback, logFlashFallback,
logSlashCommand, logSlashCommand,
logConversationFinishedEvent, logConversationFinishedEvent,
logKittySequenceOverflow,
logChatCompression, logChatCompression,
logToolOutputTruncated, logToolOutputTruncated,
logExtensionEnable, logExtensionEnable,
@@ -59,7 +58,6 @@ export {
StartSessionEvent, StartSessionEvent,
ToolCallEvent, ToolCallEvent,
ConversationFinishedEvent, ConversationFinishedEvent,
KittySequenceOverflowEvent,
ToolOutputTruncatedEvent, ToolOutputTruncatedEvent,
WebFetchFallbackAttemptEvent, WebFetchFallbackAttemptEvent,
ToolCallDecision, ToolCallDecision,
-15
View File
@@ -28,7 +28,6 @@ import type {
LoopDetectionDisabledEvent, LoopDetectionDisabledEvent,
SlashCommandEvent, SlashCommandEvent,
ConversationFinishedEvent, ConversationFinishedEvent,
KittySequenceOverflowEvent,
ChatCompressionEvent, ChatCompressionEvent,
MalformedJsonResponseEvent, MalformedJsonResponseEvent,
InvalidChunkEvent, InvalidChunkEvent,
@@ -398,20 +397,6 @@ export function logChatCompression(
}); });
} }
export function logKittySequenceOverflow(
config: Config,
event: KittySequenceOverflowEvent,
): void {
ClearcutLogger.getInstance(config)?.logKittySequenceOverflowEvent(event);
if (!isTelemetrySdkInitialized()) return;
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: event.toLogBody(),
attributes: event.toOpenTelemetryAttributes(config),
};
logger.emit(logRecord);
}
export function logMalformedJsonResponse( export function logMalformedJsonResponse(
config: Config, config: Config,
event: MalformedJsonResponseEvent, event: MalformedJsonResponseEvent,
-29
View File
@@ -949,34 +949,6 @@ export class ConversationFinishedEvent {
} }
} }
export class KittySequenceOverflowEvent {
'event.name': 'kitty_sequence_overflow';
'event.timestamp': string; // ISO 8601
sequence_length: number;
truncated_sequence: string;
constructor(sequence_length: number, truncated_sequence: string) {
this['event.name'] = 'kitty_sequence_overflow';
this['event.timestamp'] = new Date().toISOString();
this.sequence_length = sequence_length;
// Truncate to first 20 chars for logging (avoid logging sensitive data)
this.truncated_sequence = truncated_sequence.substring(0, 20);
}
toOpenTelemetryAttributes(config: Config): LogAttributes {
return {
...getCommonAttributes(config),
'event.name': this['event.name'],
'event.timestamp': this['event.timestamp'],
sequence_length: this.sequence_length,
truncated_sequence: this.truncated_sequence,
};
}
toLogBody(): string {
return `Kitty sequence buffer overflow: ${this.sequence_length} bytes`;
}
}
export const EVENT_FILE_OPERATION = 'gemini_cli.file_operation'; export const EVENT_FILE_OPERATION = 'gemini_cli.file_operation';
export class FileOperationEvent implements BaseTelemetryEvent { export class FileOperationEvent implements BaseTelemetryEvent {
'event.name': 'file_operation'; 'event.name': 'file_operation';
@@ -1444,7 +1416,6 @@ export type TelemetryEvent =
| LoopDetectedEvent | LoopDetectedEvent
| LoopDetectionDisabledEvent | LoopDetectionDisabledEvent
| NextSpeakerCheckEvent | NextSpeakerCheckEvent
| KittySequenceOverflowEvent
| MalformedJsonResponseEvent | MalformedJsonResponseEvent
| IdeConnectionEvent | IdeConnectionEvent
| ConversationFinishedEvent | ConversationFinishedEvent