gitlink-cli/cmd/interactive/repl_trigger_test.go

67 lines
2.0 KiB
Go

package interactive
import (
"testing"
tea "github.com/charmbracelet/bubbletea"
)
// TestSlashTriggersPaletteImmediately verifies that typing "/" as the first
// character (KeyRunes) switches the REPL into the palette state without
// requiring Enter. This is the regression test for the bug where the
// command palette never appeared.
func TestSlashTriggersPaletteImmediately(t *testing.T) {
m := newReplModel("owner", "repo")
if m.state != stateInput {
t.Fatalf("expected initial state stateInput, got %v", m.state)
}
// Simulate the user pressing the "/" key.
// bubbletea delivers printable chars as KeyRunes with the runes populated.
keySlash := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'/'}}
newModel, _ := m.Update(keySlash)
repl, ok := newModel.(replModel)
if !ok {
t.Fatalf("expected replModel, got %T", newModel)
}
if repl.state != statePalette {
t.Fatalf("expected state statePalette after typing '/', got %v", repl.state)
}
}
// TestSlashWithQueryCarriesIntoSearch verifies "/iss" pre-fills the palette search.
func TestSlashWithQueryCarriesIntoSearch(t *testing.T) {
m := newReplModel("owner", "repo")
keySlashQuery := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'/', 'i', 's', 's'}}
newModel, _ := m.Update(keySlashQuery)
repl, ok := newModel.(replModel)
if !ok {
t.Fatalf("expected replModel, got %T", newModel)
}
if repl.state != statePalette {
t.Fatalf("expected state statePalette, got %v", repl.state)
}
if got := repl.palette.search.Value(); got != "iss" {
t.Fatalf("expected palette search 'iss', got %q", got)
}
}
// TestNonSlashInputStaysInInputState verifies ordinary text doesn't open the palette.
func TestNonSlashInputStaysInInputState(t *testing.T) {
m := newReplModel("owner", "repo")
keyA := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}}
newModel, _ := m.Update(keyA)
repl, ok := newModel.(replModel)
if !ok {
t.Fatalf("expected replModel, got %T", newModel)
}
if repl.state != stateInput {
t.Fatalf("typing 'a' should stay in stateInput, got %v", repl.state)
}
}