github.com/anchore/syft@v1.38.2/cmd/syft/cli/ui/handle_pull_source_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 "github.com/anchore/syft/syft/event" 15 "github.com/anchore/syft/syft/event/monitor" 16 ) 17 18 func TestHandler_handlePullSourceStarted(t *testing.T) { 19 20 tests := []struct { 21 name string 22 eventFn func(*testing.T) partybus.Event 23 iterations int 24 }{ 25 { 26 name: "snap download in progress", 27 eventFn: func(t *testing.T) partybus.Event { 28 stage := progress.NewAtomicStage("") 29 manual := progress.NewManual(0) 30 manual.SetTotal(1000000) // 1MB file 31 manual.Set(250000) // 25% downloaded 32 33 taskProg := &monitor.TaskProgress{ 34 AtomicStage: stage, 35 Manual: manual, 36 } 37 38 genericTask := monitor.GenericTask{ 39 Title: monitor.Title{ 40 Default: "Downloading snap", 41 WhileRunning: "Downloading snap file...", 42 OnSuccess: "Snap downloaded", 43 }, 44 Context: "example-app_1.0_amd64.snap", 45 HideOnSuccess: false, 46 HideStageOnSuccess: true, 47 ID: "snap-download-123", 48 } 49 50 return partybus.Event{ 51 Type: event.PullSourceStarted, 52 Source: genericTask, 53 Value: taskProg, 54 } 55 }, 56 iterations: 5, 57 }, 58 { 59 name: "snap download complete", 60 eventFn: func(t *testing.T) partybus.Event { 61 stage := progress.NewAtomicStage("") 62 manual := progress.NewManual(0) 63 manual.SetTotal(1000000) // 1MB file 64 manual.Set(1000000) // 100% downloaded 65 manual.SetCompleted() 66 67 taskProg := &monitor.TaskProgress{ 68 AtomicStage: stage, 69 Manual: manual, 70 } 71 72 genericTask := monitor.GenericTask{ 73 Title: monitor.Title{ 74 Default: "Downloading snap", 75 WhileRunning: "Downloading snap file...", 76 OnSuccess: "Snap downloaded successfully", 77 }, 78 Context: "example-app_1.0_amd64.snap", 79 HideOnSuccess: false, 80 HideStageOnSuccess: true, 81 ID: "snap-download-123", 82 } 83 84 return partybus.Event{ 85 Type: event.PullSourceStarted, 86 Source: genericTask, 87 Value: taskProg, 88 } 89 }, 90 iterations: 3, 91 }, 92 } 93 for _, tt := range tests { 94 t.Run(tt.name, func(t *testing.T) { 95 event := tt.eventFn(t) 96 handler := New(DefaultHandlerConfig()) 97 handler.WindowSize = tea.WindowSizeMsg{ 98 Width: 100, 99 Height: 80, 100 } 101 102 models := handler.handlePullSourceStarted(event) 103 require.Len(t, models, 1) 104 model := models[0] 105 106 tsk, ok := model.(taskprogress.Model) 107 require.True(t, ok) 108 109 gotModel := runModel(t, tsk, tt.iterations, taskprogress.TickMsg{ 110 Time: time.Now(), 111 Sequence: tsk.Sequence(), 112 ID: tsk.ID(), 113 }) 114 115 got := gotModel.View() 116 117 t.Log(got) 118 snaps.MatchSnapshot(t, got) 119 }) 120 } 121 }