@@ -799,4 +799,134 @@ func TestSetResetModeColorSchemeAndWin32(t *testing.T) {
799799 }
800800}
801801
802+ func TestKittyKeyboardFlagSwapOnAltBuffer (t * testing.T ) {
803+ t .Parallel ()
804+
805+ type KKState struct {
806+ Flags int
807+ MainFlags int
808+ AltFlags int
809+ }
810+
811+ tests := []struct {
812+ Name string
813+ Setup func (h * InputHandler )
814+ Input string
815+ Expected KKState
816+ }{
817+ {
818+ Name : "DECSET_1049_saves_main_flags_restores_alt" ,
819+ Setup : func (h * InputHandler ) {
820+ h .coreService .KittyKeyboard .Flags = 5
821+ h .coreService .KittyKeyboard .AltFlags = 2
822+ },
823+ Input : "\x1b [?1049h" ,
824+ Expected : KKState {Flags : 2 , MainFlags : 5 , AltFlags : 2 },
825+ },
826+ {
827+ Name : "DECSET_47_saves_main_flags_restores_alt" ,
828+ Setup : func (h * InputHandler ) {
829+ h .coreService .KittyKeyboard .Flags = 3
830+ h .coreService .KittyKeyboard .AltFlags = 1
831+ },
832+ Input : "\x1b [?47h" ,
833+ Expected : KKState {Flags : 1 , MainFlags : 3 , AltFlags : 1 },
834+ },
835+ {
836+ Name : "DECSET_1047_saves_main_flags_restores_alt" ,
837+ Setup : func (h * InputHandler ) {
838+ h .coreService .KittyKeyboard .Flags = 7
839+ h .coreService .KittyKeyboard .AltFlags = 0
840+ },
841+ Input : "\x1b [?1047h" ,
842+ Expected : KKState {Flags : 0 , MainFlags : 7 , AltFlags : 0 },
843+ },
844+ {
845+ Name : "DECRST_1049_saves_alt_flags_restores_main" ,
846+ Setup : func (h * InputHandler ) {
847+ // First switch to alt buffer
848+ h .coreService .KittyKeyboard .Flags = 5
849+ h .ParseString ("\x1b [?1049h" )
850+ // Now set flags as if app changed them on alt screen
851+ h .coreService .KittyKeyboard .Flags = 9
852+ },
853+ Input : "\x1b [?1049l" ,
854+ Expected : KKState {Flags : 5 , MainFlags : 5 , AltFlags : 9 },
855+ },
856+ {
857+ Name : "DECRST_47_saves_alt_flags_restores_main" ,
858+ Setup : func (h * InputHandler ) {
859+ h .coreService .KittyKeyboard .Flags = 4
860+ h .ParseString ("\x1b [?47h" )
861+ h .coreService .KittyKeyboard .Flags = 6
862+ },
863+ Input : "\x1b [?47l" ,
864+ Expected : KKState {Flags : 4 , MainFlags : 4 , AltFlags : 6 },
865+ },
866+ {
867+ Name : "DECRST_1047_saves_alt_flags_restores_main" ,
868+ Setup : func (h * InputHandler ) {
869+ h .coreService .KittyKeyboard .Flags = 8
870+ h .ParseString ("\x1b [?1047h" )
871+ h .coreService .KittyKeyboard .Flags = 3
872+ },
873+ Input : "\x1b [?1047l" ,
874+ Expected : KKState {Flags : 8 , MainFlags : 8 , AltFlags : 3 },
875+ },
876+ {
877+ Name : "round_trip_preserves_flags" ,
878+ Setup : func (h * InputHandler ) {
879+ h .coreService .KittyKeyboard .Flags = 10
880+ h .coreService .KittyKeyboard .AltFlags = 20
881+ },
882+ Input : "\x1b [?1049h\x1b [?1049l" ,
883+ Expected : KKState {Flags : 10 , MainFlags : 10 , AltFlags : 20 },
884+ },
885+ }
886+
887+ for _ , tc := range tests {
888+ t .Run (tc .Name , func (t * testing.T ) {
889+ t .Parallel ()
890+ h := newTestInputHandler (80 , 24 )
891+ if tc .Setup != nil {
892+ tc .Setup (h )
893+ }
894+ h .ParseString (tc .Input )
895+ kk := h .coreService .KittyKeyboard
896+ got := KKState {Flags : kk .Flags , MainFlags : kk .MainFlags , AltFlags : kk .AltFlags }
897+ if diff := cmp .Diff (tc .Expected , got ); diff != "" {
898+ t .Errorf ("mismatch (-want +got):\n %s" , diff )
899+ }
900+ })
901+ }
902+ }
802903
904+ func TestKittyKeyboardStackSwapOnAltBuffer (t * testing.T ) {
905+ t .Parallel ()
906+ h := newTestInputHandler (80 , 24 )
907+
908+ // Set up main stack
909+ h .coreService .KittyKeyboard .Flags = 1
910+ h .coreService .KittyKeyboard .MainStack = nil
911+ h .coreService .KittyKeyboard .AltStack = []int {10 , 20 }
912+
913+ // Switch to alt buffer — main stack saved, alt stack restored
914+ h .ParseString ("\x1b [?1049h" )
915+ kk := h .coreService .KittyKeyboard
916+ if len (kk .AltStack ) != 0 {
917+ t .Errorf ("expected AltStack to be swapped out (len 0), got len %d" , len (kk .AltStack ))
918+ }
919+ if len (kk .MainStack ) != 2 || kk .MainStack [0 ] != 10 || kk .MainStack [1 ] != 20 {
920+ t .Errorf ("expected MainStack [10 20], got %v" , kk .MainStack )
921+ }
922+
923+ // Switch back to normal buffer — stacks swap back
924+ h .ParseString ("\x1b [?1049l" )
925+ kk = h .coreService .KittyKeyboard
926+ if len (kk .MainStack ) != 0 {
927+ t .Errorf ("expected MainStack to be swapped back (len 0), got len %d" , len (kk .MainStack ))
928+ }
929+ if len (kk .AltStack ) != 2 || kk .AltStack [0 ] != 10 || kk .AltStack [1 ] != 20 {
930+ t .Errorf ("expected AltStack [10 20], got %v" , kk .AltStack )
931+ }
932+ }
0 commit comments