gitstack

grindlemire/go-tui code browser

7.5 KB Go 370 lines 2026-02-14 ยท 8e3ad6d 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package tui

import (
	"testing"
)

// === Batch Tests ===

func TestBatch_DefersBindingExecution(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	var lastValue int
	s.Bind(func(v int) {
		callCount++
		lastValue = v
	})

	testApp.Batch(func() {
		// Binding should not be called during batch
		s.Set(42)
		if callCount != 0 {
			t.Errorf("binding called during batch: callCount = %d, want 0", callCount)
		}
	})

	// Binding should be called after batch completes
	if callCount != 1 {
		t.Errorf("after batch: callCount = %d, want 1", callCount)
	}
	if lastValue != 42 {
		t.Errorf("after batch: lastValue = %d, want 42", lastValue)
	}
}

func TestBatch_MultipleSetsToSameState(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	var receivedValues []int
	s.Bind(func(v int) {
		callCount++
		receivedValues = append(receivedValues, v)
	})

	testApp.Batch(func() {
		s.Set(1)
		s.Set(2)
		s.Set(3) // final value
	})

	// Binding should only be called once with the final value
	if callCount != 1 {
		t.Errorf("callCount = %d, want 1", callCount)
	}
	if len(receivedValues) != 1 || receivedValues[0] != 3 {
		t.Errorf("receivedValues = %v, want [3]", receivedValues)
	}
}

func TestBatch_FinalValueReceived(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState("initial")
	s.BindApp(testApp)

	var receivedValue string
	s.Bind(func(v string) {
		receivedValue = v
	})

	testApp.Batch(func() {
		s.Set("first")
		s.Set("second")
		s.Set("final")
	})

	if receivedValue != "final" {
		t.Errorf("receivedValue = %q, want %q", receivedValue, "final")
	}
}

func TestBatch_MultipleDifferentStates(t *testing.T) {
	testApp.TestResetBatch()

	s1 := NewState(0)
	s1.BindApp(testApp)
	s2 := NewState("")
	s2.BindApp(testApp)

	var s1CallCount, s2CallCount int
	var s1Value int
	var s2Value string

	s1.Bind(func(v int) {
		s1CallCount++
		s1Value = v
	})
	s2.Bind(func(v string) {
		s2CallCount++
		s2Value = v
	})

	testApp.Batch(func() {
		s1.Set(42)
		s2.Set("hello")
	})

	// Each binding should be called exactly once
	if s1CallCount != 1 {
		t.Errorf("s1CallCount = %d, want 1", s1CallCount)
	}
	if s2CallCount != 1 {
		t.Errorf("s2CallCount = %d, want 1", s2CallCount)
	}
	if s1Value != 42 {
		t.Errorf("s1Value = %d, want 42", s1Value)
	}
	if s2Value != "hello" {
		t.Errorf("s2Value = %q, want %q", s2Value, "hello")
	}
}

func TestBatch_NestedBatches(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	var lastValue int
	s.Bind(func(v int) {
		callCount++
		lastValue = v
	})

	testApp.Batch(func() {
		s.Set(1)

		// Nested batch
		testApp.Batch(func() {
			s.Set(2)

			// Further nested
			testApp.Batch(func() {
				s.Set(3)
			})

			// Bindings should still not have fired
			if callCount != 0 {
				t.Errorf("binding called during nested batch: callCount = %d, want 0", callCount)
			}
		})

		// Still in outer batch
		if callCount != 0 {
			t.Errorf("binding called before outer batch complete: callCount = %d, want 0", callCount)
		}
	})

	// Now all bindings should fire (only once with final value)
	if callCount != 1 {
		t.Errorf("after all batches: callCount = %d, want 1", callCount)
	}
	if lastValue != 3 {
		t.Errorf("after all batches: lastValue = %d, want 3", lastValue)
	}
}

