Unit Tests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import SwiftUICore
  2. import Testing
  3. @testable import Trio_Watch_App
  4. import XCTest
  5. @Suite("Watch App Tests") final class TrioWatchAppTests {
  6. var watchState = WatchState()
  7. // MARK: - Color Conversion Tests
  8. @Test("Hex string to color conversion") func testHexStringToColor() throws {
  9. // Given
  10. let whiteHex = "#FFFFFF"
  11. let blackHex = "#000000"
  12. let redHex = "#FF0000"
  13. let invalidHex = "invalid"
  14. // Then
  15. #expect(whiteHex.toColor() == Color.white)
  16. #expect(blackHex.toColor() == Color.black)
  17. #expect(redHex.toColor() == Color(red: 1, green: 0, blue: 0))
  18. #expect(invalidHex.toColor() == Color.black)
  19. }
  20. // MARK: - WatchState Tests
  21. @Test("WatchState initialization with default values") func testWatchStateInitialization() throws {
  22. #expect(watchState.currentGlucose == "--")
  23. #expect(watchState.currentGlucoseColorString == "#ffffff")
  24. #expect(watchState.glucoseValues.isEmpty)
  25. #expect(watchState.iob == "--")
  26. #expect(watchState.cob == "--")
  27. #expect(watchState.lastLoopTime == "--")
  28. }
  29. @Test("Bolus limits have correct default values") func testBolusLimits() throws {
  30. #expect(watchState.maxBolus == Decimal(10))
  31. #expect(watchState.bolusIncrement == Decimal(0.05))
  32. }
  33. @Test("Carb limits have correct default values") func testCarbLimits() throws {
  34. #expect(watchState.maxCarbs == Decimal(250))
  35. #expect(watchState.maxCOB == Decimal(120))
  36. }
  37. @Test("Bolus cancellation resets all related values") func testBolusCancellation() throws {
  38. // Given
  39. watchState.bolusProgress = 0.5
  40. watchState.activeBolusAmount = 5.0
  41. watchState.isBolusCanceled = false
  42. // When
  43. watchState.sendCancelBolusRequest()
  44. // Then
  45. #expect(watchState.isBolusCanceled)
  46. #expect(watchState.bolusProgress == 0)
  47. #expect(watchState.activeBolusAmount == 0)
  48. }
  49. @Test("Meal bolus combo state transitions work correctly") func testMealBolusComboState() throws {
  50. // Given - Initial state
  51. #expect(!watchState.isMealBolusCombo)
  52. #expect(watchState.mealBolusStep == .savingCarbs)
  53. // When - Setup meal bolus combo
  54. watchState.carbsAmount = 30
  55. watchState.bolusAmount = 3.0
  56. // Then - Test state transitions
  57. watchState.handleAcknowledgment(success: true, message: "Saving carbs...", isFinal: false)
  58. #expect(watchState.isMealBolusCombo)
  59. #expect(watchState.mealBolusStep == .savingCarbs)
  60. watchState.handleAcknowledgment(success: true, message: "Enacting bolus...", isFinal: false)
  61. #expect(watchState.isMealBolusCombo)
  62. #expect(watchState.mealBolusStep == .enactingBolus)
  63. watchState.handleAcknowledgment(success: true, message: "Carbs and bolus logged successfully", isFinal: true)
  64. #expect(!watchState.isMealBolusCombo)
  65. }
  66. @Test("Acknowledgment states transition correctly") func testAcknowledgmentStates() throws {
  67. // Given - Initial state
  68. #expect(watchState.acknowledgementStatus == .pending)
  69. #expect(!watchState.showAcknowledgmentBanner)
  70. // When/Then - Success acknowledgment
  71. watchState.handleAcknowledgment(success: true, message: "Success")
  72. #expect(watchState.acknowledgementStatus == .success)
  73. #expect(watchState.showAcknowledgmentBanner)
  74. #expect(watchState.acknowledgmentMessage == "Success")
  75. // When/Then - Failure acknowledgment
  76. watchState.handleAcknowledgment(success: false, message: "Error")
  77. #expect(watchState.acknowledgementStatus == .failure)
  78. #expect(watchState.showAcknowledgmentBanner)
  79. #expect(watchState.acknowledgmentMessage == "Error")
  80. }
  81. }