Gim/internal/action/command_history_test.go
Hayden Hargreaves a01369f407
All checks were successful
Run Test Suite / test (push) Successful in 42s
fix: cleaned up the testing mocking. New single module
2026-03-19 17:31:53 -07:00

134 lines
3.4 KiB
Go

package action
import (
"testing"
"git.gophernest.net/azpect/TextEditor/internal/core"
tea "github.com/charmbracelet/bubbletea"
)
// mockRegistry for testing command execution without actual command handlers
type mockRegistry struct{}
func (r *mockRegistry) Execute(m Model, cmdLine string) (tea.Cmd, error) {
// Mock implementation - just returns nil
return nil, nil
}
// TestCommandExecuteUpdatesHistory tests that executing commands adds them to history
func TestCommandExecuteUpdatesHistory(t *testing.T) {
t.Run("first command added to empty history", func(t *testing.T) {
m := NewMockModel()
m.ModeVal = core.CommandMode
m.CommandVal = "w test.txt"
m.CommandHistoryList = []string{}
m.CommandHistoryCur = 0
registry := &mockRegistry{}
action := CommandExecute{Registry: registry}
action.Execute(m)
history := m.CommandHistory()
if len(history) != 1 {
t.Errorf("History length = %d, want 1", len(history))
}
if history[0] != "w test.txt" {
t.Errorf("History[0] = %q, want %q", history[0], "w test.txt")
}
})
t.Run("multiple commands prepended to history", func(t *testing.T) {
m := NewMockModel()
m.ModeVal = core.CommandMode
m.CommandVal = "w file1.txt"
m.CommandHistoryList = []string{}
m.CommandHistoryCur = 0
registry := &mockRegistry{}
action := CommandExecute{Registry: registry}
// Execute first command
action.Execute(m)
// Execute second command
m.CommandVal = "q"
action.Execute(m)
// Execute third command
m.CommandVal = "set number"
action.Execute(m)
history := m.CommandHistory()
if len(history) != 3 {
t.Errorf("History length = %d, want 3", len(history))
}
// Most recent should be first (prepended)
want := []string{"set number", "q", "w file1.txt"}
for i, cmd := range want {
if history[i] != cmd {
t.Errorf("History[%d] = %q, want %q", i, history[i], cmd)
}
}
})
t.Run("empty command not added to history", func(t *testing.T) {
m := NewMockModel()
m.ModeVal = core.CommandMode
m.CommandVal = ""
m.CommandHistoryList = []string{"previous command"}
m.CommandHistoryCur = 0
registry := &mockRegistry{}
action := CommandExecute{Registry: registry}
action.Execute(m)
history := m.CommandHistory()
if len(history) != 1 {
t.Errorf("History length = %d, want 1 (empty command should not be added)", len(history))
}
if history[0] != "previous command" {
t.Errorf("History[0] = %q, want %q", history[0], "previous command")
}
})
t.Run("history cursor resets on execute", func(t *testing.T) {
m := NewMockModel()
m.ModeVal = core.CommandMode
m.CommandVal = "w"
m.CommandHistoryList = []string{}
m.CommandHistoryCur = 5 // Set to non-zero
registry := &mockRegistry{}
action := CommandExecute{Registry: registry}
action.Execute(m)
if m.CommandHistoryCursor() != 0 {
t.Errorf("CommandHistoryCursor = %d, want 0 after execute", m.CommandHistoryCursor())
}
})
t.Run("duplicate commands are added to history", func(t *testing.T) {
m := NewMockModel()
m.ModeVal = core.CommandMode
m.CommandVal = "w"
m.CommandHistoryList = []string{"w"}
m.CommandHistoryCur = 0
registry := &mockRegistry{}
action := CommandExecute{Registry: registry}
action.Execute(m)
history := m.CommandHistory()
if len(history) != 2 {
t.Errorf("History length = %d, want 2 (duplicates should be added)", len(history))
}
if history[0] != "w" || history[1] != "w" {
t.Errorf("History = %v, want ['w', 'w']", history)
}
})
}