mirror of
https://github.com/shokinn/hosts-go.git
synced 2025-08-23 08:33:02 +00:00
112 lines
3 KiB
Go
112 lines
3 KiB
Go
package tests
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"hosts-go/internal/core"
|
|
"hosts-go/internal/tui"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestModelSelection(t *testing.T) {
|
|
sample := `127.0.0.1 localhost
|
|
192.168.1.10 example.com`
|
|
lines := strings.Split(sample, "\n")
|
|
hf, _, err := core.ParseHostsContent(lines)
|
|
require.NoError(t, err)
|
|
|
|
m := tui.NewModel(hf, "")
|
|
require.NotNil(t, m.SelectedEntry())
|
|
assert.Equal(t, "localhost", m.SelectedEntry().Hostname)
|
|
|
|
// Move selection down
|
|
nm, _ := m.Update(tea.KeyMsg{Type: tea.KeyDown})
|
|
m = nm.(tui.Model)
|
|
assert.Equal(t, "example.com", m.SelectedEntry().Hostname)
|
|
}
|
|
|
|
func TestViewModeStatusBar(t *testing.T) {
|
|
sample := `127.0.0.1 localhost
|
|
# 192.168.1.10 example.com`
|
|
lines := strings.Split(sample, "\n")
|
|
hf, _, err := core.ParseHostsContent(lines)
|
|
require.NoError(t, err)
|
|
|
|
m := tui.NewModel(hf, "")
|
|
nm, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
|
|
m = nm.(tui.Model)
|
|
view := m.View()
|
|
|
|
assert.Contains(t, view, "VIEW MODE")
|
|
assert.Contains(t, view, "[✓] localhost")
|
|
assert.Contains(t, view, "[ ] example.com")
|
|
}
|
|
|
|
func TestPaneSwitching(t *testing.T) {
|
|
sample := "127.0.0.1 localhost\n192.168.1.10 example.com"
|
|
lines := strings.Split(sample, "\n")
|
|
hf, _, err := core.ParseHostsContent(lines)
|
|
require.NoError(t, err)
|
|
|
|
m := tui.NewModel(hf, "")
|
|
// Switch focus to detail pane
|
|
nm, _ := m.Update(tea.KeyMsg{Type: tea.KeyTab})
|
|
m = nm.(tui.Model)
|
|
|
|
// Attempt to move down; selection should remain on first entry
|
|
nm, _ = m.Update(tea.KeyMsg{Type: tea.KeyDown})
|
|
m = nm.(tui.Model)
|
|
assert.Equal(t, "localhost", m.SelectedEntry().Hostname)
|
|
}
|
|
|
|
func TestEditModeToggleAndActivation(t *testing.T) {
|
|
sample := "127.0.0.1 localhost\n192.168.1.10 example.com"
|
|
lines := strings.Split(sample, "\n")
|
|
hf, _, err := core.ParseHostsContent(lines)
|
|
require.NoError(t, err)
|
|
|
|
m := tui.NewModel(hf, "")
|
|
|
|
// enter edit mode
|
|
nm, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlE})
|
|
m = nm.(tui.Model)
|
|
assert.Equal(t, tui.EditMode, m.Mode())
|
|
|
|
// toggle active state of first entry
|
|
nm, _ = m.Update(tea.KeyMsg{Type: tea.KeySpace})
|
|
m = nm.(tui.Model)
|
|
assert.False(t, m.SelectedEntry().Active)
|
|
|
|
// status bar should reflect edit mode
|
|
view := m.View()
|
|
assert.Contains(t, view, "EDIT MODE")
|
|
}
|
|
|
|
func TestSaveWritesToFile(t *testing.T) {
|
|
content := "127.0.0.1 localhost\n192.168.1.10 example.com\n"
|
|
tmp, err := os.CreateTemp(t.TempDir(), "hosts")
|
|
require.NoError(t, err)
|
|
_, err = tmp.WriteString(content)
|
|
require.NoError(t, err)
|
|
require.NoError(t, tmp.Close())
|
|
|
|
hf, _, err := core.ParseHostsFile(tmp.Name())
|
|
require.NoError(t, err)
|
|
|
|
m := tui.NewModel(hf, tmp.Name())
|
|
|
|
nm, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlE})
|
|
m = nm.(tui.Model)
|
|
nm, _ = m.Update(tea.KeyMsg{Type: tea.KeySpace})
|
|
m = nm.(tui.Model)
|
|
_, _ = m.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
|
|
|
|
saved, _, err := core.ParseHostsFile(tmp.Name())
|
|
require.NoError(t, err)
|
|
assert.False(t, saved.Entries[0].Active)
|
|
}
|