schedule_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package web
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/robfig/cron/v3"
  6. )
  7. // A five-field crontab used to be refused by the WithSeconds() scheduler, so the
  8. // Telegram/Discord report and LDAP sync were never scheduled at all.
  9. func TestScheduleSettingJob(t *testing.T) {
  10. from := time.Date(2026, 10, 11, 7, 0, 0, 0, time.UTC)
  11. cases := []struct {
  12. name, spec string
  13. want time.Time
  14. }{
  15. {"five-field crontab", "30 8 * * *", time.Date(2026, 10, 11, 8, 30, 0, 0, time.UTC)},
  16. {"six-field crontab", "15 30 8 * * *", time.Date(2026, 10, 11, 8, 30, 15, 0, time.UTC)},
  17. {"empty falls back", " ", time.Date(2026, 10, 12, 0, 0, 0, 0, time.UTC)},
  18. {"invalid falls back", "every morning", time.Date(2026, 10, 12, 0, 0, 0, 0, time.UTC)},
  19. }
  20. for _, c := range cases {
  21. t.Run(c.name, func(t *testing.T) {
  22. sched := cron.New(cron.WithSeconds(), cron.WithLocation(time.UTC))
  23. id := scheduleSettingJob(sched, "test job", c.spec, "@daily", cron.FuncJob(func() {}))
  24. entry := sched.Entry(id)
  25. if entry.Schedule == nil {
  26. t.Fatalf("spec %q: job was not scheduled", c.spec)
  27. }
  28. if got := entry.Schedule.Next(from); !got.Equal(c.want) {
  29. t.Errorf("spec %q: next run %v, want %v", c.spec, got, c.want)
  30. }
  31. })
  32. }
  33. }