gitstack

grindlemire/go-tui code browser

4.8 KB Go 222 lines 2026-03-08 · 66d3508 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package tui

import (
	"testing"
)

func TestFocusManager_Unregister(t *testing.T) {
	type tc struct {
		elements          []*mockFocusable
		unregisterIndex   int
		expectedFocusedID string
		expectBlurCall    bool
	}

	tests := map[string]tc{
		"unregister non-focused element": {
			elements: []*mockFocusable{
				newMockFocusable("a", true),
				newMockFocusable("b", true),
			},
			unregisterIndex:   1,
			expectedFocusedID: "a",
			expectBlurCall:    false,
		},
		"unregister focused element moves to next": {
			elements: []*mockFocusable{
				newMockFocusable("a", true),
				newMockFocusable("b", true),
			},
			unregisterIndex:   0,
			expectedFocusedID: "b",
			expectBlurCall:    true,
		},
		"unregister last focused wraps to first": {
			elements: []*mockFocusable{
				newMockFocusable("a", true),
				newMockFocusable("b", true),
			},
			unregisterIndex:   1, // Move focus to b first, then unregister
			expectedFocusedID: "a",
			expectBlurCall:    false, // b was not focused when unregistered
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			fm := newFocusManager()
			registerAll(fm, tt.elements...)

			// Seed focus on element 0 (no auto-focus)
			fm.SetFocus(tt.elements[0])

			toUnregister := tt.elements[tt.unregisterIndex]
			initialBlurCalls := toUnregister.blurCalls

			fm.Unregister(toUnregister)

			focused := fm.Focused()
			if focused == nil {
				t.Fatal("Focused() returned nil after unregister")
			}

			mf := focused.(*mockFocusable)
			if mf.id != tt.expectedFocusedID {
				t.Errorf("After Unregister(), focused = %q, want %q", mf.id, tt.expectedFocusedID)
			}

			if tt.expectBlurCall && toUnregister.blurCalls == initialBlurCalls {
				t.Error("Expected Blur() to be called on unregistered element")
			}
		})
	}
}

func TestFocusManager_UnregisterLast(t *testing.T) {
	a := newMockFocusable("a", true)
	fm := newFocusManager()
	fm.Register(a)

	fm.Unregister(a)

	if fm.Focused() != nil {
		t.Error("After unregistering last element, Focused() should be nil")
	}
}

func TestFocusManager_Dispatch(t *testing.T) {
	type tc struct {
		handled        bool
		expectedReturn bool
	}

	tests := map[string]tc{
		"event handled": {
			handled:        true,
			expectedReturn: true,
		},
		"event not handled": {
			handled:        false,
			expectedReturn: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			mock := newMockFocusable("a", true)
			mock.handled = tt.handled

			fm := newFocusManager()
			fm.Register(mock)
			fm.SetFocus(mock)

			event := KeyEvent{Key: KeyEnter}
			result := fm.Dispatch(event)

			if result != tt.expectedReturn {
				t.Errorf("Dispatch() = %v, want %v", result, tt.expectedReturn)
			}

			ke, ok := mock.lastEvent.(KeyEvent)
			if !ok {
				t.Fatal("HandleEvent was not called with KeyEvent")
			}
			if ke.Key != KeyEnter {
				t.Errorf("HandleEvent received wrong event: %+v", ke)
			}
		})
	}
}

func TestFocusManager_DispatchNoFocusedElement(t *testing.T) {
	fm := newFocusManager() // Empty manager

	result := fm.Dispatch(KeyEvent{Key: KeyEnter})

	if result != false {
		t.Error("Dispatch() with no focused element should return false")
	}
}

func TestFocusManager_BlurOnFocusChange(t *testing.T) {
	a := newMockFocusable("a", true)
	b := newMockFocusable("b", true)

	fm := newFocusManager()
	fm.Register(a)
	fm.Register(b)

	// Seed focus on a
	fm.SetFocus(a)

	// Reset counters after SetFocus
	a.focusCalls = 0
	a.blurCalls = 0

	// Move to next (b)
	fm.Next()

	// a should be blurred
	if a.blurCalls != 1 {
		t.Errorf("After Next(), blurCalls for a = %d, want 1", a.blurCalls)
	}

	// b should be focused
	if b.focusCalls != 1 {
		t.Errorf("After Next(), focusCalls for b = %d, want 1", b.focusCalls)
	}
}

func TestFocusManager_SkipsNonFocusableInCycle(t *testing.T) {
	a := newMockFocusable("a", true)
	b := newMockFocusable("b", false) // Not focusable
	c := newMockFocusable("c", true)

	fm := newFocusManager()
	fm.Register(a)
	fm.Register(b)
	fm.Register(c)

	// First Next() focuses a
	fm.Next()
	focused := fm.Focused().(*mockFocusable)
	if focused.id != "a" {
		t.Fatalf("After first Next(), focus = %q, want 'a'", focused.id)
	}

	// Next should skip b and go to c
	fm.Next()
	focused = fm.Focused().(*mockFocusable)
	if focused.id != "c" {
		t.Errorf("After second Next(), focus = %q, want 'c'", focused.id)
	}

	// Next should wrap to a (skip b)
	fm.Next()
	focused = fm.Focused().(*mockFocusable)
	if focused.id != "a" {
		t.Errorf("After third Next(), focus = %q, want 'a'", focused.id)
	}
}

func TestFocusManager_EmptyNext(t *testing.T) {
	fm := newFocusManager()

	// Should not panic
	fm.Next()

	if fm.Focused() != nil {
		t.Error("Next() on empty manager should not set focus")
	}
}

func TestFocusManager_EmptyPrev(t *testing.T) {
	fm := newFocusManager()

	// Should not panic
	fm.Prev()

	if fm.Focused() != nil {
		t.Error("Prev() on empty manager should not set focus")
	}
}