149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"image/png"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"powerwatts/internal/chart"
|
|
"powerwatts/internal/config"
|
|
"powerwatts/internal/server"
|
|
"powerwatts/internal/store"
|
|
)
|
|
|
|
func TestHandleIndexRendersWatts(t *testing.T) {
|
|
srvStore := &store.Store{}
|
|
srvStore.Set(123)
|
|
srv := server.New("secret", 0, srvStore, embeddedStatic)
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
srv.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "123") {
|
|
t.Fatalf("response did not contain watt value: %s", rec.Body.String())
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "Last updated:") {
|
|
t.Fatalf("response did not contain last updated label: %s", rec.Body.String())
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "W") {
|
|
t.Fatalf("response did not contain watt unit: %s", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
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 := chart.FormatGermanNumber(input); got != want {
|
|
t.Fatalf("formatGermanNumber(%d) = %q, want %q", input, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
path := t.TempDir() + "/config.json"
|
|
if err := os.WriteFile(path, []byte(`{"write_secret":"secret","addr":":9090","chart_max_watts":750}`), 0o600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load returned error: %v", err)
|
|
}
|
|
if cfg.WriteSecret != "secret" {
|
|
t.Fatalf("write_secret = %q, want %q", cfg.WriteSecret, "secret")
|
|
}
|
|
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 TestHandleWriteRequiresSecret(t *testing.T) {
|
|
srv := server.New("secret", 0, &store.Store{}, embeddedStatic)
|
|
req := httptest.NewRequest(http.MethodPost, "/write", bytes.NewBufferString(`{"watts":456}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
|
|
srv.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
|
|
}
|
|
}
|
|
|
|
func TestHandleWriteStoresValue(t *testing.T) {
|
|
s := &store.Store{}
|
|
srv := server.New("secret", 0, s, embeddedStatic)
|
|
req := httptest.NewRequest(http.MethodPost, "/write", bytes.NewBufferString(`{"watts":456}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Secret", "secret")
|
|
rec := httptest.NewRecorder()
|
|
|
|
srv.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
if got := s.Get(); got != 456 {
|
|
t.Fatalf("stored watts = %d, want %d", got, 456)
|
|
}
|
|
if _, updatedAt, _ := s.Snapshot(); updatedAt.IsZero() {
|
|
t.Fatal("updatedAt was not set")
|
|
}
|
|
}
|
|
|
|
func TestBuildHistoryChart(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 := chart.BuildHistoryChart(history, 1000)
|
|
if !strings.Contains(string(chartHTML), "<polyline") {
|
|
t.Fatalf("chart did not contain polyline: %s", string(chartHTML))
|
|
}
|
|
for _, want := range []string{"1.000 W", "00:00", "06:00", "12:00"} {
|
|
if !strings.Contains(string(chartHTML), want) {
|
|
t.Fatalf("chart did not include %q: %s", want, string(chartHTML))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLogoServedAsPng(t *testing.T) {
|
|
srv := server.New("secret", 0, &store.Store{}, embeddedStatic)
|
|
req := httptest.NewRequest(http.MethodGet, "/logo.png", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
srv.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
if got := rec.Header().Get("Content-Type"); got != "image/png" {
|
|
t.Fatalf("content-type = %q, want image/png", got)
|
|
}
|
|
if _, err := png.Decode(bytes.NewReader(rec.Body.Bytes())); err != nil {
|
|
t.Fatalf("logo is not a valid png: %v", err)
|
|
}
|
|
}
|