github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/cli/ui/handle_cataloger_task_test.go (about)

     1  package ui
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	tea "github.com/charmbracelet/bubbletea"
     8  	"github.com/gkampitakis/go-snaps/snaps"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/wagoodman/go-partybus"
    11  
    12  	"github.com/anchore/bubbly/bubbles/taskprogress"
    13  	syftEvent "github.com/anchore/syft/syft/event"
    14  	"github.com/anchore/syft/syft/event/monitor"
    15  )
    16  
    17  func TestHandler_handleCatalogerTaskStarted(t *testing.T) {
    18  	tests := []struct {
    19  		name       string
    20  		eventFn    func(*testing.T) partybus.Event
    21  		iterations int
    22  	}{
    23  		{
    24  			name: "cataloging task in progress",
    25  			eventFn: func(t *testing.T) partybus.Event {
    26  				src := &monitor.CatalogerTask{
    27  					SubStatus:          false,
    28  					RemoveOnCompletion: false,
    29  					Title:              "some task title",
    30  					TitleOnCompletion:  "some task done",
    31  				}
    32  
    33  				src.SetValue("some value")
    34  
    35  				return partybus.Event{
    36  					Type:   syftEvent.CatalogerTaskStarted,
    37  					Source: src,
    38  				}
    39  			},
    40  		},
    41  		{
    42  			name: "cataloging sub task in progress",
    43  			eventFn: func(t *testing.T) partybus.Event {
    44  				src := &monitor.CatalogerTask{
    45  					SubStatus:          true,
    46  					RemoveOnCompletion: false,
    47  					Title:              "some task title",
    48  					TitleOnCompletion:  "some task done",
    49  				}
    50  
    51  				src.SetValue("some value")
    52  
    53  				return partybus.Event{
    54  					Type:   syftEvent.CatalogerTaskStarted,
    55  					Source: src,
    56  				}
    57  			},
    58  		},
    59  		{
    60  			name: "cataloging sub task complete",
    61  			eventFn: func(t *testing.T) partybus.Event {
    62  				src := &monitor.CatalogerTask{
    63  					SubStatus:          true,
    64  					RemoveOnCompletion: false,
    65  					Title:              "some task title",
    66  					TitleOnCompletion:  "some task done",
    67  				}
    68  
    69  				src.SetValue("some value")
    70  				src.SetCompleted()
    71  
    72  				return partybus.Event{
    73  					Type:   syftEvent.CatalogerTaskStarted,
    74  					Source: src,
    75  				}
    76  			},
    77  		},
    78  		{
    79  			name: "cataloging sub task complete with removal",
    80  			eventFn: func(t *testing.T) partybus.Event {
    81  				src := &monitor.CatalogerTask{
    82  					SubStatus:          true,
    83  					RemoveOnCompletion: true,
    84  					Title:              "some task title",
    85  					TitleOnCompletion:  "some task done",
    86  				}
    87  
    88  				src.SetValue("some value")
    89  				src.SetCompleted()
    90  
    91  				return partybus.Event{
    92  					Type:   syftEvent.CatalogerTaskStarted,
    93  					Source: src,
    94  				}
    95  			},
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			event := tt.eventFn(t)
   101  			handler := New(DefaultHandlerConfig())
   102  			handler.WindowSize = tea.WindowSizeMsg{
   103  				Width:  100,
   104  				Height: 80,
   105  			}
   106  
   107  			models := handler.Handle(event)
   108  			require.Len(t, models, 1)
   109  			model := models[0]
   110  
   111  			tsk, ok := model.(taskprogress.Model)
   112  			require.True(t, ok)
   113  
   114  			got := runModel(t, tsk, tt.iterations, taskprogress.TickMsg{
   115  				Time:     time.Now(),
   116  				Sequence: tsk.Sequence(),
   117  				ID:       tsk.ID(),
   118  			})
   119  			t.Log(got)
   120  			snaps.MatchSnapshot(t, got)
   121  		})
   122  	}
   123  }