/ BalanceKitTests / HealthManagerTests.swift
HealthManagerTests.swift
  1  //
  2  //  HealthManagerTests.swift
  3  //  BalanceKitTests
  4  //
  5  //  Created by Alexander Kunau on 13.12.25.
  6  //
  7  
  8  import XCTest
  9  import HealthKit
 10  @testable import BalanceKit
 11  
 12  final class HealthManagerTests: XCTestCase {
 13      var mockHealthManager: MockHealthManager!
 14      
 15      override func setUp() {
 16          super.setUp()
 17          mockHealthManager = MockHealthManager()
 18      }
 19      
 20      override func tearDown() {
 21          mockHealthManager = nil
 22          super.tearDown()
 23      }
 24      
 25      // MARK: - Authorization Tests
 26      
 27      func testAuthorizationSuccess() {
 28          mockHealthManager.shouldFailNextOperation = false
 29          mockHealthManager.requestAuthorization()
 30          
 31          XCTAssertTrue(mockHealthManager.isAuthorized)
 32          XCTAssertEqual(mockHealthManager.authorizationCallCount, 1)
 33      }
 34      
 35      func testAuthorizationDenial() {
 36          mockHealthManager.shouldFailNextOperation = true
 37          mockHealthManager.requestAuthorization()
 38          
 39          XCTAssertFalse(mockHealthManager.isAuthorized)
 40          XCTAssertEqual(mockHealthManager.authorizationCallCount, 1)
 41      }
 42      
 43      // MARK: - Water Tracking Tests
 44      
 45      func testAddWaterSuccess() {
 46          let initialWater = mockHealthManager.waterConsumed
 47          mockHealthManager.addWater(milliliters: 250)
 48          
 49          XCTAssertEqual(mockHealthManager.waterConsumed, initialWater + 250, accuracy: 0.1)
 50      }
 51      
 52      func testAddWaterFailure() {
 53          let initialWater = mockHealthManager.waterConsumed
 54          mockHealthManager.shouldFailNextOperation = true
 55          mockHealthManager.addWater(milliliters: 250)
 56          
 57          // Water should not be added on failure
 58          XCTAssertEqual(mockHealthManager.waterConsumed, initialWater, accuracy: 0.1)
 59      }
 60      
 61      func testAddWaterUnauthorized() {
 62          mockHealthManager.mockIsAuthorized = false
 63          let initialWater = mockHealthManager.waterConsumed
 64          
 65          mockHealthManager.addWater(milliliters: 250)
 66          
 67          // Should handle unauthorized gracefully
 68          XCTAssertEqual(mockHealthManager.waterConsumed, initialWater, accuracy: 0.1)
 69      }
 70      
 71      // MARK: - Food Item Saving Tests
 72      
 73      func testSaveFoodItemSuccess() {
 74          mockHealthManager.saveFoodItem(name: "Apple", calories: 95, protein: 0.5, carbs: 25, fat: 0.3)
 75          
 76          XCTAssertEqual(mockHealthManager.saveCallCount, 1)
 77      }
 78      
 79      func testSaveFoodItemFailure() {
 80          mockHealthManager.shouldFailNextOperation = true
 81          mockHealthManager.saveFoodItem(name: "Apple", calories: 95, protein: 0.5, carbs: 25, fat: 0.3)
 82          
 83          XCTAssertEqual(mockHealthManager.saveCallCount, 1)
 84          // Verify failure is handled gracefully
 85      }
 86      
 87      func testSaveFoodItemUnauthorized() {
 88          mockHealthManager.mockIsAuthorized = false
 89          mockHealthManager.saveFoodItem(name: "Apple", calories: 95, protein: 0.5, carbs: 25, fat: 0.3)
 90          
 91          // Should not attempt to save when unauthorized
 92          XCTAssertEqual(mockHealthManager.saveCallCount, 0)
 93      }
 94      
 95      // MARK: - Data Fetching Tests
 96      
 97      func testFetchTodaysDataSuccess() {
 98          mockHealthManager.fetchTodaysData()
 99          
100          XCTAssertEqual(mockHealthManager.activeCaloriesBurned, 250.0, accuracy: 0.1)
101          XCTAssertEqual(mockHealthManager.steps, 5000)
102          XCTAssertEqual(mockHealthManager.waterConsumed, 1000.0, accuracy: 0.1)
103      }
104      
105      func testFetchTodaysDataFailure() {
106          mockHealthManager.shouldFailNextOperation = true
107          mockHealthManager.fetchTodaysData()
108          
109          // Should handle failure gracefully with default values
110          XCTAssertEqual(mockHealthManager.activeCaloriesBurned, 0, accuracy: 0.1)
111          XCTAssertEqual(mockHealthManager.steps, 0)
112          XCTAssertEqual(mockHealthManager.waterConsumed, 0, accuracy: 0.1)
113      }
114      
115      func testFetchActiveCaloriesForDate() {
116          let expectation = self.expectation(description: "Fetch calories")
117          let testDate = Date()
118          
119          mockHealthManager.mockActiveCaloriesBurned = 300
120          mockHealthManager.fetchActiveCaloriesForDate(testDate) { calories in
121              XCTAssertEqual(calories, 300, accuracy: 0.1)
122              expectation.fulfill()
123          }
124          
125          waitForExpectations(timeout: 1.0)
126      }
127      
128      func testFetchStepsForDate() {
129          let expectation = self.expectation(description: "Fetch steps")
130          let testDate = Date()
131          
132          mockHealthManager.mockSteps = 8000
133          mockHealthManager.fetchStepsForDate(testDate) { steps in
134              XCTAssertEqual(steps, 8000)
135              expectation.fulfill()
136          }
137          
138          waitForExpectations(timeout: 1.0)
139      }
140      
141      func testFetchWaterIntakeForDate() {
142          let expectation = self.expectation(description: "Fetch water")
143          let testDate = Date()
144          
145          mockHealthManager.mockWaterConsumed = 1500
146          mockHealthManager.fetchWaterIntakeForDate(testDate) { water in
147              XCTAssertEqual(water, 1500, accuracy: 0.1)
148              expectation.fulfill()
149          }
150          
151          waitForExpectations(timeout: 1.0)
152      }
153  }