overwrite_test.go

  1package screens
  2
  3import (
  4	"testing"
  5
  6	tea "charm.land/bubbletea/v2"
  7
  8	"git.secluded.site/keld/internal/ui"
  9)
 10
 11func TestOverwriteTitle(t *testing.T) {
 12	t.Parallel()
 13
 14	o := NewOverwrite(testStyles())
 15	if got := o.Title(); got != "Overwrite existing files?" {
 16		t.Errorf("Title() = %q, want %q", got, "Overwrite existing files?")
 17	}
 18}
 19
 20func TestOverwriteSelectionEmpty(t *testing.T) {
 21	t.Parallel()
 22
 23	o := NewOverwrite(testStyles())
 24	if got := o.Selection(); got != "" {
 25		t.Errorf("Selection() before interaction = %q, want empty", got)
 26	}
 27}
 28
 29func TestOverwriteKeyBindings(t *testing.T) {
 30	t.Parallel()
 31
 32	o := NewOverwrite(testStyles())
 33	bindings := o.KeyBindings()
 34
 35	if len(bindings) == 0 {
 36		t.Fatal("KeyBindings() returned no bindings")
 37	}
 38}
 39
 40func TestOverwriteEscReturnsBack(t *testing.T) {
 41	t.Parallel()
 42
 43	o := NewOverwrite(testStyles())
 44	o.Init()
 45	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
 46
 47	_, cmd := o.Update(tea.KeyPressMsg{Code: tea.KeyEscape})
 48
 49	if cmd == nil {
 50		t.Fatal("expected BackCmd on Esc")
 51	}
 52	msg := cmd()
 53	if _, ok := msg.(ui.BackMsg); !ok {
 54		t.Errorf("cmd produced %T, want ui.BackMsg", msg)
 55	}
 56}
 57
 58func TestOverwriteCompleteReturnsDone(t *testing.T) {
 59	t.Parallel()
 60
 61	o := NewOverwrite(testStyles())
 62	o.Init()
 63	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
 64
 65	// Default selection is the first option (if-changed). Press enter.
 66	var screen ui.Screen
 67	var cmd tea.Cmd
 68	screen, cmd = o.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
 69	_, cmd = drain(screen.(*Overwrite), cmd)
 70
 71	if cmd == nil {
 72		t.Fatal("expected DoneCmd after form completion")
 73	}
 74	msg := cmd()
 75	if _, ok := msg.(ui.DoneMsg); !ok {
 76		t.Errorf("cmd produced %T, want ui.DoneMsg", msg)
 77	}
 78}
 79
 80func TestOverwriteDefaultIsIfChanged(t *testing.T) {
 81	t.Parallel()
 82
 83	o := NewOverwrite(testStyles())
 84	o.Init()
 85	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
 86
 87	// Press enter on the default selection.
 88	var screen ui.Screen
 89	var cmd tea.Cmd
 90	screen, cmd = o.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
 91	o, _ = drain(screen.(*Overwrite), cmd)
 92
 93	if got := o.Value(); got != "if-changed" {
 94		t.Errorf("Value() = %q, want %q (default should be if-changed)", got, "if-changed")
 95	}
 96}
 97
 98func TestOverwriteSelectionShowsValue(t *testing.T) {
 99	t.Parallel()
100
101	o := NewOverwrite(testStyles())
102	o.Init()
103	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
104
105	// Press enter on the default.
106	var screen ui.Screen
107	var cmd tea.Cmd
108	screen, cmd = o.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
109	o, _ = drain(screen.(*Overwrite), cmd)
110
111	if got := o.Selection(); got != "if-changed" {
112		t.Errorf("Selection() = %q, want %q", got, "if-changed")
113	}
114}
115
116func TestOverwriteReinitAfterBack(t *testing.T) {
117	t.Parallel()
118
119	o := NewOverwrite(testStyles())
120	o.Init()
121	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
122
123	// Complete the form.
124	var screen ui.Screen
125	var cmd tea.Cmd
126	screen, cmd = o.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
127	o, _ = drain(screen.(*Overwrite), cmd)
128
129	// Re-init (simulates back navigation).
130	o.Init()
131	o.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
132
133	// Should be functional again.
134	screen, cmd = o.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
135	_, cmd = drain(screen.(*Overwrite), cmd)
136
137	if cmd == nil {
138		t.Fatal("expected DoneCmd after re-init")
139	}
140	msg := cmd()
141	if _, ok := msg.(ui.DoneMsg); !ok {
142		t.Errorf("after re-init, cmd produced %T, want ui.DoneMsg", msg)
143	}
144}