gitstack

grindlemire/go-tui code browser

8.2 KB Go 317 lines 2026-06-03 ยท d15bb9f 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package tui

import (
	"testing"
)

// TestIntegration_MockReaderToFocusManager tests the full flow from
// MockEventReader through FocusManager to mock Focusable elements.
func TestIntegration_MockReaderToFocusManager(t *testing.T) {
	type tc struct {
		events          []Event
		expectedHandled []bool
		focusedAfter    string
		focusCycles     int // number of Next() calls to make
	}

	tests := map[string]tc{
		"single event to focused element": {
			events:          []Event{KeyEvent{Key: KeyEnter}},
			expectedHandled: []bool{true},
			focusedAfter:    "a",
			focusCycles:     1, // Need 1 Next() to seed focus on a
		},
		"multiple events same element": {
			events: []Event{
				KeyEvent{Key: KeyEnter},
				KeyEvent{Key: KeyTab},
				KeyEvent{Key: KeyEscape},
			},
			expectedHandled: []bool{true, true, true},
			focusedAfter:    "a",
			focusCycles:     1, // Need 1 Next() to seed focus on a
		},
		"events after focus change": {
			events: []Event{
				KeyEvent{Key: KeyEnter},
			},
			expectedHandled: []bool{true},
			focusedAfter:    "b",
			focusCycles:     2, // 1 to seed on a, 1 to move to b
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			// Create mock focusable elements
			elemA := newMockFocusable("a", true)
			elemA.handled = true
			elemB := newMockFocusable("b", true)
			elemB.handled = true

			// Create FocusManager with elements
			fm := newFocusManager()
			fm.Register(elemA)
			fm.Register(elemB)

			// Cycle focus if requested
			for i := 0; i < tt.focusCycles; i++ {
				fm.Next()
			}

			// Create MockEventReader with events
			reader := NewMockEventReader(tt.events...)

			// Process events through the system
			for i, expectedHandled := range tt.expectedHandled {
				event, ok := reader.PollEvent(0)
				if !ok {
					t.Fatalf("Event %d: PollEvent returned false unexpectedly", i)
				}

				handled := fm.Dispatch(event)
				if handled != expectedHandled {
					t.Errorf("Event %d: Dispatch() = %v, want %v", i, handled, expectedHandled)
				}
			}

			// Verify final focus state
			focused := fm.Focused()
			if focused == nil {
				t.Fatal("Focused() returned nil")
			}
			mf := focused.(*mockFocusable)
			if mf.id != tt.focusedAfter {
				t.Errorf("Final focused element = %q, want %q", mf.id, tt.focusedAfter)
			}
		})
	}
}

// TestIntegration_ResizeEventUpdatesBuffer tests that ResizeEvent
// properly updates buffer dimensions through App.Dispatch.
func TestIntegration_ResizeEventUpdatesBuffer(t *testing.T) {
	type tc struct {
		initialWidth  int
		initialHeight int
		resizeWidth   int
		resizeHeight  int
	}

	tests := map[string]tc{
		"grow terminal": {
			initialWidth:  80,
			initialHeight: 24,
			resizeWidth:   120,
			resizeHeight:  40,
		},
		"shrink terminal": {
			initialWidth:  120,
			initialHeight: 40,
			resizeWidth:   80,
			resizeHeight:  24,
		},
		"change width only": {
			initialWidth:  80,
			initialHeight: 24,
			resizeWidth:   100,
			resizeHeight:  24,
		},
		"change height only": {
			initialWidth:  80,
			initialHeight: 24,
			resizeWidth:   80,
			resizeHeight:  30,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			buffer := NewBuffer(tt.initialWidth, tt.initialHeight)
			app := &App{
				buffer: buffer,
				focus:  newFocusManager(),
			}

			// Create resize event
			event := ResizeEvent{Width: tt.resizeWidth, Height: tt.resizeHeight}

			// Dispatch the event
			handled := app.Dispatch(event)
			if !handled {
				t.Error("Dispatch(ResizeEvent) should return true")
			}

			// Verify buffer dimensions
			w, h := app.buffer.Size()
			if w != tt.resizeWidth || h != tt.resizeHeight {
				t.Errorf("Buffer size = (%d, %d), want (%d, %d)", w, h, tt.resizeWidth, tt.resizeHeight)
			}
		})
	}
}

