package service import ( "testing" "time" ) func at(day, clock string) time.Time { t, _ := time.ParseInLocation("2006-01-02 15:04", day+" "+clock, time.Local) return t } func TestNextAutoUpdateDaily(t *testing.T) { // 昨天 09:00 跑过,现在是今天 08:00 → 下一次今天 09:00 next := NextAutoUpdate("daily", 1, "09:00", at("2026-08-10", "09:00"), at("2026-08-11", "08:00")) if !next.Equal(at("2026-08-11", "09:00")) { t.Fatalf("want today 09:00, got %v", next) } // 今天 09:01 已跑,现在 15:00 → 明天 09:00 next = NextAutoUpdate("daily", 1, "09:00", at("2026-08-11", "09:01"), at("2026-08-11", "15:00")) if !next.Equal(at("2026-08-12", "09:00")) { t.Fatalf("want tomorrow 09:00, got %v", next) } // 昨天跑过,现在 10:00(已过 09:00)→ 今天 09:00(补跑) next = NextAutoUpdate("daily", 1, "09:00", at("2026-08-10", "09:00"), at("2026-08-11", "10:00")) if !next.Equal(at("2026-08-11", "09:00")) { t.Fatalf("want catch-up today 09:00, got %v", next) } } func TestNextAutoUpdateEveryNDays(t *testing.T) { next := NextAutoUpdate("everyNDays", 3, "22:30", at("2026-08-08", "22:30"), at("2026-08-11", "08:00")) if !next.Equal(at("2026-08-11", "22:30")) { t.Fatalf("want 08-11 22:30, got %v", next) } } func TestNextAutoUpdateEveryNHours(t *testing.T) { next := NextAutoUpdate("everyNHours", 4, "", at("2026-08-11", "06:15"), at("2026-08-11", "08:00")) if !next.Equal(at("2026-08-11", "10:15")) { t.Fatalf("want 10:15, got %v", next) } } func TestNextAutoUpdateBadClock(t *testing.T) { next := NextAutoUpdate("daily", 1, "bogus", at("2026-08-10", "09:00"), at("2026-08-11", "08:00")) if !next.Equal(at("2026-08-11", "09:00")) { t.Fatalf("bad clock should fall back to 09:00, got %v", next) } } func TestDigestDue(t *testing.T) { if DigestDue("21:00", at("2026-08-11", "20:59")) { t.Fatal("20:59 should not be due for 21:00") } if !DigestDue("21:00", at("2026-08-11", "21:00")) { t.Fatal("21:00 sharp should be due") } if !DigestDue("21:00", at("2026-08-11", "23:40")) { t.Fatal("23:40 should be due") } // 非法时间回退 09:00(与 parseClock 兜底一致) if DigestDue("bogus", at("2026-08-11", "08:00")) { t.Fatal("bad clock falls back to 09:00, 08:00 not due") } if !DigestDue("bogus", at("2026-08-11", "09:30")) { t.Fatal("bad clock falls back to 09:00, 09:30 due") } }