feat: add navigation and window size handlers with corresponding tests
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/term-tier/internal/core"
|
||||
"github.com/StevanFreeborn/term-tier/internal/handlers"
|
||||
"github.com/StevanFreeborn/term-tier/internal/state"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestNewDownNavigationHandler(t *testing.T) {
|
||||
h := handlers.NewDownNavigationHandler()
|
||||
|
||||
if h.Description() != "Move down" {
|
||||
t.Errorf("Expected description 'Move down', got %q", h.Description())
|
||||
}
|
||||
|
||||
keys := h.KeysHandled()
|
||||
|
||||
if !slices.Contains(keys, "down") || !slices.Contains(keys, "j") {
|
||||
t.Errorf("Expected keys ['down', 'j'], got %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownNavigationHandler_Match(t *testing.T) {
|
||||
h := handlers.NewDownNavigationHandler()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mode state.Mode
|
||||
key string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Matches 'down' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "down",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Matches 'j' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "j",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'down' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: "down",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'j' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: "j",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Ignores other keys in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "up",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := state.New(state.WithMode(tt.mode))
|
||||
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(tt.key)}
|
||||
|
||||
if tt.key == "down" {
|
||||
msg = tea.KeyMsg{Type: tea.KeyDown}
|
||||
}
|
||||
|
||||
if got := h.Match(s, msg); got != tt.expected {
|
||||
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownNavigationHandler_Handle(t *testing.T) {
|
||||
h := handlers.NewDownNavigationHandler()
|
||||
|
||||
fullTiers := make([]core.Tier, 6)
|
||||
fullTiers[0] = core.Tier{Name: "S", Items: []core.Item{"1", "2", "3"}}
|
||||
fullTiers[1] = core.Tier{Name: "A", Items: []core.Item{"4"}}
|
||||
|
||||
s := state.New(
|
||||
state.WithTiers(fullTiers),
|
||||
state.WithCursor(0, 2), // Start at the end of Row 0
|
||||
state.WithMode(state.NavigationMode),
|
||||
)
|
||||
|
||||
msg := tea.KeyMsg{Type: tea.KeyDown}
|
||||
cmd := h.Handle(s, msg)
|
||||
|
||||
if s.CursorRow() != 1 {
|
||||
t.Errorf("Expected cursor to move to Row 1, got %d", s.CursorRow())
|
||||
}
|
||||
|
||||
if s.CursorCol() != 0 {
|
||||
t.Errorf("Expected cursor column to clamp to 0, got %d", s.CursorCol())
|
||||
}
|
||||
|
||||
if cmd != nil {
|
||||
t.Error("Expected Handle to return nil, got a command")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user