summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/model_test.go
blob: 413b571c85c22ac243fa24c985919c54a99d56f0 (plain)
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
package flamegraph

import (
	coreflamegraph "ior/internal/flamegraph"
	"testing"

	tea "charm.land/bubbletea/v2"
)

func TestNewModelDefaults(t *testing.T) {
	m := NewModel(nil)
	if m.liveTrie != nil {
		t.Fatalf("expected nil liveTrie when constructor input is nil")
	}
	if m.matchIndices == nil {
		t.Fatalf("expected matchIndices map to be initialized")
	}
	if len(m.fieldPresets) == 0 {
		t.Fatalf("expected default field presets to be initialized")
	}
	if !m.isDark {
		t.Fatalf("expected dark mode enabled by default")
	}
}

func TestSetViewportAndDarkMode(t *testing.T) {
	m := NewModel(nil)
	m.SetViewport(120, 40)
	m.SetDarkMode(false)
	if m.width != 120 || m.height != 40 {
		t.Fatalf("expected viewport 120x40, got %dx%d", m.width, m.height)
	}
	if m.isDark {
		t.Fatalf("expected dark mode to be disabled")
	}
}

func TestRefreshFromLiveTrieTracksVersionAndSnapshot(t *testing.T) {
	trie := coreflamegraph.NewLiveTrie([]string{"comm", "path"}, "count")
	m := NewModel(trie)

	if changed := m.RefreshFromLiveTrie(); !changed {
		t.Fatalf("expected first refresh to load baseline snapshot")
	}
	if m.snapshot == nil {
		t.Fatalf("expected snapshot to be populated after refresh")
	}

	if changed := m.RefreshFromLiveTrie(); changed {
		t.Fatalf("expected no refresh when version is unchanged")
	}
}

func TestKeyboardNavigationDeepNarrowTree(t *testing.T) {
	m := NewModel(nil)
	m.frames = []tuiFrame{
		{Name: "root", Depth: 0, Col: 0, Path: "root"},
		{Name: "child", Depth: 1, Col: 0, Path: "root" + pathSeparator + "child"},
		{Name: "leaf", Depth: 2, Col: 0, Path: "root" + pathSeparator + "child" + pathSeparator + "leaf"},
	}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'k'}[0], Text: "k"})
	if m.selectedIdx != 1 {
		t.Fatalf("expected selection to move deeper to idx 1, got %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'k'}[0], Text: "k"})
	if m.selectedIdx != 2 {
		t.Fatalf("expected selection to move deeper to idx 2, got %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'j'}[0], Text: "j"})
	if m.selectedIdx != 1 {
		t.Fatalf("expected selection to move shallower to idx 1, got %d", m.selectedIdx)
	}
}

func TestKeyboardNavigationShallowWideSiblings(t *testing.T) {
	m := NewModel(nil)
	m.frames = []tuiFrame{
		{Name: "root", Depth: 0, Col: 0, Path: "root"},
		{Name: "A", Depth: 1, Col: 0, Path: "root" + pathSeparator + "A"},
		{Name: "B", Depth: 1, Col: 30, Path: "root" + pathSeparator + "B"},
		{Name: "C", Depth: 1, Col: 60, Path: "root" + pathSeparator + "C"},
	}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'k'}[0], Text: "k"})
	if m.selectedIdx != 1 {
		t.Fatalf("expected first deeper frame to be A, got idx %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'l'}[0], Text: "l"})
	if m.selectedIdx != 2 {
		t.Fatalf("expected next sibling B, got idx %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'l'}[0], Text: "l"})
	if m.selectedIdx != 3 {
		t.Fatalf("expected next sibling C, got idx %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'l'}[0], Text: "l"})
	if m.selectedIdx != 3 {
		t.Fatalf("expected selection to clamp at last sibling, got idx %d", m.selectedIdx)
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'h'}[0], Text: "h"})
	if m.selectedIdx != 2 {
		t.Fatalf("expected previous sibling B, got idx %d", m.selectedIdx)
	}
}

func TestKeyboardNavigationSingleNodeClamped(t *testing.T) {
	m := NewModel(nil)
	m.frames = []tuiFrame{{Name: "root", Depth: 0, Col: 0, Path: "root"}}

	keys := []tea.KeyPressMsg{
		{Code: []rune{'j'}[0], Text: "j"},
		{Code: []rune{'k'}[0], Text: "k"},
		{Code: []rune{'h'}[0], Text: "h"},
		{Code: []rune{'l'}[0], Text: "l"},
		{Code: tea.KeyDown},
		{Code: tea.KeyUp},
		{Code: tea.KeyLeft},
		{Code: tea.KeyRight},
	}
	for _, keyMsg := range keys {
		m = pressFlameKey(t, m, keyMsg)
		if m.selectedIdx != 0 {
			t.Fatalf("expected single-node selection to stay at idx 0, got %d", m.selectedIdx)
		}
	}
}

func TestZoomInUndoResetAndNestedZoom(t *testing.T) {
	m := newZoomModel()

	m.selectedIdx = mustFrameIndex(t, m.frames, "root"+pathSeparator+"A")
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyEnter})
	if got, want := m.zoomPath, "root"+pathSeparator+"A"; got != want {
		t.Fatalf("expected zoomPath %q, got %q", want, got)
	}
	if len(m.zoomStack) != 1 || m.zoomStack[0].path != "" {
		t.Fatalf("expected one zoom stack entry from root, got %#v", m.zoomStack)
	}
	if m.zoomRoot == nil || m.zoomRoot.Name != "A" {
		t.Fatalf("expected zoomRoot A, got %+v", m.zoomRoot)
	}

	m.selectedIdx = mustFrameIndex(t, m.frames, "root"+pathSeparator+"A"+pathSeparator+"A1")
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyEnter})
	if got, want := m.zoomPath, "root"+pathSeparator+"A"+pathSeparator+"A1"; got != want {
		t.Fatalf("expected nested zoomPath %q, got %q", want, got)
	}
	if len(m.zoomStack) != 2 || m.zoomStack[1].path != "root"+pathSeparator+"A" {
		t.Fatalf("expected nested zoom stack to preserve parent path, got %#v", m.zoomStack)
	}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyBackspace})
	if got, want := m.zoomPath, "root"+pathSeparator+"A"; got != want {
		t.Fatalf("expected zoomPath after undo %q, got %q", want, got)
	}
	if len(m.zoomStack) != 1 {
		t.Fatalf("expected one stack entry after undo, got %d", len(m.zoomStack))
	}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyEsc})
	if m.zoomPath != "" || m.zoomRoot != nil || len(m.zoomStack) != 0 {
		t.Fatalf("expected zoom reset to root state, got path=%q root=%+v stack=%d", m.zoomPath, m.zoomRoot, len(m.zoomStack))
	}
}

