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 TestNewDeleteItemHandler(t *testing.T) { h := handlers.NewDeleteItemHandler() if h.Description() != "Delete item" { t.Errorf("Expected description 'Delete item', got %q", h.Description()) } keys := h.KeysHandled() if !slices.Contains(keys, "d") || !slices.Contains(keys, "backspace") { t.Errorf("Expected keys ['d', 'backspace'], got %v", keys) } } func TestDeleteItemHandler_Match(t *testing.T) { h := handlers.NewDeleteItemHandler() tests := []struct { name string mode state.Mode key string expected bool }{ { name: "Matches 'd' in Navigation Mode", mode: state.NavigationMode, key: "d", expected: true, }, { name: "Matches 'backspace' in Navigation Mode", mode: state.NavigationMode, key: "backspace", expected: true, }, { name: "Ignores 'd' in Input Mode", mode: state.InputMode, key: "d", expected: false, }, { name: "Ignores other keys in Navigation Mode", mode: state.NavigationMode, key: "x", 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 == "backspace" { msg = tea.KeyMsg{Type: tea.KeyBackspace} } if got := h.Match(s, msg); got != tt.expected { t.Errorf("Match() = %v, want %v", got, tt.expected) } }) } } func TestDeleteItemHandler_Handle(t *testing.T) { h := handlers.NewDeleteItemHandler() tiers := make([]core.Tier, 6) tiers[0] = core.Tier{Name: "S", Items: []core.Item{"Delete Me"}} s := state.New( state.WithTiers(tiers), state.WithCursor(0, 0), state.WithMode(state.NavigationMode), ) msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("d")} h.Handle(s, msg) if len(s.Tiers()[0].Items) != 0 { t.Errorf("Expected item to be deleted, but %d items remain", len(s.Tiers()[0].Items)) } if s.StatusMsg() != "Item Deleted" { t.Errorf("Expected status 'Item Deleted', got %q", s.StatusMsg()) } }