Files
uptimesebi/internal/api/quiet_hours_test.go
T
Sebastian Unterschütz 1c31003baf
ci / image (pull_request) Successful in 4m28s
ci / build (pull_request) Successful in 8m16s
ci / lint (pull_request) Successful in 12m11s
ci / test (pull_request) Successful in 15m40s
style(alerting): align quiet hours code and tests with Go style guide
2026-11-22 19:43:17 +01:00

153 lines
6.5 KiB
Go

package api
import (
"slices"
"testing"
"time"
)
func TestValidateQuietHours(t *testing.T) {
in := []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"},
}
got, code := validateQuietHours(in)
if code != "" {
t.Fatalf("validateQuietHours(%+v) error code = %q, want %q", in, code, "")
}
if len(got) != 3 {
t.Fatalf("len(validateQuietHours(...)) = %d, want 3", len(got))
}
if want := []int{1, 3, 5}; !slices.Equal(got[0].Weekdays, want) {
t.Errorf("got[0].Weekdays = %v, want %v", got[0].Weekdays, want)
}
if got[1].Weekdays == nil || len(got[1].Weekdays) != 0 {
t.Errorf("got[1].Weekdays = %#v, want non-nil empty []int{}", got[1].Weekdays)
}
invalid := []struct {
name string
req quietHourRequest
want string
}{
{
name: "empty time zone",
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: ""},
want: "invalid_time_zone",
},
{
name: "local time zone",
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Local"},
want: "invalid_time_zone",
},
{
name: "unknown time zone",
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Mars/Olympus"},
want: "invalid_time_zone",
},
{
name: "invalid start hour",
req: quietHourRequest{Start: "24:00", End: "07:00", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
{
name: "invalid end minute",
req: quietHourRequest{Start: "22:00", End: "24:01", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
{
name: "short clock format",
req: quietHourRequest{Start: "9:00", End: "17:00", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
{
name: "equal non-zero start and end",
req: quietHourRequest{Start: "12:00", End: "12:00", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
{
name: "negative weekday",
req: quietHourRequest{Weekdays: flexInts{-1}, Start: "22:00", End: "07:00", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
{
name: "weekday out of range",
req: quietHourRequest{Weekdays: flexInts{7}, Start: "22:00", End: "07:00", TimeZone: "UTC"},
want: "invalid_quiet_hours",
},
}
for _, tc := range invalid {
t.Run(tc.name, func(t *testing.T) {
_, got := validateQuietHours([]quietHourRequest{tc.req})
if got != tc.want {
t.Errorf("validateQuietHours(%+v) error code = %q, want %q", tc.req, got, tc.want)
}
})
}
}
func TestQuietHoursContains(t *testing.T) {
berlin, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Fatalf("time.LoadLocation(%q) error = %v", "Europe/Berlin", 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,
// and 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
window quietHourView
ts time.Time
want bool
}{
{name: "daytime before start", window: daytime, ts: at(2026, 11, 23, 8, 59, berlin), want: false},
{name: "daytime at start", window: daytime, ts: at(2026, 11, 23, 9, 0, berlin), want: true},
{name: "daytime inside", window: daytime, ts: at(2026, 11, 23, 16, 59, berlin), want: true},
{name: "daytime at end", window: daytime, ts: at(2026, 11, 23, 17, 0, berlin), want: false},
{name: "daytime wrong day", window: daytime, ts: at(2026, 11, 28, 12, 0, berlin), want: false},
{name: "overnight mon before", window: overnight, ts: at(2026, 11, 23, 21, 59, berlin), want: false},
{name: "overnight mon start", window: overnight, ts: at(2026, 11, 23, 22, 0, berlin), want: true},
{name: "overnight mon early from sun", window: overnight, ts: at(2026, 11, 23, 3, 0, berlin), want: false},
{name: "overnight tue early from mon", window: overnight, ts: at(2026, 11, 24, 6, 59, berlin), want: true},
{name: "overnight tue at end", window: overnight, ts: at(2026, 11, 24, 7, 0, berlin), want: false},
{name: "overnight sat early from fri", window: overnight, ts: at(2026, 11, 28, 3, 0, berlin), want: true},
{name: "overnight sat late", window: overnight, ts: at(2026, 11, 28, 23, 0, berlin), want: false},
{name: "overnight sun early", window: overnight, ts: at(2026, 11, 29, 3, 0, berlin), want: false},
{name: "weekend 00-00 sat", window: weekendZero, ts: at(2026, 11, 28, 0, 0, berlin), want: true},
{name: "weekend 00-00 sun late", window: weekendZero, ts: at(2026, 11, 29, 23, 59, berlin), want: true},
{name: "weekend 00-00 mon", window: weekendZero, ts: at(2026, 11, 30, 0, 0, berlin), want: false},
{name: "weekend 00-24 sat", window: weekend24, ts: at(2026, 11, 28, 15, 0, berlin), want: true},
{name: "weekend 00-24 mon", window: weekend24, ts: at(2026, 11, 30, 0, 0, berlin), want: false},
{name: "evening 18-00 fri inside", window: eveningEndZero, ts: at(2026, 11, 27, 23, 59, time.UTC), want: true},
{name: "evening 18-00 sat midnight", window: eveningEndZero, ts: at(2026, 11, 28, 0, 0, time.UTC), want: false},
// Winter (CET = UTC+1) vs summer (CEST = UTC+2) in Europe/Berlin.
{name: "daily winter 22:30 UTC is 23:30 CET", window: dailyNight, ts: at(2026, 1, 15, 22, 30, time.UTC), want: true},
{name: "daily winter 21:30 UTC is 22:30 CET", window: dailyNight, ts: at(2026, 1, 15, 21, 30, time.UTC), want: false},
{name: "daily summer 21:30 UTC is 23:30 CEST", window: dailyNight, ts: at(2026, 7, 15, 21, 30, time.UTC), want: true},
{name: "daily summer 03:30 UTC is 05:30 CEST", window: dailyNight, ts: at(2026, 7, 15, 3, 30, time.UTC), want: true},
{name: "daily summer 04:00 UTC is 06:00 CEST", window: dailyNight, ts: at(2026, 7, 15, 4, 0, time.UTC), want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := tc.window.contains(tc.ts); got != tc.want {
t.Errorf("contains(%s) = %t, want %t", tc.ts.Format(time.RFC3339), got, tc.want)
}
})
}
}