"""Tests for AgentIdPError."""

from sentryagent_idp.errors import AgentIdPError


def test_basic_construction() -> None:
    err = AgentIdPError("AgentNotFoundError", "Agent not found.", 404)
    assert err.code == "AgentNotFoundError"
    assert err.http_status == 404
    assert str(err) == "Agent not found."
    assert err.details is None


def test_from_api_error_valid_body() -> None:
    body = {"code": "AgentNotFoundError", "message": "Not found.", "details": {"id": "x"}}
    err = AgentIdPError.from_api_error(body, 404)
    assert err.code == "AgentNotFoundError"
    assert err.http_status == 404
    assert err.details == {"id": "x"}


def test_from_api_error_unknown_body() -> None:
    err = AgentIdPError.from_api_error("plain string", 500)
    assert err.code == "UNKNOWN_ERROR"
    assert err.http_status == 500


def test_from_oauth2_error() -> None:
    body = {"error": "invalid_client", "error_description": "Bad credentials."}
    err = AgentIdPError.from_oauth2_error(body, 401)
    assert err.code == "invalid_client"
    assert str(err) == "Bad credentials."
    assert err.http_status == 401


def test_from_oauth2_error_unknown() -> None:
    err = AgentIdPError.from_oauth2_error("garbage", 400)
    assert err.code == "unknown_error"


def test_network_error() -> None:
    cause = ConnectionError("refused")
    err = AgentIdPError.network_error(cause)
    assert err.code == "NETWORK_ERROR"
    assert err.http_status == 0
    assert "refused" in str(err)


def test_repr() -> None:
    err = AgentIdPError("CODE", "msg", 400)
    assert "AgentIdPError" in repr(err)
    assert "CODE" in repr(err)
