ProfileTargetsTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Target Profile") struct TargetTests {
  5. let standardTargets = BGTargets(
  6. units: .mgdL,
  7. userPreferredUnits: .mgdL,
  8. targets: [
  9. BGTargetEntry(low: 100, high: 120, start: "00:00:00", offset: 0),
  10. BGTargetEntry(low: 90, high: 110, start: "03:00:00", offset: 180),
  11. BGTargetEntry(low: 110, high: 130, start: "06:00:00", offset: 360)
  12. ]
  13. )
  14. let tempTargets = [
  15. TempTarget(
  16. name: nil,
  17. createdAt: Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!,
  18. targetTop: 100,
  19. targetBottom: 80,
  20. duration: 120,
  21. enteredBy: nil,
  22. reason: nil,
  23. isPreset: nil,
  24. enabled: nil,
  25. halfBasalTarget: nil
  26. )
  27. ]
  28. let profile = Profile()
  29. @Test("should return correct target from schedule") func correctTargetFromSchedule() async throws {
  30. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 1))!
  31. let (_, result) = try Targets
  32. .bgTargetsLookup(targets: standardTargets, tempTargets: tempTargets, profile: profile, now: now)
  33. #expect(result.high == 100)
  34. #expect(result.low == 100)
  35. }
  36. @Test("should override from Profile targetBg") func profileOverride() async throws {
  37. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 1))!
  38. var profile = Profile()
  39. profile.targetBg = 110
  40. let (_, result) = try Targets
  41. .bgTargetsLookup(targets: standardTargets, tempTargets: tempTargets, profile: profile, now: now)
  42. #expect(result.high == 110)
  43. #expect(result.low == 110)
  44. }
  45. @Test("should handle target schedule changes") func handleScheduleChanges() async throws {
  46. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 4))!
  47. let (_, result) = try Targets
  48. .bgTargetsLookup(targets: standardTargets, tempTargets: tempTargets, profile: profile, now: now)
  49. #expect(result.high == 90)
  50. #expect(result.low == 90)
  51. }
  52. @Test("should handle temp targets") func handleTempTargets() async throws {
  53. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2, minute: 30))!
  54. let (_, result) = try Targets
  55. .bgTargetsLookup(targets: standardTargets, tempTargets: tempTargets, profile: profile, now: now)
  56. #expect(result.high == 100)
  57. #expect(result.low == 80)
  58. #expect(result.temptargetSet == true)
  59. }
  60. @Test("should handle temp target cancellation") func handleTempTargetCancellation() async throws {
  61. let cancelTempTargets = [
  62. TempTarget(
  63. name: nil,
  64. createdAt: Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2, minute: 30))!,
  65. targetTop: 0,
  66. targetBottom: 0,
  67. duration: 0,
  68. enteredBy: nil,
  69. reason: nil,
  70. isPreset: nil,
  71. enabled: nil,
  72. halfBasalTarget: nil
  73. )
  74. ]
  75. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2, minute: 45))!
  76. let (_, result) = try Targets
  77. .bgTargetsLookup(targets: standardTargets, tempTargets: cancelTempTargets, profile: profile, now: now)
  78. #expect(result.high == 100)
  79. #expect(result.low == 100)
  80. }
  81. @Test("should enforce hard limits on target range") func enforceHardLimits() async throws {
  82. let extremeTargets = BGTargets(
  83. units: .mgdL,
  84. userPreferredUnits: .mgdL,
  85. targets: [
  86. BGTargetEntry(low: 40, high: 250, start: "00:00:00", offset: 0)
  87. ]
  88. )
  89. let (_, result) = try Targets.bgTargetsLookup(targets: extremeTargets, tempTargets: [], profile: profile, now: Date())
  90. #expect(result.maxBg == 80)
  91. #expect(result.minBg == 80)
  92. }
  93. }