gitstack

grindlemire/go-tui code browser

6.2 KB Go 318 lines 2026-07-02 · 98c07de 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
318
package tui

import (
	"testing"
)

// --- OnUpdate Hook Tests ---

func TestElement_SetOnUpdate_CalledDuringRender(t *testing.T) {
	updateCalled := false
	e := New(WithSize(10, 10))
	e.SetOnUpdate(func() {
		updateCalled = true
	})

	buf := NewBuffer(20, 20)
	e.Render(buf, 20, 20)

	if !updateCalled {
		t.Error("onUpdate hook should be called during Render()")
	}
}

func TestElement_Render_NilOnUpdateDoesNotPanic(t *testing.T) {
	// Create an element without an onUpdate hook
	e := New(WithSize(10, 10))

	buf := NewBuffer(20, 20)

	// This should not panic
	e.Render(buf, 20, 20)
}

func TestElement_WithOnUpdate_SetsHook(t *testing.T) {
	updateCalled := false
	e := New(
		WithSize(10, 10),
		WithOnUpdate(func() {
			updateCalled = true
		}),
	)

	buf := NewBuffer(20, 20)
	e.Render(buf, 20, 20)

	if !updateCalled {
		t.Error("WithOnUpdate should set the onUpdate hook")
	}
}

func TestElement_OnUpdate_CalledOnEachRender(t *testing.T) {
	callCount := 0
	e := New(WithSize(10, 10))
	e.SetOnUpdate(func() {
		callCount++
	})

	buf := NewBuffer(20, 20)

	// Render multiple times
	e.Render(buf, 20, 20)
	e.Render(buf, 20, 20)
	e.Render(buf, 20, 20)

	if callCount != 3 {
		t.Errorf("onUpdate should be called on each render, got %d calls, want 3", callCount)
	}
}

// --- Focusable Tests ---

func TestElement_WithFocusable(t *testing.T) {
	type tc struct {
		focusable bool
	}

	tests := map[string]tc{
		"focusable true":  {focusable: true},
		"focusable false": {focusable: false},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			e := New(WithFocusable(tt.focusable))

			if e.IsFocusable() != tt.focusable {
				t.Errorf("WithFocusable(%v) = %v, want %v", tt.focusable, e.IsFocusable(), tt.focusable)
			}
		})
	}
}

func TestElement_SetFocusable(t *testing.T) {
	e := New()

	if e.IsFocusable() {
		t.Error("element should not be focusable by default")
	}

	e.SetFocusable(true)
	if !e.IsFocusable() {
		t.Error("SetFocusable(true) should make element focusable")
	}

	e.SetFocusable(false)
	if e.IsFocusable() {
		t.Error("SetFocusable(false) should make element not focusable")
	}
}

func TestElement_RemoveAllChildren(t *testing.T) {
	parent := New()
	child1 := New()
	child2 := New()
	child3 := New()

	parent.AddChild(child1, child2, child3)

	if len(parent.Children()) != 3 {
		t.Fatalf("setup failed: expected 3 children, got %d", len(parent.Children()))
	}

	// Clear dirty flag to test that RemoveAllChildren marks dirty
	parent.dirty = false
	testApp.MarkDirty() // Reset global dirty
	_ = testApp.checkAndClearDirty()

	parent.RemoveAllChildren()

	if len(parent.Children()) != 0 {
		t.Errorf("RemoveAllChildren should remove all children, got %d", len(parent.Children()))
	}

	if child1.Parent() != nil {
		t.Error("removed child1's parent should be nil")
	}
	if child2.Parent() != nil {
		t.Error("removed child2's parent should be nil")
	}
	if child3.Parent() != nil {
		t.Error("removed child3's parent should be nil")
	}

	if !parent.IsDirty() {
		t.Error("RemoveAllChildren should mark parent dirty")
	}
}

func TestElement_RemoveAllChildren_Empty(t *testing.T) {
	parent := New()

	// Should not panic on empty element
	parent.RemoveAllChildren()

	if len(parent.Children()) != 0 {
		t.Error("RemoveAllChildren on empty element should result in no children")
	}
}

// --- Global Dirty Flag Tests ---

func TestElement_MarkDirty_SetsGlobalDirtyFlag(t *testing.T) {
	// Reset global dirty flag
	_ = testApp.checkAndClearDirty()

	e := New()
	e.app = testApp
	e.dirty = false // Clear local dirty flag

	e.MarkDirty()

	if !testApp.checkAndClearDirty() {
		t.Error("MarkDirty should set the global dirty flag")
	}
}

func TestElement_ScrollBy_MarksDirty(t *testing.T) {
	// Reset global dirty flag
	_ = testApp.checkAndClearDirty()

	e := New(
		WithHeight(10),
		WithScrollable(ScrollVertical),
		WithDirection(Column),
	)
	e.app = testApp
	// Set up content that exceeds viewport
	for range 20 {
		e.AddChild(New(WithHeight(1)))
	}

	// Render to compute content bounds (scrollable content needs this)
	buf := NewBuffer(80, 25)
	e.Render(buf, 80, 10)

	// Clear dirty flags
	e.dirty = false
	_ = testApp.checkAndClearDirty()

	e.ScrollBy(0, 5)

	if !testApp.checkAndClearDirty() {
		t.Error("ScrollBy should mark the global dirty flag")
	}
}

func TestElement_SetText_MarksDirty(t *testing.T) {
	// Reset global dirty flag
	_ = testApp.checkAndClearDirty()

	e := New(WithText("hello"))
	e.app = testApp

	// Clear dirty flags
	e.dirty = false
	_ = testApp.checkAndClearDirty()

	e.SetText("world")

	if !testApp.checkAndClearDirty() {
		t.Error("SetText should mark the global dirty flag")
	}
}

func TestElement_AddChild_MarksDirty(t *testing.T) {
	// Reset global dirty flag
	_ = testApp.checkAndClearDirty()

	parent := New()
	parent.app = testApp

	// Clear dirty flags
	parent.dirty = false
	_ = testApp.checkAndClearDirty()

	child := New()
	parent.AddChild(child)

	if !testApp.checkAndClearDirty() {
		t.Error("AddChild should mark the global dirty flag")
	}
}

// --- Wrap API Tests ---

func TestElement_Wrap(t *testing.T) {
	type tc struct {
		opts []Option
		want bool
	}

	tests := map[string]tc{
		"default is true": {
			opts: nil,
			want: true,
		},
		"WithWrap false": {
			opts: []Option{WithWrap(false)},
			want: false,
		},
		"WithWrap true": {
			opts: []Option{WithWrap(true)},
			want: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			e := New(tt.opts...)
			if got := e.Wrap(); got != tt.want {
				t.Errorf("Wrap() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestElement_SetWrap(t *testing.T) {
	type tc struct {
		set  bool
		want bool
	}

	tests := map[string]tc{
		"disable wrapping": {
			set:  false,
			want: false,
		},
		"enable wrapping": {
			set:  true,
			want: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			e := New(WithWrap(!tt.set))
			e.SetWrap(tt.set)
			if got := e.Wrap(); got != tt.want {
				t.Errorf("Wrap() after SetWrap(%v) = %v, want %v", tt.set, got, tt.want)
			}
		})
	}
}

func TestElement_SetWrap_MarksDirty(t *testing.T) {
	// Reset global dirty flag
	_ = testApp.checkAndClearDirty()

	e := New()
	e.app = testApp
	e.dirty = false

	e.SetWrap(false)

	if !testApp.checkAndClearDirty() {
		t.Error("SetWrap should mark the global dirty flag")
	}
}