DetermineBasalEnableSmbTests.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("DosingEngine: shouldEnableSmb Tests") struct DetermineBasalEnableSmbTests {
  5. /// Helper to create a default set of inputs.
  6. /// Each test can then modify the specific properties relevant to its case.
  7. private func createDefaultInputs() -> (
  8. profile: Profile,
  9. meal: ComputedCarbs,
  10. currentGlucose: Decimal,
  11. adjustedTargetGlucose: Decimal,
  12. minGuardGlucose: Decimal,
  13. threshold: Decimal,
  14. glucoseStatus: GlucoseStatus,
  15. trioCustomOrefVariables: TrioCustomOrefVariables,
  16. clock: Date
  17. ) {
  18. var profile = Profile()
  19. // Ensure default is false so we can test enabling conditions.
  20. profile.enableSMBAlways = false
  21. profile.temptargetSet = false
  22. let meal = ComputedCarbs(
  23. carbs: 0,
  24. mealCOB: 0,
  25. currentDeviation: 0,
  26. maxDeviation: 0,
  27. minDeviation: 0,
  28. slopeFromMaxDeviation: 0,
  29. slopeFromMinDeviation: 0,
  30. allDeviations: [],
  31. lastCarbTime: Date().timeIntervalSince1970
  32. )
  33. let glucoseStatus = GlucoseStatus(
  34. delta: 0,
  35. glucose: 120,
  36. noise: 0,
  37. shortAvgDelta: 0,
  38. longAvgDelta: 0,
  39. date: Date(),
  40. lastCalIndex: nil,
  41. device: "test"
  42. )
  43. let trioCustomOrefVariables = TrioCustomOrefVariables(
  44. average_total_data: 0,
  45. weightedAverage: 0,
  46. currentTDD: 0,
  47. past2hoursAverage: 0,
  48. date: Date(),
  49. overridePercentage: 100,
  50. useOverride: false,
  51. duration: 0,
  52. unlimited: false,
  53. overrideTarget: 0,
  54. smbIsOff: false,
  55. advancedSettings: false,
  56. isfAndCr: false,
  57. isf: false,
  58. cr: false,
  59. smbIsScheduledOff: false,
  60. start: 0,
  61. end: 0,
  62. smbMinutes: 0,
  63. uamMinutes: 0
  64. )
  65. return (
  66. profile: profile,
  67. meal: meal,
  68. currentGlucose: 120,
  69. adjustedTargetGlucose: 100,
  70. minGuardGlucose: 110,
  71. threshold: 70,
  72. glucoseStatus: glucoseStatus,
  73. trioCustomOrefVariables: trioCustomOrefVariables,
  74. clock: Date()
  75. )
  76. }
  77. // MARK: - Disabling Conditions
  78. @Test("Should return false by default with no enabling preferences") func defaultIsFalse() throws {
  79. let inputs = createDefaultInputs()
  80. let decision = try DosingEngine.makeSMBDosingDecision(
  81. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  82. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  83. minGuardGlucose: inputs.minGuardGlucose,
  84. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  85. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  86. )
  87. #expect(decision.isEnabled == false)
  88. }
  89. @Test("Should disable SMB when smbIsOff is true") func disableWhenSmbIsOff() throws {
  90. var inputs = createDefaultInputs()
  91. inputs.trioCustomOrefVariables.smbIsOff = true
  92. inputs.profile.enableSMBAlways = true // Ensure smbIsOff takes precedence
  93. let decision = try DosingEngine.makeSMBDosingDecision(
  94. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  95. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  96. minGuardGlucose: inputs.minGuardGlucose,
  97. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  98. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  99. )
  100. #expect(decision.isEnabled == false)
  101. }
  102. @Test("Should disable SMB with high temp target when not allowed") func disableWithHighTempTarget() throws {
  103. var inputs = createDefaultInputs()
  104. inputs.profile.allowSMBWithHighTemptarget = false
  105. inputs.profile.temptargetSet = true
  106. inputs.adjustedTargetGlucose = 120
  107. let decision = try DosingEngine.makeSMBDosingDecision(
  108. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  109. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  110. minGuardGlucose: inputs.minGuardGlucose,
  111. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  112. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  113. )
  114. #expect(decision.isEnabled == false)
  115. }
  116. @Test("Should disable SMB when minGuardGlucose is below threshold") func disableWhenMinGuardBelowThreshold() throws {
  117. var inputs = createDefaultInputs()
  118. inputs.profile.enableSMBAlways = true // Enable SMB initially to test the safety override
  119. inputs.minGuardGlucose = 65
  120. inputs.threshold = 70
  121. let decision = try DosingEngine.makeSMBDosingDecision(
  122. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  123. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  124. minGuardGlucose: inputs.minGuardGlucose,
  125. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  126. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  127. )
  128. #expect(decision.isEnabled == false)
  129. #expect(decision.minGuardGlucose == 65)
  130. }
  131. @Test("Should disable SMB when maxDelta is too high") func disableWhenMaxDeltaTooHigh() throws {
  132. var inputs = createDefaultInputs()
  133. inputs.profile.enableSMBAlways = true // Enable SMB initially
  134. inputs.profile.maxDeltaBgThreshold = 0.2
  135. inputs.currentGlucose = 100
  136. // Set maxDelta to be > 20% of currentGlucose
  137. inputs.glucoseStatus = GlucoseStatus(
  138. delta: 21,
  139. glucose: 100,
  140. noise: 0,
  141. shortAvgDelta: 5,
  142. longAvgDelta: 5,
  143. date: Date(),
  144. lastCalIndex: nil,
  145. device: "test"
  146. )
  147. let decision = try DosingEngine.makeSMBDosingDecision(
  148. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  149. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  150. minGuardGlucose: inputs.minGuardGlucose,
  151. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  152. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  153. )
  154. #expect(decision.isEnabled == false)
  155. #expect(decision.reason != nil)
  156. }
  157. // MARK: - Enabling Conditions
  158. @Test("Should enable SMB when enableSMBAlways is true") func enableWhenAlwaysOn() throws {
  159. var inputs = createDefaultInputs()
  160. inputs.profile.enableSMBAlways = true
  161. let decision = try DosingEngine.makeSMBDosingDecision(
  162. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  163. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  164. minGuardGlucose: inputs.minGuardGlucose,
  165. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  166. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  167. )
  168. #expect(decision.isEnabled == true)
  169. }
  170. @Test("Should enable SMB with COB") func enableWithCob() throws {
  171. var inputs = createDefaultInputs()
  172. inputs.profile.enableSMBWithCOB = true
  173. inputs.meal = ComputedCarbs(
  174. carbs: 20,
  175. mealCOB: 10,
  176. currentDeviation: 0,
  177. maxDeviation: 0,
  178. minDeviation: 0,
  179. slopeFromMaxDeviation: 0,
  180. slopeFromMinDeviation: 0,
  181. allDeviations: [],
  182. lastCarbTime: Date().timeIntervalSince1970
  183. )
  184. let decision = try DosingEngine.makeSMBDosingDecision(
  185. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  186. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  187. minGuardGlucose: inputs.minGuardGlucose,
  188. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  189. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  190. )
  191. #expect(decision.isEnabled == true)
  192. }
  193. @Test("Should enable SMB after carbs") func enableAfterCarbs() throws {
  194. var inputs = createDefaultInputs()
  195. inputs.profile.enableSMBAfterCarbs = true
  196. inputs.meal = ComputedCarbs(
  197. carbs: 20,
  198. mealCOB: 0,
  199. currentDeviation: 0,
  200. maxDeviation: 0,
  201. minDeviation: 0,
  202. slopeFromMaxDeviation: 0,
  203. slopeFromMinDeviation: 0,
  204. allDeviations: [],
  205. lastCarbTime: Date().timeIntervalSince1970
  206. )
  207. let decision = try DosingEngine.makeSMBDosingDecision(
  208. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  209. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  210. minGuardGlucose: inputs.minGuardGlucose,
  211. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  212. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  213. )
  214. #expect(decision.isEnabled == true)
  215. }
  216. @Test("Should enable SMB with low temp target") func enableWithLowTempTarget() throws {
  217. var inputs = createDefaultInputs()
  218. inputs.profile.enableSMBWithTemptarget = true
  219. inputs.profile.temptargetSet = true
  220. inputs.adjustedTargetGlucose = 90
  221. let decision = try DosingEngine.makeSMBDosingDecision(
  222. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  223. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  224. minGuardGlucose: inputs.minGuardGlucose,
  225. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  226. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  227. )
  228. #expect(decision.isEnabled == true)
  229. }
  230. @Test("Should enable SMB for high BG") func enableWithHighBg() throws {
  231. var inputs = createDefaultInputs()
  232. inputs.profile.enableSMBHighBg = true
  233. inputs.profile.enableSMBHighBgTarget = 140
  234. inputs.currentGlucose = 145
  235. let decision = try DosingEngine.makeSMBDosingDecision(
  236. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  237. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  238. minGuardGlucose: inputs.minGuardGlucose,
  239. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  240. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  241. )
  242. #expect(decision.isEnabled == true)
  243. }
  244. // MARK: - Scheduled Off Tests
  245. @Test("Scheduled Off (Normal): should disable SMB inside the window") func scheduledOffNormal_Inside() throws {
  246. var inputs = createDefaultInputs()
  247. inputs.profile.enableSMBAlways = true // Ensure schedule is the only reason for failure
  248. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  249. inputs.trioCustomOrefVariables.start = 9 // 9 AM
  250. inputs.trioCustomOrefVariables.end = 17 // 5 PM
  251. inputs.clock = Calendar.current.date(bySettingHour: 14, minute: 0, second: 0, of: Date())!
  252. let decision = try DosingEngine.makeSMBDosingDecision(
  253. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  254. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  255. minGuardGlucose: inputs.minGuardGlucose,
  256. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  257. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  258. )
  259. #expect(decision.isEnabled == false)
  260. }
  261. @Test("Scheduled Off (Normal): should NOT disable SMB outside the window") func scheduledOffNormal_Outside() throws {
  262. var inputs = createDefaultInputs()
  263. inputs.profile.enableSMBAlways = true
  264. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  265. inputs.trioCustomOrefVariables.start = 9 // 9 AM
  266. inputs.trioCustomOrefVariables.end = 17 // 5 PM
  267. inputs.clock = Calendar.current.date(bySettingHour: 18, minute: 0, second: 0, of: Date())!
  268. let decision = try DosingEngine.makeSMBDosingDecision(
  269. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  270. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  271. minGuardGlucose: inputs.minGuardGlucose,
  272. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  273. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  274. )
  275. #expect(decision.isEnabled == true)
  276. }
  277. @Test(
  278. "Scheduled Off (Wrapping): should disable SMB inside the window (after midnight)"
  279. ) func scheduledOffWrapping_InsideAfterMidnight() throws {
  280. var inputs = createDefaultInputs()
  281. inputs.profile.enableSMBAlways = true
  282. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  283. inputs.trioCustomOrefVariables.start = 22 // 10 PM
  284. inputs.trioCustomOrefVariables.end = 6 // 6 AM
  285. inputs.clock = Calendar.current.date(bySettingHour: 2, minute: 0, second: 0, of: Date())!
  286. let decision = try DosingEngine.makeSMBDosingDecision(
  287. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  288. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  289. minGuardGlucose: inputs.minGuardGlucose,
  290. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  291. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  292. )
  293. #expect(decision.isEnabled == false)
  294. }
  295. @Test(
  296. "Scheduled Off (Wrapping): should disable SMB inside the window (before midnight)"
  297. ) func scheduledOffWrapping_InsideBeforeMidnight() throws {
  298. var inputs = createDefaultInputs()
  299. inputs.profile.enableSMBAlways = true
  300. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  301. inputs.trioCustomOrefVariables.start = 22 // 10 PM
  302. inputs.trioCustomOrefVariables.end = 6 // 6 AM
  303. inputs.clock = Calendar.current.date(bySettingHour: 23, minute: 0, second: 0, of: Date())!
  304. let decision = try DosingEngine.makeSMBDosingDecision(
  305. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  306. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  307. minGuardGlucose: inputs.minGuardGlucose,
  308. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  309. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  310. )
  311. #expect(decision.isEnabled == false)
  312. }
  313. @Test("Scheduled Off (Wrapping): should NOT disable SMB outside the window") func scheduledOffWrapping_Outside() throws {
  314. var inputs = createDefaultInputs()
  315. inputs.profile.enableSMBAlways = true
  316. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  317. inputs.trioCustomOrefVariables.start = 22 // 10 PM
  318. inputs.trioCustomOrefVariables.end = 6 // 6 AM
  319. inputs.clock = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
  320. let decision = try DosingEngine.makeSMBDosingDecision(
  321. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  322. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  323. minGuardGlucose: inputs.minGuardGlucose,
  324. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  325. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  326. )
  327. #expect(decision.isEnabled == true)
  328. }
  329. @Test("Scheduled Off (All Day): should disable SMB") func scheduledOffAllDay() throws {
  330. var inputs = createDefaultInputs()
  331. inputs.profile.enableSMBAlways = true
  332. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  333. inputs.trioCustomOrefVariables.start = 0
  334. inputs.trioCustomOrefVariables.end = 0
  335. inputs.clock = Calendar.current.date(bySettingHour: 15, minute: 0, second: 0, of: Date())!
  336. let decision = try DosingEngine.makeSMBDosingDecision(
  337. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  338. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  339. minGuardGlucose: inputs.minGuardGlucose,
  340. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  341. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  342. )
  343. #expect(decision.isEnabled == false)
  344. }
  345. @Test("Scheduled Off (Single Hour): should disable SMB inside the window") func scheduledOffSingleHour_Inside() throws {
  346. var inputs = createDefaultInputs()
  347. inputs.profile.enableSMBAlways = true
  348. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  349. inputs.trioCustomOrefVariables.start = 11 // 11 AM
  350. inputs.trioCustomOrefVariables.end = 11 // 11 AM
  351. inputs.clock = Calendar.current.date(bySettingHour: 11, minute: 30, second: 0, of: Date())!
  352. let decision = try DosingEngine.makeSMBDosingDecision(
  353. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  354. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  355. minGuardGlucose: inputs.minGuardGlucose,
  356. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  357. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  358. )
  359. #expect(decision.isEnabled == false)
  360. }
  361. @Test("Scheduled Off (Single Hour): should NOT disable SMB outside the window") func scheduledOffSingleHour_Outside() throws {
  362. var inputs = createDefaultInputs()
  363. inputs.profile.enableSMBAlways = true
  364. inputs.trioCustomOrefVariables.smbIsScheduledOff = true
  365. inputs.trioCustomOrefVariables.start = 11 // 11 AM
  366. inputs.trioCustomOrefVariables.end = 11 // 11 AM
  367. inputs.clock = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
  368. let decision = try DosingEngine.makeSMBDosingDecision(
  369. profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
  370. adjustedTargetGlucose: inputs.adjustedTargetGlucose,
  371. minGuardGlucose: inputs.minGuardGlucose,
  372. threshold: inputs.threshold, glucoseStatus: inputs.glucoseStatus,
  373. trioCustomOrefVariables: inputs.trioCustomOrefVariables, clock: inputs.clock
  374. )
  375. #expect(decision.isEnabled == true)
  376. }
  377. }