Files
archie/internal/domain/scheduling/schedule_test.go
T
sam cc5789eb5f feat(scheduling): schedules and runs in the State Store with grid timing and run history
Schedules leave the schedules control-plane resource for State Store tables
behind a ScheduleService contract. A run is claimed per schedule slot, missed
slots fire once, the next run stays on the schedule's grid, cron is read in the
schedule-settings time zone, and failed runs are recorded and advance. Three
failures in a row raise a schedule.failing alert. A fired run records a
schedule-fired capture. Run history is pruned by age and count.

Removes the chat and spawn-task runners, the dangerous-payload guard and the
old schedules settings page.
2026-10-26 00:49:24 +11:00

114 lines
3.2 KiB
Go

package scheduling
import (
"testing"
"time"
)
func TestTimingNext(t *testing.T) {
sydney, err := time.LoadLocation("Australia/Sydney")
if err != nil {
t.Fatal(err)
}
utc := func(s string) time.Time {
v, err := time.Parse(time.RFC3339, s)
if err != nil {
t.Fatal(err)
}
return v
}
at := utc("2026-11-01T09:00:00Z")
tests := []struct {
name string
timing Timing
loc *time.Location
after time.Time
want time.Time
none bool
}{
{
name: "interval before its start fires at the start",
timing: Timing{Kind: TimingInterval, Interval: Duration(time.Hour), Start: utc("2026-10-01T00:30:00Z")},
after: utc("2026-10-01T00:00:00Z"),
want: utc("2026-10-01T00:30:00Z"),
},
{
name: "interval after three days down stays on its grid",
timing: Timing{Kind: TimingInterval, Interval: Duration(time.Hour), Start: utc("2026-10-01T00:30:00Z")},
after: utc("2026-10-04T07:12:00Z"),
want: utc("2026-10-04T07:30:00Z"),
},
{
name: "interval exactly on a slot moves to the next one",
timing: Timing{Kind: TimingInterval, Interval: Duration(time.Hour), Start: utc("2026-10-01T00:30:00Z")},
after: utc("2026-10-01T02:30:00Z"),
want: utc("2026-10-01T03:30:00Z"),
},
{
name: "cron reads the hour in the instance zone",
timing: Timing{Kind: TimingCron, Cron: "0 9 * * *"},
loc: sydney,
after: utc("2026-07-01T00:00:00Z"),
want: utc("2026-07-01T23:00:00Z"), // 09:00 AEST
},
{
name: "cron keeps local time across the spring-forward change",
timing: Timing{Kind: TimingCron, Cron: "0 9 * * *"},
loc: sydney,
after: utc("2026-10-04T00:00:00Z"), // 11:00 AEDT on the change day
want: utc("2026-10-04T22:00:00Z"), // 09:00 AEDT
},
{
name: "cron in an hour that clocks skip fires the next day",
timing: Timing{Kind: TimingCron, Cron: "30 2 * * *"},
loc: sydney,
after: utc("2026-10-03T15:00:00Z"), // 01:00 AEST, clocks jump 02:00 to 03:00
want: utc("2026-10-04T15:30:00Z"), // 02:30 AEDT next day
},
{
name: "cron in an hour repeated when clocks go back fires once",
timing: Timing{Kind: TimingCron, Cron: "30 2 * * *"},
loc: sydney,
after: utc("2026-04-04T15:31:00Z"), // just after the first 02:30 AEDT
want: utc("2026-04-05T16:30:00Z"), // 02:30 AEST next day, not the repeat
},
{
name: "cron in a half-hour zone",
timing: Timing{Kind: TimingCron, Cron: "0 10 * * *"},
loc: time.FixedZone("ACST", 9*3600+1800),
after: utc("2026-07-01T00:10:00Z"),
want: utc("2026-07-01T00:30:00Z"),
},
{
name: "once before its time",
timing: Timing{Kind: TimingOnce, At: &at},
after: utc("2026-10-01T00:00:00Z"),
want: at,
},
{
name: "once after its time never fires again",
timing: Timing{Kind: TimingOnce, At: &at},
after: at,
none: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
loc := tt.loc
if loc == nil {
loc = time.UTC
}
got, ok, err := tt.timing.Next(tt.after, loc)
if err != nil {
t.Fatal(err)
}
if ok == tt.none {
t.Fatalf("Next ok = %v, want %v", ok, !tt.none)
}
if !got.Equal(tt.want) {
t.Fatalf("Next = %v, want %v", got, tt.want)
}
})
}
}