1
0

calendar_weekly_renew_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestNextWeeklyRenewal_RepeatedMidnight(t *testing.T) {
  7. loc, err := time.LoadLocation("America/Havana")
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. for _, tt := range []struct{ expiry, want string }{
  12. {"2026-10-25T00:00:00-04:00", "2026-11-01T00:00:00-04:00"},
  13. {"2026-11-01T00:00:00-04:00", "2026-11-08T00:00:00-05:00"},
  14. {"2026-11-01T00:00:00-05:00", "2026-11-08T00:00:00-05:00"},
  15. } {
  16. from, err := time.Parse(time.RFC3339, tt.expiry)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. got := nextWeeklyRenewal(from, 7, loc).Format(time.RFC3339)
  21. if got != tt.want {
  22. t.Fatalf("next Sunday after %s = %s, want %s", tt.expiry, got, tt.want)
  23. }
  24. }
  25. }
  26. func TestNextWeeklyRenewal_SkippedDates(t *testing.T) {
  27. for _, tt := range []struct {
  28. zone, from, want string
  29. weekday int
  30. }{
  31. {"America/Havana", "2026-03-01T00:00:00-05:00", "2026-03-08T01:00:00-04:00", 7},
  32. {"Pacific/Apia", "2011-12-23T00:00:00-10:00", "2012-01-06T00:00:00+14:00", 5},
  33. } {
  34. t.Run(tt.zone, func(t *testing.T) {
  35. loc, err := time.LoadLocation(tt.zone)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. from, err := time.Parse(time.RFC3339, tt.from)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. got := nextWeeklyRenewal(from, tt.weekday, loc).Format(time.RFC3339)
  44. if got != tt.want {
  45. t.Fatalf("next weekly cutoff = %s, want %s", got, tt.want)
  46. }
  47. })
  48. }
  49. }