package com.gametime.watch.bridge

import androidx.health.services.client.data.DataType
import java.util.concurrent.ExecutionException
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class WatchExerciseMetricsRetryPolicyTest {
    @Test
    fun distanceSecurityFailureMatchesWrappedSecurityException() {
        val error = ExecutionException(
            SecurityException("Missing permissions: [android.permission.ACCESS_FINE_LOCATION]"),
        )

        assertTrue(
            WatchExerciseMetricsRetryPolicy.isDistanceSecurityFailure(
                setOf(DataType.DISTANCE, DataType.CALORIES, DataType.HEART_RATE_BPM),
                error,
            ),
        )
    }

    @Test
    fun nonDistanceSecurityFailureDoesNotScheduleDistanceRetry() {
        val error = ExecutionException(
            SecurityException("Missing permissions: [android.permission.ACCESS_FINE_LOCATION]"),
        )

        assertFalse(
            WatchExerciseMetricsRetryPolicy.isDistanceSecurityFailure(
                setOf(DataType.CALORIES, DataType.HEART_RATE_BPM),
                error,
            ),
        )
    }

    @Test
    fun distanceNonSecurityFailureDoesNotScheduleDistanceRetry() {
        val error = ExecutionException(IllegalStateException("Health Services busy"))

        assertFalse(
            WatchExerciseMetricsRetryPolicy.isDistanceSecurityFailure(
                setOf(DataType.DISTANCE, DataType.CALORIES, DataType.HEART_RATE_BPM),
                error,
            ),
        )
    }

    @Test
    fun distanceSecurityRetriesAreBounded() {
        assertFalse(WatchExerciseMetricsRetryPolicy.canRetryDistance(0))
        assertTrue(WatchExerciseMetricsRetryPolicy.canRetryDistance(1))
        assertTrue(WatchExerciseMetricsRetryPolicy.canRetryDistance(2))
        assertTrue(WatchExerciseMetricsRetryPolicy.canRetryDistance(3))
        assertFalse(WatchExerciseMetricsRetryPolicy.canRetryDistance(4))
    }
}
