MealTotalTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("MealTotal Tests") struct MealTotalTests {
  5. // Helper methods for testing
  6. func createBasicProfile() -> Profile {
  7. var profile = Profile()
  8. profile.dia = 4
  9. profile.maxMealAbsorptionTime = 6
  10. profile.maxCOB = 120
  11. // profile.carbsAbsorptionRate = 30
  12. profile.min5mCarbImpact = 3
  13. profile.carbRatio = 10
  14. profile.currentBasal = 1.0
  15. // Note: In Swift we need to set sensitivities differently than in JS
  16. profile
  17. .isfProfile = ComputedInsulinSensitivities(
  18. units: .mgdL,
  19. userPreferredUnits: .mgdL,
  20. sensitivities: [ComputedInsulinSensitivityEntry(sensitivity: 40, offset: 0, start: "00:00:00")]
  21. )
  22. return profile
  23. }
  24. func createBasicBasalProfile() -> [BasalProfileEntry] {
  25. [BasalProfileEntry(start: "00:00:00", minutes: 0, rate: 1.0)]
  26. }
  27. func createGlucoseData(baseTime: Date, pattern: [Int]) -> [BloodGlucose] {
  28. var result: [BloodGlucose] = []
  29. for (i, bg) in pattern.enumerated() {
  30. let timestamp = baseTime.addingTimeInterval(TimeInterval(i * 5 * 60))
  31. result.append(BloodGlucose(
  32. sgv: bg,
  33. date: Decimal(timestamp.timeIntervalSince1970 * 1000), // JS uses ms
  34. dateString: timestamp
  35. ))
  36. }
  37. return result.reversed()
  38. }
  39. @Test("should calculate carb absorption correctly") func calculateCarbAbsorption() async throws {
  40. let baseTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  41. let mealTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  42. let testTime = Date.from(isoString: "2016-06-19T13:00:00-04:00") // 1 hour after meal
  43. // Create glucose data showing rise after carbs
  44. var bgValues = Array(repeating: 100, count: 13)
  45. for i in 3 ..< 8 {
  46. bgValues[i] = 100 + ((i - 2) * 10) // 100, 110, 120, 130, 140
  47. }
  48. for i in 8 ..< 13 {
  49. bgValues[i] = 150 // plateau
  50. }
  51. let glucoseData = createGlucoseData(baseTime: baseTime, pattern: bgValues)
  52. // Create insulin data - bolus at same time as carbs
  53. let pumpHistory = [
  54. PumpHistoryEvent(
  55. id: UUID().uuidString,
  56. type: .bolus,
  57. timestamp: mealTime,
  58. amount: 3.0
  59. )
  60. ]
  61. // Carb treatment
  62. let treatments = [
  63. MealInput(
  64. timestamp: mealTime,
  65. carbs: 30,
  66. bolus: nil
  67. )
  68. ]
  69. let profile = createBasicProfile()
  70. let basalProfile = createBasicBasalProfile()
  71. let result = try MealTotal.recentCarbs(
  72. treatments: treatments,
  73. pumpHistory: pumpHistory,
  74. profile: profile,
  75. basalProfile: basalProfile,
  76. glucose: glucoseData,
  77. time: testTime
  78. )
  79. // After 1 hour, we should see partial carb absorption
  80. #expect(result != nil)
  81. // at this level JS is rounding, thus the 0.5
  82. #expect(result!.mealCOB.isWithin(0.5, of: 10) == true, "mealCOB: \(result!.mealCOB.description)")
  83. #expect(
  84. result!.currentDeviation == 3.6,
  85. "currentDeviation: \(result!.currentDeviation.description)"
  86. )
  87. }
  88. @Test("should return nil when no treatments provided") func emptyObjectWhenNoTreatments() async throws {
  89. let time = Date.from(isoString: "2016-06-19T13:00:00-04:00")
  90. let glucoseData = [
  91. BloodGlucose(
  92. sgv: 100,
  93. date: Decimal(time.timeIntervalSince1970 * 1000),
  94. dateString: time
  95. )
  96. ]
  97. let profile = createBasicProfile()
  98. let basalProfile = createBasicBasalProfile()
  99. let result = try MealTotal.recentCarbs(
  100. treatments: [],
  101. pumpHistory: [],
  102. profile: profile,
  103. basalProfile: basalProfile,
  104. glucose: glucoseData,
  105. time: time
  106. )
  107. #expect(result == nil)
  108. }
  109. @Test("should calculate carbs correctly for treatments within the meal window") func calcCarbsWithinMealWindow() async throws {
  110. let baseTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  111. let testTime = Date.from(isoString: "2016-06-19T13:00:00-04:00")
  112. let treatments = [
  113. MealInput(
  114. timestamp: baseTime,
  115. carbs: 20,
  116. bolus: nil
  117. )
  118. ]
  119. // Create glucose pattern with slight rise
  120. let glucoseData = [
  121. BloodGlucose(
  122. sgv: 110,
  123. date: Decimal(baseTime.addingTimeInterval(60 * 60).timeIntervalSince1970 * 1000),
  124. dateString: baseTime.addingTimeInterval(60 * 60)
  125. ),
  126. BloodGlucose(
  127. sgv: 105,
  128. date: Decimal(baseTime.addingTimeInterval(30 * 60).timeIntervalSince1970 * 1000),
  129. dateString: baseTime.addingTimeInterval(30 * 60)
  130. ),
  131. BloodGlucose(
  132. sgv: 100,
  133. date: Decimal(baseTime.timeIntervalSince1970 * 1000),
  134. dateString: baseTime
  135. )
  136. ]
  137. let profile = createBasicProfile()
  138. let basalProfile = createBasicBasalProfile()
  139. let result = try MealTotal.recentCarbs(
  140. treatments: treatments,
  141. pumpHistory: [],
  142. profile: profile,
  143. basalProfile: basalProfile,
  144. glucose: glucoseData,
  145. time: testTime
  146. )
  147. #expect(result != nil)
  148. #expect(result!.carbs == 20)
  149. #expect(
  150. result!.currentDeviation.isWithin(0.02, of: 0.67) == true,
  151. "currentDeviation: \(result!.currentDeviation.description)"
  152. )
  153. #expect(result!.mealCOB.isWithin(0.25, of: 14) == true, "mealCOB: \(result!.mealCOB.description)")
  154. }
  155. @Test("should ignore treatments outside the meal window") func ignoreTreatmentsOutsideMealWindow() async throws {
  156. let baseTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  157. let treatmentTime = Date.from(isoString: "2016-06-19T06:00:00-04:00") // 6 hours before
  158. let testTime = Date.from(isoString: "2016-06-19T13:00:00-04:00")
  159. let treatments = [
  160. MealInput(
  161. timestamp: treatmentTime,
  162. carbs: 20,
  163. bolus: nil
  164. )
  165. ]
  166. // Create glucose pattern with slight rise
  167. let glucoseData = [
  168. BloodGlucose(
  169. sgv: 110,
  170. date: Decimal(baseTime.addingTimeInterval(60 * 60).timeIntervalSince1970 * 1000),
  171. dateString: baseTime.addingTimeInterval(60 * 60)
  172. ),
  173. BloodGlucose(
  174. sgv: 105,
  175. date: Decimal(baseTime.addingTimeInterval(30 * 60).timeIntervalSince1970 * 1000),
  176. dateString: baseTime.addingTimeInterval(30 * 60)
  177. ),
  178. BloodGlucose(
  179. sgv: 100,
  180. date: Decimal(baseTime.timeIntervalSince1970 * 1000),
  181. dateString: baseTime
  182. )
  183. ]
  184. let profile = createBasicProfile()
  185. let basalProfile = createBasicBasalProfile()
  186. let result = try MealTotal.recentCarbs(
  187. treatments: treatments,
  188. pumpHistory: [],
  189. profile: profile,
  190. basalProfile: basalProfile,
  191. glucose: glucoseData,
  192. time: testTime
  193. )
  194. #expect(result != nil)
  195. #expect(result?.carbs == 0)
  196. #expect(result?.mealCOB == 0)
  197. #expect(
  198. result?.currentDeviation.isWithin(0.02, of: 0.67) == true,
  199. "currentDeviation: \(result!.currentDeviation.description)"
  200. )
  201. }
  202. @Test("should respect maxMealAbsorptionTime from profile") func respectMaxMealAbsorptionTime() async throws {
  203. let baseTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  204. let treatmentTime = Date.from(isoString: "2016-06-19T10:00:00-04:00") // 2 hours before
  205. let testTime = Date.from(isoString: "2016-06-19T13:00:00-04:00")
  206. let treatments = [
  207. MealInput(
  208. timestamp: treatmentTime,
  209. carbs: 20,
  210. bolus: nil
  211. )
  212. ]
  213. // Create glucose pattern with slight rise
  214. let glucoseData = [
  215. BloodGlucose(
  216. sgv: 110,
  217. date: Decimal(baseTime.addingTimeInterval(60 * 60).timeIntervalSince1970 * 1000),
  218. dateString: baseTime.addingTimeInterval(60 * 60)
  219. ),
  220. BloodGlucose(
  221. sgv: 105,
  222. date: Decimal(baseTime.addingTimeInterval(30 * 60).timeIntervalSince1970 * 1000),
  223. dateString: baseTime.addingTimeInterval(30 * 60)
  224. ),
  225. BloodGlucose(
  226. sgv: 100,
  227. date: Decimal(baseTime.timeIntervalSince1970 * 1000),
  228. dateString: baseTime
  229. )
  230. ]
  231. var profile = createBasicProfile()
  232. profile.maxMealAbsorptionTime = 2 // 2 hour window
  233. let basalProfile = createBasicBasalProfile()
  234. let result = try MealTotal.recentCarbs(
  235. treatments: treatments,
  236. pumpHistory: [],
  237. profile: profile,
  238. basalProfile: basalProfile,
  239. glucose: glucoseData,
  240. time: testTime
  241. )
  242. #expect(result != nil)
  243. #expect(result?.carbs == 0)
  244. #expect(result?.mealCOB == 0)
  245. }
  246. @Test("should respect maxCOB from profile") func respectMaxCOB() async throws {
  247. let baseTime = Date.from(isoString: "2016-06-19T12:00:00-04:00")
  248. let testTime = Date.from(isoString: "2016-06-19T13:00:00-04:00")
  249. let treatments = [
  250. MealInput(
  251. timestamp: baseTime,
  252. carbs: 200,
  253. bolus: nil
  254. )
  255. ]
  256. // Create glucose pattern with slight rise
  257. let glucoseData = [
  258. BloodGlucose(
  259. sgv: 110,
  260. date: Decimal(baseTime.addingTimeInterval(60 * 60).timeIntervalSince1970 * 1000),
  261. dateString: baseTime.addingTimeInterval(60 * 60)
  262. ),
  263. BloodGlucose(
  264. sgv: 105,
  265. date: Decimal(baseTime.addingTimeInterval(30 * 60).timeIntervalSince1970 * 1000),
  266. dateString: baseTime.addingTimeInterval(30 * 60)
  267. ),
  268. BloodGlucose(
  269. sgv: 100,
  270. date: Decimal(baseTime.timeIntervalSince1970 * 1000),
  271. dateString: baseTime
  272. )
  273. ]
  274. let profile = createBasicProfile()
  275. let basalProfile = createBasicBasalProfile()
  276. let result = try MealTotal.recentCarbs(
  277. treatments: treatments,
  278. pumpHistory: [],
  279. profile: profile,
  280. basalProfile: basalProfile,
  281. glucose: glucoseData,
  282. time: testTime
  283. )
  284. #expect(result != nil)
  285. #expect(result!.mealCOB <= 120)
  286. }
  287. }