ProfileBasalTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Basal Tests") struct BasalTests {
  5. @Test("should find current basal rate in a sample profile") func findCurrentBasalRate() async throws {
  6. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 1, hour: 2))!
  7. let basalProfile = [
  8. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0),
  9. BasalProfileEntry(start: "02:00", minutes: 120, rate: 2.0),
  10. BasalProfileEntry(start: "03:00", minutes: 180, rate: 3.0)
  11. ]
  12. let rate = try Basal.basalLookup(basalProfile, now: now)
  13. #expect(rate == 2.0)
  14. }
  15. @Test("should find current basal rate for midnight in a sample profile") func findMidnightBasalRate() async throws {
  16. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 1, hour: 0))!
  17. let basalProfile = [
  18. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0),
  19. BasalProfileEntry(start: "02:00", minutes: 120, rate: 2.0),
  20. BasalProfileEntry(start: "03:00", minutes: 180, rate: 3.0)
  21. ]
  22. let rate = try Basal.basalLookup(basalProfile, now: now)
  23. #expect(rate == 1.0)
  24. }
  25. @Test("should find current basal rate for 3am in a sample profile") func findThreeAmBasalRate() async throws {
  26. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 1, hour: 3))!
  27. let basalProfile = [
  28. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0),
  29. BasalProfileEntry(start: "02:00", minutes: 120, rate: 2.0),
  30. BasalProfileEntry(start: "03:00", minutes: 180, rate: 3.0)
  31. ]
  32. let rate = try Basal.basalLookup(basalProfile, now: now)
  33. #expect(rate == 3.0)
  34. }
  35. @Test("should return nil with an empty profile") func handleEmptyProfile() async throws {
  36. let rate = try Basal.basalLookup([], now: Date())
  37. #expect(rate == nil)
  38. }
  39. @Test("should handle a profile with just one rate") func handleSingleRateProfile() async throws {
  40. let basalProfile = [
  41. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0)
  42. ]
  43. let rate = try Basal.basalLookup(basalProfile, now: Date())
  44. #expect(rate == 1.0)
  45. }
  46. @Test("should return nil with a zero rate") func handleZeroRate() async throws {
  47. let basalProfile = [
  48. BasalProfileEntry(start: "00:00", minutes: 0, rate: 0.0)
  49. ]
  50. let rate = try Basal.basalLookup(basalProfile, now: Date())
  51. #expect(rate == nil)
  52. }
  53. @Test("should properly compute maxDailyBasal") func computeMaxDailyBasal() async throws {
  54. let basalProfile = [
  55. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0),
  56. BasalProfileEntry(start: "02:00", minutes: 120, rate: 2.0),
  57. BasalProfileEntry(start: "03:00", minutes: 180, rate: 3.0)
  58. ]
  59. let maxRate = Basal.maxDailyBasal(basalProfile)
  60. #expect(maxRate == 3.0)
  61. }
  62. @Test("should return nil for maxDailyBasal with empty profile") func handleEmptyProfileForMaxDaily() async throws {
  63. let maxRate = Basal.maxDailyBasal([])
  64. #expect(maxRate == nil)
  65. }
  66. }