"""Tests para compute_backoff_delay."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
from compute_backoff_delay import compute_backoff_delay


def test_attempt_0_retorna_base_delay_sin_jitter():
    result = compute_backoff_delay(0, base_delay=0.5, max_delay=8.0, jitter=False)
    assert result == 0.5


def test_attempt_alto_se_cappea_a_max_delay():
    result = compute_backoff_delay(10, base_delay=0.5, max_delay=8.0, jitter=False)
    assert result == 8.0


def test_sin_jitter_es_determinista():
    r1 = compute_backoff_delay(3, base_delay=1.0, max_delay=16.0, jitter=False)
    r2 = compute_backoff_delay(3, base_delay=1.0, max_delay=16.0, jitter=False)
    assert r1 == r2
    # attempt=3: 1.0 * 2^3 = 8.0
    assert r1 == 8.0


def test_escala_exponencial():
    d0 = compute_backoff_delay(0, base_delay=1.0, max_delay=100.0, jitter=False)
    d1 = compute_backoff_delay(1, base_delay=1.0, max_delay=100.0, jitter=False)
    d2 = compute_backoff_delay(2, base_delay=1.0, max_delay=100.0, jitter=False)
    assert d0 == 1.0
    assert d1 == 2.0
    assert d2 == 4.0


def test_con_jitter_no_excede_max_delay_mas_base():
    # Con jitter, delay base + jitter <= max_delay + base_delay
    for attempt in range(5):
        result = compute_backoff_delay(attempt, base_delay=0.5, max_delay=8.0, jitter=True)
        assert result >= 0.5
        assert result <= 8.0 + 0.5
