Files
uptimesebi/internal/web/charts_test.go
Sebastian UnterschützandClaude Opus 5.5 273a958d76 feat(web): switch charts between 1h, 3h, 12h, 24h and 7d with matching SLA
Bars use a resolution fitting the range and the latency line is placed
by time. The SLA is the share of time a monitor was not down, counted
from incidents and only since the monitor exists. Monitor pages list the
SLA from one hour to 90 days.

Closes #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 09:29:34 +01:00

47 lines
1.4 KiB
Go

package web
import (
"strings"
"testing"
"time"
)
func TestUptimeBars(t *testing.T) {
end := time.Date(2026, 1, 2, 10, 30, 0, 0, time.UTC)
hour := func(h int) time.Time { return time.Date(2026, 1, 2, h, 0, 0, 0, time.UTC) }
bars := UptimeBars([]Bucket{
{Start: hour(10), Total: 60, OK: 60},
{Start: hour(9), Total: 60, OK: 58},
{Start: hour(8), Total: 60, OK: 30},
}, end, time.Hour, 4, "15:04")
want := []Status{StatusUnknown, StatusDown, StatusDegraded, StatusUp}
for i, b := range bars {
if b.Status != want[i] {
t.Errorf("bar %d = %s (%s), want %s", i, b.Status, b.Title, want[i])
}
}
if bars[3].Title != "10:00 · 100%" {
t.Errorf("last bar title = %q", bars[3].Title)
}
}
func TestPercent(t *testing.T) {
for r, want := range map[float64]string{1: "100%", 0.99951: "99.95%", 0.9: "90.0%"} {
if got := Percent(r); got != want {
t.Errorf("Percent(%v) = %q, want %q", r, got, want)
}
}
}
func TestLatencyPlacesPointsByTime(t *testing.T) {
from := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
to := from.Add(10 * time.Minute)
l := Latency([]Bucket{
{Start: from.Add(5 * time.Minute), Total: 1, Latency: 100},
{Start: from.Add(10 * time.Minute), Total: 1, Latency: 50},
}, from, to, 100, 10)
if !strings.HasPrefix(l.Points, "50.0,") || !strings.Contains(l.Points, " 100.0,") {
t.Errorf("Latency().Points = %q, want points at x=50 and x=100", l.Points)
}
}