Skip to content

Commit

Permalink
test: Add test cases for TUI components
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent c8b11f4 commit fe10038
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 2 deletions.
63 changes: 63 additions & 0 deletions cmd/cli/commands/install/page_10_welcome_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package install

import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestWelcomePage(t *testing.T) {
tests := []struct {
name string
buttonIndex int
buttonLabel string
expectStop bool
expectNewPage bool
}{
{
name: "clicks next button",
buttonIndex: 0,
buttonLabel: tui.ButtonNext,
expectStop: false,
expectNewPage: true,
},
{
name: "clicks close button",
buttonIndex: 1,
buttonLabel: tui.ButtonClose,
expectStop: true,
expectNewPage: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup.
app := tview.NewApplication()

// Create a new welcome page with our mock display.
mockDisplay := &InstallDisplay{
app: app,
log: logrus.New(),
networkConfigPage: &NetworkConfigPage{
page: &tui.Page{ID: "network-config"},
},
}
page := NewWelcomePage(mockDisplay)

// Verify the page was created.
assert.NotNil(t, page, "page should be created")
assert.NotNil(t, page.content, "page content should be set")
assert.IsType(t, &tview.Modal{}, page.content, "content should be a modal")

// Verify the page ID and title.
assert.Equal(t, "install-welcome", page.page.ID)
assert.Equal(t, "Welcome", page.page.Title)
})
}
}
82 changes: 82 additions & 0 deletions cmd/cli/commands/install/page_20_network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package install

import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/service/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestNetworkConfigPage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *service.ContributoorConfig) *InstallDisplay {
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(cfg).AnyTimes()
mockConfig.EXPECT().Update(gomock.Any()).Return(nil).AnyTimes()

return &InstallDisplay{
app: tview.NewApplication(),
log: logrus.New(),
configService: mockConfig,
}
}

t.Run("creates page with correct structure", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)

// Verify the page was created.
assert.NotNil(t, page, "page should be created")
assert.NotNil(t, page.content, "page content should be set")
assert.IsType(t, &tview.Grid{}, page.content, "content should be a grid")

// Verify the page ID and title.
assert.Equal(t, "install-network", page.page.ID)
assert.Equal(t, "Network Selection", page.page.Title)
})

t.Run("has correct network options", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)

// Verify we have network options.
assert.NotNil(t, page.content, "content should be set")
assert.IsType(t, &tview.Grid{}, page.content, "content should be a grid")

// Verify we have the correct number of networks available.
assert.Greater(t, len(tui.AvailableNetworks), 0, "should have available networks")
})

t.Run("has correct styling", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewNetworkConfigPage(mockDisplay)

// Verify basic styling.
assert.NotNil(t, page.content, "content should be set")
assert.IsType(t, &tview.Grid{}, page.content, "content should be a grid")
})
}
74 changes: 74 additions & 0 deletions cmd/cli/commands/install/page_30_beacon_node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package install

import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/service/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestBeaconNodePage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *service.ContributoorConfig) *InstallDisplay {
mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(cfg).AnyTimes()

return &InstallDisplay{
app: tview.NewApplication(),
log: logrus.New(),
configService: mockConfig,
networkConfigPage: &NetworkConfigPage{
page: &tui.Page{ID: "network-config"},
},
}
}

t.Run("creates page with correct structure", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{
BeaconNodeAddress: "http://localhost:5052",
})

// Create the page.
page := NewBeaconNodePage(mockDisplay)

// Verify the page was created.
assert.NotNil(t, page, "page should be created")
assert.NotNil(t, page.content, "page content should be set")
assert.IsType(t, &tview.Grid{}, page.content, "content should be a grid")
assert.NotNil(t, page.form, "form should be set")

// Verify the page ID and title.
assert.Equal(t, "install-beacon", page.page.ID)
assert.Equal(t, "Beacon Node", page.page.Title)

// Verify form structure.
assert.Equal(t, 1, page.form.GetFormItemCount(), "should have one form item initially")
assert.NotNil(t, page.form.GetButton(0), "should have Next button")
})

t.Run("has correct parent page", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{
BeaconNodeAddress: "http://localhost:5052",
})

// Create the page.
page := NewBeaconNodePage(mockDisplay)

// Verify parent page is set correctly.
assert.Equal(t, "network-config", page.page.Parent.ID)
})
}
90 changes: 90 additions & 0 deletions cmd/cli/commands/install/page_40_output_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package install

import (
"testing"

"github.com/ethpandaops/contributoor-installer/internal/service"
"github.com/ethpandaops/contributoor-installer/internal/service/mock"
"github.com/ethpandaops/contributoor-installer/internal/tui"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

// This is about the best we can do re testing TUI components.
// They're heavily dependent on the terminal state.
func TestOutputServerPage(t *testing.T) {
setupMockDisplay := func(ctrl *gomock.Controller, cfg *service.ContributoorConfig) *InstallDisplay {
if cfg.OutputServer == nil {
cfg.OutputServer = &service.OutputServerConfig{}
}

mockConfig := mock.NewMockConfigManager(ctrl)
mockConfig.EXPECT().Get().Return(cfg).AnyTimes()
mockConfig.EXPECT().Update(gomock.Any()).Return(nil).AnyTimes()

return &InstallDisplay{
app: tview.NewApplication(),
log: logrus.New(),
configService: mockConfig,
beaconPage: &BeaconNodePage{
page: &tui.Page{ID: "beacon-node"},
},
}
}

t.Run("creates page with correct structure", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewOutputServerPage(mockDisplay)

// Verify the page was created.
assert.NotNil(t, page, "page should be created")
assert.NotNil(t, page.content, "page content should be set")
assert.IsType(t, &tview.Grid{}, page.content, "content should be a grid")
assert.NotNil(t, page.form, "form should be set")

// Verify the page ID and title.
assert.Equal(t, "install-output", page.page.ID)
assert.Equal(t, "Output Server", page.page.Title)

// Verify form structure - initially just has dropdown and button
assert.Equal(t, 1, page.form.GetFormItemCount(), "should have one form item initially")
assert.NotNil(t, page.form.GetButton(0), "should have Next button")
})

t.Run("has correct parent page", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewOutputServerPage(mockDisplay)

// Verify parent page is set correctly.
assert.Equal(t, "beacon-node", page.page.Parent.ID)
})

t.Run("initializes with default output server config", func(t *testing.T) {
// Setup.
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDisplay := setupMockDisplay(ctrl, &service.ContributoorConfig{})

// Create the page.
page := NewOutputServerPage(mockDisplay)

// Verify the page was created with default config.
assert.NotNil(t, page, "page should be created")
assert.NotNil(t, page.form, "form should be set")
})
}
Loading

0 comments on commit fe10038

Please sign in to comment.