ProfileCarbsTests.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 reject invalid ratios") func rejectInvalidRatios() async throws {
  34. let invalidSchedule = CarbRatios(
  35. units: .grams,
  36. schedule: [
  37. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 2),
  38. CarbRatioEntry(start: "03:00:00", offset: 180, ratio: 15)
  39. ]
  40. )
  41. let now = Calendar.current.date(from: DateComponents(year: 2025, month: 1, day: 26, hour: 2))!
  42. var ratio = Carbs.carbRatioLookup(carbRatio: invalidSchedule, now: now)
  43. #expect(ratio == nil)
  44. let invalidSchedule2 = CarbRatios(
  45. units: .grams,
  46. schedule: [
  47. CarbRatioEntry(start: "00:00:00", offset: 0, ratio: 200)
  48. ]
  49. )
  50. ratio = Carbs.carbRatioLookup(carbRatio: invalidSchedule2, now: now)
  51. #expect(ratio == nil)
  52. }
  53. }