Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
50 lines
1.8 KiB
Go
50 lines
1.8 KiB
Go
package catalog
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAScheduleWindowHolds(t *testing.T) {
|
|
ist := time.FixedZone("TRT", 3*3600)
|
|
at := func(day, hm string) time.Time {
|
|
d, _ := time.ParseInLocation("2006-01-02 15:04", day+" "+hm, ist)
|
|
return d
|
|
}
|
|
// 2026-10-05 is a Monday.
|
|
work := Scaling{Schedule: []ScaleWindow{{Days: []string{"weekdays"}, From: "08:00", To: "18:00", Min: 2}},
|
|
Prewarm: 30 * time.Minute}
|
|
night := Scaling{Schedule: []ScaleWindow{{From: "22:00", To: "02:00", Min: 1}}}
|
|
for _, c := range []struct {
|
|
s Scaling
|
|
t time.Time
|
|
want bool
|
|
}{
|
|
{work, at("2026-10-05", "09:00"), true},
|
|
{work, at("2026-10-05", "07:40"), true}, // prewarm
|
|
{work, at("2026-10-05", "07:20"), false},
|
|
{work, at("2026-10-05", "18:00"), false},
|
|
{work, at("2026-10-04", "10:00"), false}, // Sunday
|
|
{work, at("2026-10-04", "23:50"), false},
|
|
{night, at("2026-10-05", "23:00"), true},
|
|
{night, at("2026-10-06", "01:30"), true}, // after midnight, from yesterday's start
|
|
{night, at("2026-10-06", "02:00"), false},
|
|
{night, at("2026-10-06", "12:00"), false},
|
|
} {
|
|
if _, got := c.s.Window(c.t); got != c.want {
|
|
t.Errorf("%v at %s: %v, want %v", c.s.Schedule[0], c.t.Format("Mon 15:04"), got, c.want)
|
|
}
|
|
}
|
|
// Opened the evening before by its lead.
|
|
early := Scaling{Schedule: []ScaleWindow{{Days: []string{"tue"}, From: "00:15", To: "06:00", Min: 1}}, Prewarm: time.Hour}
|
|
if _, ok := early.Window(at("2026-10-05", "23:30")); !ok {
|
|
t.Error("a window after midnight was not prewarmed the evening before")
|
|
}
|
|
for _, bad := range []ScaleWindow{{From: "8:00", To: "18:00"}, {From: "08:00", To: "08:00"},
|
|
{From: "08:00", To: "18:00", Days: []string{"monday"}}, {From: "08:00", To: "18:00", Min: 3, Max: 2}} {
|
|
if bad.validate() == nil {
|
|
t.Errorf("%+v accepted", bad)
|
|
}
|
|
}
|
|
}
|