github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/cli/ui/handle_fetch_image_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  	"github.com/wagoodman/go-progress"
    12  
    13  	"github.com/anchore/bubbly/bubbles/taskprogress"
    14  	stereoscopeEvent "github.com/anchore/stereoscope/pkg/event"
    15  )
    16  
    17  func TestHandler_handleFetchImage(t *testing.T) {
    18  
    19  	tests := []struct {
    20  		name       string
    21  		eventFn    func(*testing.T) partybus.Event
    22  		iterations int
    23  	}{
    24  		{
    25  			name: "fetch image in progress",
    26  			eventFn: func(t *testing.T) partybus.Event {
    27  				prog := &progress.Manual{}
    28  				prog.SetTotal(100)
    29  				prog.Set(50)
    30  
    31  				mon := struct {
    32  					progress.Progressable
    33  					progress.Stager
    34  				}{
    35  					Progressable: prog,
    36  					Stager: &progress.Stage{
    37  						Current: "current",
    38  					},
    39  				}
    40  
    41  				return partybus.Event{
    42  					Type:   stereoscopeEvent.FetchImage,
    43  					Source: "the-image",
    44  					Value:  mon,
    45  				}
    46  			},
    47  		},
    48  		{
    49  			name: "fetch image complete",
    50  			eventFn: func(t *testing.T) partybus.Event {
    51  				prog := &progress.Manual{}
    52  				prog.SetTotal(100)
    53  				prog.Set(100)
    54  				prog.SetCompleted()
    55  
    56  				mon := struct {
    57  					progress.Progressable
    58  					progress.Stager
    59  				}{
    60  					Progressable: prog,
    61  					Stager: &progress.Stage{
    62  						Current: "current",
    63  					},
    64  				}
    65  
    66  				return partybus.Event{
    67  					Type:   stereoscopeEvent.FetchImage,
    68  					Source: "the-image",
    69  					Value:  mon,
    70  				}
    71  			},
    72  		},
    73  	}
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			event := tt.eventFn(t)
    77  			handler := New(DefaultHandlerConfig())
    78  			handler.WindowSize = tea.WindowSizeMsg{
    79  				Width:  100,
    80  				Height: 80,
    81  			}
    82  
    83  			models := handler.Handle(event)
    84  			require.Len(t, models, 1)
    85  			model := models[0]
    86  
    87  			tsk, ok := model.(taskprogress.Model)
    88  			require.True(t, ok)
    89  
    90  			got := runModel(t, tsk, tt.iterations, taskprogress.TickMsg{
    91  				Time:     time.Now(),
    92  				Sequence: tsk.Sequence(),
    93  				ID:       tsk.ID(),
    94  			})
    95  			t.Log(got)
    96  			snaps.MatchSnapshot(t, got)
    97  		})
    98  	}
    99  }