func TestBatch_DeduplicationByBindingID(t *testing.T) {
	testApp.TestResetBatch()

	// This test verifies that deduplication uses binding IDs, not function
	// pointer comparison. Multiple bindings on the same state should each
	// fire once, even if they have the same function signature.
	s := NewState(0)
	s.BindApp(testApp)

	var binding1Count, binding2Count int
	s.Bind(func(v int) { binding1Count++ })
	s.Bind(func(v int) { binding2Count++ })

	testApp.Batch(func() {
		s.Set(1)
		s.Set(2)
		s.Set(3)
	})

	// Each binding should fire exactly once
	if binding1Count != 1 {
		t.Errorf("binding1Count = %d, want 1", binding1Count)
	}
	if binding2Count != 1 {
		t.Errorf("binding2Count = %d, want 1", binding2Count)
	}
}

func TestBatch_NoSetsDoesntError(t *testing.T) {
	testApp.TestResetBatch()

	// Batch with no Set calls should not error
	testApp.Batch(func() {
		// do nothing
	})
	// Test passes if no panic occurs
}

func TestBatch_EmptyPendingAfterExecution(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	s.Bind(func(v int) {
		callCount++
	})

	// First batch
	testApp.Batch(func() {
		s.Set(1)
	})

	if callCount != 1 {
		t.Errorf("after first batch: callCount = %d, want 1", callCount)
	}

	// Second batch - should work independently
	testApp.Batch(func() {
		s.Set(2)
	})

	if callCount != 2 {
		t.Errorf("after second batch: callCount = %d, want 2", callCount)
	}
}

func TestBatch_SetOutsideBatchStillWorks(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	s.Bind(func(v int) {
		callCount++
	})

	// Set outside batch should work immediately
	s.Set(1)
	if callCount != 1 {
		t.Errorf("after Set outside batch: callCount = %d, want 1", callCount)
	}

	// Batch should also work
	testApp.Batch(func() {
		s.Set(2)
	})
	if callCount != 2 {
		t.Errorf("after batch: callCount = %d, want 2", callCount)
	}

	// Set after batch should work immediately again
	s.Set(3)
	if callCount != 3 {
		t.Errorf("after Set after batch: callCount = %d, want 3", callCount)
	}
}

func TestBatch_MarksDirty(t *testing.T) {
	testApp.resetDirty()
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	// Should not be dirty initially
	if testApp.checkAndClearDirty() {
		t.Error("should not be dirty before batch")
	}

	testApp.Batch(func() {
		s.Set(1)
		// Dirty should be marked immediately within batch
		if !testApp.checkAndClearDirty() {
			t.Error("should be dirty after Set within batch")
		}
	})
}

func TestBatch_MultipleBindingsPerState(t *testing.T) {
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var values1, values2, values3 []int
	s.Bind(func(v int) { values1 = append(values1, v) })
	s.Bind(func(v int) { values2 = append(values2, v) })
	s.Bind(func(v int) { values3 = append(values3, v) })

	testApp.Batch(func() {
		s.Set(10)
		s.Set(20)
		s.Set(30)
	})

	// Each binding should receive only the final value
	expected := []int{30}
	if len(values1) != 1 || values1[0] != 30 {
		t.Errorf("values1 = %v, want %v", values1, expected)
	}
	if len(values2) != 1 || values2[0] != 30 {
		t.Errorf("values2 = %v, want %v", values2, expected)
	}
	if len(values3) != 1 || values3[0] != 30 {
		t.Errorf("values3 = %v, want %v", values3, expected)
	}
}

func TestBatch_PanicRecovery(t *testing.T) {
	// Test that if fn panics, the batch state is properly cleaned up
	// and subsequent batches work correctly.
	testApp.TestResetBatch()

	s := NewState(0)
	s.BindApp(testApp)

	var callCount int
	s.Bind(func(v int) {
		callCount++
	})

	// First batch that panics
	func() {
		defer func() {
			if r := recover(); r == nil {
				t.Error("expected panic did not occur")
			}
		}()
		testApp.Batch(func() {
			s.Set(1)
			panic("test panic")
		})
	}()

	// After panic, batch depth should be reset to 0
	// A subsequent batch should work correctly
	testApp.Batch(func() {
		s.Set(2)
	})

	// The binding should have fired for the second batch
	// (and possibly for the first if bindings ran before panic cleanup)
	// Most importantly, subsequent batches should work
	if callCount < 1 {
		t.Errorf("callCount = %d, want at least 1 (batch should work after panic)", callCount)
	}

	// Final value should be 2
	if got := s.Get(); got != 2 {
		t.Errorf("Get() = %d, want 2", got)
	}
}