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

     1  package ui
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	tea "github.com/charmbracelet/bubbletea"
     9  	"github.com/gkampitakis/go-snaps/snaps"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/wagoodman/go-partybus"
    12  	"github.com/wagoodman/go-progress"
    13  
    14  	"github.com/anchore/bubbly/bubbles/taskprogress"
    15  	syftEvent "github.com/anchore/syft/syft/event"
    16  	"github.com/anchore/syft/syft/event/monitor"
    17  )
    18  
    19  func TestHandler_handleAttestationStarted(t *testing.T) {
    20  
    21  	tests := []struct {
    22  		name       string
    23  		eventFn    func(*testing.T) partybus.Event
    24  		iterations int
    25  	}{
    26  		{
    27  			name: "attesting in progress",
    28  			// note: this model depends on a background reader. Multiple iterations ensures that the
    29  			// reader has time to at least start and process the test fixture before the runModel
    30  			// test harness completes (which is a fake event loop anyway).
    31  			iterations: 1,
    32  			eventFn: func(t *testing.T) partybus.Event {
    33  				reader := strings.NewReader("contents\nof\nstuff!")
    34  
    35  				src := monitor.GenericTask{
    36  					Title: monitor.Title{
    37  						Default:      "Create a thing",
    38  						WhileRunning: "Creating a thing",
    39  						OnSuccess:    "Created a thing",
    40  					},
    41  					Context: "running a thing",
    42  				}
    43  
    44  				mon := progress.NewManual(-1)
    45  				mon.Set(50)
    46  
    47  				value := &monitor.ShellProgress{
    48  					Reader:       reader,
    49  					Progressable: mon,
    50  				}
    51  
    52  				return partybus.Event{
    53  					Type:   syftEvent.AttestationStarted,
    54  					Source: src,
    55  					Value:  value,
    56  				}
    57  			},
    58  		},
    59  		{
    60  			name: "attesting complete",
    61  			// note: this model depends on a background reader. Multiple iterations ensures that the
    62  			// reader has time to at least start and process the test fixture before the runModel
    63  			// test harness completes (which is a fake event loop anyway).
    64  			iterations: 1,
    65  			eventFn: func(t *testing.T) partybus.Event {
    66  				reader := strings.NewReader("contents\nof\nstuff!")
    67  
    68  				src := monitor.GenericTask{
    69  					Title: monitor.Title{
    70  						Default:      "Create a thing",
    71  						WhileRunning: "Creating a thing",
    72  						OnSuccess:    "Created a thing",
    73  					},
    74  					Context: "running a thing",
    75  				}
    76  
    77  				mon := progress.NewManual(-1)
    78  				mon.Set(50)
    79  				mon.SetCompleted()
    80  
    81  				value := &monitor.ShellProgress{
    82  					Reader:       reader,
    83  					Progressable: mon,
    84  				}
    85  
    86  				return partybus.Event{
    87  					Type:   syftEvent.AttestationStarted,
    88  					Source: src,
    89  					Value:  value,
    90  				}
    91  			},
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			event := tt.eventFn(t)
    97  			handler := New(DefaultHandlerConfig())
    98  			handler.WindowSize = tea.WindowSizeMsg{
    99  				Width:  100,
   100  				Height: 80,
   101  			}
   102  
   103  			models := handler.Handle(event)
   104  			require.Len(t, models, 2)
   105  
   106  			t.Run("task line", func(t *testing.T) {
   107  				tsk, ok := models[0].(taskprogress.Model)
   108  				require.True(t, ok)
   109  
   110  				got := runModel(t, tsk, tt.iterations, taskprogress.TickMsg{
   111  					Time:     time.Now(),
   112  					Sequence: tsk.Sequence(),
   113  					ID:       tsk.ID(),
   114  				})
   115  				t.Log(got)
   116  				snaps.MatchSnapshot(t, got)
   117  			})
   118  
   119  			t.Run("log", func(t *testing.T) {
   120  				log, ok := models[1].(attestLogFrame)
   121  				require.True(t, ok)
   122  				got := runModel(t, log, tt.iterations, attestLogFrameTickMsg{
   123  					Time:     time.Now(),
   124  					Sequence: log.sequence,
   125  					ID:       log.id,
   126  				}, log.reader.running)
   127  				t.Log(got)
   128  				snaps.MatchSnapshot(t, got)
   129  			})
   130  
   131  		})
   132  	}
   133  }