DetermineBasalEarlyExitTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("DetermineBasal early exits before core dosing logic") struct DetermineBasalEarlyExitTests {
  5. private func createDefaultInputs(currentTime: Date = Date()) -> (
  6. profile: Profile,
  7. preferences: Preferences,
  8. currentTemp: TempBasal,
  9. iobData: [IobResult],
  10. mealData: ComputedCarbs,
  11. autosensData: Autosens,
  12. reservoirData: Decimal,
  13. glucoseStatus: GlucoseStatus,
  14. trioCustomOrefVariables: TrioCustomOrefVariables,
  15. currentTime: Date
  16. ) {
  17. var profile = Profile()
  18. profile.maxIob = 2.5
  19. profile.dia = 3
  20. profile.currentBasal = 0.9
  21. profile.maxDailyBasal = 1.3
  22. profile.maxBasal = 3.5
  23. profile.maxBg = 120
  24. profile.minBg = 110
  25. profile.sens = 40
  26. profile.carbRatio = 10
  27. profile.thresholdSetting = 80
  28. profile.temptargetSet = false
  29. profile.bolusIncrement = 0.1
  30. profile.useCustomPeakTime = false
  31. profile.curve = .rapidActing
  32. var preferences = Preferences()
  33. preferences.useNewFormula = false
  34. preferences.sigmoid = false
  35. preferences.adjustmentFactor = 0.8
  36. preferences.adjustmentFactorSigmoid = 0.5
  37. preferences.curve = .rapidActing
  38. preferences.useCustomPeakTime = false
  39. let currentTemp = TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: currentTime)
  40. let iobData = [IobResult(
  41. iob: 0,
  42. activity: 0,
  43. basaliob: 0,
  44. bolusiob: 0,
  45. netbasalinsulin: 0,
  46. bolusinsulin: 0,
  47. time: currentTime,
  48. iobWithZeroTemp: IobResult.IobWithZeroTemp(
  49. iob: 0,
  50. activity: 0,
  51. basaliob: 0,
  52. bolusiob: 0,
  53. netbasalinsulin: 0,
  54. bolusinsulin: 0,
  55. time: currentTime
  56. ),
  57. lastBolusTime: nil,
  58. lastTemp: IobResult.LastTemp(
  59. rate: 0,
  60. timestamp: currentTime,
  61. started_at: currentTime,
  62. date: UInt64(currentTime.timeIntervalSince1970 * 1000),
  63. duration: 30
  64. )
  65. )]
  66. let mealData = ComputedCarbs(
  67. carbs: 0,
  68. mealCOB: 0,
  69. currentDeviation: 0,
  70. maxDeviation: 0,
  71. minDeviation: 0,
  72. slopeFromMaxDeviation: 0,
  73. slopeFromMinDeviation: 0,
  74. allDeviations: [0, 0, 0, 0, 0],
  75. lastCarbTime: 0
  76. )
  77. let autosensData = Autosens(ratio: 1.0, newisf: nil)
  78. let glucoseStatus = GlucoseStatus(
  79. delta: 0,
  80. glucose: 115,
  81. noise: 1,
  82. shortAvgDelta: 0,
  83. longAvgDelta: 0.1,
  84. date: currentTime,
  85. lastCalIndex: nil,
  86. device: "test"
  87. )
  88. let trioCustomOrefVariables = TrioCustomOrefVariables(
  89. average_total_data: 0,
  90. weightedAverage: 0,
  91. currentTDD: 0,
  92. past2hoursAverage: 0,
  93. date: currentTime,
  94. overridePercentage: 100,
  95. useOverride: false,
  96. duration: 0,
  97. unlimited: false,
  98. overrideTarget: 0,
  99. smbIsOff: false,
  100. advancedSettings: false,
  101. isfAndCr: false,
  102. isf: false,
  103. cr: false,
  104. smbIsScheduledOff: false,
  105. start: 0,
  106. end: 0,
  107. smbMinutes: 30,
  108. uamMinutes: 30,
  109. shouldProtectDueToHIGH: false
  110. )
  111. return (
  112. profile: profile,
  113. preferences: preferences,
  114. currentTemp: currentTemp,
  115. iobData: iobData,
  116. mealData: mealData,
  117. autosensData: autosensData,
  118. reservoirData: 100,
  119. glucoseStatus: glucoseStatus,
  120. trioCustomOrefVariables: trioCustomOrefVariables,
  121. currentTime: currentTime
  122. )
  123. }
  124. // Test 1 from JS
  125. @Test("should fail if current_basal is missing") func missingCurrentBasal() throws {
  126. var (
  127. profile,
  128. preferences,
  129. currentTemp,
  130. iobData,
  131. mealData,
  132. autosensData,
  133. reservoirData,
  134. glucoseStatus,
  135. trioCustomOrefVariables,
  136. currentTime
  137. ) = createDefaultInputs()
  138. profile.currentBasal = nil
  139. profile.basalprofile = [] // ensure basalFor also returns nil
  140. #expect(throws: DeterminationError.missingCurrentBasal) {
  141. _ = try DeterminationGenerator.determineBasal(
  142. profile: profile,
  143. preferences: preferences,
  144. currentTemp: currentTemp,
  145. iobData: iobData,
  146. mealData: mealData,
  147. autosensData: autosensData,
  148. reservoirData: reservoirData,
  149. glucoseStatus: glucoseStatus,
  150. trioCustomOrefVariables: trioCustomOrefVariables,
  151. currentTime: currentTime
  152. )
  153. }
  154. }
  155. // Test 2 from JS
  156. @Test("should cancel high temp if BG is 38") func cancelHighTempBG38() throws {
  157. let (
  158. profile,
  159. preferences,
  160. _,
  161. iobData,
  162. mealData,
  163. autosensData,
  164. reservoirData,
  165. _,
  166. trioCustomOrefVariables,
  167. currentTime
  168. ) = createDefaultInputs()
  169. let glucoseStatus = GlucoseStatus(
  170. delta: 0,
  171. glucose: 38,
  172. noise: 1,
  173. shortAvgDelta: 0,
  174. longAvgDelta: 0.1,
  175. date: currentTime,
  176. lastCalIndex: nil,
  177. device: "test"
  178. )
  179. let currentTemp = TempBasal(duration: 30, rate: 1.5, temp: .absolute, timestamp: currentTime)
  180. let result = try DeterminationGenerator.determineBasal(
  181. profile: profile,
  182. preferences: preferences,
  183. currentTemp: currentTemp,
  184. iobData: iobData,
  185. mealData: mealData,
  186. autosensData: autosensData,
  187. reservoirData: reservoirData,
  188. glucoseStatus: glucoseStatus,
  189. trioCustomOrefVariables: trioCustomOrefVariables,
  190. currentTime: currentTime
  191. )
  192. #expect(result?.rate == 0)
  193. #expect(result?.duration == 0)
  194. #expect(result?.reason.contains("Canceling high temp basal") == true)
  195. }
  196. // Test 3 from JS
  197. @Test("should shorten long zero temp if BG data is too old") func shortenLongZeroTempTooOldBG() throws {
  198. let (
  199. profile,
  200. preferences,
  201. _,
  202. iobData,
  203. mealData,
  204. autosensData,
  205. reservoirData,
  206. _,
  207. trioCustomOrefVariables,
  208. currentTime
  209. ) = createDefaultInputs()
  210. let glucoseTime = currentTime.addingTimeInterval(-15 * 60)
  211. let glucoseStatus = GlucoseStatus(
  212. delta: 0,
  213. glucose: 115,
  214. noise: 1,
  215. shortAvgDelta: 0,
  216. longAvgDelta: 0.1,
  217. date: glucoseTime,
  218. lastCalIndex: nil,
  219. device: "test"
  220. )
  221. let currentTemp = TempBasal(duration: 60, rate: 0, temp: .absolute, timestamp: currentTime)
  222. let result = try DeterminationGenerator.determineBasal(
  223. profile: profile,
  224. preferences: preferences,
  225. currentTemp: currentTemp,
  226. iobData: iobData,
  227. mealData: mealData,
  228. autosensData: autosensData,
  229. reservoirData: reservoirData,
  230. glucoseStatus: glucoseStatus,
  231. trioCustomOrefVariables: trioCustomOrefVariables,
  232. currentTime: currentTime
  233. )
  234. #expect(result?.rate == 0)
  235. #expect(result?.duration == 30)
  236. #expect(result?.reason.contains("Shortening") == true)
  237. }
  238. // Test 4 from JS
  239. @Test("should do nothing if BG is too old and temp is not high") func doNothingOldBGNotHighTemp() throws {
  240. let (
  241. profile,
  242. preferences,
  243. _,
  244. iobData,
  245. mealData,
  246. autosensData,
  247. reservoirData,
  248. _,
  249. trioCustomOrefVariables,
  250. currentTime
  251. ) = createDefaultInputs()
  252. let glucoseTime = currentTime.addingTimeInterval(-15 * 60)
  253. let glucoseStatus = GlucoseStatus(
  254. delta: 0,
  255. glucose: 115,
  256. noise: 1,
  257. shortAvgDelta: 0,
  258. longAvgDelta: 0.1,
  259. date: glucoseTime,
  260. lastCalIndex: nil,
  261. device: "test"
  262. )
  263. let currentTemp = TempBasal(duration: 30, rate: 0.5, temp: .absolute, timestamp: currentTime)
  264. let result = try DeterminationGenerator.determineBasal(
  265. profile: profile,
  266. preferences: preferences,
  267. currentTemp: currentTemp,
  268. iobData: iobData,
  269. mealData: mealData,
  270. autosensData: autosensData,
  271. reservoirData: reservoirData,
  272. glucoseStatus: glucoseStatus,
  273. trioCustomOrefVariables: trioCustomOrefVariables,
  274. currentTime: currentTime
  275. )
  276. #expect(result?.rate == 0.5)
  277. #expect(result?.duration == 30)
  278. #expect(result?.reason.contains("doing nothing") == true)
  279. }
  280. // Test 5 from JS
  281. @Test("should error if target_bg cannot be determined") func errorIfTargetBGMissing() throws {
  282. var (
  283. profile,
  284. preferences,
  285. currentTemp,
  286. iobData,
  287. mealData,
  288. autosensData,
  289. reservoirData,
  290. glucoseStatus,
  291. trioCustomOrefVariables,
  292. currentTime
  293. ) = createDefaultInputs()
  294. profile.minBg = nil
  295. #expect(throws: DeterminationError.invalidProfileTarget) {
  296. _ = try DeterminationGenerator.determineBasal(
  297. profile: profile,
  298. preferences: preferences,
  299. currentTemp: currentTemp,
  300. iobData: iobData,
  301. mealData: mealData,
  302. autosensData: autosensData,
  303. reservoirData: reservoirData,
  304. glucoseStatus: glucoseStatus,
  305. trioCustomOrefVariables: trioCustomOrefVariables,
  306. currentTime: currentTime
  307. )
  308. }
  309. }
  310. // Test 6 from JS
  311. @Test("should cancel temp if currenttemp and lastTemp from pumphistory do not match") func cancelTempMismatch() throws {
  312. let (
  313. profile,
  314. preferences,
  315. _,
  316. iobData,
  317. mealData,
  318. autosensData,
  319. reservoirData,
  320. glucoseStatus,
  321. trioCustomOrefVariables,
  322. currentTime
  323. ) = createDefaultInputs()
  324. let currentTemp = TempBasal(duration: 30, rate: 1.5, temp: .absolute, timestamp: currentTime)
  325. let lastTempTime = currentTime.addingTimeInterval(-15 * 60)
  326. let lastTemp = IobResult.LastTemp(
  327. rate: 1.0,
  328. timestamp: lastTempTime,
  329. started_at: lastTempTime,
  330. date: UInt64(lastTempTime.timeIntervalSince1970 * 1000),
  331. duration: 30
  332. )
  333. var mutableIobData = iobData
  334. mutableIobData[0].lastTemp = lastTemp
  335. let result = try DeterminationGenerator.determineBasal(
  336. profile: profile,
  337. preferences: preferences,
  338. currentTemp: currentTemp,
  339. iobData: mutableIobData,
  340. mealData: mealData,
  341. autosensData: autosensData,
  342. reservoirData: reservoirData,
  343. glucoseStatus: glucoseStatus,
  344. trioCustomOrefVariables: trioCustomOrefVariables,
  345. currentTime: currentTime
  346. )
  347. #expect(result?.rate == 0)
  348. #expect(result?.duration == 0)
  349. // Note: In swift we use a different reason then JS
  350. #expect(
  351. result?
  352. .reason ==
  353. "Safety check: currentTemp does not match lastTemp in IOB or lastTemp ended long ago; canceling temp basal."
  354. )
  355. }
  356. // Test 7 from JS
  357. @Test("should cancel temp if lastTemp from pumphistory ended long ago") func cancelTempOldLastTemp() throws {
  358. let (
  359. profile,
  360. preferences,
  361. _,
  362. iobData,
  363. mealData,
  364. autosensData,
  365. reservoirData,
  366. glucoseStatus,
  367. trioCustomOrefVariables,
  368. currentTime
  369. ) = createDefaultInputs()
  370. let currentTemp = TempBasal(duration: 30, rate: 1.5, temp: .absolute, timestamp: currentTime)
  371. let lastTempTime = currentTime.addingTimeInterval(-40 * 60)
  372. let lastTemp = IobResult.LastTemp(
  373. rate: 1.5,
  374. timestamp: lastTempTime,
  375. started_at: lastTempTime,
  376. date: UInt64(lastTempTime.timeIntervalSince1970 * 1000),
  377. duration: 30
  378. )
  379. var mutableIobData = iobData
  380. mutableIobData[0].lastTemp = lastTemp
  381. let result = try DeterminationGenerator.determineBasal(
  382. profile: profile,
  383. preferences: preferences,
  384. currentTemp: currentTemp,
  385. iobData: mutableIobData,
  386. mealData: mealData,
  387. autosensData: autosensData,
  388. reservoirData: reservoirData,
  389. glucoseStatus: glucoseStatus,
  390. trioCustomOrefVariables: trioCustomOrefVariables,
  391. currentTime: currentTime
  392. )
  393. #expect(result?.rate == 0)
  394. #expect(result?.duration == 0)
  395. // Note: In swift we use a different reason then JS
  396. #expect(
  397. result?
  398. .reason ==
  399. "Safety check: currentTemp does not match lastTemp in IOB or lastTemp ended long ago; canceling temp basal."
  400. )
  401. }
  402. // Test 8 from JS
  403. @Test("should throw error if eventualBG cannot be calculated") func eventualBGNaN() throws {
  404. var (
  405. profile,
  406. preferences,
  407. currentTemp,
  408. iobData,
  409. mealData,
  410. autosensData,
  411. reservoirData,
  412. glucoseStatus,
  413. trioCustomOrefVariables,
  414. currentTime
  415. ) = createDefaultInputs()
  416. profile.sens = .nan
  417. #expect(throws: DeterminationError.eventualGlucoseCalculationError(sensitivity: .nan, deviation: .nan)) {
  418. _ = try DeterminationGenerator.determineBasal(
  419. profile: profile,
  420. preferences: preferences,
  421. currentTemp: currentTemp,
  422. iobData: iobData,
  423. mealData: mealData,
  424. autosensData: autosensData,
  425. reservoirData: reservoirData,
  426. glucoseStatus: glucoseStatus,
  427. trioCustomOrefVariables: trioCustomOrefVariables,
  428. currentTime: currentTime
  429. )
  430. }
  431. }
  432. // Test 9 from JS
  433. @Test("should low-temp if BG is below threshold") func lowGlucoseSuspend() throws {
  434. let (
  435. profile,
  436. preferences,
  437. currentTemp,
  438. iobData,
  439. mealData,
  440. autosensData,
  441. reservoirData,
  442. _,
  443. trioCustomOrefVariables,
  444. currentTime
  445. ) = createDefaultInputs()
  446. let glucoseStatus = GlucoseStatus(
  447. delta: 0,
  448. glucose: 70,
  449. noise: 1,
  450. shortAvgDelta: 0,
  451. longAvgDelta: 0.1,
  452. date: currentTime,
  453. lastCalIndex: nil,
  454. device: "test"
  455. )
  456. let result = try DeterminationGenerator.determineBasal(
  457. profile: profile,
  458. preferences: preferences,
  459. currentTemp: currentTemp,
  460. iobData: iobData,
  461. mealData: mealData,
  462. autosensData: autosensData,
  463. reservoirData: reservoirData,
  464. glucoseStatus: glucoseStatus,
  465. trioCustomOrefVariables: trioCustomOrefVariables,
  466. currentTime: currentTime
  467. )
  468. #expect(result?.rate == 0)
  469. #expect((result?.duration ?? 0) >= 30)
  470. #expect(result?.reason.contains("minGuardBG") == true)
  471. }
  472. // Test 10 from JS
  473. @Test("should cancel temp before the hour if not doing SMB") func skipNeutralTemp() throws {
  474. // Create a date that is 56 minutes past the hour
  475. var components = Calendar.current.dateComponents(in: .current, from: Date())
  476. components.minute = 56
  477. let currentTime = Calendar.current.date(from: components)!
  478. var (
  479. profile,
  480. preferences,
  481. currentTemp,
  482. iobData,
  483. mealData,
  484. autosensData,
  485. reservoirData,
  486. glucoseStatus,
  487. trioCustomOrefVariables,
  488. _
  489. ) = createDefaultInputs(currentTime: currentTime)
  490. profile.skipNeutralTemps = true
  491. let result = try DeterminationGenerator.determineBasal(
  492. profile: profile,
  493. preferences: preferences,
  494. currentTemp: currentTemp,
  495. iobData: iobData,
  496. mealData: mealData,
  497. autosensData: autosensData,
  498. reservoirData: reservoirData,
  499. glucoseStatus: glucoseStatus,
  500. trioCustomOrefVariables: trioCustomOrefVariables,
  501. currentTime: currentTime
  502. )
  503. #expect(result?.rate == 0)
  504. #expect(result?.duration == 0)
  505. #expect(result?.reason.contains("Canceling temp") == true)
  506. }
  507. }