"""Tests for compute_retry_cooldown and MAX_TOTAL_RETRIES."""

from datetime import timedelta

from local_deep_research.library.download_management.status_tracker import (
    MAX_TOTAL_RETRIES,
    compute_retry_cooldown,
)


class TestComputeRetryCooldown:
    """Tests for the backoff schedule function."""

    def test_first_attempt_uses_default_cooldown(self):
        default = timedelta(hours=1)
        assert compute_retry_cooldown(1, default) == default

    def test_first_attempt_preserves_arbitrary_default(self):
        default = timedelta(minutes=30)
        assert compute_retry_cooldown(1, default) == default

    def test_second_attempt_waits_one_day(self):
        assert compute_retry_cooldown(2, timedelta(hours=1)) == timedelta(
            days=1
        )

    def test_third_attempt_waits_30_days(self):
        assert compute_retry_cooldown(3, timedelta(hours=1)) == timedelta(
            days=30
        )

    def test_fourth_attempt_waits_30_days(self):
        assert compute_retry_cooldown(4, timedelta(hours=1)) == timedelta(
            days=30
        )

    def test_fifth_attempt_returns_none_permanent(self):
        assert compute_retry_cooldown(5, timedelta(hours=1)) is None

    def test_beyond_max_returns_none(self):
        assert compute_retry_cooldown(10, timedelta(hours=1)) is None
        assert compute_retry_cooldown(100, timedelta(hours=1)) is None

    def test_schedule_matches_documented_table(self):
        """Verify the full schedule matches the PR documentation."""
        default = timedelta(hours=1)
        expected = [
            (1, default),
            (2, timedelta(days=1)),
            (3, timedelta(days=30)),
            (4, timedelta(days=30)),
            (5, None),
        ]
        for attempt, expected_cooldown in expected:
            result = compute_retry_cooldown(attempt, default)
            assert result == expected_cooldown, (
                f"attempt {attempt}: expected {expected_cooldown}, got {result}"
            )

    def test_default_cooldown_ignored_after_first_attempt(self):
        """Default cooldown only matters for attempt 1."""
        short = timedelta(seconds=1)
        long = timedelta(days=365)
        # Attempt 2+ should return the same regardless of default
        assert compute_retry_cooldown(2, short) == compute_retry_cooldown(
            2, long
        )
        assert compute_retry_cooldown(3, short) == compute_retry_cooldown(
            3, long
        )
        assert compute_retry_cooldown(5, short) == compute_retry_cooldown(
            5, long
        )


class TestMaxTotalRetries:
    """Tests for the MAX_TOTAL_RETRIES constant."""

    def test_value_is_five(self):
        assert MAX_TOTAL_RETRIES == 5

    def test_cooldown_returns_none_at_exactly_max(self):
        assert (
            compute_retry_cooldown(MAX_TOTAL_RETRIES, timedelta(hours=1))
            is None
        )

    def test_cooldown_returns_value_just_below_max(self):
        result = compute_retry_cooldown(
            MAX_TOTAL_RETRIES - 1, timedelta(hours=1)
        )
        assert result is not None
