Add support for debug logging of keystrokes to investigate #6227 (#6844)

Co-authored-by: Arya Gummadi <aryagummadi@google.com>
This commit is contained in:
Deepankar Sharma
2025-08-22 19:31:55 -04:00
committed by GitHub
parent fef89f5429
commit 53067fda74
7 changed files with 333 additions and 2 deletions

View File

@@ -17,6 +17,10 @@ import { EventEmitter } from 'events';
import {
KITTY_KEYCODE_ENTER,
KITTY_KEYCODE_NUMPAD_ENTER,
CHAR_CODE_ESC,
CHAR_CODE_LEFT_BRACKET,
CHAR_CODE_1,
CHAR_CODE_2,
} from '../utils/platformConstants.js';
// Mock the 'ink' module to control stdin
@@ -310,4 +314,208 @@ describe('KeypressContext - Kitty Protocol', () => {
);
});
});
describe('debug keystroke logging', () => {
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
let consoleWarnSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
consoleLogSpy.mockRestore();
consoleWarnSpy.mockRestore();
});
it('should not log keystrokes when debugKeystrokeLogging is false', async () => {
const keyHandler = vi.fn();
const wrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider
kittyProtocolEnabled={true}
debugKeystrokeLogging={false}
>
{children}
</KeypressProvider>
);
const { result } = renderHook(() => useKeypressContext(), { wrapper });
act(() => {
result.current.subscribe(keyHandler);
});
// Send a kitty sequence
act(() => {
stdin.sendKittySequence('\x1b[27u');
});
expect(keyHandler).toHaveBeenCalled();
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining('[DEBUG] Kitty'),
);
});
it('should log kitty buffer accumulation 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 complete kitty sequence for escape
act(() => {
stdin.sendKittySequence('\x1b[27u');
});
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer accumulating:',
expect.stringContaining('\x1b[27u'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty sequence parsed successfully:',
expect.stringContaining('\x1b[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 an invalid long sequence to trigger overflow
const longInvalidSequence = '\x1b[' + 'x'.repeat(100);
act(() => {
stdin.sendKittySequence(longInvalidSequence);
});
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty 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);
});
// Send incomplete kitty sequence
act(() => {
stdin.pressKey({
name: undefined,
ctrl: false,
meta: false,
shift: false,
sequence: '\x1b[1',
});
});
// Send Ctrl+C
act(() => {
stdin.pressKey({
name: 'c',
ctrl: true,
meta: false,
shift: false,
sequence: '\x03',
});
});
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer cleared on Ctrl+C:',
'\x1b[1',
);
// Verify Ctrl+C was handled
expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({
name: 'c',
ctrl: true,
}),
);
});
it('should show char codes when debugKeystrokeLogging is true even without debug mode', 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 incomplete kitty sequence
const sequence = '\x1b[12';
act(() => {
stdin.pressKey({
name: undefined,
ctrl: false,
meta: false,
shift: false,
sequence,
});
});
// Verify debug logging for accumulation
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer accumulating:',
sequence,
);
// Verify warning for char codes
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Kitty sequence buffer has char codes:',
[CHAR_CODE_ESC, CHAR_CODE_LEFT_BRACKET, CHAR_CODE_1, CHAR_CODE_2],
);
});
});
});