IobJsonTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. class BundleReference {}
  5. @Suite("IoB using real pump history JSON", .disabled()) struct IobJsonTests {
  6. @Test("should produce the same JSON IobResult as Javascript") func createIobResultFromJson() async throws {
  7. let testBundle = Bundle(for: BundleReference.self)
  8. guard let path = testBundle.path(forResource: "pump_history", ofType: "json"),
  9. let data = try? Data(contentsOf: URL(fileURLWithPath: path)),
  10. let pumpHistory: [PumpHistoryEvent] = try! JSONBridge.from(string: String(data: data, encoding: .utf8)!),
  11. let path2 = testBundle.path(forResource: "iob_result", ofType: "json"),
  12. let data2 = try? Data(contentsOf: URL(fileURLWithPath: path2)),
  13. let iobResultsJson: [IobResult] = try! JSONBridge.from(string: String(data: data2, encoding: .utf8)!)
  14. else {
  15. #expect(Bool(false))
  16. return
  17. }
  18. let basalProfile = [
  19. BasalProfileEntry(start: "00:00", minutes: 0, rate: 0.5)
  20. ]
  21. var profile = Profile()
  22. profile.dia = 10
  23. profile.basalprofile = basalProfile
  24. profile.currentBasal = 1
  25. profile.maxDailyBasal = 1
  26. profile.curve = .ultraRapid
  27. let clock = Date("2025-02-18T23:23:31.036Z")!
  28. let iobResult = try IobGenerator.generate(history: pumpHistory, profile: profile, clock: clock, autosens: nil)
  29. #expect(iobResult.count == iobResultsJson.count)
  30. for (swift, javascript) in zip(iobResult, iobResultsJson) {
  31. #expect(swift.approximatelyEquals(javascript))
  32. }
  33. }
  34. @Test("should produce the same JSON history as Javascript") func createIobHistoryFromJson() async throws {
  35. let testBundle = Bundle(for: BundleReference.self)
  36. guard let path = testBundle.path(forResource: "pump_history", ofType: "json"),
  37. let data = try? Data(contentsOf: URL(fileURLWithPath: path)),
  38. let path2 = testBundle.path(forResource: "iob_history", ofType: "json"),
  39. let data2 = try? Data(contentsOf: URL(fileURLWithPath: path2)),
  40. let pumpHistory: [PumpHistoryEvent] = try! JSONBridge.from(string: String(data: data, encoding: .utf8)!),
  41. let iobHistoryJson: [HistoryRecord] = try! JSONBridge.from(string: String(data: data2, encoding: .utf8)!)
  42. else {
  43. #expect(Bool(false))
  44. return
  45. }
  46. let basalProfile = [
  47. BasalProfileEntry(start: "00:00", minutes: 0, rate: 0.5)
  48. ]
  49. var profile = Profile()
  50. profile.dia = 10
  51. profile.basalprofile = basalProfile
  52. profile.currentBasal = 1
  53. profile.maxDailyBasal = 1
  54. profile.curve = .ultraRapid
  55. let clock = Date("2025-02-18T23:23:31.036Z")!
  56. let computedHistory = pumpHistory.map { $0.computedEvent() }
  57. let history = try IobHistory.calcTempTreatments(
  58. history: computedHistory,
  59. profile: profile,
  60. clock: clock,
  61. autosens: nil,
  62. zeroTempDuration: nil
  63. )
  64. #expect(history.count == iobHistoryJson.count)
  65. let historyBolusCount = history.filter({ $0.insulin != nil }).count
  66. let jsonBolusCount = iobHistoryJson.filter({ record in
  67. switch record {
  68. case .insulin: return true
  69. case .basal: return false
  70. }
  71. }).count
  72. #expect(historyBolusCount == jsonBolusCount)
  73. let historyBasalCount = history.filter({ $0.rate != nil }).count
  74. let jsonBasalCount = iobHistoryJson.filter({ record in
  75. switch record {
  76. case .insulin: return false
  77. case .basal: return true
  78. }
  79. }).count
  80. #expect(historyBasalCount == jsonBasalCount)
  81. let historyInsulin = history.compactMap(\.insulin).reduce(0, +)
  82. let jsonInsulin = iobHistoryJson.compactMap({ record in
  83. switch record {
  84. case let .insulin(r):
  85. return r.insulin
  86. case .basal:
  87. return nil
  88. }
  89. }).reduce(0, +)
  90. #expect(historyInsulin == jsonInsulin)
  91. }
  92. }