AutosensTests.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Autosens Temp Target Deviation Tests") struct TempTargetDeviationTests {
  5. // Helper function to create a basic profile with highTemptargetRaisesSensitivity enabled
  6. func createProfileWithSensitivity(enabled: Bool = true) -> Profile {
  7. var profile = Profile()
  8. profile.highTemptargetRaisesSensitivity = enabled
  9. return profile
  10. }
  11. // Helper function to create temp targets at specific times
  12. func createTempTarget(
  13. createdAt: Date,
  14. targetTop: Decimal,
  15. targetBottom: Decimal,
  16. duration: Decimal
  17. ) -> TempTarget {
  18. TempTarget(
  19. name: nil,
  20. createdAt: createdAt,
  21. targetTop: targetTop,
  22. targetBottom: targetBottom,
  23. duration: duration,
  24. enteredBy: nil,
  25. reason: nil,
  26. isPreset: nil,
  27. enabled: nil,
  28. halfBasalTarget: nil
  29. )
  30. }
  31. @Test("should return nil when highTemptargetRaisesSensitivity is false") func returnNilWhenSensitivityDisabled() async throws {
  32. let profile = createProfileWithSensitivity(enabled: false)
  33. let now = Date()
  34. let tempTargets = [
  35. createTempTarget(
  36. createdAt: now - 30.minutesToSeconds,
  37. targetTop: 140,
  38. targetBottom: 120,
  39. duration: 60
  40. )
  41. ]
  42. let result = AutosensGenerator.tempTargetDeviation(
  43. tempTargets: tempTargets,
  44. profile: profile,
  45. time: now
  46. )
  47. #expect(result == nil)
  48. }
  49. @Test("should return nil when no temp targets are active") func returnNilWhenNoActiveTempTargets() async throws {
  50. let profile = createProfileWithSensitivity()
  51. let now = Date()
  52. let tempTargets = [
  53. createTempTarget(
  54. createdAt: now - 120.minutesToSeconds, // 2 hours ago
  55. targetTop: 140,
  56. targetBottom: 120,
  57. duration: 60 // 1 hour duration, so expired
  58. )
  59. ]
  60. let result = AutosensGenerator.tempTargetDeviation(
  61. tempTargets: tempTargets,
  62. profile: profile,
  63. time: now
  64. )
  65. #expect(result == nil)
  66. }
  67. @Test("should return nil when temp target is at or below 100") func returnNilWhenTempTargetAtOrBelow100() async throws {
  68. let profile = createProfileWithSensitivity()
  69. let now = Date()
  70. let tempTargets = [
  71. createTempTarget(
  72. createdAt: now - 30.minutesToSeconds,
  73. targetTop: 100,
  74. targetBottom: 100,
  75. duration: 60
  76. )
  77. ]
  78. let result = AutosensGenerator.tempTargetDeviation(
  79. tempTargets: tempTargets,
  80. profile: profile,
  81. time: now
  82. )
  83. #expect(result == nil)
  84. }
  85. @Test("should calculate correct deviation for temp target above 100") func calculateCorrectDeviationAbove100() async throws {
  86. let profile = createProfileWithSensitivity()
  87. let now = Date()
  88. let tempTargets = [
  89. createTempTarget(
  90. createdAt: now - 30.minutesToSeconds,
  91. targetTop: 140,
  92. targetBottom: 120,
  93. duration: 60
  94. )
  95. ]
  96. let result = AutosensGenerator.tempTargetDeviation(
  97. tempTargets: tempTargets,
  98. profile: profile,
  99. time: now
  100. )
  101. // Average target = (140 + 120) / 2 = 130
  102. // Deviation = -(130 - 100) / 20 = -30 / 20 = -1.5
  103. #expect(result == -1.5)
  104. }
  105. }
  106. @Suite("Determine Last Site Change Tests") struct DetermineLastSiteChangeTests {
  107. @Test(
  108. "should return rewind timestamp when rewind event exists and rewindResetsAutosens is true"
  109. ) func returnRewindTimestampWhenRewindExists() async throws {
  110. let now = Date()
  111. let rewindTime = now - 6.hoursToSeconds
  112. let pumpHistory = [
  113. PumpHistoryEvent(
  114. id: "1",
  115. type: .tempBasal,
  116. timestamp: now - 1.hoursToSeconds,
  117. amount: nil,
  118. duration: nil,
  119. durationMin: nil,
  120. rate: 1.5,
  121. temp: .absolute,
  122. carbInput: nil
  123. ),
  124. PumpHistoryEvent(
  125. id: "2",
  126. type: .rewind,
  127. timestamp: rewindTime,
  128. amount: nil,
  129. duration: nil,
  130. durationMin: nil,
  131. rate: nil,
  132. temp: nil,
  133. carbInput: nil
  134. ),
  135. PumpHistoryEvent(
  136. id: "3",
  137. type: .tempBasal,
  138. timestamp: now - 12.hoursToSeconds,
  139. amount: nil,
  140. duration: nil,
  141. durationMin: nil,
  142. rate: 2.0,
  143. temp: .absolute,
  144. carbInput: nil
  145. )
  146. ]
  147. var profile = Profile()
  148. profile.rewindResetsAutosens = true
  149. let result = AutosensGenerator.determineLastSiteChange(
  150. pumpHistory: pumpHistory,
  151. profile: profile,
  152. clock: now
  153. )
  154. #expect(result == rewindTime)
  155. }
  156. }