gitstack

grindlemire/go-tui code browser

5.6 KB Go 254 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
package tui

import (
	"testing"
	"time"
)

// mockViewable implements Viewable interface for testing
type mockViewable struct {
	root     *Element
	watchers []Watcher
}

func newMockViewable(root *Element, watchers ...Watcher) *mockViewable {
	return &mockViewable{root: root, watchers: watchers}
}

func (m *mockViewable) GetRoot() *Element {
	return m.root
}

func (m *mockViewable) GetWatchers() []Watcher {
	return m.watchers
}

// mockWatcher tracks whether Start was called
type mockWatcher struct {
	started     bool
	eventQueue  chan<- func()
	stopCh      <-chan struct{}
	startCalled chan struct{} // signaled when Start is called
}

func newMockWatcher() *mockWatcher {
	return &mockWatcher{
		startCalled: make(chan struct{}),
	}
}

func (m *mockWatcher) Start(eventQueue chan<- func(), stopCh <-chan struct{}) {
	m.started = true
	m.eventQueue = eventQueue
	m.stopCh = stopCh
	close(m.startCalled)
}

type mockComponentRoot struct{}

func (m *mockComponentRoot) Render(app *App) *Element { return New() }

type stopAwareWatcher struct {
	stopped chan struct{}
}

func newStopAwareWatcher() *stopAwareWatcher {
	return &stopAwareWatcher{stopped: make(chan struct{}, 1)}
}

func (w *stopAwareWatcher) Start(eventQueue chan<- func(), stopCh <-chan struct{}) {
	go func() {
		<-stopCh
		w.stopped <- struct{}{}
	}()
}

func TestApp_SetRoot_WithViewable(t *testing.T) {
	type tc struct {
		numWatchers int
		expectRoot  bool
	}

	tests := map[string]tc{
		"viewable with no watchers": {
			numWatchers: 0,
			expectRoot:  true,
		},
		"viewable with one watcher": {
			numWatchers: 1,
			expectRoot:  true,
		},
		"viewable with multiple watchers": {
			numWatchers: 3,
			expectRoot:  true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			app := &App{
				focus:        newFocusManager(),
				buffer:       NewBuffer(80, 24),
				watcherQueue: make(chan func(), 256),
				merged:       make(chan Event, 256),
				stopCh:       make(chan struct{}),
			}

			root := New()
			watchers := make([]Watcher, tt.numWatchers)
			mockWatchers := make([]*mockWatcher, tt.numWatchers)
			for i := 0; i < tt.numWatchers; i++ {
				mw := newMockWatcher()
				mockWatchers[i] = mw
				watchers[i] = mw
			}

			view := newMockViewable(root, watchers...)
			app.SetRootView(view)

			// Verify root was set
			if tt.expectRoot && app.Root() != root {
				t.Error("Root() should return the root from Viewable")
			}

			// Verify all watchers were started
			for i, mw := range mockWatchers {
				if !mw.started {
					t.Errorf("Watcher %d was not started", i)
				}
				if mw.eventQueue != app.watcherQueue {
					t.Errorf("Watcher %d received wrong eventQueue", i)
				}
			}
		})
	}
}

func TestApp_SetRoot_WithElement(t *testing.T) {
	app := &App{
		focus:        newFocusManager(),
		buffer:       NewBuffer(80, 24),
		watcherQueue: make(chan func(), 256),
		merged:       make(chan Event, 256),
		stopCh:       make(chan struct{}),
	}

	root := New()
	app.SetRoot(root)

	if app.Root() != root {
		t.Error("Root() should return the element passed to SetRoot()")
	}
}

func TestApp_Run_EventLoopLogic(t *testing.T) {
	// Test the core event loop logic without a real terminal.
	// We simulate what Run() does: process events from events channel, check dirty, etc.

	app := &App{
		focus:        newFocusManager(),
		buffer:       NewBuffer(80, 24),
		merged:       make(chan Event, 256),
		watcherQueue: make(chan func(), 256),
		stopCh:       make(chan struct{}),
		stopped:      false,
	}

	// Queue an event
	var eventProcessed bool
	app.merged <- UpdateEvent{fn: func() {
		eventProcessed = true
	}}

	// Process one event manually (simulating the Run loop)
	select {
	case ev := <-app.merged:
		app.Dispatch(ev)
	case <-time.After(100 * time.Millisecond):
		t.Fatal("Expected event in queue")
	}

	if !eventProcessed {
		t.Error("Event was not processed")
	}

	// Test that Stop() closes stopCh
	app.Stop()

	select {
	case <-app.stopCh:
		// Expected - stopCh was closed
	default:
		t.Error("Stop() should close stopCh")
	}
}

func TestApp_Stop_IsIdempotent(t *testing.T) {
	app := &App{
		focus:        newFocusManager(),
		buffer:       NewBuffer(80, 24),
		merged:       make(chan Event, 256),
		watcherQueue: make(chan func(), 256),
		stopCh:       make(chan struct{}),
		stopped:      false,
	}

	// First call should work
	app.Stop()

	if !app.stopped {
		t.Error("Stop() should set stopped to true")
	}

	// Second call should not panic
	app.Stop()

	// Still stopped
	if !app.stopped {
		t.Error("stopped should still be true after second Stop() call")
	}
}

func TestApp_SetRoot_ClearsRootComponent(t *testing.T) {
	app := &App{
		focus:        newFocusManager(),
		buffer:       NewBuffer(80, 24),
		merged:       make(chan Event, 256),
		watcherQueue: make(chan func(), 256),
		stopCh:       make(chan struct{}),
		mounts:       newMountState(),
	}

	app.SetRootComponent(&mockComponentRoot{})
	if app.rootComponent == nil {
		t.Fatal("expected rootComponent to be set after SetRootComponent")
	}

	app.SetRoot(New())
	if app.rootComponent != nil {
		t.Fatal("expected rootComponent to be cleared after SetRoot")
	}
}

func TestApp_SetRoot_StopsPreviousRootWatchers(t *testing.T) {
	app := &App{
		focus:        newFocusManager(),
		buffer:       NewBuffer(80, 24),
		merged:       make(chan Event, 256),
		watcherQueue: make(chan func(), 256),
		stopCh:       make(chan struct{}),
		mounts:       newMountState(),
	}

	w1 := newStopAwareWatcher()
	view1 := newMockViewable(New(), w1)
	app.SetRootView(view1)

	app.SetRoot(New())

	select {
	case <-w1.stopped:
	case <-time.After(200 * time.Millisecond):
		t.Fatal("expected previous root watcher to be stopped")
	}
}