ProfileIsfTests.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("ISF Profile") struct ISFTests {
  5. let standardISF = InsulinSensitivities(
  6. units: .mgdL,
  7. userPreferredUnits: .mgdL,
  8. sensitivities: [
  9. InsulinSensitivityEntry(sensitivity: 100, offset: 0, start: "00:00:00"),
  10. InsulinSensitivityEntry(sensitivity: 80, offset: 180, start: "03:00:00"),
  11. InsulinSensitivityEntry(sensitivity: 90, offset: 360, start: "06:00:00")
  12. ]
  13. )
  14. @Test("should return current insulin sensitivity factor from schedule") func currentISF() async throws {
  15. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!
  16. let (sensitivity, _) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  17. #expect(sensitivity == 100)
  18. }
  19. @Test("should handle sensitivity schedule changes") func handleScheduleChanges() async throws {
  20. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 4))!
  21. let (sensitivity, _) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  22. #expect(sensitivity == 80)
  23. }
  24. @Test("should use last sensitivity if past schedule end") func useLastSensitivity() async throws {
  25. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 23))!
  26. let (sensitivity, _) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  27. #expect(sensitivity == 90)
  28. }
  29. @Test("should produce the same result without a cache") func cacheLastResult() async throws {
  30. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 4, minute: 30))!
  31. let (sensitivity1, _) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  32. let (sensitivity2, _) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  33. #expect(sensitivity1 == sensitivity2)
  34. #expect(sensitivity1 == 80)
  35. }
  36. @Test("should provide updated inputs with the `endOffset` parameter") func updatedInputs() async throws {
  37. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 4))!
  38. let (sensitivity, isfUpdated) = try Isf.isfLookup(isfDataInput: standardISF, timestamp: now)
  39. #expect(sensitivity == 80)
  40. #expect(isfUpdated.sensitivities[0].endOffset == nil)
  41. #expect(isfUpdated.sensitivities[1].endOffset == 360)
  42. #expect(isfUpdated.sensitivities[2].endOffset == nil)
  43. }
  44. @Test("should return -1 for invalid profile with non-zero first offset") func handleInvalidProfile() async throws {
  45. let invalidISF = InsulinSensitivities(
  46. units: .mgdL,
  47. userPreferredUnits: .mgdL,
  48. sensitivities: [
  49. InsulinSensitivityEntry(sensitivity: 100, offset: 30, start: "00:30:00")
  50. ]
  51. )
  52. let (sensitivity, _) = try Isf.isfLookup(isfDataInput: invalidISF, timestamp: Date())
  53. #expect(sensitivity == -1)
  54. }
  55. }