import XCTest
@testable import HealthProbe

final class LocalMetricTimeoutProfileTests: XCTestCase {
    override func tearDown() {
        LocalMetricTimeoutProfileStore.removeAll()
        super.tearDown()
    }

    func testRecordsSuccessAndPersistsProfile() {
        var profile = LocalMetricTimeoutProfile(metricIdentifier: "step_count", displayName: "Step Count")

        profile.recordSuccess(elapsed: 12, displayName: "Step Count")
        LocalMetricTimeoutProfileStore.save(profile)

        let loaded = LocalMetricTimeoutProfileStore.profile(for: MonitoredType(
            id: "step_count",
            displayName: "Step Count",
            category: .activity,
            isEnabledByDefault: true,
            objectType: nil
        ))
        XCTAssertEqual(loaded.successCount, 1)
        XCTAssertEqual(loaded.lastSuccessfulElapsed, 12)
        XCTAssertEqual(loaded.timeoutMode, "adaptive")
    }

    func testManualTimeoutIsClampedAndResettable() {
        var profile = LocalMetricTimeoutProfile(metricIdentifier: "heart_rate", displayName: "Heart Rate")

        profile.setManualTimeout(1_000)
        XCTAssertEqual(profile.effectiveTimeout, LocalMetricTimeoutProfile.maximumTimeout)
        XCTAssertEqual(profile.timeoutMode, "manual")

        profile.resetLearning()
        XCTAssertEqual(profile.effectiveTimeout, LocalMetricTimeoutProfile.defaultInitialTimeout)
        XCTAssertEqual(profile.timeoutMode, "default")
    }
}
