summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/sparkline_test.go
blob: d7acd3385ce05c534fe40adff470c0d0bad1234e (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
package dashboard

import (
	"strings"
	"testing"
)

func TestRenderSparklineEmptyOrInvalidWidth(t *testing.T) {
	if got := renderSparkline(nil, 5); got != "" {
		t.Fatalf("expected empty sparkline for nil data, got %q", got)
	}
	if got := renderSparkline([]float64{1, 2, 3}, 0); got != "" {
		t.Fatalf("expected empty sparkline for width 0, got %q", got)
	}
}

func TestRenderSparklineSingleValue(t *testing.T) {
	got := renderSparkline([]float64{10}, 8)
	if got != "        \n       █" {
		t.Fatalf("expected two-line constant sparkline, got %q", got)
	}
}

func TestRenderSparklineAllEqualValues(t *testing.T) {
	got := renderSparkline([]float64{5, 5, 5, 5}, 4)
	if got != "    \n████" {
		t.Fatalf("expected two-line flat sparkline, got %q", got)
	}
}

func TestRenderSparklineRightAlignsShortHistory(t *testing.T) {
	got := renderSparkline([]float64{1, 2, 3}, 6)
	lines := strings.Split(got, "\n")
	if len(lines) != 2 {
		t.Fatalf("expected 2 lines, got %q", got)
	}
	if !strings.HasPrefix(lines[1], "   ") {
		t.Fatalf("expected left padding for short history, got %q", lines[1])
	}
}

func TestRenderSparklineRespectsWidthTruncation(t *testing.T) {
	got := renderSparkline([]float64{1, 2, 3, 4, 5, 6, 7, 8}, 4)
	lines := strings.Split(got, "\n")
	if len(lines) != 2 {
		t.Fatalf("expected 2 lines, got %q", got)
	}
	if len([]rune(lines[0])) != 4 || len([]rune(lines[1])) != 4 {
		t.Fatalf("expected 4 runes per line, got %q", got)
	}
}

func TestSampleForWidthUsesRecentTail(t *testing.T) {
	got := sampleForWidth([]float64{1, 2, 3, 4, 5, 6}, 3)
	want := []float64{4, 5, 6}
	if len(got) != len(want) {
		t.Fatalf("expected tail length %d, got %d", len(want), len(got))
	}
	for i := range want {
		if got[i] != want[i] {
			t.Fatalf("expected tail %v, got %v", want, got)
		}
	}
}

func TestRenderSparklineSpansLowToHigh(t *testing.T) {
	got := renderSparkline([]float64{0, 10}, 2)
	lines := strings.Split(got, "\n")
	if len(lines) != 2 {
		t.Fatalf("expected 2 lines, got %q", got)
	}
	if !strings.Contains(got, "█") {
		t.Fatalf("expected high bar, got %q", got)
	}
}

func TestRenderLabeledSparklineAlignsSecondRow(t *testing.T) {
	got := renderLabeledSparkline("Latency:", []float64{0, 10}, 2)
	lines := strings.Split(got, "\n")
	if len(lines) != 2 {
		t.Fatalf("expected 2 lines, got %q", got)
	}
	if !strings.HasPrefix(lines[0], "Latency: ") {
		t.Fatalf("expected label prefix on first row, got %q", lines[0])
	}
	if !strings.HasPrefix(lines[1], "         ") {
		t.Fatalf("expected padding on second row to align sparkline, got %q", lines[1])
	}
}