Files
uptimesebi/internal/web/ranges_test.go
T
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

50 lines
1.5 KiB
Go

package web
import (
"testing"
"time"
)
func TestSLA(t *testing.T) {
t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
at := func(m int) time.Time { return t0.Add(time.Duration(m) * time.Minute) }
tests := []struct {
name string
down []Span
from, to time.Time
want float64
}{
{"no incidents", nil, at(0), at(60), 1},
{"six minutes down", []Span{{at(10), at(16)}}, at(0), at(60), 0.9},
{"clipped at window start", []Span{{at(-30), at(6)}}, at(0), at(60), 0.9},
{"ongoing", []Span{{at(54), time.Time{}}}, at(0), at(60), 0.9},
{"overlapping counted once", []Span{{at(10), at(20)}, {at(15), at(22)}}, at(0), at(60), 0.8},
{"outside window", []Span{{at(70), at(80)}}, at(0), at(60), 1},
{"empty window", nil, at(60), at(60), -1},
}
for _, tt := range tests {
if got := SLA(tt.down, tt.from, tt.to); got < tt.want-1e-9 || got > tt.want+1e-9 {
t.Errorf("%s: SLA() = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestPercentPrecise(t *testing.T) {
for r, want := range map[float64]string{1: "100%", 0.999996: "99.999%", 0.9995: "99.950%", 0.9912: "99.12%", 0.5: "50.0%"} {
if got := PercentPrecise(r); got != want {
t.Errorf("PercentPrecise(%v) = %q, want %q", r, got, want)
}
}
}
func TestRanges(t *testing.T) {
for _, r := range Ranges {
if n := r.Bars(); n < 24 || n > 72 {
t.Errorf("range %s has %d bars, want a readable number", r.Key, n)
}
}
if RangeByKey("bogus").Key != DefaultRange {
t.Error("unknown range does not fall back to the default")
}
}