Skip to content

Commit

Permalink
test: update tests to new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Oct 21, 2024
1 parent bd15066 commit 0c8edce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
5 changes: 3 additions & 2 deletions internal/status_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package internal

import (
"strings"
"testing"
)

func Test_StatusModel(t *testing.T) {
m := StatusModel{LastRound: 0}
if m.String() != "Last round: 0" {
t.Fatal("expected \"Last round: 0\", got ", m.String())
if !strings.Contains(m.String(), "LastRound: 0") {
t.Fatal("expected \"LastRound: 0\", got ", m.String())
}
}
23 changes: 15 additions & 8 deletions ui/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ import (
)

func Test_ProtocolViewRender(t *testing.T) {
status := internal.StatusModel{
HeartBeat: make(chan uint64),
LastRound: 0,
NeedsUpdate: true,
State: "SYNCING",
state := internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
RX: 0,
TPS: 0,
},
}

// Create the Model
m := MakeProtocolViewModel(&status)
m := MakeProtocolViewModel(&state)

tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(80, 40),
teatest.WithInitialTermSize(120, 80),
)

// Wait for prompt to exit
Expand All @@ -44,7 +51,7 @@ func Test_ProtocolViewRender(t *testing.T) {
// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("q"),
Runes: []rune("ctrl+c"),
})

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
Expand Down
44 changes: 15 additions & 29 deletions ui/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,32 @@ package ui

import (
"bytes"
"context"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
"strings"
"testing"
"time"
)

func Test_InvalidStatusViewModel(t *testing.T) {
client, err := api.NewClientWithResponses("http://255.255.255.255:4001")
if err != nil {
t.Fatal(err)
}

// Test Invalid Node
_, err = MakeStatusViewModel(context.Background(), client)
if !strings.Contains(err.Error(), "dial tcp 255.255.255.255:4001") {
t.Fatal(err)
}
}
func Test_StatusViewRender(t *testing.T) {
status := internal.StatusModel{
HeartBeat: make(chan uint64),
LastRound: 0,
NeedsUpdate: true,
State: "SYNCING",
}
// Create the Model
m := StatusViewModel{
Status: &status,
Metrics: &internal.MetricsModel{
state := internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
RX: 0,
TPS: 0,
},
ViewWidth: 80,
IsVisible: true,
}
// Create the Model
m := StatusViewModel{
Data: &state,
TerminalWidth: 80,
IsVisible: true,
}

tm := teatest.NewTestModel(
Expand All @@ -53,7 +39,7 @@ func Test_StatusViewRender(t *testing.T) {
teatest.WaitFor(
t, tm.Output(),
func(bts []byte) bool {
return bytes.Contains(bts, []byte("Latest Round: 0"))
return bytes.Contains(bts, []byte("Latest Round: 1337"))
},
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second*3),
Expand All @@ -68,7 +54,7 @@ func Test_StatusViewRender(t *testing.T) {
// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("q"),
Runes: []rune("ctrl+c"),
})

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
Expand Down
19 changes: 16 additions & 3 deletions ui/viewport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ui

import (
"bytes"
"context"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
Expand All @@ -17,8 +17,21 @@ func Test_ViewportViewRender(t *testing.T) {
if err != nil {
t.Fatal(err)
}
state := internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
RX: 0,
TPS: 0,
},
}
// Create the Model
m, err := MakeViewportViewModel(context.Background(), client)
m, err := MakeViewportViewModel(&state, client)
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,7 +54,7 @@ func Test_ViewportViewRender(t *testing.T) {
// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("q"),
Runes: []rune("ctrl+c"),
})

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
Expand Down

0 comments on commit 0c8edce

Please sign in to comment.