summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/renderer_test.go
blob: 354b40a36d03a84d9589914000ae6a18a0587ee1 (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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package flamegraph

import (
	"image/color"
	"strings"
	"testing"
)

func TestBuildTerminalLayoutWidthScaling(t *testing.T) {
	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},
		},
	}

	tests := []struct {
		width   int
		wantA   int
		wantB   int
		wantA1  int
		wantA2  int
		wantAll int
	}{
		{width: 80, wantA: 48, wantB: 32, wantA1: 24, wantA2: 24, wantAll: 5},
		{width: 120, wantA: 72, wantB: 48, wantA1: 36, wantA2: 36, wantAll: 5},
		{width: 200, wantA: 120, wantB: 80, wantA1: 60, wantA2: 60, wantAll: 5},
	}

	for _, tc := range tests {
		frames := BuildTerminalLayout(snapshot, tc.width, 10)
		if len(frames) != tc.wantAll {
			t.Fatalf("width %d: expected %d frames, got %d", tc.width, tc.wantAll, len(frames))
		}
		root := mustFindFrame(t, frames, "root")
		if root.Width != tc.width || root.Row != 0 || root.Col != 0 {
			t.Fatalf("width %d: unexpected root frame %+v", tc.width, root)
		}
		a := mustFindFrame(t, frames, "root"+pathSeparator+"A")
		b := mustFindFrame(t, frames, "root"+pathSeparator+"B")
		a1 := mustFindFrame(t, frames, "root"+pathSeparator+"A"+pathSeparator+"A1")
		a2 := mustFindFrame(t, frames, "root"+pathSeparator+"A"+pathSeparator+"A2")

		if a.Width != tc.wantA || b.Width != tc.wantB {
			t.Fatalf("width %d: unexpected child widths A=%d B=%d", tc.width, a.Width, b.Width)
		}
		if a1.Width != tc.wantA1 || a2.Width != tc.wantA2 {
			t.Fatalf("width %d: unexpected grandchild widths A1=%d A2=%d", tc.width, a1.Width, a2.Width)
		}
		if b.Col != a.Col+a.Width {
			t.Fatalf("width %d: expected B col %d, got %d", tc.width, a.Col+a.Width, b.Col)
		}
	}
}

func TestBuildTerminalLayoutCullsSubCellFramesAndRespectsHeight(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 100,
		Children: []*snapshotNode{
			{
				Name:  "big",
				Total: 99,
				Children: []*snapshotNode{
					{Name: "deep", Total: 99},
				},
			},
			{Name: "tiny", Total: 1},
		},
	}

	frames := BuildTerminalLayout(snapshot, 80, 2)
	if hasFrame(frames, "root"+pathSeparator+"tiny") {
		t.Fatalf("expected tiny frame to be culled (<1 terminal cell)")
	}
	if hasFrame(frames, "root"+pathSeparator+"big"+pathSeparator+"deep") {
		t.Fatalf("expected deep frame to be omitted due height limit")
	}
	if !hasFrame(frames, "root"+pathSeparator+"big") {
		t.Fatalf("expected big frame to be present")
	}
}

func TestBuildTerminalLayoutKeepsChildrenVisibleWhenRoundingWouldCullAll(t *testing.T) {
	children := make([]*snapshotNode, 0, 200)
	for i := 0; i < 200; i++ {
		children = append(children, &snapshotNode{Name: "c", Total: 1})
	}
	snapshot := &snapshotNode{Name: "root", Children: children}

	frames := BuildTerminalLayout(snapshot, 120, 6)
	depthOne := 0
	for _, frame := range frames {
		if frame.Depth == 1 {
			depthOne++
		}
	}
	if depthOne == 0 {
		t.Fatalf("expected at least one visible depth-1 frame, got none")
	}
}

func TestBuildTerminalLayoutUsesPathSeparatorAndColor(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 10,
		Children: []*snapshotNode{
			{Name: "child", Total: 10},
		},
	}

	frames := BuildTerminalLayout(snapshot, 80, 4)
	child := mustFindFrame(t, frames, "root"+pathSeparator+"child")
	if !strings.Contains(child.Path, pathSeparator) {
		t.Fatalf("expected path %q to contain separator %q", child.Path, pathSeparator)
	}
	if child.Fill == nil {
		t.Fatalf("expected frame color to be set")
	}
}