// TestIntegration_EventDispatchToMultipleFocusables tests event dispatch
// across multiple focusable elements with focus navigation.
func TestIntegration_EventDispatchToMultipleFocusables(t *testing.T) {
	// Create elements
	elem1 := newMockFocusable("elem1", true)
	elem1.handled = true
	elem2 := newMockFocusable("elem2", true)
	elem2.handled = true
	elem3 := newMockFocusable("elem3", true)
	elem3.handled = true

	fm := newFocusManager()
	fm.Register(elem1)
	fm.Register(elem2)
	fm.Register(elem3)

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

	// Verify focus on elem1
	if fm.Focused().(*mockFocusable).id != "elem1" {
		t.Errorf("Focus should be elem1, got %s", fm.Focused().(*mockFocusable).id)
	}

	// Send event to elem1
	event1 := KeyEvent{Key: KeyRune, Rune: 'a'}
	fm.Dispatch(event1)
	if elem1.lastEvent == nil {
		t.Error("elem1 should have received the event")
	}

	// Move focus to elem2
	fm.Next()
	if fm.Focused().(*mockFocusable).id != "elem2" {
		t.Error("Focus should have moved to elem2")
	}

	// Send event to elem2
	event2 := KeyEvent{Key: KeyRune, Rune: 'b'}
	fm.Dispatch(event2)
	if elem2.lastEvent == nil {
		t.Error("elem2 should have received the event")
	}

	// elem1's last event should still be event1
	if elem1.lastEvent.(KeyEvent).Rune != 'a' {
		t.Error("elem1's last event should still be 'a'")
	}

	// Move focus to elem3
	fm.Next()
	if fm.Focused().(*mockFocusable).id != "elem3" {
		t.Error("Focus should have moved to elem3")
	}

	// Move back to elem1 (wrap around)
	fm.Next()
	if fm.Focused().(*mockFocusable).id != "elem1" {
		t.Error("Focus should have wrapped to elem1")
	}
}

// TestIntegration_FocusCycleWithNonFocusable tests focus cycling
// correctly skips non-focusable elements.
func TestIntegration_FocusCycleWithNonFocusable(t *testing.T) {
	// Create elements - some not focusable
	elem1 := newMockFocusable("elem1", true)
	elem2 := newMockFocusable("elem2", false) // Not focusable
	elem3 := newMockFocusable("elem3", true)
	elem4 := newMockFocusable("elem4", false) // Not focusable
	elem5 := newMockFocusable("elem5", true)

	fm := newFocusManager()
	fm.Register(elem1)
	fm.Register(elem2)
	fm.Register(elem3)
	fm.Register(elem4)
	fm.Register(elem5)

	// Track focus order (no auto-focus, first Next() goes to elem1)
	focusOrder := []string{}

	// Navigate forward through all focusable elements
	for range 6 { // More iterations than needed to test wraparound
		fm.Next()
		focusOrder = append(focusOrder, fm.Focused().(*mockFocusable).id)
	}

	// Expected order: elem1 -> elem3 -> elem5 -> elem1 -> elem3 -> elem5
	expected := []string{"elem1", "elem3", "elem5", "elem1", "elem3", "elem5"}
	if len(focusOrder) != len(expected) {
		t.Fatalf("Focus order length = %d, want %d", len(focusOrder), len(expected))
	}
	for i, id := range expected {
		if focusOrder[i] != id {
			t.Errorf("Focus order[%d] = %q, want %q", i, focusOrder[i], id)
		}
	}
}

// TestIntegration_EventReaderWithFocusManager tests the integration
// of MockEventReader with FocusManager for a sequence of events.
func TestIntegration_EventReaderWithFocusManager(t *testing.T) {
	events := []Event{
		KeyEvent{Key: KeyRune, Rune: 'h'},
		KeyEvent{Key: KeyRune, Rune: 'e'},
		KeyEvent{Key: KeyRune, Rune: 'l'},
		KeyEvent{Key: KeyRune, Rune: 'l'},
		KeyEvent{Key: KeyRune, Rune: 'o'},
		KeyEvent{Key: KeyEnter},
	}

	reader := NewMockEventReader(events...)
	elem := newMockFocusable("input", true)
	elem.handled = true
	fm := newFocusManager()
	fm.Register(elem)
	fm.SetFocus(elem)

	// Process all events
	eventCount := 0
	for {
		event, ok := reader.PollEvent(0)
		if !ok {
			break
		}
		fm.Dispatch(event)
		eventCount++
	}

	// Verify all events were processed
	if eventCount != len(events) {
		t.Errorf("Processed %d events, want %d", eventCount, len(events))
	}

	// Last event should be KeyEnter
	lastEvent := elem.lastEvent.(KeyEvent)
	if lastEvent.Key != KeyEnter {
		t.Errorf("Last event key = %v, want KeyEnter", lastEvent.Key)
	}
}

// TestIntegration_UnhandledEventPropagation tests that unhandled events
// return false, allowing applications to implement bubbling/fallback.
func TestIntegration_UnhandledEventPropagation(t *testing.T) {
	elem := newMockFocusable("elem", true)
	elem.handled = false // Element doesn't handle events

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

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

	if handled {
		t.Error("Dispatch should return false when element doesn't handle event")
	}

	// Event should still have been passed to element
	if elem.lastEvent == nil {
		t.Error("Event should have been passed to element even if not handled")
	}
}