Files
uptimesebi/internal/api/quiet_hours_test.go
T
Sebastian Unterschütz ee3769f578
ci / image (pull_request) Successful in 6m35s
ci / test (pull_request) Successful in 7m56s
ci / build (pull_request) Successful in 8m24s
ci / lint (pull_request) Successful in 11m55s
feat(alerting): add quiet hours to alert groups
2026-11-22 19:18:23 +01:00

110 lines
5.4 KiB
Go

package api
import (
"slices"
"testing"
"time"
)
func TestValidateQuietHours(t *testing.T) {
got, code := validateQuietHours([]quietHourRequest{
{Weekdays: flexInts{5, 1, 5, 3}, Start: "22:00", End: "07:00", TimeZone: "Europe/Berlin"},
{Weekdays: nil, Start: "00:00", End: "24:00", TimeZone: "UTC"},
{Weekdays: flexInts{0, 6}, Start: "00:00", End: "00:00", TimeZone: "UTC"},
})
if code != "" {
t.Fatalf("unexpected error code %q", code)
}
if len(got) != 3 {
t.Fatalf("len = %d, want 3", len(got))
}
if !slices.Equal(got[0].Weekdays, []int{1, 3, 5}) {
t.Errorf("weekdays not sorted/deduped: %v", got[0].Weekdays)
}
if got[1].Weekdays == nil || len(got[1].Weekdays) != 0 {
t.Errorf("nil weekdays should normalize to empty slice: %#v", got[1].Weekdays)
}
invalid := []struct {
name string
req quietHourRequest
want string
}{
{"empty tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: ""}, "invalid_time_zone"},
{"local tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Local"}, "invalid_time_zone"},
{"unknown tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Mars/Olympus"}, "invalid_time_zone"},
{"bad start", quietHourRequest{Start: "24:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
{"bad end", quietHourRequest{Start: "22:00", End: "24:01", TimeZone: "UTC"}, "invalid_quiet_hours"},
{"short format", quietHourRequest{Start: "9:00", End: "17:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
{"equal non-zero", quietHourRequest{Start: "12:00", End: "12:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
{"weekday negative", quietHourRequest{Weekdays: flexInts{-1}, Start: "22:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
{"weekday > 6", quietHourRequest{Weekdays: flexInts{7}, Start: "22:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
}
for _, tc := range invalid {
if _, code := validateQuietHours([]quietHourRequest{tc.req}); code != tc.want {
t.Errorf("%s: code = %q, want %q", tc.name, code, tc.want)
}
}
}
func TestQuietHoursContains(t *testing.T) {
berlin, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Fatal(err)
}
at := func(y int, m time.Month, d, hh, mm int, loc *time.Location) time.Time {
return time.Date(y, m, d, hh, mm, 0, 0, loc)
}
// 2026-11-23 is Monday, 2026-11-27 is Friday, 2026-11-28 is Saturday, 2026-11-29 is Sunday.
daytime := quietHourView{Weekdays: []int{1, 2, 3, 4, 5}, Start: "09:00", End: "17:00", TimeZone: "Europe/Berlin"}
overnight := quietHourView{Weekdays: []int{1, 2, 3, 4, 5}, Start: "22:00", End: "07:00", TimeZone: "Europe/Berlin"}
weekendZero := quietHourView{Weekdays: []int{0, 6}, Start: "00:00", End: "00:00", TimeZone: "Europe/Berlin"}
weekend24 := quietHourView{Weekdays: []int{0, 6}, Start: "00:00", End: "24:00", TimeZone: "Europe/Berlin"}
eveningEndZero := quietHourView{Weekdays: []int{5}, Start: "18:00", End: "00:00", TimeZone: "UTC"}
dailyNight := quietHourView{Weekdays: []int{}, Start: "23:00", End: "06:00", TimeZone: "Europe/Berlin"}
cases := []struct {
name string
w quietHourView
ts time.Time
want bool
}{
{"daytime before start", daytime, at(2026, 11, 23, 8, 59, berlin), false},
{"daytime at start", daytime, at(2026, 11, 23, 9, 0, berlin), true},
{"daytime inside", daytime, at(2026, 11, 23, 16, 59, berlin), true},
{"daytime at end", daytime, at(2026, 11, 23, 17, 0, berlin), false},
{"daytime wrong day", daytime, at(2026, 11, 28, 12, 0, berlin), false},
{"overnight mon before", overnight, at(2026, 11, 23, 21, 59, berlin), false},
{"overnight mon start", overnight, at(2026, 11, 23, 22, 0, berlin), true},
{"overnight mon early morning (from sun)", overnight, at(2026, 11, 23, 3, 0, berlin), false},
{"overnight tue early morning (from mon)", overnight, at(2026, 11, 24, 6, 59, berlin), true},
{"overnight tue at end", overnight, at(2026, 11, 24, 7, 0, berlin), false},
{"overnight sat early morning (from fri)", overnight, at(2026, 11, 28, 3, 0, berlin), true},
{"overnight sat late evening", overnight, at(2026, 11, 28, 23, 0, berlin), false},
{"overnight sun early morning", overnight, at(2026, 11, 29, 3, 0, berlin), false},
{"weekend 00-00 sat", weekendZero, at(2026, 11, 28, 0, 0, berlin), true},
{"weekend 00-00 sun late", weekendZero, at(2026, 11, 29, 23, 59, berlin), true},
{"weekend 00-00 mon", weekendZero, at(2026, 11, 30, 0, 0, berlin), false},
{"weekend 00-24 sat", weekend24, at(2026, 11, 28, 15, 0, berlin), true},
{"weekend 00-24 mon", weekend24, at(2026, 11, 30, 0, 0, berlin), false},
{"evening 18-00 fri inside", eveningEndZero, at(2026, 11, 27, 23, 59, time.UTC), true},
{"evening 18-00 sat midnight", eveningEndZero, at(2026, 11, 28, 0, 0, time.UTC), false},
// Winter (CET = UTC+1) vs summer (CEST = UTC+2) in Europe/Berlin.
{"daily winter 22:30 UTC is 23:30 CET", dailyNight, at(2026, 1, 15, 22, 30, time.UTC), true},
{"daily winter 21:30 UTC is 22:30 CET", dailyNight, at(2026, 1, 15, 21, 30, time.UTC), false},
{"daily summer 21:30 UTC is 23:30 CEST", dailyNight, at(2026, 7, 15, 21, 30, time.UTC), true},
{"daily summer 03:30 UTC is 05:30 CEST", dailyNight, at(2026, 7, 15, 3, 30, time.UTC), true},
{"daily summer 04:00 UTC is 06:00 CEST", dailyNight, at(2026, 7, 15, 4, 0, time.UTC), false},
}
for _, tc := range cases {
if got := tc.w.contains(tc.ts); got != tc.want {
t.Errorf("%s: contains(%s) = %t, want %t", tc.name, tc.ts.Format(time.RFC3339), got, tc.want)
}
}
}