tui: add TestHistoryNavigation to verify history navigation logic

This commit is contained in:
Luxferre
2026-03-22 18:37:12 +02:00
parent 04bacc19fb
commit 7e7729457a
+46
View File
@@ -6,6 +6,7 @@ import (
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"sidekick"
) )
// Since sidekick-tui/main.go uses global variables and is a tea.Model, // Since sidekick-tui/main.go uses global variables and is a tea.Model,
@@ -87,3 +88,48 @@ func TestMessageWrapping(t *testing.T) {
t.Errorf("expected long message to be wrapped, but no newline found in viewport") t.Errorf("expected long message to be wrapped, but no newline found in viewport")
} }
} }
func TestHistoryNavigation(t *testing.T) {
m := initialModel()
m.ready = true
m.history = append(m.history, sidekick.Message{Role: sidekick.MessageRoleUser, Content: "First"})
m.history = append(m.history, sidekick.Message{Role: sidekick.MessageRoleAssistant, Content: "AI Response"})
m.history = append(m.history, sidekick.Message{Role: sidekick.MessageRoleUser, Content: "Second"})
// Test UP from empty draft
m2, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp})
tm2 := m2.(model)
if tm2.textarea.Value() != "Second" {
t.Errorf("expected 'Second', got '%s'", tm2.textarea.Value())
}
if tm2.historyIndex != 1 {
t.Errorf("expected historyIndex 1, got %d", tm2.historyIndex)
}
// Test UP again
m3, _ := tm2.Update(tea.KeyMsg{Type: tea.KeyUp})
tm3 := m3.(model)
if tm3.textarea.Value() != "First" {
t.Errorf("expected 'First', got '%s'", tm3.textarea.Value())
}
if tm3.historyIndex != 0 {
t.Errorf("expected historyIndex 0, got %d", tm3.historyIndex)
}
// Test DOWN
m4, _ := tm3.Update(tea.KeyMsg{Type: tea.KeyDown})
tm4 := m4.(model)
if tm4.textarea.Value() != "Second" {
t.Errorf("expected 'Second', got '%s'", tm4.textarea.Value())
}
// Test DOWN to draft
m5, _ := tm4.Update(tea.KeyMsg{Type: tea.KeyDown})
tm5 := m5.(model)
if tm5.textarea.Value() != "" {
t.Errorf("expected empty draft, got '%s'", tm5.textarea.Value())
}
if tm5.historyIndex != -1 {
t.Error("expected historyIndex to reset to -1")
}
}