Skip to content
Myr

cursed_renderer_test.go

This file

Path

cursed_renderer_test.go

203 lines · 5.4 KiB · Go

Last changed by

fix: restore kitty keyboard stack on exit (#1750)deepakganesh78before adoption0d3e281

History of this file

Take it

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 1–3why, one step back
1package tea
23import (

fix: restore kitty keyboard stack on exit (#1750)deepakganesh78before adoption0d3e281

line 4why, one step back
4 "bytes"

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 5–6why, one step back
5 "fmt"
6 "io"

fix: restore kitty keyboard stack on exit (#1750)deepakganesh78before adoption0d3e281

line 7why, one step back
7 "strings"

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 8–9why, one step back
8 "testing"
9 "time"

fix: restore kitty keyboard stack on exit (#1750)deepakganesh78before adoption0d3e281

lines 10–11why, one step back
10
11 "github.com/charmbracelet/x/ansi"

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 12–82why, one step back
12)
1314type mouseRaceModel struct {15 i int16}1718func (m *mouseRaceModel) Init() Cmd { return nil }1920func (m *mouseRaceModel) Update(msg Msg) (Model, Cmd) {21 switch msg.(type) {22 case MouseClickMsg, MouseMotionMsg, MouseWheelMsg:23 m.i++24 }25 return m, nil26}2728func (m *mouseRaceModel) View() View {29 return View{30 Content: fmt.Sprintf("tick-%d\n", m.i),31 MouseMode: MouseModeCellMotion,32 }33}3435// Fixes: https://github.com/charmbracelet/bubbletea/issues/169036func TestCursedRenderer_mouseVsFlush(t *testing.T) {37 t.Parallel()3839 pr, pw := io.Pipe()40 defer func() { _ = pw.Close() }()4142 m := &mouseRaceModel{}43 p := NewProgram(44 m,45 WithContext(t.Context()),46 WithInput(pr),47 WithOutput(io.Discard),48 WithEnvironment([]string{49 "TERM=xterm-256color",50 "TERM_PROGRAM=Apple_Terminal",51 }),52 WithoutSignals(),53 WithWindowSize(80, 24),54 )5556 runDone := make(chan struct{})57 go func() {58 defer close(runDone)59 _, _ = p.Run()60 }()6162 time.Sleep(150 * time.Millisecond)6364 const iterations = 10065 for i := range iterations {66 switch i % 4 {67 case 0:68 p.Send(MouseClickMsg{X: i % 80, Y: i % 24, Button: MouseLeft})69 case 1:70 p.Send(MouseMotionMsg{X: i % 80, Y: i % 24})71 case 2:72 p.Send(MouseWheelMsg{X: 0, Y: 0, Button: MouseWheelUp})73 default:74 p.Send(MouseReleaseMsg{X: i % 80, Y: i % 24, Button: MouseLeft})75 }76 }7778 p.Quit()79 select {80 case <-runDone:81 case <-time.After(5 * time.Second):82 t.Fatal("program did not exit after Quit")

fix: restore kitty keyboard stack on exit (#1750)deepakganesh78before adoption0d3e281

lines 83–201why, one step back
83 }
84}8586func assertInOrder(t *testing.T, got string, wants ...string) {87 t.Helper()88 rest := got89 for _, want := range wants {90 idx := strings.Index(rest, want)91 if idx < 0 {92 t.Fatalf("expected %q to appear after the previous sequences in %q", want, got)93 }94 rest = rest[idx+len(want):]95 }96}9798func TestCursedRenderer_restoresKittyKeyboardStack(t *testing.T) {99 t.Parallel()100101 var out bytes.Buffer102 r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)103 r.start()104105 view := NewView("hello")106 view.KeyboardEnhancements.ReportEventTypes = true107 pushMain := ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements))108 pop := ansi.PopKittyKeyboard(1)109110 render := func(v View) {111 t.Helper()112 r.render(v)113 if err := r.flush(false); err != nil {114 t.Fatal(err)115 }116 }117118 render(view)119120 // Stop the renderer (as on suspend or ExecProcess) and start it again:121 // close pops the stack entry, start pushes it back.122 if err := r.close(); err != nil {123 t.Fatal(err)124 }125 r.start()126127 // Enter and leave the alt screen. The terminal keeps a separate Kitty128 // keyboard stack per screen, so each screen gets its own push and pop.129 view.AltScreen = true130 render(view)131 view.AltScreen = false132 render(view)133134 if err := r.close(); err != nil {135 t.Fatal(err)136 }137138 got := out.String()139 // The flags are pushed once per screen activation: the first flush,140 // the flush after the renderer was restarted, and on each screen141 // switch. start() itself does not write to [out].142 if n := strings.Count(got, pushMain); n != 4 {143 t.Fatalf("expected kitty keyboard protocol to be pushed 4 times with %q (%d times), got %q", pushMain, n, got)144 }145 // One pop per stop/start cycle and per screen switch: closing pops the146 // current screen's entry, and switching screens pops the entry of the147 // screen being left.148 if n := strings.Count(got, pop); n != 4 {149 t.Fatalf("expected kitty keyboard protocol to be popped 4 times with %q (%d times), got %q", pop, n, got)150 }151 // Every pop must come after a push: the stack is balanced when pushes152 // and pops alternate. The resumed flush pushes twice in a row (once in153 // start(), once in flush()), and both entries are popped afterwards.154 assertInOrder(t, got,155 pushMain, pop, // close pops the entry pushed by the first flush156 pop, // entering the alt screen pops the resumed entry157 pushMain, pop, // leaving the alt screen158 pushMain, pop, // the resumed main screen entry and the final close159 )160 if strings.Contains(got, ansi.KittyKeyboard(0, 1)) {161 t.Fatalf("expected kitty keyboard protocol not to be reset in-place with %q, got %q", ansi.KittyKeyboard(0, 1), got)162 }163}164165func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {166 t.Parallel()167168 var out bytes.Buffer169 r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)170171 render := func(v View) {172 t.Helper()173 r.render(v)174 if err := r.flush(false); err != nil {175 t.Fatal(err)176 }177 }178179 view := NewView("hello")180 render(view)181182 // Changing the enhancement flags without switching screens updates the183 // current stack entry in place instead of pushing a new one.184 changed := view185 changed.KeyboardEnhancements.ReportEventTypes = true186 render(changed)187188 wantUpdate := ansi.KittyKeyboard(keyboardEnhancementsFlags(changed.KeyboardEnhancements), 1)189 got := out.String()190 if !strings.Contains(got, wantUpdate) {191 t.Fatalf("expected kitty keyboard flags to be updated in place with %q, got %q", wantUpdate, got)192 }193 assertInOrder(t, got,194 ansi.PushKittyKeyboard(keyboardEnhancementsFlags(view.KeyboardEnhancements)),195 wantUpdate,196 )197 if strings.Contains(got, ansi.PopKittyKeyboard(1)) {198 t.Fatalf("expected kitty keyboard protocol not to be popped with %q, got %q", ansi.PopKittyKeyboard(1), got)199 }200 if n := strings.Count(got, ansi.PushKittyKeyboard(0)); n > 1 {201 t.Fatalf("expected kitty keyboard protocol to be pushed once, got %d pushes in %q", n, got)

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 202–203why, one step back
202 }
203}