gitstack

grindlemire/go-tui code browser

5.0 KB Go 237 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
package tui

import (
	"testing"
)

func TestState_NewState(t *testing.T) {
	type tc struct {
		initial int
	}

	tests := map[string]tc{
		"creates state with zero value": {
			initial: 0,
		},
		"creates state with positive value": {
			initial: 42,
		},
		"creates state with negative value": {
			initial: -10,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			s := NewState(tt.initial)
			if s.Get() != tt.initial {
				t.Errorf("NewState(%d).Get() = %d, want %d", tt.initial, s.Get(), tt.initial)
			}
		})
	}
}

func TestState_NewState_TypeInference(t *testing.T) {
	// Test that NewState correctly infers various types
	t.Run("int", func(t *testing.T) {
		s := NewState(42)
		if got := s.Get(); got != 42 {
			t.Errorf("Get() = %d, want 42", got)
		}
	})

	t.Run("string", func(t *testing.T) {
		s := NewState("hello")
		if got := s.Get(); got != "hello" {
			t.Errorf("Get() = %q, want %q", got, "hello")
		}
	})

	t.Run("bool", func(t *testing.T) {
		s := NewState(true)
		if got := s.Get(); got != true {
			t.Errorf("Get() = %v, want true", got)
		}
	})

	t.Run("slice", func(t *testing.T) {
		s := NewState([]string{"a", "b"})
		got := s.Get()
		if len(got) != 2 || got[0] != "a" || got[1] != "b" {
			t.Errorf("Get() = %v, want [a b]", got)
		}
	})

	t.Run("struct pointer", func(t *testing.T) {
		type User struct{ Name string }
		s := NewState(&User{Name: "Alice"})
		got := s.Get()
		if got == nil || got.Name != "Alice" {
			t.Errorf("Get() = %v, want &User{Name:Alice}", got)
		}
	})
}

func TestState_Get(t *testing.T) {
	s := NewState(100)

	// Get should return current value
	if got := s.Get(); got != 100 {
		t.Errorf("Get() = %d, want 100", got)
	}

	// Get should be idempotent
	if got := s.Get(); got != 100 {
		t.Errorf("Get() second call = %d, want 100", got)
	}
}

func TestState_Set(t *testing.T) {
	// Reset dirty flag before test
	testApp.resetDirty()

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

	if got := s.Get(); got != 42 {
		t.Errorf("after Set(42), Get() = %d, want 42", got)
	}
}

func TestState_Set_MarksDirty(t *testing.T) {
	// Reset dirty flag before test
	testApp.resetDirty()

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

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

	s.Set(1)

	// Should be dirty after Set
	if !testApp.checkAndClearDirty() {
		t.Error("should be dirty after Set()")
	}
}

func TestState_Set_ReentrantCycleBreaks(t *testing.T) {
	a := NewState(0)
	b := NewState(0)
	a.BindApp(testApp)
	b.BindApp(testApp)
	testApp.resetDirty()

	// Create a cycle: a's binding sets b, b's binding sets a
	var aBindCount, bBindCount int
	a.Bind(func(v int) {
		aBindCount++
		b.Set(v * 2)
	})
	b.Bind(func(v int) {
		bBindCount++
		a.Set(v + 1)
	})

	a.Set(5)

	// a should have been set twice: once explicitly (5), once by b's binding (11)
	if got := a.Get(); got != 11 {
		t.Errorf("a.Get() = %d, want 11", got)
	}
	// b should have been set once by a's binding (10)
	if got := b.Get(); got != 10 {
		t.Errorf("b.Get() = %d, want 10", got)
	}
	// a's binding should fire once (the re-entrant Set from b skips a's bindings)
	if aBindCount != 1 {
		t.Errorf("a binding fired %d times, want 1", aBindCount)
	}
	// b's binding should fire once
	if bBindCount != 1 {
		t.Errorf("b binding fired %d times, want 1", bBindCount)
	}
}

func TestState_Set_ReentrantSelfReference(t *testing.T) {
	s := NewState(0)
	s.BindApp(testApp)
	testApp.resetDirty()

	var bindCount int
	s.Bind(func(v int) {
		bindCount++
		if v < 10 {
			s.Set(v + 1) // re-entrant: updates value but skips bindings
		}
	})

	s.Set(1)

	// Value should be 2 (binding ran once, set to 1+1=2, re-entrant Set skipped bindings)
	if got := s.Get(); got != 2 {
		t.Errorf("s.Get() = %d, want 2", got)
	}
	if bindCount != 1 {
		t.Errorf("binding fired %d times, want 1", bindCount)
	}
}

func TestState_Set_TreeDependencyNoReentrant(t *testing.T) {
	// A → B and C → B (no cycle, B should get set twice)
	a := NewState(0)
	b := NewState(0)
	c := NewState(0)
	a.BindApp(testApp)
	b.BindApp(testApp)
	c.BindApp(testApp)
	testApp.resetDirty()

	var bBindCount int
	a.Bind(func(v int) { b.Set(v * 10) })
	c.Bind(func(v int) { b.Set(v * 100) })
	b.Bind(func(_ int) { bBindCount++ })

	a.Set(1)
	c.Set(2)

	// b should hold the last value set (from c's binding)
	if got := b.Get(); got != 200 {
		t.Errorf("b.Get() = %d, want 200", got)
	}
	// b's binding should fire twice (once per Set from a and c)
	if bBindCount != 2 {
		t.Errorf("b binding fired %d times, want 2", bBindCount)
	}
}

func TestState_Set_ReentrantWithNoApp(t *testing.T) {
	// Same cycle detection should work without an app bound
	a := NewState(0)
	b := NewState(0)

	var aBindCount int
	a.Bind(func(v int) {
		aBindCount++
		b.Set(v * 2)
	})
	b.Bind(func(v int) {
		a.Set(v + 1)
	})

	a.Set(5)

	if got := a.Get(); got != 11 {
		t.Errorf("a.Get() = %d, want 11", got)
	}
	if got := b.Get(); got != 10 {
		t.Errorf("b.Get() = %d, want 10", got)
	}
	if aBindCount != 1 {
		t.Errorf("a binding fired %d times, want 1", aBindCount)
	}
}