func TestTerminalFrameColorSemanticPalette(t *testing.T) {
	tests := []struct {
		name  string
		label string
		want  color.RGBA
	}{
		{name: "read", label: "sys_enter_read", want: color.RGBA{R: 78, G: 132, B: 201, A: 255}},
		{name: "write", label: "sys_enter_write", want: color.RGBA{R: 222, G: 122, B: 58, A: 255}},
		{name: "metadata", label: "sys_enter_openat", want: color.RGBA{R: 196, G: 168, B: 72, A: 255}},
		{name: "path", label: "/var/log/app.log", want: color.RGBA{R: 88, G: 156, B: 84, A: 255}},
		{name: "pid", label: "pid=1234", want: color.RGBA{R: 67, G: 151, B: 149, A: 255}},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got := terminalFrameColor(tc.label)
			if got != tc.want {
				t.Fatalf("unexpected semantic color for %q: got=%v want=%v", tc.label, got, tc.want)
			}
		})
	}
}

func TestRenderTerminalViewShowsNarrowMessage(t *testing.T) {
	out := RenderTerminalView(nil, 50, 10, 0, nil, nil, nil, 0, "events", true, false, "")
	if !strings.Contains(out, "terminal too narrow") {
		t.Fatalf("expected narrow terminal warning, got %q", out)
	}
}

func TestComputeBarHeightCappedAtThree(t *testing.T) {
	if got := computeBarHeight(30, 4, 3); got != 3 {
		t.Fatalf("expected bar height cap at 3, got %d", got)
	}
	if got := computeBarHeight(5, 10, 3); got != 1 {
		t.Fatalf("expected bar height minimum 1 when depth exceeds rows, got %d", got)
	}
}

func TestRenderTerminalViewIncludesToolbarAndStatus(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 10,
		Children: []*snapshotNode{
			{Name: "child", Total: 10},
		},
	}
	frames := BuildTerminalLayout(snapshot, 80, 6)

	out := RenderTerminalView(frames, 80, 6, 1, nil, nil, nil, 0, "events", true, false, "")
	if !strings.Contains(out, "Flame | view:root | frames:2") {
		t.Fatalf("expected toolbar to include frame count, got %q", out)
	}
	if !strings.Contains(out, "Selected: child") {
		t.Fatalf("expected status line to show selected frame, got %q", out)
	}
}

func TestRenderTerminalViewFillsAvailableHeightForShallowTree(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 10,
		Children: []*snapshotNode{
			{Name: "child", Total: 10},
		},
	}
	frames := BuildTerminalLayout(snapshot, 100, 20)

	out := RenderTerminalView(frames, 100, 20, 1, nil, nil, nil, 0, "events", true, false, "")
	lines := strings.Split(out, "\n")
	if got, want := len(lines), 20; got != want {
		t.Fatalf("expected render to fill viewport height (%d lines), got %d", want, got)
	}
}

func TestFrameLabelAddsSelectionAndMatchMarkers(t *testing.T) {
	if got := frameLabel("child", 7, true, false); got != ">child<" {
		t.Fatalf("expected selected marker label, got %q", got)
	}
	if got := frameLabel("child", 6, false, true); got != "*child" {
		t.Fatalf("expected match marker label, got %q", got)
	}
}

func TestSelectedFrameStyleDoesNotUnderline(t *testing.T) {
	style := styleForFrame(
		1,
		tuiFrame{
			Name: "child",
			Path: "root" + pathSeparator + "child",
			Fill: color.RGBA{R: 120, G: 80, B: 160, A: 255},
		},
		"root"+pathSeparator+"child",
		map[int]bool{1: true},
		nil,
		1,
		true,
		false,
		false,
	)
	rendered := style.Render(" child ")
	if strings.Contains(rendered, "\x1b[4m") || strings.Contains(rendered, "[4;") || strings.Contains(rendered, ";4m") {
		t.Fatalf("expected selected flame frame style without underline, got %q", rendered)
	}
}

func TestRenderTerminalViewShowsPersistentFilterContext(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 10,
		Children: []*snapshotNode{
			{Name: "child", Total: 10},
		},
	}
	frames := BuildTerminalLayout(snapshot, 80, 6)
	matchSet := map[int]bool{1: true}

	out := RenderTerminalView(frames, 140, 6, 1, nil, matchSet, nil, 0, "events", true, false, "child")
	if !strings.Contains(out, `Filter "child"`) {
		t.Fatalf("expected filter context in status line, got %q", out)
	}
}

