JSONImporterTests.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //
  2. // JSONImporterTests.swift
  3. // Trio
  4. //
  5. // Created by Cengiz Deniz on 21.04.25.
  6. //
  7. import CoreData
  8. import Foundation
  9. import Swinject
  10. import Testing
  11. @testable import Trio
  12. class BundleReference {}
  13. @Suite("JSON Importer Tests", .serialized) struct JSONImporterTests: Injectable {
  14. var coreDataStack: CoreDataStack!
  15. var context: NSManagedObjectContext!
  16. var importer: JSONImporter!
  17. init() async throws {
  18. // In-memory Core Data for tests
  19. coreDataStack = try await CoreDataStack.createForTests()
  20. context = coreDataStack.newTaskContext()
  21. importer = JSONImporter(context: context, coreDataStack: coreDataStack)
  22. }
  23. @Test("Import glucose history with value checks") func testImportGlucoseHistoryDetails() async throws {
  24. let testBundle = Bundle(for: BundleReference.self)
  25. let path = testBundle.path(forResource: "glucose", ofType: "json")!
  26. let url = URL(filePath: path)
  27. let now = Date("2025-04-28T19:32:52.000Z")!
  28. try await importer.importGlucoseHistory(url: url, now: now)
  29. // run the import againt to check our deduplication logic
  30. try await importer.importGlucoseHistory(url: url, now: now)
  31. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  32. ofType: GlucoseStored.self,
  33. onContext: context,
  34. predicate: NSPredicate(format: "TRUEPREDICATE"),
  35. key: "date",
  36. ascending: false
  37. ) as? [GlucoseStored] ?? []
  38. #expect(allReadings.count == 274)
  39. #expect(allReadings.first?.glucose == 115)
  40. #expect(allReadings.first?.date == Date("2025-04-28T19:32:51.727Z"))
  41. #expect(allReadings.last?.glucose == 127)
  42. #expect(allReadings.last?.date == Date("2025-04-27T19:37:50.327Z"))
  43. let manualCount = allReadings.filter({ $0.isManual }).count
  44. #expect(manualCount == 1)
  45. }
  46. @Test("Skip importing old glucose values") func testSkipImportOldGlucoseValues() async throws {
  47. let testBundle = Bundle(for: BundleReference.self)
  48. let path = testBundle.path(forResource: "glucose", ofType: "json")!
  49. let url = URL(filePath: path)
  50. // more than 24 hours in the future from the most recent entry
  51. let now = Date("2025-04-29T19:32:52.000Z")!
  52. try await importer.importGlucoseHistory(url: url, now: now)
  53. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  54. ofType: GlucoseStored.self,
  55. onContext: context,
  56. predicate: NSPredicate(format: "TRUEPREDICATE"),
  57. key: "date",
  58. ascending: false
  59. ) as? [GlucoseStored] ?? []
  60. #expect(allReadings.isEmpty)
  61. }
  62. @Test("Import pump history with value checks") func testImportPumpHistoryDetails() async throws {
  63. let testBundle = Bundle(for: BundleReference.self)
  64. let path = testBundle.path(forResource: "pumphistory-24h-zoned", ofType: "json")!
  65. let url = URL(filePath: path)
  66. let now = Date("2025-04-29T01:33:58.000Z")!
  67. try await importer.importPumpHistory(url: url, now: now)
  68. // test out deduplication logic
  69. try await importer.importPumpHistory(url: url, now: now)
  70. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  71. ofType: PumpEventStored.self,
  72. onContext: context,
  73. predicate: NSPredicate(format: "TRUEPREDICATE"),
  74. key: "timestamp",
  75. ascending: false
  76. ) as? [PumpEventStored] ?? []
  77. let objectIds = allReadings.map(\.objectID)
  78. let parsedHistory = OpenAPS.loadAndMapPumpEvents(objectIds, orphanedResumes: [], from: context)
  79. var bolusTotal = 0.0
  80. var bolusCount = 0
  81. var smbCount = 0
  82. var rateTotal = 0.0
  83. var tempBasalCount = 0
  84. var durationTotal = 0
  85. var suspendCount = 0
  86. var resumeCount = 0
  87. for event in parsedHistory {
  88. switch event {
  89. case let .bolus(bolus):
  90. bolusTotal += bolus.amount
  91. bolusCount += 1
  92. if bolus.isSMB {
  93. smbCount += 1
  94. }
  95. case let .tempBasal(tempBasal):
  96. rateTotal += tempBasal.rate
  97. tempBasalCount += 1
  98. case let .tempBasalDuration(tempBasalDuration):
  99. durationTotal += tempBasalDuration.duration
  100. case .suspend:
  101. suspendCount += 1
  102. case .resume:
  103. resumeCount += 1
  104. default:
  105. fatalError("unhandled pump event")
  106. }
  107. }
  108. // see the scripts/pump-history-stats.py file for where these come from
  109. #expect(parsedHistory.count == 77)
  110. #expect(bolusCount == 23)
  111. #expect(smbCount == 21)
  112. #expect(bolusTotal.isApproximatelyEqual(to: 8.1, epsilon: 0.01))
  113. #expect(tempBasalCount == 26)
  114. #expect(rateTotal.isApproximatelyEqual(to: 20.08, epsilon: 0.001))
  115. #expect(durationTotal == 900)
  116. #expect(suspendCount == 1)
  117. #expect(resumeCount == 1)
  118. }
  119. @Test("Skipping old pump history entries") func testSkipOldPumpHistoryEntries() async throws {
  120. let testBundle = Bundle(for: BundleReference.self)
  121. let path = testBundle.path(forResource: "pumphistory-24h-zoned", ofType: "json")!
  122. let url = URL(filePath: path)
  123. let now = Date("2025-04-30T01:33:58.000Z")!
  124. try await importer.importPumpHistory(url: url, now: now)
  125. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  126. ofType: PumpEventStored.self,
  127. onContext: context,
  128. predicate: NSPredicate(format: "TRUEPREDICATE"),
  129. key: "timestamp",
  130. ascending: false
  131. ) as? [PumpEventStored] ?? []
  132. #expect(allReadings.isEmpty)
  133. }
  134. @Test("Import pump history with external insulin") func testImportPumpHistoryWithExternalInsulin() async throws {
  135. let testBundle = Bundle(for: BundleReference.self)
  136. let path = testBundle.path(forResource: "pumphistory-with-external", ofType: "json")!
  137. let url = URL(filePath: path)
  138. let now = Date("2025-05-04T04:37:44.654Z")!
  139. try await importer.importPumpHistory(url: url, now: now)
  140. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  141. ofType: PumpEventStored.self,
  142. onContext: context,
  143. predicate: NSPredicate(format: "TRUEPREDICATE"),
  144. key: "timestamp",
  145. ascending: false
  146. ) as? [PumpEventStored] ?? []
  147. let objectIds = allReadings.map(\.objectID)
  148. let parsedHistory = OpenAPS.loadAndMapPumpEvents(objectIds, orphanedResumes: [], from: context)
  149. #expect(parsedHistory.count == 1)
  150. let bolus: BolusDTO? = {
  151. switch parsedHistory.first! {
  152. case let .bolus(bolus):
  153. return bolus
  154. default:
  155. return nil
  156. }
  157. }()
  158. #expect(bolus != nil)
  159. #expect(bolus!.isExternal)
  160. #expect(bolus!.amount.isApproximatelyEqual(to: 0.88, epsilon: 0.01))
  161. }
  162. @Test("Import carb history with value checks") func testImportCarbHistoryDetails() async throws {
  163. let testBundle = Bundle(for: BundleReference.self)
  164. let path = testBundle.path(forResource: "carbhistory", ofType: "json")!
  165. let url = URL(filePath: path)
  166. let now = Date("2025-04-28T19:32:52.000Z")!
  167. try await importer.importCarbHistory(url: url, now: now)
  168. // run the import againt to check our deduplication logic
  169. try await importer.importCarbHistory(url: url, now: now)
  170. let allCarbEntries = try await coreDataStack.fetchEntitiesAsync(
  171. ofType: CarbEntryStored.self,
  172. onContext: context,
  173. predicate: NSPredicate(format: "TRUEPREDICATE"),
  174. key: "date",
  175. ascending: false
  176. ) as? [CarbEntryStored] ?? []
  177. #expect(allCarbEntries.count == 8)
  178. #expect(allCarbEntries.first?.carbs == 10)
  179. #expect(allCarbEntries.first?.note == "Snack 🍪")
  180. #expect(allCarbEntries.first?.date == Date("2025-04-28T18:36:06.968Z"))
  181. #expect(allCarbEntries.last?.carbs == 25)
  182. #expect(allCarbEntries.last?.date == Date("2025-04-28T05:03:43.332Z"))
  183. }
  184. @Test("Skip importing old carb entries") func testSkipImportOldCarbEntries() async throws {
  185. let testBundle = Bundle(for: BundleReference.self)
  186. let path = testBundle.path(forResource: "carbhistory", ofType: "json")!
  187. let url = URL(filePath: path)
  188. // more than 24 hours in the future from the most recent entry
  189. let now = Date("2025-04-29T19:32:52.000Z")!
  190. try await importer.importCarbHistory(url: url, now: now)
  191. let allCarbEntries = try await coreDataStack.fetchEntitiesAsync(
  192. ofType: CarbEntryStored.self,
  193. onContext: context,
  194. predicate: NSPredicate(format: "TRUEPREDICATE"),
  195. key: "date",
  196. ascending: false
  197. ) as? [CarbEntryStored] ?? []
  198. #expect(allCarbEntries.isEmpty)
  199. }
  200. @Test("Import determination data with value checks") func testImportDeterminationDetails() async throws {
  201. let testBundle = Bundle(for: BundleReference.self)
  202. let enactedPath = testBundle.path(forResource: "enacted", ofType: "json")!
  203. let enactedUrl = URL(filePath: enactedPath)
  204. let suggestedPath = testBundle.path(forResource: "suggested", ofType: "json")!
  205. let suggestedUrl = URL(filePath: suggestedPath)
  206. let now = Date("2025-04-28T20:50:00.000Z")!
  207. try await importer.importOrefDetermination(enactedUrl: enactedUrl, suggestedUrl: suggestedUrl, now: now)
  208. // run the import againt to check our deduplication logic
  209. try await importer.importOrefDetermination(enactedUrl: enactedUrl, suggestedUrl: suggestedUrl, now: now)
  210. let determinations = try await coreDataStack.fetchEntitiesAsync(
  211. ofType: OrefDetermination.self,
  212. onContext: context,
  213. predicate: NSPredicate(format: "TRUEPREDICATE"),
  214. key: "deliverAt",
  215. ascending: false
  216. ) as? [OrefDetermination] ?? []
  217. #expect(determinations.count == 1) // single determination, as enacted.deliverAt and suggested.deliverAt match
  218. let determination = determinations.first!
  219. #expect(determination.deliverAt == Date("2025-04-28T19:41:43.564Z"))
  220. #expect(determination.timestamp == Date("2025-04-28T19:41:48.453Z"))
  221. #expect(determination.enacted == true)
  222. #expect(determination.reason?.starts(with: "Autosens ratio: 0.99") == true)
  223. #expect(determination.insulinReq == Decimal(string: "0.29").map(NSDecimalNumber.init))
  224. #expect(determination.eventualBG! == NSDecimalNumber(160))
  225. #expect(determination.sensitivityRatio == Decimal(string: "0.9863849810728643").map(NSDecimalNumber.init))
  226. #expect(determination.rate == Decimal(string: "0").map(NSDecimalNumber.init))
  227. #expect(determination.duration == NSDecimalNumber(60))
  228. #expect(determination.iob == Decimal(string: "1.249").map(NSDecimalNumber.init))
  229. #expect(determination.cob == 34)
  230. #expect(determination.temp == "absolute")
  231. #expect(determination.glucose == NSDecimalNumber(85))
  232. #expect(determination.reservoir == Decimal(string: "3735928559").map(NSDecimalNumber.init))
  233. #expect(determination.insulinSensitivity == Decimal(string: "4.6").map(NSDecimalNumber.init))
  234. #expect(determination.currentTarget == Decimal(string: "94").map(NSDecimalNumber.init))
  235. #expect(determination.insulinForManualBolus == Decimal(string: "0.8").map(NSDecimalNumber.init))
  236. #expect(determination.manualBolusErrorString == Decimal(string: "0").map(NSDecimalNumber.init))
  237. #expect(determination.minDelta == NSDecimalNumber(5))
  238. #expect(determination.expectedDelta == Decimal(string: "-5.9").map(NSDecimalNumber.init))
  239. #expect(determination.threshold == Decimal(string: "3.7").map(NSDecimalNumber.init))
  240. #expect(determination.carbRatio == nil) // not present in JSON
  241. let forecasts = try await coreDataStack.fetchEntitiesAsync(
  242. ofType: Forecast.self,
  243. onContext: context,
  244. predicate: NSPredicate(format: "orefDetermination = %@", determination.objectID),
  245. key: "type",
  246. ascending: true,
  247. relationshipKeyPathsForPrefetching: ["forecastValues"]
  248. )
  249. var forecastHierarchy: [(forecastID: NSManagedObjectID, forecastValueIDs: [NSManagedObjectID])] = []
  250. await context.perform {
  251. if let forecasts = forecasts as? [Forecast] {
  252. for forecast in forecasts {
  253. // Use the helper property that already sorts by index
  254. let sortedValues = forecast.forecastValuesArray
  255. forecastHierarchy.append((
  256. forecastID: forecast.objectID,
  257. forecastValueIDs: sortedValues.map(\.objectID)
  258. ))
  259. }
  260. }
  261. for entry in forecastHierarchy {
  262. var forecastValueTuple: (Forecast?, [ForecastValue]) = (nil, [])
  263. var forecast: Forecast?
  264. var forecastValues: [ForecastValue] = []
  265. do {
  266. // Fetch the forecast object
  267. forecast = try context.existingObject(with: entry.forecastID) as? Forecast
  268. // Fetch the first 3h of forecast values
  269. for forecastValueID in entry.forecastValueIDs.prefix(36) {
  270. if let forecastValue = try context.existingObject(with: forecastValueID) as? ForecastValue {
  271. forecastValues.append(forecastValue)
  272. }
  273. }
  274. } catch {
  275. debugPrint(
  276. "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to fetch forecast Values with error: \(error.localizedDescription)"
  277. )
  278. }
  279. forecastValueTuple = (forecast, forecastValues)
  280. // Basic checks
  281. #expect(forecastValueTuple.0 != nil)
  282. #expect(forecastValueTuple.1.isNotEmpty == true)
  283. if let forecast = forecastValueTuple.0 {
  284. let sortedValues = forecastValueTuple.1.sorted { $0.index < $1.index }
  285. let prefix = sortedValues.prefix(5).compactMap(\.value)
  286. let type = forecast.type?.lowercased()
  287. switch type {
  288. case "zt":
  289. #expect(prefix == [85, 78, 71, 64, 58])
  290. case "iob":
  291. #expect(prefix == [85, 89, 92, 95, 97])
  292. case "uam":
  293. #expect(prefix == [85, 89, 93, 96, 99])
  294. case "cob":
  295. #expect(prefix == [85, 90, 94, 99, 103])
  296. default:
  297. break // Skip unknown forecast types silently
  298. }
  299. }
  300. }
  301. }
  302. }
  303. @Test("Skip importing old determinations") func testSkipImportOldDeterminationData() async throws {
  304. let testBundle = Bundle(for: BundleReference.self)
  305. let enactedPath = testBundle.path(forResource: "enacted", ofType: "json")!
  306. let enactedUrl = URL(filePath: enactedPath)
  307. let suggestedPath = testBundle.path(forResource: "suggested", ofType: "json")!
  308. let suggestedUrl = URL(filePath: suggestedPath)
  309. // more than 24 hours in the future from the most recent entry
  310. let now = Date("2025-04-29T22:00:00.000Z")!
  311. try await importer.importOrefDetermination(enactedUrl: enactedUrl, suggestedUrl: suggestedUrl, now: now)
  312. let determinations = try await coreDataStack.fetchEntitiesAsync(
  313. ofType: OrefDetermination.self,
  314. onContext: context,
  315. predicate: NSPredicate(format: "TRUEPREDICATE"),
  316. key: "deliverAt",
  317. ascending: false
  318. ) as? [OrefDetermination] ?? []
  319. #expect(determinations.isEmpty)
  320. }
  321. @Test("Import determination data with suggested newer than enacted") func testImportDeterminationDetailsWithNewerSuggested(
  322. ) async throws {
  323. let testBundle = Bundle(for: BundleReference.self)
  324. let enactedPath = testBundle.path(forResource: "enacted", ofType: "json")!
  325. let enactedUrl = URL(filePath: enactedPath)
  326. let suggestedPath = testBundle.path(forResource: "newerSuggested", ofType: "json")!
  327. let suggestedUrl = URL(filePath: suggestedPath)
  328. let now = Date("2025-04-28T20:50:00.000Z")!
  329. try await importer.importOrefDetermination(enactedUrl: enactedUrl, suggestedUrl: suggestedUrl, now: now)
  330. let determinations = try await coreDataStack.fetchEntitiesAsync(
  331. ofType: OrefDetermination.self,
  332. onContext: context,
  333. predicate: NSPredicate(format: "TRUEPREDICATE"),
  334. key: "deliverAt",
  335. ascending: false
  336. ) as? [OrefDetermination] ?? []
  337. #expect(determinations.count == 2) // two determinations, suggested is more recent than enacted
  338. let suggested = determinations.first(where: { !$0.enacted && $0.deliverAt == $0.timestamp })!
  339. let enacted = determinations.first(where: { $0.enacted })!
  340. #expect(suggested.deliverAt == Date("2025-04-28T19:51:48.453Z"))
  341. #expect(enacted.timestamp == Date("2025-04-28T19:41:48.453Z"))
  342. }
  343. }
  344. extension Double {
  345. func isApproximatelyEqual(to other: Double, epsilon: Double?) -> Bool {
  346. // If no epsilon provided, require exact match
  347. guard let epsilon = epsilon else {
  348. return self == other
  349. }
  350. // Handle exact equality
  351. if self == other {
  352. return true
  353. }
  354. // Handle infinity and NaN
  355. if isInfinite || other.isInfinite || isNaN || other.isNaN {
  356. return self == other
  357. }
  358. // For values, use simple absolute difference
  359. return abs(self - other) <= epsilon
  360. }
  361. }