Refactor for readability
This commit is contained in:
1 parent
ff9860023b
commit
17baba1490
11 files changed
+536
-405
No files matched your search
@@ -0,0 +1,65 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user