ProfileCarbsTests.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Carb Ratio Profile") struct CarbRatioTests {
  5. let standardSchedule = CarbRatios(
  6. units: .grams,
  7. schedule: [
  8. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 15),
  9. CarbRatioEntry(start: "03:00:00", offset: 180, ratio: 18),
  10. CarbRatioEntry(start: "06:00:00", offset: 360, ratio: 20)
  11. ]
  12. )
  13. @Test("should return current carb ratio from schedule") func currentCarbRatio() async throws {
  14. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!
  15. let ratio = Carbs.carbRatioLookup(carbRatio: standardSchedule, now: now)
  16. #expect(ratio == 15)
  17. }
  18. @Test("should handle ratio schedule changes") func handleScheduleChanges() async throws {
  19. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 4))!
  20. let ratio = Carbs.carbRatioLookup(carbRatio: standardSchedule, now: now)
  21. #expect(ratio == 18)
  22. }
  23. @Test("should handle exchanges unit conversion") func handleExchanges() async throws {
  24. let exchangeSchedule = CarbRatios(
  25. units: .exchanges,
  26. schedule: [
  27. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 12)
  28. ]
  29. )
  30. let ratio = Carbs.carbRatioLookup(carbRatio: exchangeSchedule, now: Date())
  31. #expect(ratio == 1) // 12 grams per exchange
  32. }
  33. @Test("should allow ratios of 1") func allowLowRatios() async throws {
  34. let schedule = CarbRatios(
  35. units: .grams,
  36. schedule: [
  37. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 1)
  38. ]
  39. )
  40. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!
  41. let ratio = Carbs.carbRatioLookup(carbRatio: schedule, now: now)
  42. #expect(ratio == 1)
  43. }
  44. @Test("should reject invalid ratios") func rejectInvalidRatios() async throws {
  45. let invalidSchedule = CarbRatios(
  46. units: .grams,
  47. schedule: [
  48. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 0.5),
  49. CarbRatioEntry(start: "03:00:00", offset: 180, ratio: 15)
  50. ]
  51. )
  52. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!
  53. var ratio = Carbs.carbRatioLookup(carbRatio: invalidSchedule, now: now)
  54. #expect(ratio == nil)
  55. let invalidSchedule2 = CarbRatios(
  56. units: .grams,
  57. schedule: [
  58. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 200)
  59. ]
  60. )
  61. ratio = Carbs.carbRatioLookup(carbRatio: invalidSchedule2, now: now)
  62. #expect(ratio == nil)
  63. }
  64. }