52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package tui
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type recorder struct {
|
|
mu sync.Mutex
|
|
msgs []tea.Msg
|
|
}
|
|
|
|
func (r *recorder) Send(m tea.Msg) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.msgs = append(r.msgs, m)
|
|
}
|
|
|
|
func (r *recorder) get() []tea.Msg {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
out := make([]tea.Msg, len(r.msgs))
|
|
copy(out, r.msgs)
|
|
return out
|
|
}
|
|
|
|
func TestTeaReporterForwardsProgressAndLog(t *testing.T) {
|
|
rec := &recorder{}
|
|
r := NewTeaReporter(rec)
|
|
r.Progress("mod", 1, 4, "step one")
|
|
r.Log("mod", "hello world")
|
|
|
|
msgs := rec.get()
|
|
if len(msgs) != 2 {
|
|
t.Fatalf("got %d msgs", len(msgs))
|
|
}
|
|
if p, ok := msgs[0].(ProgressMsg); !ok || p.ModuleID != "mod" || p.Current != 1 || p.Total != 4 {
|
|
t.Errorf("msg[0] = %+v", msgs[0])
|
|
}
|
|
if l, ok := msgs[1].(LogMsg); !ok || l.Line != "hello world" {
|
|
t.Errorf("msg[1] = %+v", msgs[1])
|
|
}
|
|
}
|
|
|
|
func TestTeaReporterNilSenderIsSafe(t *testing.T) {
|
|
r := NewTeaReporter(nil)
|
|
r.Progress("x", 1, 1, "y") // must not panic
|
|
r.Log("x", "y")
|
|
}
|