101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/StevanFreeborn/term-tier/internal/handlers"
|
|
"github.com/StevanFreeborn/term-tier/internal/state"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
func TestNewUpNavigationHandler(t *testing.T) {
|
|
h := handlers.NewUpNavigationHandler()
|
|
|
|
if h.Description() != "Move up" {
|
|
t.Errorf("Expected description 'Move up', got %q", h.Description())
|
|
}
|
|
|
|
keys := h.KeysHandled()
|
|
if !slices.Contains(keys, "up") || !slices.Contains(keys, "k") {
|
|
t.Errorf("Expected keys ['up', 'k'], got %v", keys)
|
|
}
|
|
}
|
|
|
|
func TestUpNavigationHandler_Match(t *testing.T) {
|
|
h := handlers.NewUpNavigationHandler()
|
|
|
|
tests := []struct {
|
|
name string
|
|
mode state.Mode
|
|
key string
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "Matches 'up' in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "up",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Matches 'k' in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "k",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Ignores 'up' in Input Mode",
|
|
mode: state.InputMode,
|
|
key: "up",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Ignores 'k' in Input Mode",
|
|
mode: state.InputMode,
|
|
key: "k",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Ignores other keys in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "down",
|
|
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 == "up" {
|
|
msg = tea.KeyMsg{Type: tea.KeyUp}
|
|
}
|
|
|
|
if got := h.Match(s, msg); got != tt.expected {
|
|
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpNavigationHandler_Handle(t *testing.T) {
|
|
h := handlers.NewUpNavigationHandler()
|
|
|
|
s := state.New(
|
|
state.WithCursor(1, 0),
|
|
state.WithMode(state.NavigationMode),
|
|
)
|
|
|
|
msg := tea.KeyMsg{Type: tea.KeyUp}
|
|
cmd := h.Handle(s, msg)
|
|
|
|
if s.CursorRow() != 0 {
|
|
t.Errorf("Expected cursor to move to Row 0, got %d", s.CursorRow())
|
|
}
|
|
|
|
if cmd != nil {
|
|
t.Error("Expected Handle to return nil, got a command")
|
|
}
|
|
}
|