import HealthKit
import XCTest
@testable import HealthProbe

final class DistributionCaptureConfigurationTests: XCTestCase {
    func testHeartRateInitialImportUsesLargerWriteChunks() {
        let strategy = DistributionCaptureConfiguration.initialImportStrategy(
            for: HKQuantityTypeIdentifier.heartRate.rawValue
        )

        XCTAssertEqual(strategy.queryPageLimit, 2_000)
        XCTAssertEqual(strategy.initialWriteChunkSize, 5_000)
        XCTAssertEqual(strategy.initialArchiveFlushSampleLimit, 10_000)
        XCTAssertEqual(strategy.minimumWriteChunkSize, 500)
        XCTAssertEqual(strategy.slowBatchThresholdSeconds, 2.5)
        XCTAssertEqual(strategy.severeBatchThresholdSeconds, 6)
    }

    func testHeartRateWriteChunkCanBackOffOnSlowDevices() {
        let strategy = DistributionCaptureConfiguration.initialImportStrategy(
            for: HKQuantityTypeIdentifier.heartRate.rawValue
        )
        var state = strategy.makePersistenceState()

        state.registerBatchDuration(strategy.severeBatchThresholdSeconds)

        XCTAssertEqual(state.currentWriteChunkSize, 2_500)

        state.registerBatchDuration(strategy.severeBatchThresholdSeconds)

        XCTAssertEqual(state.currentWriteChunkSize, 1_250)
    }
}
