calendar_renew_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestNextCalendarRenewal_ClampsToShortMonths(t *testing.T) {
  7. utc := time.UTC
  8. cases := []struct {
  9. name string
  10. from time.Time
  11. day int
  12. want time.Time
  13. }{
  14. {
  15. name: "31st in a 28-day February",
  16. from: time.Date(2026, time.January, 31, 0, 0, 0, 0, utc),
  17. day: 31,
  18. want: time.Date(2026, time.February, 28, 0, 0, 0, 0, utc),
  19. },
  20. {
  21. name: "31st returns to the 31st after the short month",
  22. from: time.Date(2026, time.February, 28, 0, 0, 0, 0, utc),
  23. day: 31,
  24. want: time.Date(2026, time.March, 31, 0, 0, 0, 0, utc),
  25. },
  26. {
  27. name: "29th in a leap February",
  28. from: time.Date(2028, time.January, 29, 0, 0, 0, 0, utc),
  29. day: 29,
  30. want: time.Date(2028, time.February, 29, 0, 0, 0, 0, utc),
  31. },
  32. {
  33. name: "31st in a 30-day month",
  34. from: time.Date(2026, time.March, 31, 0, 0, 0, 0, utc),
  35. day: 31,
  36. want: time.Date(2026, time.April, 30, 0, 0, 0, 0, utc),
  37. },
  38. {
  39. name: "December rolls into January",
  40. from: time.Date(2026, time.December, 5, 0, 0, 0, 0, utc),
  41. day: 5,
  42. want: time.Date(2027, time.January, 5, 0, 0, 0, 0, utc),
  43. },
  44. {
  45. name: "later in the same month renews this month",
  46. from: time.Date(2026, time.June, 3, 12, 0, 0, 0, utc),
  47. day: 20,
  48. want: time.Date(2026, time.June, 20, 0, 0, 0, 0, utc),
  49. },
  50. }
  51. for _, tc := range cases {
  52. t.Run(tc.name, func(t *testing.T) {
  53. got := nextCalendarRenewal(tc.from, tc.day, utc)
  54. if !got.Equal(tc.want) {
  55. t.Fatalf("next renewal = %s, want %s", got.Format(time.RFC3339), tc.want.Format(time.RFC3339))
  56. }
  57. })
  58. }
  59. }
  60. // The renewal instant is midnight in the panel's zone, not in UTC: an operator
  61. // billing on the 1st expects the period to turn over at their local midnight.
  62. func TestNextCalendarRenewal_UsesPanelZone(t *testing.T) {
  63. loc, err := time.LoadLocation("Asia/Tehran")
  64. if err != nil {
  65. t.Skip("zone database unavailable")
  66. }
  67. from := time.Date(2026, time.June, 15, 12, 0, 0, 0, time.UTC)
  68. got := nextCalendarRenewal(from, 1, loc)
  69. if got.Location() != loc {
  70. t.Fatalf("renewal computed in %s, want the panel zone", got.Location())
  71. }
  72. y, m, d := got.Date()
  73. if y != 2026 || m != time.July || d != 1 {
  74. t.Fatalf("renewal date = %04d-%02d-%02d, want 2026-07-01", y, m, d)
  75. }
  76. if h, mi, s := got.Clock(); h != 0 || mi != 0 || s != 0 {
  77. t.Fatalf("renewal at %02d:%02d:%02d, want local midnight", h, mi, s)
  78. }
  79. }
  80. // Crossing a DST boundary must still land on local midnight rather than
  81. // drifting an hour, which a fixed 24h*N step cannot promise.
  82. func TestNextCalendarRenewal_SurvivesDaylightSaving(t *testing.T) {
  83. loc, err := time.LoadLocation("Europe/Berlin")
  84. if err != nil {
  85. t.Skip("zone database unavailable")
  86. }
  87. // Berlin moves to summer time on 29 March 2026.
  88. from := time.Date(2026, time.March, 10, 0, 0, 0, 0, loc)
  89. got := nextCalendarRenewal(from, 10, loc)
  90. if h, mi, _ := got.Clock(); h != 0 || mi != 0 {
  91. t.Fatalf("renewal at %02d:%02d local, want midnight across the DST change", h, mi)
  92. }
  93. if got.Day() != 10 || got.Month() != time.April {
  94. t.Fatalf("renewal = %s, want 10 April", got.Format(time.RFC3339))
  95. }
  96. }
  97. func TestNextCalendarRenewal_AlwaysMovesForward(t *testing.T) {
  98. utc := time.UTC
  99. from := time.Date(2026, time.May, 20, 0, 0, 0, 0, utc)
  100. // Same day: the boundary has already passed today, so the next one is a
  101. // month away rather than the instant we started from.
  102. got := nextCalendarRenewal(from, 20, utc)
  103. if !got.After(from) {
  104. t.Fatalf("next renewal %s is not after %s", got, from)
  105. }
  106. if got.Month() != time.June {
  107. t.Fatalf("next renewal = %s, want June", got.Format(time.RFC3339))
  108. }
  109. }