Skip to content

Commit

Permalink
fix(console): refactor, retries, sleep to address flaky tests (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
meganwolf0 authored Oct 2, 2024
1 parent 1ab0eef commit 02101a5
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 372 deletions.
42 changes: 42 additions & 0 deletions src/internal/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"os"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/common/oscal"
)
Expand Down Expand Up @@ -32,3 +35,42 @@ func CreateTempFile(t *testing.T, ext string) *os.File {

return tempFile
}

// RunTestModelView runs a test model view with a given model and messages, impelements a retry loop if final model is nil
func RunTestModelView(t *testing.T, m tea.Model, msgs []tea.Msg, timeout time.Duration, maxRetries, height, width int) error {

testModelView := func(t *testing.T) (bool, error) {
tm := teatest.NewTestModel(t, m, teatest.WithInitialTermSize(width, height))

for _, msg := range msgs {
tm.Send(msg)
time.Sleep(time.Millisecond * 50)
}

if err := tm.Quit(); err != nil {
return false, err
}

fm := tm.FinalModel(t, teatest.WithFinalTimeout(timeout))

if fm == nil {
return true, nil
}

teatest.RequireEqualOutput(t, []byte(fm.View()))

return false, nil
}

for i := 0; i < maxRetries; i++ {
retry, err := testModelView(t)
if retry {
continue
}
if err != nil {
return err
}
break
}
return nil
}
40 changes: 40 additions & 0 deletions src/internal/tui/assessment_results/assessment-results_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package assessmentresults_test

import (
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/defenseunicorns/lula/src/internal/testhelpers"
assessmentresults "github.com/defenseunicorns/lula/src/internal/tui/assessment_results"
"github.com/defenseunicorns/lula/src/internal/tui/common"
"github.com/muesli/termenv"
)

const (
timeout = time.Second * 20
maxRetries = 3
height = common.DefaultHeight
width = common.DefaultWidth

validAssessmentResults = "../../../test/unit/common/oscal/valid-assessment-results.yaml"
)

func init() {
lipgloss.SetColorProfile(termenv.Ascii)
}

// TestAssessmentResultsBasicView tests that the model is created correctly from an assessment results model
func TestAssessmentResultsBasicView(t *testing.T) {
oscalModel := testhelpers.OscalFromPath(t, validAssessmentResults)
model := assessmentresults.NewAssessmentResultsModel(oscalModel.AssessmentResults)
model.Open(height, width)

msgs := []tea.Msg{}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
? toggle help
╭────────────────────────────────────────╮ ╭────────────────────────────────────────╮
Selected Result │Lula Validation Result - 41787700-2a4c-…│ Compare Result │No Result Selected │
╰────────────────────────────────────────╯ ╰────────────────────────────────────────╯
╭───────────────╮ ╭─────────╮
│ Findings List ├─────────────────────────────╮ │ Summary ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
╰───────────────╯ ╰─────────╯
│ │ │ ⚠️ Summary Under Construction ⚠️ │
│ 1 item │ │ │
│ │ │ │
│ ID-1 │ │ │
│ not-satisfied │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
│ │ ╭──────────────╮
│ │ │ Observations ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │ ╰──────────────╯
│ │ │ ⚠️ Observations Under Construction ⚠️ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ ↑/k up • ↓/j down • ↳ confirm • ? toggle │ │ │
╰────────────────────────────────────────────╯ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Loading

0 comments on commit 02101a5

Please sign in to comment.