github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/cli/ui/util_test.go (about) 1 package ui 2 3 import ( 4 "reflect" 5 "sync" 6 "testing" 7 8 tea "github.com/charmbracelet/bubbletea" 9 ) 10 11 func runModel(t testing.TB, m tea.Model, iterations int, message tea.Msg, h ...*sync.WaitGroup) tea.Model { 12 t.Helper() 13 if iterations == 0 { 14 iterations = 1 15 } 16 m.Init() 17 var cmd tea.Cmd = func() tea.Msg { 18 return message 19 } 20 21 for _, each := range h { 22 if each != nil { 23 each.Wait() 24 } 25 } 26 27 for i := 0; cmd != nil && i < iterations; i++ { 28 msgs := flatten(cmd()) 29 var nextCmds []tea.Cmd 30 var next tea.Cmd 31 for _, msg := range msgs { 32 t.Logf("Message: %+v %+v\n", reflect.TypeOf(msg), msg) 33 m, next = m.Update(msg) 34 nextCmds = append(nextCmds, next) 35 } 36 cmd = tea.Batch(nextCmds...) 37 } 38 39 return m 40 } 41 42 func flatten(ps ...tea.Msg) (msgs []tea.Msg) { 43 for _, p := range ps { 44 if bm, ok := p.(tea.BatchMsg); ok { 45 for _, m := range bm { 46 if m == nil { 47 continue 48 } 49 msgs = append(msgs, flatten(m())...) 50 } 51 } else { 52 msgs = []tea.Msg{p} 53 } 54 } 55 return msgs 56 }