"""Tests para classify_api_error."""

import sys
import os

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


def test_error_429_es_transitorio():
    err = Exception("HTTP 429 TooManyRequests")
    assert classify_api_error(err) == "transient"


def test_error_401_es_permanente():
    err = Exception("HTTP 401 Unauthorized")
    assert classify_api_error(err) == "permanent"


def test_error_timeout_es_transitorio():
    err = Exception("Connection timeout occurred")
    assert classify_api_error(err) == "transient"


def test_error_desconocido_retorna_unknown():
    err = Exception("Something completely unexpected happened")
    assert classify_api_error(err) == "unknown"


def test_error_con___cause___transitorio():
    cause = Exception("Connection reset by peer")
    err = Exception("Request failed")
    err.__cause__ = cause
    assert classify_api_error(err) == "transient"


def test_permanente_tiene_prioridad_sobre_transitorio():
    # Mensaje que contiene patrones de ambos tipos: 401 (permanent) y 503 (transient)
    err = Exception("401 503 mixed error")
    assert classify_api_error(err) == "permanent"


def test_error_403_forbidden_es_permanente():
    err = Exception("403 Forbidden")
    assert classify_api_error(err) == "permanent"


def test_error_500_es_transitorio():
    err = Exception("Internal server error 500")
    assert classify_api_error(err) == "transient"
