munin/internal/web/chart_test.go
2026-10-19 10:13:56 +02:00

82 lines
2.6 KiB
Go

package web
import (
"testing"
"time"
"munin/internal/stats"
)
func TestRound125(t *testing.T) {
for _, tc := range []struct{ in, out int }{
{0, 1}, {1, 1}, {2, 2}, {3, 5}, {5, 5}, {6, 10}, {11, 20}, {37, 50}, {51, 100}, {999, 1000}, {1001, 2000},
} {
if got := round125(tc.in); got != tc.out {
t.Errorf("runde125(%d) = %d, erwartet %d", tc.in, got, tc.out)
}
}
}
// TestChart: Die Balken stapeln sich von der Nulllinie nach oben, der
// höchste reicht bis zur Skala, ein kleiner Wert bleibt sichtbar, und unter
// dem letzten Tag steht ein Datum.
func TestChart(t *testing.T) {
tag0 := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
days := make([]stats.Tag, 8)
for i := range days {
days[i].Date = tag0.AddDate(0, 0, i)
}
days[3].AgentWrites, days[3].HumanWrites = 6, 4 // Summe 10 = Skala
days[5].AgentWrites = 1
d := buildChart("Test", chartUnit{"Schreibvorgang", "Schreibvorgänge"}, days,
chartSeries{value: func(t stats.Tag) int { return t.AgentWrites }, class: "a", name: "Agenten"},
chartSeries{value: func(t stats.Tag) int { return t.HumanWrites }, class: "m", name: "Menschen"},
)
if d.Empty || len(d.Bars) != len(days) {
t.Fatalf("Leer=%v, %d Balken", d.Empty, len(d.Bars))
}
full := d.Bars[3].Parts
if len(full) != 2 {
t.Fatalf("Tag 3: %d Teile, erwartet 2", len(full))
}
if full[0].Y+full[0].Height != d.Baseline {
t.Error("unterster Teil steht nicht auf der Nulllinie")
}
if full[1].Y+full[1].Height != full[0].Y {
t.Error("zweiter Teil liegt nicht auf dem ersten")
}
if full[1].Y != chartTop {
t.Errorf("höchster Balken endet bei y=%d, erwartet %d (die Skala)", full[1].Y, chartTop)
}
if small := d.Bars[5].Parts; len(small) != 1 || small[0].Height < 1 {
t.Errorf("ein einzelner Schreibvorgang ist unsichtbar: %+v", small)
}
if len(d.Bars[0].Parts) != 0 {
t.Error("ein Tag ohne Aktivität hat Balken")
}
if got := d.Bars[3].Title; got != "So 04.10.: 10 Schreibvorgänge (Agenten 6, Menschen 4)" {
t.Errorf("Tooltip %q", got)
}
if last := d.Axis[len(d.Axis)-1]; last.Text != "08.10." {
t.Errorf("letzte Beschriftung %q, erwartet den letzten Tag", last.Text)
}
if empty := buildChart("Leer", chartUnit{"Suche", "Suchen"}, days[:2]); !empty.Empty {
t.Error("Diagramm ohne Werte ist nicht als leer markiert")
}
}
func TestByteSize(t *testing.T) {
for _, tc := range []struct {
in int64
out string
}{
{0, "0 B"}, {1023, "1023 B"}, {1536, "1,5 KB"}, {812 << 10, "812 KB"}, {3 << 30, "3,0 GB"},
} {
if got := byteSize(tc.in); got != tc.out {
t.Errorf("groesse(%d) = %q, erwartet %q", tc.in, got, tc.out)
}
}
}