Debian Codegraph @

exp/teatest/v2/app_test.go

raw
1 package teatest_test
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "regexp"
8 "testing"
9 "time"
10
11 tea "github.com/charmbracelet/bubbletea/v2"
12 "github.com/charmbracelet/x/exp/teatest/v2"
13 )
14
15 func TestApp(t *testing.T) {
16 m := model(10)
17 tm := teatest.NewTestModel(
18 t, m,
19 teatest.WithInitialTermSize(70, 30),
20 )
21 t.Cleanup(func() {
22 if err := tm.Quit(); err != nil {
23 t.Fatal(err)
24 }
25 })
26
27 time.Sleep(time.Second + time.Millisecond*200)
28 tm.Type("I'm typing things, but it'll be ignored by my program")
29 tm.Send("ignored msg")
30 tm.Send(tea.KeyPressMsg{
31 Code: tea.KeyEnter,
32 })
33
34 if err := tm.Quit(); err != nil {
35 t.Fatal(err)
36 }
37
38 out := readBts(t, tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
39 if !regexp.MustCompile(`This program will exit in \d+ seconds`).Match(out) {
40 t.Fatalf("output does not match the given regular expression: %s", string(out))
41 }
42 teatest.RequireEqualOutput(t, out)
43
44 if tm.FinalModel(t).(model) != 9 {
45 t.Errorf("expected model to be 10, was %d", m)
46 }
47 }
48
49 func TestAppInteractive(t *testing.T) {
50 m := model(10)
51 tm := teatest.NewTestModel(
52 t, m,
53 teatest.WithInitialTermSize(70, 30),
54 )
55
56 time.Sleep(time.Second + time.Millisecond*200)
57 tm.Send("ignored msg")
58
59 if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("9 seconds")) {
60 t.Fatalf("output does not match: expected %q", string(bts))
61 }
62
63 teatest.WaitFor(t, tm.Output(), func(out []byte) bool {
64 return bytes.Contains(out, []byte("7"))
65 }, teatest.WithDuration(5*time.Second), teatest.WithCheckInterval(time.Millisecond*10))
66
67 tm.Send(tea.KeyPressMsg{
68 Code: tea.KeyEnter,
69 })
70
71 if err := tm.Quit(); err != nil {
72 t.Fatal(err)
73 }
74
75 if tm.FinalModel(t).(model) != 7 {
76 t.Errorf("expected model to be 7, was %d", m)
77 }
78 }
79
80 func readBts(tb testing.TB, r io.Reader) []byte {
81 tb.Helper()
82 bts, err := io.ReadAll(r)
83 if err != nil {
84 tb.Fatal(err)
85 }
86 return bts
87 }
88
89 // A model can be more or less any type of data. It holds all the data for a
90 // program, so often it's a struct. For this simple example, however, all
91 // we'll need is a simple integer.
92 type model int
93
94 // Init optionally returns an initial command we should run. In this case we
95 // want to start the timer.
96 func (m model) Init() tea.Cmd {
97 return tick
98 }
99
100 // Update is called when messages are received. The idea is that you inspect the
101 // message and send back an updated model accordingly. You can also return
102 // a command, which is a function that performs I/O and returns a message.
103 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
104 switch msg.(type) {
105 case tea.KeyMsg:
106 return m, tea.Quit
107 case tickMsg:
108 m--
109 if m <= 0 {
110 return m, tea.Quit
111 }
112 return m, tick
113 }
114 return m, nil
115 }
116
117 // View returns a string based on data in the model. That string which will be
118 // rendered to the terminal.
119 func (m model) View() string {
120 return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)
121 }
122
123 // Messages are events that we respond to in our Update function. This
124 // particular one indicates that the timer has ticked.
125 type tickMsg time.Time
126
127 func tick() tea.Msg {
128 time.Sleep(time.Second)
129 return tickMsg{}
130 }