changelog generator & diff tool
stormlightlabs.github.io/git-storm/
changelog
changeset
markdown
golang
git
1package testutils
2
3import (
4 "io"
5 "testing"
6 "time"
7
8 tea "github.com/charmbracelet/bubbletea"
9 "github.com/charmbracelet/x/exp/teatest"
10)
11
12// WithTermSize adjust terminal size for tests
13func WithTermSize(width, height int) teatest.TestOption {
14 return teatest.WithInitialTermSize(width, height)
15}
16
17// RunModel runs a Bubble Tea model until it completes, and returns final output.
18func RunModel(t *testing.T, m tea.Model, opts ...teatest.TestOption) io.Reader {
19 t.Helper()
20 tm := teatest.NewTestModel(t, m, opts...)
21 return tm.FinalOutput(t)
22}
23
24// RunModelWithInteraction runs a model, sends messages, waits for finish, returns final model.
25func RunModelWithInteraction(t *testing.T, m tea.Model, sendMsgs []tea.Msg, opts ...teatest.TestOption) tea.Model {
26 t.Helper()
27 tm := teatest.NewTestModel(t, m, opts...)
28
29 for _, msg := range sendMsgs {
30 tm.Send(msg)
31 }
32
33 tm.WaitFinished(t, teatest.WithFinalTimeout(2*time.Second))
34 return tm.FinalModel(t)
35}
36
37// AssertModelField asserts a field on the final model using a selector function
38func AssertModelField[T comparable](t *testing.T, finalModel tea.Model, fieldName string, getVal func(tea.Model) T, expected T) {
39 t.Helper()
40 actual := getVal(finalModel)
41 if actual != expected {
42 t.Errorf("model field %s: expected %v, got %v", fieldName, expected, actual)
43 }
44}
45
46// WaitUntil waits for a condition on the output reader within timeout
47func WaitUntil(t *testing.T, reader io.Reader, condition func([]byte) bool, timeout time.Duration) {
48 t.Helper()
49 teatest.WaitFor(t, reader, condition, teatest.WithDuration(timeout))
50}