Gim/internal/action/mock.go
Hayden Hargreaves 64c448c639
All checks were successful
Run Test Suite / test (push) Successful in 37s
test: updated tests and pulled theme into EditorSettings.
2026-04-08 17:19:32 -07:00

163 lines
6.3 KiB
Go

package action
import (
"git.gophernest.net/azpect/TextEditor/internal/core"
"git.gophernest.net/azpect/TextEditor/internal/theme"
tea "github.com/charmbracelet/bubbletea"
)
// MockModel is a shared test implementation of the Model interface.
// Used by test files across multiple packages to avoid duplication.
// All fields are exported to allow direct manipulation in tests.
type MockModel struct {
WindowsList []*core.Window
ActiveWindowVal *core.Window
BuffersList []*core.Buffer
SettingsVal core.EditorSettings
ModeVal core.Mode
RegistersMap map[rune]core.Register
InsertKeysList []string
CommandVal string
CommandCursorVal int
CommandOutputVal *core.CommandOutput
CommandHistoryList []string
CommandHistoryCur int
LastFindVal core.LastFindCommand
ThemesMap map[string]theme.EditorTheme
LastChangeKeysList []string
}
// NewMockModel creates a mock with an empty buffer and 24x80 window.
func NewMockModel() *MockModel {
buf := core.NewBufferBuilder().
WithLines([]string{""}).
Build()
win := core.NewWindowBuilder().
WithBuffer(&buf).
WithHeight(24).
WithWidth(80).
Build()
return &MockModel{
WindowsList: []*core.Window{&win},
ActiveWindowVal: &win,
BuffersList: []*core.Buffer{&buf},
SettingsVal: core.NewDefaultSettings(),
ModeVal: core.NormalMode,
RegistersMap: core.DefaultRegisters(),
ThemesMap: map[string]theme.EditorTheme{"default": {}},
}
}
// NewMockModelWithBuffer creates a mock with a custom buffer.
func NewMockModelWithBuffer(buf *core.Buffer) *MockModel {
win := core.NewWindowBuilder().
WithBuffer(buf).
WithHeight(24).
WithWidth(80).
Build()
return &MockModel{
WindowsList: []*core.Window{&win},
ActiveWindowVal: &win,
BuffersList: []*core.Buffer{buf},
SettingsVal: core.NewDefaultSettings(),
ModeVal: core.NormalMode,
RegistersMap: core.DefaultRegisters(),
ThemesMap: map[string]theme.EditorTheme{"default": {}},
}
}
// NewMockModelWithWindow creates a mock with a custom window.
func NewMockModelWithWindow(win *core.Window) *MockModel {
return &MockModel{
WindowsList: []*core.Window{win},
ActiveWindowVal: win,
BuffersList: []*core.Buffer{win.Buffer},
SettingsVal: core.NewDefaultSettings(),
ModeVal: core.NormalMode,
RegistersMap: core.DefaultRegisters(),
ThemesMap: map[string]theme.EditorTheme{"default": {}},
}
}
// ==================================================
// Model Interface Implementation
// ==================================================
// Core Data Access
func (m *MockModel) Windows() []*core.Window { return m.WindowsList }
func (m *MockModel) ActiveWindow() *core.Window { return m.ActiveWindowVal }
func (m *MockModel) Buffers() []*core.Buffer { return m.BuffersList }
func (m *MockModel) SetBuffers(bufs []*core.Buffer) { m.BuffersList = bufs }
func (m *MockModel) ActiveBuffer() *core.Buffer { return m.ActiveWindowVal.Buffer }
// Insert Mode State
func (m *MockModel) InsertKeys() []string { return m.InsertKeysList }
func (m *MockModel) SetInsertKeys(keys []string) { m.InsertKeysList = keys }
func (m *MockModel) SetInsertRecording(count int, a Action) {}
func (m *MockModel) ExitInsertMode() {}
func (m *MockModel) SetLastFind(char string, forward, inclusive bool) {
m.LastFindVal = core.LastFindCommand{Char: char, Forward: forward, Inclusive: inclusive}
}
func (m *MockModel) GetLastFind() *core.LastFindCommand { return &m.LastFindVal }
// Command Mode State
func (m *MockModel) Command() string { return m.CommandVal }
func (m *MockModel) SetCommand(cmd string) { m.CommandVal = cmd }
func (m *MockModel) CommandCursor() int { return m.CommandCursorVal }
func (m *MockModel) SetCommandCursor(cur int) { m.CommandCursorVal = cur }
func (m *MockModel) CommandOutput() *core.CommandOutput { return m.CommandOutputVal }
func (m *MockModel) SetCommandOutput(out *core.CommandOutput) { m.CommandOutputVal = out }
func (m *MockModel) CommandHistory() []string { return m.CommandHistoryList }
func (m *MockModel) SetCommandHistory(history []string) { m.CommandHistoryList = history }
func (m *MockModel) CommandHistoryCursor() int { return m.CommandHistoryCur }
func (m *MockModel) SetCommandHistoryCursor(cur int) { m.CommandHistoryCur = cur }
// Editor-wide State
func (m *MockModel) Mode() core.Mode { return m.ModeVal }
func (m *MockModel) SetMode(mode core.Mode) { m.ModeVal = mode }
func (m *MockModel) Settings() core.EditorSettings { return m.SettingsVal }
func (m *MockModel) SetSettings(s core.EditorSettings) { m.SettingsVal = s }
func (m *MockModel) Theme() (string, theme.EditorTheme) {
if m.ThemesMap != nil {
if t, ok := m.ThemesMap[m.SettingsVal.CurrentTheme]; ok {
return m.SettingsVal.CurrentTheme, t
}
if t, ok := m.ThemesMap["default"]; ok {
return "default", t
}
}
return m.SettingsVal.CurrentTheme, theme.EditorTheme{}
}
func (m *MockModel) SetTheme(name string) { m.SettingsVal.CurrentTheme = name }
func (m *MockModel) Themes() map[string]theme.EditorTheme {
if m.ThemesMap == nil {
m.ThemesMap = map[string]theme.EditorTheme{}
}
return m.ThemesMap
}
func (m *MockModel) SetThemes(t map[string]theme.EditorTheme) { m.ThemesMap = t }
// Registers
func (m *MockModel) Registers() map[rune]core.Register { return m.RegistersMap }
func (m *MockModel) GetRegister(name rune) (core.Register, bool) {
reg, ok := m.RegistersMap[name]
return reg, ok
}
func (m *MockModel) SetRegister(name rune, t core.RegisterType, cnt []string) error {
m.RegistersMap[name] = core.Register{Type: t, Content: cnt}
return nil
}
func (m *MockModel) UpdateDefaultRegister(t core.RegisterType, cnt []string) {
m.RegistersMap['"'] = core.Register{Type: t, Content: cnt}
}
// Dot operator
func (m *MockModel) SetLastChangeKeys(keys []string) { m.LastChangeKeysList = keys }
func (m *MockModel) LastChangeKeys() []string { return m.LastChangeKeysList }
func (m *MockModel) ClearLastChangeKeys() { m.LastChangeKeysList = []string{} }
func (m *MockModel) HandleKey(key string) tea.Cmd { return nil }