func TestSearchLifecycleAndMatchNavigation(t *testing.T) {
	m := NewModel(nil)
	m.frames = []tuiFrame{
		{Name: "alpha", Path: "root" + pathSeparator + "alpha"},
		{Name: "beta", Path: "root" + pathSeparator + "beta"},
		{Name: "alphabet", Path: "root" + pathSeparator + "alphabet"},
	}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'/'}[0], Text: "/"})
	if !m.searchActive {
		t.Fatalf("expected search mode to activate on '/'")
	}
	for _, r := range []rune{'a', 'l', 'p'} {
		m = pressFlameKey(t, m, tea.KeyPressMsg{Code: r, Text: string(r)})
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyEnter})

	if m.searchActive {
		t.Fatalf("expected search mode to close on enter")
	}
	if got := len(m.matchIndices); got != 2 {
		t.Fatalf("expected 2 matches for 'alp', got %d", got)
	}
	first := m.selectedIdx
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'n'}[0], Text: "n"})
	if m.selectedIdx == first {
		t.Fatalf("expected 'n' to jump to next match")
	}
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'N'}[0], Text: "N"})
	if m.selectedIdx != first {
		t.Fatalf("expected 'N' to jump back to previous match")
	}
}

func TestSearchEscapeClearsState(t *testing.T) {
	m := NewModel(nil)
	m.frames = []tuiFrame{{Name: "alpha", Path: "root" + pathSeparator + "alpha"}}

	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'/'}[0], Text: "/"})
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: []rune{'a'}[0], Text: "a"})
	m = pressFlameKey(t, m, tea.KeyPressMsg{Code: tea.KeyEsc})

	if m.searchActive {
		t.Fatalf("expected search mode to close on escape")
	}
	if m.searchQuery != "" || len(m.matchIndices) != 0 {
		t.Fatalf("expected search state to reset on escape, got query=%q matches=%d", m.searchQuery, len(m.matchIndices))
	}
}

func newZoomModel() Model {
	m := NewModel(nil)
	m.width = 120
	m.height = 30
	m.snapshot = &snapshotNode{
		Name:  "root",
		Total: 100,
		Children: []*snapshotNode{
			{
				Name:  "A",
				Total: 60,
				Children: []*snapshotNode{
					{Name: "A1", Total: 30},
					{Name: "A2", Total: 30},
				},
			},
			{Name: "B", Total: 40},
		},
	}
	m.rebuildFrames()
	return m
}

func mustFrameIndex(t *testing.T, frames []tuiFrame, path string) int {
	t.Helper()
	for idx, frame := range frames {
		if frame.Path == path {
			return idx
		}
	}
	t.Fatalf("frame path %q not found", path)
	return -1
}

func pressFlameKey(t *testing.T, m Model, keyMsg tea.KeyPressMsg) Model {
	t.Helper()
	next, _ := m.Update(keyMsg)
	return next.(Model)
}