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") } }