-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test cases for TUI components
- Loading branch information
Showing
7 changed files
with
502 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
Oops, something went wrong.