Skip to content

Commit

Permalink
Merge branch 'main' into refactor/view-transitions
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
#	ui/controls/controls_test.go
  • Loading branch information
PhearZero committed Nov 19, 2024
2 parents 6458d01 + a6c55e6 commit b54dbf3
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.3.2
github.com/charmbracelet/x/exp/golden v0.0.0-20241022174419-46d9bb99a691 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20241022174419-46d9bb99a691
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down
107 changes: 107 additions & 0 deletions ui/pages/accounts/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package accounts

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

func Test_Snapshot(t *testing.T) {
t.Run("Visible", func(t *testing.T) {
model := New(&internal.StateModel{
Status: internal.StatusModel{},
Metrics: internal.MetricsModel{},
Accounts: nil,
ParticipationKeys: nil,
Admin: false,
Watching: false,
})
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
}

func Test_Messages(t *testing.T) {
var testKeys = []api.ParticipationKey{
{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "",
Key: api.AccountParticipation{
SelectionParticipationKey: nil,
StateProofKey: nil,
VoteFirstValid: 0,
VoteKeyDilution: 0,
VoteLastValid: 0,
VoteParticipationKey: nil,
},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
},
}
sm := &internal.StateModel{
Status: internal.StatusModel{},
Metrics: internal.MetricsModel{},
Accounts: nil,
ParticipationKeys: &testKeys,
Admin: false,
Watching: false,
}
values := make(map[string]internal.Account)
for _, key := range *sm.ParticipationKeys {
val, ok := values[key.Address]
if !ok {
values[key.Address] = internal.Account{
Address: key.Address,
Status: "Offline",
Balance: 0,
Expires: time.Unix(0, 0),
Keys: 1,
}
} else {
val.Keys++
values[key.Address] = val
}
}
sm.Accounts = values
// Create the Model
m := New(sm)

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

// Wait for prompt to exit
teatest.WaitFor(
t, tm.Output(),
func(bts []byte) bool {
return bytes.Contains(bts, []byte("(k)eys"))
},
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second*3),
)

tm.Send(*sm)

tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})

tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("ctrl+c"),
})

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
40 changes: 40 additions & 0 deletions ui/pages/accounts/testdata/Test_Snapshot/Visible.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
╭──Accounts────────────────────────────────────────────────────────────────────╮
│ Account Keys Status Expires Balance │
│─────────────────────────────────────────────────────────────────────────── │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
╰────( (g)enerate )─────────────────────────────────────| accounts | keys |────╯
114 changes: 113 additions & 1 deletion ui/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,105 @@ import (
"bytes"
"github.com/algorandfoundation/hack-tui/internal"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/charmbracelet/x/exp/teatest"
"testing"
"time"
)

func Test_ProtocolViewRender(t *testing.T) {
var protocolViewSnapshots = map[string]ProtocolViewModel{
"Hidden": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: true,
NeedsUpdate: true,
LastRound: 0,
},
TerminalWidth: 60,
TerminalHeight: 40,
IsVisible: false,
},
"HiddenHeight": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: true,
NeedsUpdate: true,
LastRound: 0,
},
TerminalWidth: 70,
TerminalHeight: 20,
IsVisible: true,
},
"Visible": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: true,
NeedsUpdate: true,
LastRound: 0,
},
TerminalWidth: 160,
TerminalHeight: 80,
IsVisible: true,
},
"VisibleSmall": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: true,
NeedsUpdate: true,
LastRound: 0,
},
TerminalWidth: 80,
TerminalHeight: 40,
IsVisible: true,
},
"NoVoteOrUpgrade": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: false,
NeedsUpdate: false,
LastRound: 0,
},
TerminalWidth: 160,
TerminalHeight: 80,
IsVisible: true,
},
"NoVoteOrUpgradeSmall": {
Data: internal.StatusModel{
State: "SYNCING",
Version: "v0.0.0-test",
Network: "test-v1",
Voting: false,
NeedsUpdate: false,
LastRound: 0,
},
TerminalWidth: 80,
TerminalHeight: 40,
IsVisible: true,
},
}

func Test_ProtocolSnapshot(t *testing.T) {
for name, model := range protocolViewSnapshots {
t.Run(name, func(t *testing.T) {
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
}
}

// Test_ProtocolMessages handles any additional tests like sending messages
func Test_ProtocolMessages(t *testing.T) {
state := internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
Expand Down Expand Up @@ -41,6 +134,25 @@ func Test_ProtocolViewRender(t *testing.T) {
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second*3),
)
tm.Send(internal.StatusModel{
State: "",
Version: "",
Network: "",
Voting: false,
NeedsUpdate: false,
LastRound: 0,
})
// Send hide key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("h"),
})

// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("ctrl+c"),
})

// Send quit msg
tm.Send(tea.QuitMsg{})
Expand Down
77 changes: 76 additions & 1 deletion ui/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,72 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/charmbracelet/x/exp/teatest"
)

func Test_StatusViewRender(t *testing.T) {
var statusViewSnapshots = map[string]StatusViewModel{
"Syncing": {
Data: &internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
},
},
TerminalWidth: 180,
TerminalHeight: 80,
IsVisible: true,
},
"Hidden": {
Data: &internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
},
},
TerminalWidth: 180,
TerminalHeight: 80,
IsVisible: false,
},
"Loading": {
Data: &internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
NeedsUpdate: true,
State: "SYNCING",
},
Metrics: internal.MetricsModel{
RoundTime: 0,
TX: 0,
},
},
TerminalWidth: 0,
TerminalHeight: 0,
IsVisible: true,
},
}

func Test_StatusSnapshot(t *testing.T) {
for name, model := range statusViewSnapshots {
t.Run(name, func(t *testing.T) {
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
}
}

func Test_StatusMessages(t *testing.T) {
state := internal.StateModel{
Status: internal.StatusModel{
LastRound: 1337,
Expand Down Expand Up @@ -47,6 +108,20 @@ func Test_StatusViewRender(t *testing.T) {
teatest.WithDuration(time.Second*3),
)

// Send the state
tm.Send(state)

// Send hide key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("h"),
})

// Send quit key
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("ctrl+c"),
})
// Send quit msg
tm.Send(tea.QuitMsg{})

Expand Down
Loading

0 comments on commit b54dbf3

Please sign in to comment.