Files
powerwatts/internal/chart/chart_test.go
T
2026-04-20 15:42:34 +02:00

66 lines
1.9 KiB
Go

package chart
import (
"strings"
"testing"
"time"
"powerwatts/internal/store"
)
func TestFormatGermanNumber(t *testing.T) {
tests := map[int]string{
0: "0",
12: "12",
999: "999",
1000: "1.000",
10000: "10.000",
1234567: "1.234.567",
-1000: "-1.000",
}
for input, want := range tests {
if got := FormatGermanNumber(input); got != want {
t.Fatalf("FormatGermanNumber(%d) = %q, want %q", input, got, want)
}
}
}
func TestBuildHistoryChartEmpty(t *testing.T) {
chart := BuildHistoryChart(nil, 0)
if !strings.Contains(string(chart), "No power history yet") {
t.Fatalf("empty chart did not contain fallback text: %s", string(chart))
}
}
func TestBuildHistoryChartRendersPolyline(t *testing.T) {
history := []store.Sample{
{At: time.Date(2026, 4, 20, 0, 0, 0, 0, time.UTC), Value: 100},
{At: time.Date(2026, 4, 20, 6, 0, 0, 0, time.UTC), Value: 200},
{At: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), Value: 150},
}
chartHTML := BuildHistoryChart(history, 0)
if !strings.Contains(string(chartHTML), "<polyline") {
t.Fatalf("chart did not contain polyline: %s", string(chartHTML))
}
for _, want := range []string{"W", "00:00", "06:00", "12:00"} {
if !strings.Contains(string(chartHTML), want) {
t.Fatalf("chart did not include %q: %s", want, string(chartHTML))
}
}
if !strings.Contains(string(chartHTML), "0 W") {
t.Fatalf("chart did not include zero baseline: %s", string(chartHTML))
}
}
func TestBuildHistoryChartUsesConfiguredMax(t *testing.T) {
history := []store.Sample{{At: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), Value: 250}}
chartHTML := BuildHistoryChart(history, 1000)
if !strings.Contains(string(chartHTML), "1.000 W") {
t.Fatalf("chart did not include configured max: %s", string(chartHTML))
}
if strings.Contains(string(chartHTML), "250 W") {
t.Fatalf("chart unexpectedly used sample max instead of configured max: %s", string(chartHTML))
}
}