summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/controls_test.go
blob: 4a703fd7ffeb9d593c4d84fd57c4c9dfabcccfb6 (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
package flamegraph

import (
	"errors"
	"testing"

	coreflamegraph "ior/internal/flamegraph"
)

type setHeightErrorTrie struct {
	*coreflamegraph.LiveTrie
	err error
}

func (t *setHeightErrorTrie) SetHeightField(string) error {
	return t.err
}

type forcedHeightFieldTrie struct {
	*coreflamegraph.LiveTrie
	field string
}

func (t *forcedHeightFieldTrie) HeightField() string {
	return t.field
}

func TestToggleHeightFieldErrorKeepsExistingState(t *testing.T) {
	base := coreflamegraph.NewLiveTrie([]string{"comm", "path", "tracepoint"}, "count", "")
	liveTrie := &setHeightErrorTrie{
		LiveTrie: base,
		err:      errors.New("set-height failed"),
	}
	m := NewModel(liveTrie)
	m.snapshot = &snapshotNode{Name: "root", Total: 10}
	m.frames = []tuiFrame{{Name: "root", Path: "root", Total: 10}}

	m.toggleHeightField()

	if got, want := m.heightField, ""; got != want {
		t.Fatalf("heightField = %q, want %q on SetHeightField error", got, want)
	}
	if got, want := m.statusMessage, "Height toggle error: set-height failed"; got != want {
		t.Fatalf("statusMessage = %q, want %q", got, want)
	}
	if m.snapshot == nil || len(m.frames) == 0 {
		t.Fatalf("expected snapshot/layout state to remain intact on SetHeightField error")
	}
}

func TestHeightFieldLabelReturnsRawValueForUnknownField(t *testing.T) {
	m := NewModel(nil)
	m.heightField = "custom-height"

	if got, want := m.heightFieldLabel(), "custom-height"; got != want {
		t.Fatalf("heightFieldLabel() = %q, want %q", got, want)
	}
}

func TestSyncHeightFieldToTrieNilLiveTrieClearsField(t *testing.T) {
	m := NewModel(nil)
	m.heightField = "bytes"

	m.syncHeightFieldToTrie()

	if got, want := m.heightField, ""; got != want {
		t.Fatalf("heightField = %q, want %q for nil liveTrie", got, want)
	}
}

func TestSyncHeightFieldToTrieUnknownFieldFallsBackToOff(t *testing.T) {
	base := coreflamegraph.NewLiveTrie([]string{"comm", "path", "tracepoint"}, "count", "")
	liveTrie := &forcedHeightFieldTrie{
		LiveTrie: base,
		field:    "mystery",
	}
	m := NewModel(nil)
	m.liveTrie = liveTrie
	m.heightField = "duration"

	m.syncHeightFieldToTrie()

	if got, want := m.heightField, ""; got != want {
		t.Fatalf("heightField = %q, want %q for unknown trie height field", got, want)
	}
}