nicer plot

This commit is contained in:
flo_isea committed 2026-04-20 11:01:59 +02:00
1 parent 624ff1da42
commit 1275e5a821
2 files changed
+72 -14

No files matched your search

+36 -3
View File
@@ -66,7 +66,7 @@ func TestHandleWriteStoresValue(t *testing.T) {
func TestLoadConfig(t *testing.T) {
path := t.TempDir() + "/config.json"
if err := os.WriteFile(path, []byte(`{"write_secret":"secret","addr":":9090"}`), 0o600); err != nil {
if err := os.WriteFile(path, []byte(`{"write_secret":"secret","addr":":9090","chart_max_watts":750}`), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
@@ -80,10 +80,13 @@ func TestLoadConfig(t *testing.T) {
if cfg.Addr != ":9090" {
t.Fatalf("addr = %q, want %q", cfg.Addr, ":9090")
}
if cfg.ChartMaxWatts != 750 {
t.Fatalf("chart_max_watts = %d, want %d", cfg.ChartMaxWatts, 750)
}
}
func TestBuildHistoryChartEmpty(t *testing.T) {
chart := buildHistoryChart(nil)
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))
}
@@ -96,7 +99,7 @@ func TestBuildHistoryChartRendersPolyline(t *testing.T) {
{at: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), value: 150},
}
chart := buildHistoryChart(history)
chart := buildHistoryChart(history, 0)
if !strings.Contains(string(chart), "<polyline") {
t.Fatalf("chart did not contain polyline: %s", string(chart))
}
@@ -109,3 +112,33 @@ func TestBuildHistoryChartRendersPolyline(t *testing.T) {
t.Fatalf("chart did not include zero baseline: %s", string(chart))
}
}
func TestBuildHistoryChartUsesConfiguredMax(t *testing.T) {
history := []sample{{at: time.Date(2026, 4, 20, 12, 0, 0, 0, time.UTC), value: 250}}
chart := buildHistoryChart(history, 1000)
if !strings.Contains(string(chart), "1000 W") {
t.Fatalf("chart did not include configured max: %s", string(chart))
}
if strings.Contains(string(chart), "250 W") {
t.Fatalf("chart unexpectedly used sample max instead of configured max: %s", string(chart))
}
}
func TestStoreTrimsHistoryToMaxPoints(t *testing.T) {
s := &store{}
base := time.Date(2026, 4, 20, 0, 0, 0, 0, time.UTC)
s.mu.Lock()
for i := 0; i < maxHistoryPoints+25; i++ {
s.history = append(s.history, sample{at: base.Add(time.Duration(i) * time.Second), value: i})
}
s.trimHistoryLocked(base.Add(time.Duration(maxHistoryPoints+25) * time.Second))
got := len(s.history)
s.mu.Unlock()
if got != maxHistoryPoints {
t.Fatalf("history length = %d, want %d", got, maxHistoryPoints)
}
if s.history[0].value != 25 {
t.Fatalf("oldest retained value = %d, want %d", s.history[0].value, 25)
}
}