GlucoseAlertConfigurationTests.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Trio Alerts: GlucoseAlertConfiguration.isNight") struct GlucoseAlertConfigurationTests {
  5. /// Fixed UTC calendar so date math is deterministic regardless of host TZ.
  6. private static let utcCalendar: Calendar = {
  7. var cal = Calendar(identifier: .gregorian)
  8. cal.timeZone = TimeZone(identifier: "UTC")!
  9. return cal
  10. }()
  11. /// Build a Date on a fixed day (2026-06-22) via the UTC calendar.
  12. private static func makeDate(hour: Int, minute: Int, second: Int = 0) -> Date {
  13. var components = DateComponents()
  14. components.year = 2026
  15. components.month = 6
  16. components.day = 22
  17. components.hour = hour
  18. components.minute = minute
  19. components.second = second
  20. return utcCalendar.date(from: components)!
  21. }
  22. // MARK: - Default window (day 06:00 / night 22:00, wrap)
  23. @Test("Default: 02:00 is night") func defaultEarlyMorningIsNight() {
  24. let config = GlucoseAlertConfiguration()
  25. #expect(config.isNight(at: Self.makeDate(hour: 2, minute: 0), calendar: Self.utcCalendar))
  26. }
  27. @Test("Default: 10:00 is day") func defaultMorningIsDay() {
  28. let config = GlucoseAlertConfiguration()
  29. #expect(!config.isNight(at: Self.makeDate(hour: 10, minute: 0), calendar: Self.utcCalendar))
  30. }
  31. @Test("Default: 14:00 is day") func defaultAfternoonIsDay() {
  32. let config = GlucoseAlertConfiguration()
  33. #expect(!config.isNight(at: Self.makeDate(hour: 14, minute: 0), calendar: Self.utcCalendar))
  34. }
  35. @Test("Default: 23:00 is night") func defaultLateEveningIsNight() {
  36. let config = GlucoseAlertConfiguration()
  37. #expect(config.isNight(at: Self.makeDate(hour: 23, minute: 0), calendar: Self.utcCalendar))
  38. }
  39. // MARK: - Boundary (default)
  40. @Test("Default boundary: 22:00:00 is night (nightStart inclusive)") func defaultNightStartInclusive() {
  41. let config = GlucoseAlertConfiguration()
  42. #expect(config.isNight(at: Self.makeDate(hour: 22, minute: 0, second: 0), calendar: Self.utcCalendar))
  43. }
  44. @Test("Default boundary: 06:00:00 is day (dayStart exclusive)") func defaultDayStartExclusive() {
  45. let config = GlucoseAlertConfiguration()
  46. #expect(!config.isNight(at: Self.makeDate(hour: 6, minute: 0, second: 0), calendar: Self.utcCalendar))
  47. }
  48. @Test("Default boundary: 05:59:59 is night") func defaultJustBeforeDayStartIsNight() {
  49. let config = GlucoseAlertConfiguration()
  50. #expect(config.isNight(at: Self.makeDate(hour: 5, minute: 59, second: 59), calendar: Self.utcCalendar))
  51. }
  52. @Test("Default boundary: 21:59:59 is day") func defaultJustBeforeNightStartIsDay() {
  53. let config = GlucoseAlertConfiguration()
  54. #expect(!config.isNight(at: Self.makeDate(hour: 21, minute: 59, second: 59), calendar: Self.utcCalendar))
  55. }
  56. // MARK: - Non-wrap config (dayStart 07:00, nightStart 01:00)
  57. @Test("Non-wrap: 03:00 is night") func nonWrapInsideNightIsNight() {
  58. let config = GlucoseAlertConfiguration(
  59. dayStart: TimeOfDay(hour: 7, minute: 0),
  60. nightStart: TimeOfDay(hour: 1, minute: 0)
  61. )
  62. #expect(config.isNight(at: Self.makeDate(hour: 3, minute: 0), calendar: Self.utcCalendar))
  63. }
  64. @Test("Non-wrap: 12:00 is day") func nonWrapMiddayIsDay() {
  65. let config = GlucoseAlertConfiguration(
  66. dayStart: TimeOfDay(hour: 7, minute: 0),
  67. nightStart: TimeOfDay(hour: 1, minute: 0)
  68. )
  69. #expect(!config.isNight(at: Self.makeDate(hour: 12, minute: 0), calendar: Self.utcCalendar))
  70. }
  71. @Test("Non-wrap: 00:30 is day (before nightStart)") func nonWrapBeforeNightStartIsDay() {
  72. let config = GlucoseAlertConfiguration(
  73. dayStart: TimeOfDay(hour: 7, minute: 0),
  74. nightStart: TimeOfDay(hour: 1, minute: 0)
  75. )
  76. #expect(!config.isNight(at: Self.makeDate(hour: 0, minute: 30), calendar: Self.utcCalendar))
  77. }
  78. @Test("Non-wrap boundary: 07:00:00 is day (dayStart exclusive)") func nonWrapDayStartExclusive() {
  79. let config = GlucoseAlertConfiguration(
  80. dayStart: TimeOfDay(hour: 7, minute: 0),
  81. nightStart: TimeOfDay(hour: 1, minute: 0)
  82. )
  83. #expect(!config.isNight(at: Self.makeDate(hour: 7, minute: 0, second: 0), calendar: Self.utcCalendar))
  84. }
  85. @Test("Non-wrap boundary: 01:00:00 is night (nightStart inclusive)") func nonWrapNightStartInclusive() {
  86. let config = GlucoseAlertConfiguration(
  87. dayStart: TimeOfDay(hour: 7, minute: 0),
  88. nightStart: TimeOfDay(hour: 1, minute: 0)
  89. )
  90. #expect(config.isNight(at: Self.makeDate(hour: 1, minute: 0, second: 0), calendar: Self.utcCalendar))
  91. }
  92. // MARK: - Degenerate equal (dayStart == nightStart == 06:00) — proves wrap >= branch
  93. @Test("Equal: 05:00 is night") func equalBeforeIsNight() {
  94. let config = GlucoseAlertConfiguration(
  95. dayStart: TimeOfDay(hour: 6, minute: 0),
  96. nightStart: TimeOfDay(hour: 6, minute: 0)
  97. )
  98. #expect(config.isNight(at: Self.makeDate(hour: 5, minute: 0), calendar: Self.utcCalendar))
  99. }
  100. @Test("Equal: 06:00:00 is night (wrap >= branch)") func equalAtBoundaryIsNight() {
  101. let config = GlucoseAlertConfiguration(
  102. dayStart: TimeOfDay(hour: 6, minute: 0),
  103. nightStart: TimeOfDay(hour: 6, minute: 0)
  104. )
  105. #expect(config.isNight(at: Self.makeDate(hour: 6, minute: 0, second: 0), calendar: Self.utcCalendar))
  106. }
  107. @Test("Equal: 10:00 is night") func equalAfterIsNight() {
  108. let config = GlucoseAlertConfiguration(
  109. dayStart: TimeOfDay(hour: 6, minute: 0),
  110. nightStart: TimeOfDay(hour: 6, minute: 0)
  111. )
  112. #expect(config.isNight(at: Self.makeDate(hour: 10, minute: 0), calendar: Self.utcCalendar))
  113. }
  114. }