func TestRenderTerminalViewFilterKeepsNonMatchingBranchesVisible(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 100,
		Children: []*snapshotNode{
			{
				Name:  "keep",
				Total: 60,
				Children: []*snapshotNode{
					{Name: "needle", Total: 60},
				},
			},
			{
				Name:  "drop",
				Total: 40,
				Children: []*snapshotNode{
					{Name: "noise", Total: 40},
				},
			},
		},
	}
	frames := BuildTerminalLayout(snapshot, 80, 8)
	needleIdx := frameIndexByPathRenderer(frames, "root"+pathSeparator+"keep"+pathSeparator+"needle")
	if needleIdx < 0 {
		t.Fatalf("expected needle frame in layout")
	}
	matchSet := map[int]bool{needleIdx: true}

	out := RenderTerminalView(frames, 180, 8, needleIdx, nil, matchSet, nil, 100, "bytes", true, false, "needle")
	if !strings.Contains(out, `Filter "needle": 60.0% bytes`) {
		t.Fatalf("expected filter status to report 60.0%% bytes share, got %q", out)
	}
	if !strings.Contains(out, "keep") || !strings.Contains(out, "needle") {
		t.Fatalf("expected matching branch to remain visible, got %q", out)
	}
	if !strings.Contains(out, "drop") || !strings.Contains(out, "noise") {
		t.Fatalf("expected non-matching branch to remain visible (greyed), got %q", out)
	}
	if !strings.Contains(out, "100.00% filtered bytes") {
		t.Fatalf("expected selected match share to be computed against filtered total, got %q", out)
	}
}

func TestBuildTerminalLayoutWithPathNormalizesZoomRootChildrenToFullWidth(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 100,
		Children: []*snapshotNode{
			{
				Name:  "zoom",
				Total: 80,
				Children: []*snapshotNode{
					{Name: "left", Total: 10},
					{Name: "right", Total: 10},
				},
			},
			{Name: "other", Total: 20},
		},
	}

	frames := buildTerminalLayoutWithPath(snapshot.Children[0], 120, 12, "root"+pathSeparator+"zoom")
	left := mustFindFrame(t, frames, "root"+pathSeparator+"zoom"+pathSeparator+"left")
	right := mustFindFrame(t, frames, "root"+pathSeparator+"zoom"+pathSeparator+"right")
	if got := left.Width + right.Width; got != 120 {
		t.Fatalf("expected zoom-root children to fill full width 120, got %d", got)
	}
	if left.Col != 0 {
		t.Fatalf("expected left child to start at column 0, got %d", left.Col)
	}
	if right.Col != left.Width {
		t.Fatalf("expected right child to start after left child, got %d want %d", right.Col, left.Width)
	}
}

func TestFilterSampleCoverageAvoidsDoubleCountingNestedMatches(t *testing.T) {
	frames := []tuiFrame{
		{Path: "root", Total: 100},
		{Path: "root" + pathSeparator + "A", Total: 60},
		{Path: "root" + pathSeparator + "A" + pathSeparator + "A1", Total: 30},
		{Path: "root" + pathSeparator + "B", Total: 40},
	}
	matchSet := map[int]bool{
		1: true, // A
		2: true, // A1 (nested under A)
	}
	if got := filterSampleCoverage(frames, matchSet, 100); got != 60 {
		t.Fatalf("expected nested matches to count once at 60%%, got %.1f%%", got)
	}
}

func TestRenderTerminalViewShowsDeepLevelTruncationHint(t *testing.T) {
	snapshot := &snapshotNode{
		Name:  "root",
		Total: 4,
		Children: []*snapshotNode{
			{
				Name:  "a",
				Total: 4,
				Children: []*snapshotNode{
					{
						Name:  "b",
						Total: 4,
						Children: []*snapshotNode{
							{
								Name:  "c",
								Total: 4,
								Children: []*snapshotNode{
									{Name: "d", Total: 4},
								},
							},
						},
					},
				},
			},
		},
	}
	frames := BuildTerminalLayout(snapshot, 80, 10)
	out := RenderTerminalView(frames, 80, 4, 0, nil, nil, nil, 0, "events", true, false, "")
	if !strings.Contains(out, "showing deepest levels") {
		t.Fatalf("expected truncation hint in toolbar, got %q", out)
	}
}

func TestComputeSubtreeSetIncludesAncestorsAndDescendants(t *testing.T) {
	frames := []tuiFrame{
		{Path: "root"},
		{Path: "root" + pathSeparator + "A"},
		{Path: "root" + pathSeparator + "A" + pathSeparator + "A1"},
		{Path: "root" + pathSeparator + "B"},
	}

	set := computeSubtreeSet(frames, 1)
	if !set[0] || !set[1] || !set[2] {
		t.Fatalf("expected root/A/A1 to be in selected subtree: %#v", set)
	}
	if set[3] {
		t.Fatalf("did not expect sibling branch B in subtree: %#v", set)
	}
}

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

func hasFrame(frames []tuiFrame, path string) bool {
	for _, frame := range frames {
		if frame.Path == path {
			return true
		}
	}
	return false
}

func frameIndexByPathRenderer(frames []tuiFrame, path string) int {
	for idx, frame := range frames {
		if frame.Path == path {
			return idx
		}
	}
	return -1
}