Debian Codegraph @

exp/teatest/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"
12 "github.com/charmbracelet/x/exp/teatest"
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.KeyMsg{
31 Type: 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 fm := tm.FinalModel(t)
45 if fm == nil {
46 t.Fatal("expected a model, got nil")
47 }
48
49 if fmm, ok := fm.(model); !ok || fmm != 9 {
50 t.Errorf("expected model to be 9, was %d", m)
51 }
52 }
53
54 func TestAppInteractive(t *testing.T) {
55 m := model(10)
56 tm := teatest.NewTestModel(
57 t, m,
58 teatest.WithInitialTermSize(70, 30),
59 )
60
61 time.Sleep(time.Second + time.Millisecond*200)
62 tm.Send("ignored msg")
63
64 if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("This program will exit in 9 seconds")) {
65 t.Fatalf("output does not match: expected %q", string(bts))
66 }
67
68 teatest.WaitFor(t, tm.Output(), func(out []byte) bool {
69 return bytes.Contains(out, []byte("This program will exit in 7 seconds"))
70 }, teatest.WithDuration(5*time.Second), teatest.WithCheckInterval(time.Millisecond*10))
71
72 tm.Send(tea.KeyMsg{
73 Type: tea.KeyEnter,
74 })
75
76 if err := tm.Quit(); err != nil {
77 t.Fatal(err)
78 }
79
80 fm := tm.FinalModel(t)
81 if fm == nil {
82 t.Fatal("expected a model, got nil")
83 }
84
85 if fmm, ok := fm.(model); !ok || fmm != 7 {
86 t.Errorf("expected model to be 7, was %d", m)
87 }
88 }
89
90 func readBts(tb testing.TB, r io.Reader) []byte {
91 tb.Helper()
92 bts, err := io.ReadAll(r)
93 if err != nil {
94 tb.Fatal(err)
95 }
96 return bts
97 }
98
99 // A model can be more or less any type of data. It holds all the data for a
100 // program, so often it's a struct. For this simple example, however, all
101 // we'll need is a simple integer.
102 type model int
103
104 // Init optionally returns an initial command we should run. In this case we
105 // want to start the timer.
106 func (m model) Init() tea.Cmd {
107 return tick
108 }
109
110 // Update is called when messages are received. The idea is that you inspect the
111 // message and send back an updated model accordingly. You can also return
112 // a command, which is a function that performs I/O and returns a message.
113 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
114 switch msg.(type) {
115 case tea.KeyMsg:
116 return m, tea.Quit
117 case tickMsg:
118 m--
119 if m <= 0 {
120 return m, tea.Quit
121 }
122 return m, tick
123 }
124 return m, nil
125 }
126
127 // View returns a string based on data in the model. That string which will be
128 // rendered to the terminal.
129 func (m model) View() string {
130 return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)
131 }
132
133 // Messages are events that we respond to in our Update function. This
134 // particular one indicates that the timer has ticked.
135 type tickMsg time.Time
136
137 func tick() tea.Msg {
138 time.Sleep(time.Second)
139 return tickMsg{}
140 }