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
@@ -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