102 lines
2.2 KiB
Go
102 lines
2.2 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 TestNewLeftNavigationHandler(t *testing.T) {
|
|
h := handlers.NewLeftNavigationHandler()
|
|
|
|
if h.Description() != "Move left" {
|
|
t.Errorf("Expected description 'Move left', got %q", h.Description())
|
|
}
|
|
|
|
keys := h.KeysHandled()
|
|
|
|
if !slices.Contains(keys, "left") || !slices.Contains(keys, "h") {
|
|
t.Errorf("Expected keys ['left', 'h'], got %v", keys)
|
|
}
|
|
}
|
|
|
|
func TestLeftNavigationHandler_Match(t *testing.T) {
|
|
h := handlers.NewLeftNavigationHandler()
|
|
|
|
tests := []struct {
|
|
name string
|
|
mode state.Mode
|
|
key string
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "Matches 'left' in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "left",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Matches 'h' in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "h",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Ignores 'left' in Input Mode",
|
|
mode: state.InputMode,
|
|
key: "left",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Ignores 'h' in Input Mode",
|
|
mode: state.InputMode,
|
|
key: "h",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Ignores other keys in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
key: "right",
|
|
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 == "left" {
|
|
msg = tea.KeyMsg{Type: tea.KeyLeft}
|
|
}
|
|
|
|
if got := h.Match(s, msg); got != tt.expected {
|
|
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLeftNavigationHandler_Handle(t *testing.T) {
|
|
h := handlers.NewLeftNavigationHandler()
|
|
|
|
s := state.New(
|
|
state.WithCursor(0, 1),
|
|
state.WithMode(state.NavigationMode),
|
|
)
|
|
|
|
msg := tea.KeyMsg{Type: tea.KeyLeft}
|
|
cmd := h.Handle(s, msg)
|
|
|
|
if s.CursorCol() != 0 {
|
|
t.Errorf("Expected cursor to move to Col 0, got %d", s.CursorCol())
|
|
}
|
|
|
|
if cmd != nil {
|
|
t.Error("Expected Handle to return nil, got a command")
|
|
}
|
|
}
|