"""Tests for the error class hierarchy."""

from __future__ import annotations

from licensesos.errors import ApiError, LicensesOSError, NetworkError


class TestLicensesOSError:
    def test_is_instance_of_exception(self) -> None:
        err = LicensesOSError("test")
        assert isinstance(err, Exception)
        assert isinstance(err, LicensesOSError)

    def test_has_correct_message(self) -> None:
        err = LicensesOSError("something broke")
        assert str(err) == "something broke"


class TestApiError:
    def test_extends_licensesos_error(self) -> None:
        err = ApiError("License not found", "LICENSE_NOT_FOUND", 404)
        assert isinstance(err, LicensesOSError)
        assert isinstance(err, ApiError)
        assert isinstance(err, Exception)

    def test_exposes_code_and_status_code(self) -> None:
        err = ApiError("Rate limited", "RATE_LIMITED", 429)
        assert str(err) == "Rate limited"
        assert err.code == "RATE_LIMITED"
        assert err.status_code == 429

    def test_repr(self) -> None:
        err = ApiError("Not found", "NOT_FOUND", 404)
        r = repr(err)
        assert "NOT_FOUND" in r
        assert "404" in r


class TestNetworkError:
    def test_extends_licensesos_error(self) -> None:
        err = NetworkError("timeout")
        assert isinstance(err, LicensesOSError)
        assert isinstance(err, NetworkError)
        assert isinstance(err, Exception)

    def test_stores_the_cause(self) -> None:
        cause = ConnectionError("ECONNREFUSED")
        err = NetworkError("connection failed", cause)
        assert str(err) == "connection failed"
        assert err.cause is cause

    def test_works_without_a_cause(self) -> None:
        err = NetworkError("timeout")
        assert err.cause is None
