IobGenerateTests.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("IoB generate tests") struct IobGenerateTests {
  5. /// One of our performance optimizations where we filter old pump events has subtle interactions
  6. /// with the JS implementation. In particular, JS will hardcode 8 hours for DIA in the suspend logic
  7. /// when a pump history has a resume as the first suspend/resume event. This hard coded value
  8. /// can cause some old netbasalinsulin to get dropped if DIA > 8 hours. We fixed this bug by
  9. /// not filtering suspend and resume events, and this test case checks for the bug fix.
  10. @Test("should test suspend filtering") func testSuspendFiltering() async throws {
  11. let now = Calendar.current.startOfDay(for: Date()) + 20.hoursToSeconds
  12. let history = [
  13. PumpHistoryEvent(id: UUID().uuidString, type: .pumpSuspend, timestamp: now - 15.hoursToSeconds),
  14. PumpHistoryEvent(id: UUID().uuidString, type: .pumpResume, timestamp: now - 1.hoursToSeconds)
  15. ]
  16. var profile = Profile()
  17. profile.dia = 10
  18. profile.currentBasal = 1
  19. profile.maxDailyBasal = 1
  20. profile.basalprofile = [
  21. BasalProfileEntry(
  22. start: "00:00:00",
  23. minutes: 0,
  24. rate: 1
  25. )
  26. ]
  27. profile.suspendZerosIob = true
  28. let iob = try IobGenerator.generate(history: history, profile: profile, clock: now, autosens: nil)
  29. // Matches the long suspend test in JS iob.test.js
  30. #expect(iob[0].netbasalinsulin == -8.95)
  31. }
  32. }