@@ -16,7 +16,9 @@ import {
1616 KeypressProvider ,
1717 useKeypressContext ,
1818 ESC_TIMEOUT ,
19+ FAST_RETURN_TIMEOUT ,
1920} from './KeypressContext.js' ;
21+ import { terminalCapabilityManager } from '../utils/terminalCapabilityManager.js' ;
2022import { useStdin } from 'ink' ;
2123import { EventEmitter } from 'node:events' ;
2224
@@ -154,6 +156,53 @@ describe('KeypressContext', () => {
154156 ) ;
155157 } ) ;
156158
159+ describe ( 'Fast return buffering' , ( ) => {
160+ let kittySpy : ReturnType < typeof vi . spyOn > ;
161+
162+ beforeEach ( ( ) => {
163+ kittySpy = vi
164+ . spyOn ( terminalCapabilityManager , 'isKittyProtocolEnabled' )
165+ . mockReturnValue ( false ) ;
166+ } ) ;
167+
168+ afterEach ( ( ) => kittySpy . mockRestore ( ) ) ;
169+
170+ it ( 'should buffer return key pressed quickly after another key' , async ( ) => {
171+ const { keyHandler } = setupKeypressTest ( ) ;
172+
173+ act ( ( ) => stdin . write ( 'a' ) ) ;
174+ expect ( keyHandler ) . toHaveBeenLastCalledWith (
175+ expect . objectContaining ( { name : 'a' } ) ,
176+ ) ;
177+
178+ act ( ( ) => stdin . write ( '\r' ) ) ;
179+
180+ expect ( keyHandler ) . toHaveBeenLastCalledWith (
181+ expect . objectContaining ( {
182+ name : '' ,
183+ sequence : '\r' ,
184+ insertable : true ,
185+ } ) ,
186+ ) ;
187+ } ) ;
188+
189+ it ( 'should NOT buffer return key if delay is long enough' , async ( ) => {
190+ const { keyHandler } = setupKeypressTest ( ) ;
191+
192+ act ( ( ) => stdin . write ( 'a' ) ) ;
193+
194+ vi . advanceTimersByTime ( FAST_RETURN_TIMEOUT + 1 ) ;
195+
196+ act ( ( ) => stdin . write ( '\r' ) ) ;
197+
198+ expect ( keyHandler ) . toHaveBeenLastCalledWith (
199+ expect . objectContaining ( {
200+ name : 'return' ,
201+ } ) ,
202+ ) ;
203+ } ) ;
204+ } ) ;
205+
157206 describe ( 'Escape key handling' , ( ) => {
158207 it ( 'should recognize escape key (keycode 27) in kitty protocol' , async ( ) => {
159208 const { keyHandler } = setupKeypressTest ( ) ;
0 commit comments