@@ -338,6 +338,18 @@ describe("resolveGatewayDisconnectState", () => {
338338} ) ;
339339
340340describe ( "createBackspaceDeduper" , ( ) => {
341+ function withLegacyBackspaceEnv < T > ( fn : ( ) => T ) : T {
342+ return withEnv (
343+ {
344+ WT_SESSION : undefined ,
345+ SSH_CONNECTION : undefined ,
346+ SSH_CLIENT : undefined ,
347+ SSH_TTY : undefined ,
348+ } ,
349+ fn ,
350+ ) ;
351+ }
352+
341353 function createTimedDedupe ( start = 1000 ) {
342354 let now = start ;
343355 const dedupe = createBackspaceDeduper ( {
@@ -353,27 +365,114 @@ describe("createBackspaceDeduper", () => {
353365 }
354366
355367 it ( "suppresses duplicate backspace events within the dedupe window" , ( ) => {
356- const { dedupe, advance } = createTimedDedupe ( ) ;
368+ withLegacyBackspaceEnv ( ( ) => {
369+ const { dedupe, advance } = createTimedDedupe ( ) ;
357370
358- expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
359- advance ( 1 ) ;
360- expect ( dedupe ( "\x08" ) ) . toBe ( "" ) ;
371+ expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
372+ advance ( 1 ) ;
373+ expect ( dedupe ( "\x08" ) ) . toBe ( "" ) ;
374+ } ) ;
361375 } ) ;
362376
363377 it ( "preserves backspace events outside the dedupe window" , ( ) => {
364- const { dedupe, advance } = createTimedDedupe ( ) ;
378+ withLegacyBackspaceEnv ( ( ) => {
379+ const { dedupe, advance } = createTimedDedupe ( ) ;
365380
366- expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
367- advance ( 10 ) ;
368- expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
381+ expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
382+ advance ( 10 ) ;
383+ expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
384+ } ) ;
369385 } ) ;
370386
371387 it ( "treats ASCII BS as backspace when it is the first event" , ( ) => {
372- const { dedupe, advance } = createTimedDedupe ( ) ;
388+ withLegacyBackspaceEnv ( ( ) => {
389+ const { dedupe, advance } = createTimedDedupe ( ) ;
390+
391+ expect ( dedupe ( "\x08" ) ) . toBe ( "\x08" ) ;
392+ advance ( 1 ) ;
393+ expect ( dedupe ( "\x7f" ) ) . toBe ( "" ) ;
394+ } ) ;
395+ } ) ;
396+
397+ it . each ( [
398+ {
399+ name : "consecutive DEL events" ,
400+ input : [ "\x7f" , "\x7f" ] ,
401+ expected : [ "\x7f" , "\x7f" ] ,
402+ } ,
403+ {
404+ name : "consecutive ASCII BS events" ,
405+ input : [ "\x08" , "\x08" ] ,
406+ expected : [ "\x08" , "\x08" ] ,
407+ } ,
408+ {
409+ name : "an intervening printable key" ,
410+ input : [ "\x7f" , "a" , "\x08" ] ,
411+ expected : [ "\x7f" , "a" , "\x08" ] ,
412+ } ,
413+ {
414+ name : "Kitty backspace press, repeat, and release events" ,
415+ input : [ "\x1b[127;1u" , "\x1b[127;1:2u" , "\x1b[127;1:3u" ] ,
416+ expected : [ "\x1b[127;1u" , "\x1b[127;1:2u" , "\x1b[127;1:3u" ] ,
417+ } ,
418+ {
419+ name : "bracketed paste between legacy backspaces" ,
420+ input : [ "\x7f" , "\x1b[200~\x08\x1b[201~" , "\x08" ] ,
421+ expected : [ "\x7f" , "\x1b[200~\x08\x1b[201~" , "\x08" ] ,
422+ } ,
423+ {
424+ name : "independently repeated complementary legacy pairs" ,
425+ input : [ "\x7f" , "\x08" , "\x7f" , "\x08" ] ,
426+ expected : [ "\x7f" , "" , "\x7f" , "" ] ,
427+ } ,
428+ ] ) ( "handles $name" , ( { input, expected } ) => {
429+ withLegacyBackspaceEnv ( ( ) => {
430+ const { dedupe } = createTimedDedupe ( ) ;
431+
432+ expect ( input . map ( dedupe ) ) . toEqual ( expected ) ;
433+ } ) ;
434+ } ) ;
435+
436+ it ( "preserves complementary legacy events outside the dedupe window" , ( ) => {
437+ withLegacyBackspaceEnv ( ( ) => {
438+ const { dedupe, advance } = createTimedDedupe ( ) ;
439+
440+ expect ( dedupe ( "\x7f" ) ) . toBe ( "\x7f" ) ;
441+ advance ( 10 ) ;
442+ expect ( dedupe ( "\x08" ) ) . toBe ( "\x08" ) ;
443+ } ) ;
444+ } ) ;
373445
374- expect ( dedupe ( "\x08" ) ) . toBe ( "\x08" ) ;
375- advance ( 1 ) ;
376- expect ( dedupe ( "\x7f" ) ) . toBe ( "" ) ;
446+ it ( "preserves Ctrl+Backspace in Windows Terminal" , ( ) => {
447+ withEnv (
448+ {
449+ WT_SESSION : "openclaw-tui-test" ,
450+ SSH_CONNECTION : undefined ,
451+ SSH_CLIENT : undefined ,
452+ SSH_TTY : undefined ,
453+ } ,
454+ ( ) => {
455+ const { dedupe } = createTimedDedupe ( ) ;
456+
457+ expect ( [ "\x7f" , "\x08" , "\x7f" ] . map ( dedupe ) ) . toEqual ( [ "\x7f" , "\x08" , "\x7f" ] ) ;
458+ } ,
459+ ) ;
460+ } ) ;
461+
462+ it ( "still deduplicates legacy backspace through an SSH session in Windows Terminal" , ( ) => {
463+ withEnv (
464+ {
465+ WT_SESSION : "openclaw-tui-test" ,
466+ SSH_CONNECTION : "192.0.2.10 12345 192.0.2.20 22" ,
467+ SSH_CLIENT : undefined ,
468+ SSH_TTY : undefined ,
469+ } ,
470+ ( ) => {
471+ const { dedupe } = createTimedDedupe ( ) ;
472+
473+ expect ( [ "\x7f" , "\x08" ] . map ( dedupe ) ) . toEqual ( [ "\x7f" , "" ] ) ;
474+ } ,
475+ ) ;
377476 } ) ;
378477
379478 it ( "never suppresses non-backspace keys" , ( ) => {
0 commit comments