DetermineBasalIobGreaterThanMaxTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. /// These tests should be an exact copy of the JS tests here:
  5. /// - https://github.com/kingst/trio-oref/blob/dev-fixes-for-swift-comparison/tests/determine-basal-iob-greater-than-max.test.js
  6. @Suite("DosingEngine.iobGreaterThanMax") struct DetermineBasalIobGreaterThanMaxTests {
  7. private func defaultProfile() -> Profile {
  8. var profile = Profile()
  9. profile.maxIob = 1.5
  10. profile.currentBasal = 1.0
  11. profile.maxBasal = 1.5
  12. profile.maxDailyBasal = 3.5
  13. profile.outUnits = .mgdL
  14. return profile
  15. }
  16. private func callIobGreaterThanMax(
  17. iob: Decimal = 1.5,
  18. maxIob: Decimal? = nil,
  19. currentTemp: TempBasal = TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: Date()),
  20. basal: Decimal? = nil,
  21. profile: Profile? = nil,
  22. determination: Determination? = nil
  23. ) throws -> (shouldSetTempBasal: Bool, determination: Determination) {
  24. let testProfile = profile ?? defaultProfile()
  25. let testDetermination = determination ?? Determination(
  26. id: nil,
  27. reason: "",
  28. units: nil,
  29. insulinReq: nil,
  30. eventualBG: nil,
  31. sensitivityRatio: nil,
  32. rate: nil,
  33. duration: nil,
  34. iob: nil,
  35. cob: nil,
  36. predictions: nil,
  37. deliverAt: nil,
  38. carbsReq: nil,
  39. temp: nil,
  40. bg: nil,
  41. reservoir: nil,
  42. isf: nil,
  43. timestamp: nil,
  44. tdd: nil,
  45. current_target: nil,
  46. minDelta: nil,
  47. expectedDelta: nil,
  48. minGuardBG: nil,
  49. minPredBG: nil,
  50. threshold: nil,
  51. carbRatio: nil,
  52. received: nil
  53. )
  54. return try DosingEngine.iobGreaterThanMax(
  55. iob: iob,
  56. maxIob: maxIob ?? testProfile.maxIob,
  57. currentTemp: currentTemp,
  58. basal: basal ?? testProfile.currentBasal!,
  59. profile: testProfile,
  60. determination: testDetermination
  61. )
  62. }
  63. @Test("Guard: iob not greater than max") func testIobNotGreaterThanMax() throws {
  64. let (shouldSet, determination) = try callIobGreaterThanMax(iob: 1.5, maxIob: 1.5)
  65. #expect(shouldSet == false)
  66. #expect(determination.reason == "")
  67. }
  68. @Test("Continue current temp") func testContinueCurrentTemp() throws {
  69. let profile = defaultProfile()
  70. let currentTemp = TempBasal(duration: 20, rate: profile.currentBasal!, temp: .absolute, timestamp: Date())
  71. let (shouldSet, determination) = try callIobGreaterThanMax(
  72. iob: 1.6,
  73. currentTemp: currentTemp,
  74. basal: profile.currentBasal!,
  75. profile: profile
  76. )
  77. #expect(shouldSet == true)
  78. #expect(determination.rate == nil) // No change
  79. #expect(determination.reason.contains("temp \(currentTemp.rate) ~ req \(profile.currentBasal!)U/hr."))
  80. }
  81. @Test("Set new temp when duration is short") func testSetNewTempDurationShort() throws {
  82. let profile = defaultProfile()
  83. let currentTemp = TempBasal(duration: 10, rate: 1.0, temp: .absolute, timestamp: Date())
  84. let basal: Decimal = 1.2
  85. let (shouldSet, determination) = try callIobGreaterThanMax(
  86. iob: 1.6,
  87. currentTemp: currentTemp,
  88. basal: basal,
  89. profile: profile
  90. )
  91. #expect(shouldSet == true)
  92. #expect(determination.rate == basal)
  93. #expect(determination.duration == 30)
  94. #expect(determination.reason.contains("setting current basal of \(basal) as temp."))
  95. }
  96. @Test("Set new temp when rates differ") func testSetNewTempRatesDiffer() throws {
  97. let profile = defaultProfile()
  98. let currentTemp = TempBasal(duration: 20, rate: 1.0, temp: .absolute, timestamp: Date())
  99. let basal: Decimal = 1.2
  100. let (shouldSet, determination) = try callIobGreaterThanMax(
  101. iob: 1.6,
  102. currentTemp: currentTemp,
  103. basal: basal,
  104. profile: profile
  105. )
  106. #expect(shouldSet == true)
  107. #expect(determination.rate == basal)
  108. #expect(determination.duration == 30)
  109. #expect(determination.reason.contains("setting current basal of \(basal) as temp."))
  110. }
  111. }