recurrence_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // recurrence_test.go - tests de la programación recurrente de firmware
  2. // (#761): vencimiento weekly/monthly/once (patrón de scheduler744_test) y
  3. // persistencia kv roundtrip.
  4. package firmware
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. func intPtr(v int) *int { return &v }
  10. func TestRecurrenceDueWeekly(t *testing.T) {
  11. // Domingo 2026-09-13 10:00 local (fecha del desarrollo de #761).
  12. now := time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local)
  13. slot := time.Date(2026, 9, 13, 3, 30, 0, 0, time.Local)
  14. cases := []struct {
  15. name string
  16. rc Recurrence
  17. now time.Time
  18. want bool
  19. }{
  20. {"domingo despues del slot sin lastRun", Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(0), Time: "03:30"}, now, true},
  21. {"domingo antes del slot", Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(0), Time: "11:00"}, now, false},
  22. {"otro dia", Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(3), Time: "03:30"}, now, false},
  23. {"ya disparado hoy", Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(0), Time: "03:30", LastRunMs: slot.UnixMilli()}, now, false},
  24. {"disparado ayer", Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(0), Time: "03:30", LastRunMs: slot.Add(-24 * time.Hour).UnixMilli()}, now, true},
  25. {"disabled", Recurrence{Enabled: false, Kind: RecurrenceWeekly, DayOfWeek: intPtr(0), Time: "03:30"}, now, false},
  26. }
  27. for _, c := range cases {
  28. t.Run(c.name, func(t *testing.T) {
  29. if got := RecurrenceDue(c.rc, c.now); got != c.want {
  30. t.Fatalf("RecurrenceDue = %v, esperaba %v", got, c.want)
  31. }
  32. })
  33. }
  34. }
  35. func TestRecurrenceDueMonthly(t *testing.T) {
  36. now := time.Date(2026, 9, 12, 10, 0, 0, 0, time.Local)
  37. slot := time.Date(2026, 9, 12, 4, 0, 0, 0, time.Local)
  38. cases := []struct {
  39. name string
  40. rc Recurrence
  41. now time.Time
  42. want bool
  43. }{
  44. {"dia 12 a las 04", Recurrence{Enabled: true, Kind: RecurrenceMonthly, DayOfMonth: intPtr(12), Time: "04:00"}, now, true},
  45. {"dia 13", Recurrence{Enabled: true, Kind: RecurrenceMonthly, DayOfMonth: intPtr(13), Time: "04:00"}, now, false},
  46. {"ya disparado este mes", Recurrence{Enabled: true, Kind: RecurrenceMonthly, DayOfMonth: intPtr(12), Time: "04:00", LastRunMs: slot.UnixMilli()}, now, false},
  47. {"clamp 31 en febrero", Recurrence{Enabled: true, Kind: RecurrenceMonthly, DayOfMonth: intPtr(31), Time: "04:00"}, time.Date(2026, 2, 28, 5, 0, 0, 0, time.Local), true},
  48. }
  49. for _, c := range cases {
  50. t.Run(c.name, func(t *testing.T) {
  51. if got := RecurrenceDue(c.rc, c.now); got != c.want {
  52. t.Fatalf("RecurrenceDue = %v, esperaba %v", got, c.want)
  53. }
  54. })
  55. }
  56. }
  57. func TestRecurrenceDueOnce(t *testing.T) {
  58. now := time.Now()
  59. past := now.Add(-time.Hour).UnixMilli()
  60. future := now.Add(time.Hour).UnixMilli()
  61. if !RecurrenceDue(Recurrence{Enabled: true, Kind: RecurrenceOnce, AtMs: past}, now) {
  62. t.Fatalf("once vencido sin disparar debe disparar")
  63. }
  64. if RecurrenceDue(Recurrence{Enabled: true, Kind: RecurrenceOnce, AtMs: past, LastRunMs: past + 1}, now) {
  65. t.Fatalf("once ya disparado no repite")
  66. }
  67. if RecurrenceDue(Recurrence{Enabled: true, Kind: RecurrenceOnce, AtMs: future}, now) {
  68. t.Fatalf("once futuro no dispara")
  69. }
  70. }
  71. func TestValidateRecurrence(t *testing.T) {
  72. if err := ValidateRecurrence(Recurrence{Kind: RecurrenceWeekly}); err == nil {
  73. t.Fatalf("weekly sin dayOfWeek/time debe fallar")
  74. }
  75. if err := ValidateRecurrence(Recurrence{Kind: RecurrenceMonthly, DayOfMonth: intPtr(12)}); err == nil {
  76. t.Fatalf("monthly sin time debe fallar")
  77. }
  78. if err := ValidateRecurrence(Recurrence{Kind: RecurrenceOnce}); err == nil {
  79. t.Fatalf("once sin atMs debe fallar")
  80. }
  81. if err := ValidateRecurrence(Recurrence{Kind: "hourly"}); err == nil {
  82. t.Fatalf("kind desconocido debe fallar")
  83. }
  84. if err := ValidateRecurrence(Recurrence{Kind: RecurrenceWeekly, DayOfWeek: intPtr(5), Time: "23:59"}); err != nil {
  85. t.Fatalf("weekly válida: %v", err)
  86. }
  87. }
  88. func TestRecurrenceSaveLoadRoundtrip(t *testing.T) {
  89. st := open(t)
  90. rc := Recurrence{
  91. Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(2), Time: "04:15",
  92. }
  93. if err := SaveRecurrence(st.db, "rt1", rc); err != nil {
  94. t.Fatalf("save: %v", err)
  95. }
  96. got := LoadRecurrence(st.db, "rt1")
  97. if !got.Enabled || got.Kind != RecurrenceWeekly || got.DayOfWeek == nil || *got.DayOfWeek != 2 || got.Time != "04:15" {
  98. t.Fatalf("roundtrip: %+v", got)
  99. }
  100. SetRecurrenceLastRun(st.db, "rt1", 12345)
  101. if LoadRecurrence(st.db, "rt1").LastRunMs != 12345 {
  102. t.Fatalf("lastRun no persistió")
  103. }
  104. DisableRecurrence(st.db, "rt1")
  105. if LoadRecurrence(st.db, "rt1").Enabled {
  106. t.Fatalf("disable no aplicó")
  107. }
  108. }
  109. func TestNextRecurrenceAtWeekly(t *testing.T) {
  110. // Domingo 10:00, programado martes 04:15 -> próximo martes.
  111. now := time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local)
  112. rc := Recurrence{Enabled: true, Kind: RecurrenceWeekly, DayOfWeek: intPtr(2), Time: "04:15"}
  113. next := NextRecurrenceAt(rc, now)
  114. if next.Weekday() != time.Tuesday || next.Hour() != 4 || next.Minute() != 15 {
  115. t.Fatalf("next = %v (%s), esperaba martes 04:15", next, next.Weekday())
  116. }
  117. }