132 lines
2.9 KiB
Go
132 lines
2.9 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/StevanFreeborn/term-tier/internal/handlers"
|
|
"github.com/StevanFreeborn/term-tier/internal/state"
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
func TestNewTextInputHandler(t *testing.T) {
|
|
h := handlers.NewTextInputHandler()
|
|
|
|
if len(h.KeysHandled()) != 0 {
|
|
t.Errorf("Expected 0 keys handled, got %d", len(h.KeysHandled()))
|
|
}
|
|
|
|
if h.Description() != "" {
|
|
t.Errorf("Expected empty description, got %q", h.Description())
|
|
}
|
|
}
|
|
|
|
func TestTextInputHandler_Match(t *testing.T) {
|
|
h := handlers.NewTextInputHandler()
|
|
|
|
tests := []struct {
|
|
name string
|
|
mode state.Mode
|
|
msg tea.KeyMsg
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "Matches in Input Mode",
|
|
mode: state.InputMode,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Ignores in Navigation Mode",
|
|
mode: state.NavigationMode,
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Ignores in Help Mode",
|
|
mode: state.HelpMode,
|
|
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("a")}
|
|
|
|
if got := h.Match(s, msg); got != tt.expected {
|
|
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTextInputHandler_Handle(t *testing.T) {
|
|
h := handlers.NewTextInputHandler()
|
|
|
|
t.Run("Handle Escape (Exit Input Mode)", func(t *testing.T) {
|
|
s := state.New(state.WithMode(state.InputMode))
|
|
|
|
msg := tea.KeyMsg{Type: tea.KeyEsc}
|
|
cmd := h.Handle(s, msg)
|
|
|
|
if s.Mode() != state.NavigationMode {
|
|
t.Errorf("Expected mode to switch to NavigationMode, got %v", s.Mode())
|
|
}
|
|
|
|
if cmd != nil {
|
|
t.Error("Expected nil command on escape")
|
|
}
|
|
})
|
|
|
|
t.Run("Handle Enter (Submit Input)", func(t *testing.T) {
|
|
s := state.New(state.WithMode(state.InputMode))
|
|
|
|
submitted := false
|
|
|
|
s.StartInputMode("Test", func(val string, s state.State) tea.Cmd {
|
|
submitted = true
|
|
return nil
|
|
})
|
|
|
|
msg := tea.KeyMsg{Type: tea.KeyEnter}
|
|
|
|
h.Handle(s, msg)
|
|
|
|
if !submitted {
|
|
t.Error("Expected Enter to submit input and trigger callback")
|
|
}
|
|
|
|
if s.Mode() != state.NavigationMode {
|
|
t.Error("Expected mode to reset to NavigationMode after submit")
|
|
}
|
|
})
|
|
|
|
t.Run("Handle Typing (Update Input)", func(t *testing.T) {
|
|
ti := textinput.New()
|
|
ti.Focus()
|
|
|
|
s := state.New(
|
|
state.WithMode(state.InputMode),
|
|
state.WithInput(ti, "Test Input"),
|
|
)
|
|
|
|
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("a")}
|
|
cmd := h.Handle(s, msg)
|
|
|
|
if s.Input().Value() != "a" {
|
|
t.Errorf("Expected input value 'a', got %q", s.Input().Value())
|
|
}
|
|
|
|
if cmd == nil {
|
|
t.Error("Expected command (blink) from text input update")
|
|
}
|
|
|
|
msg = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("b")}
|
|
h.Handle(s, msg)
|
|
|
|
if s.Input().Value() != "ab" {
|
|
t.Errorf("Expected input value 'ab', got %q", s.Input().Value())
|
|
}
|
|
})
|
|
}
|