Spaces:
Running
Running
Download tests/providers/test_discovery_errors.py from dherain24/claude-proxy: direct link, hf CLI and curl.
- Browser
- Download file 5.66 kB
-
https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/providers/test_discovery_errors.py
- Command line
-
hf download hf://spaces/dherain24/claude-proxy/tests/providers/test_discovery_errors.py
-
curl -L -o test_discovery_errors.py https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/providers/test_discovery_errors.py
5.66 kB
| """Shareable discovery errors retain transport causes without upstream secrets.""" | |
| import socket | |
| import ssl | |
| from unittest.mock import AsyncMock | |
| import anyio | |
| import anyio.streams.tls | |
| import httpx | |
| import httpx2 | |
| import openai | |
| import pytest | |
| from free_claude_code.application.errors import ApplicationUnavailableError | |
| from free_claude_code.core.failures import ExecutionFailure, FailureKind | |
| from free_claude_code.providers.admission import ( | |
| ProviderAdmissionController, | |
| ProviderOperationKind, | |
| ) | |
| from free_claude_code.providers.model_listing import ModelListResponseError | |
| from free_claude_code.providers.recovery_errors import ProviderRecoveryExhausted | |
| from free_claude_code.providers.runtime.discovery import _provider_query_failure_reason | |
| def test_wrapped_discovery_errors_are_specific_and_secret_safe(cause, expected): | |
| error = openai.APIConnectionError( | |
| message="secret-canary", request=httpx2.Request("GET", "https://example.test") | |
| ) | |
| error.__cause__ = cause | |
| message = _provider_query_failure_reason(error) | |
| assert expected in message | |
| assert "secret-canary" not in message | |
| def test_sdk_status_error_preserves_status_without_body(): | |
| response = httpx2.Response( | |
| 401, request=httpx2.Request("GET", "https://example.test") | |
| ) | |
| error = openai.APIStatusError( | |
| "secret-canary", response=response, body={"secret": "secret-canary"} | |
| ) | |
| assert ( | |
| _provider_query_failure_reason(error) | |
| == "The model-list request returned HTTP 401." | |
| ) | |
| async def test_sdk_connection_pool_preserves_os_failure_diagnostics( | |
| monkeypatch, failure, expected | |
| ): | |
| stream = AsyncMock(spec=anyio.abc.ByteStream) | |
| connect = AsyncMock(return_value=stream) | |
| if failure == "dns": | |
| connect.side_effect = socket.gaierror(-2, "secret-canary") | |
| monkeypatch.setattr(anyio, "connect_tcp", connect) | |
| monkeypatch.setattr( | |
| anyio.streams.tls.TLSStream, | |
| "wrap", | |
| AsyncMock(side_effect=ssl.SSLCertVerificationError(1, "secret-canary")), | |
| ) | |
| async with openai.AsyncOpenAI( | |
| api_key="secret-canary", | |
| base_url="https://provider.invalid/v1", | |
| max_retries=0, | |
| http_client=httpx2.AsyncClient(trust_env=False), | |
| ) as client: | |
| with pytest.raises(openai.APIConnectionError) as caught: | |
| await client.models.list() | |
| message = _provider_query_failure_reason(caught.value) | |
| assert expected in message | |
| assert "secret-canary" not in message | |
| def test_http_status_is_preserved_without_url_body_or_exception_message(client): | |
| request = client.Request("GET", "https://user:secret-canary@example.test/models") | |
| response = client.Response(403, request=request, text="secret-canary") | |
| error = client.HTTPStatusError("secret-canary", request=request, response=response) | |
| message = _provider_query_failure_reason(error) | |
| assert "HTTP 403" in message | |
| assert "secret-canary" not in message | |
| def test_exception_context_and_cycles_are_safe(): | |
| error = RuntimeError("secret-canary") | |
| cause = socket.gaierror(-2, "secret-canary") | |
| error.__context__ = cause | |
| cause.__context__ = error | |
| assert "DNS" in _provider_query_failure_reason(error) | |
| error.__suppress_context__ = True | |
| message = _provider_query_failure_reason(error) | |
| assert "DNS" in message | |
| assert "secret-canary" not in message | |
| async def test_real_admission_cooldown_retains_discovery_failure(error): | |
| controller = ProviderAdmissionController( | |
| provider_name="TEST", | |
| max_attempts=1, | |
| base_delay=30, | |
| max_delay=30, | |
| jitter=0, | |
| ) | |
| fetch = AsyncMock(side_effect=error) | |
| with pytest.raises(type(error)): | |
| await controller.start_execution().run_call( | |
| fetch, operation_kind=ProviderOperationKind.MODEL_DISCOVERY | |
| ) | |
| with pytest.raises(ProviderRecoveryExhausted) as cooldown: | |
| await controller.start_execution().run_call( | |
| fetch, operation_kind=ProviderOperationKind.MODEL_DISCOVERY | |
| ) | |
| fetch.assert_awaited_once() | |
| assert cooldown.value.__cause__ is None | |
| assert cooldown.value.__context__ is None | |
| assert _provider_query_failure_reason( | |
| cooldown.value | |
| ) == _provider_query_failure_reason(error) | |
| def test_recovery_wrapper_cycles_do_not_hang_classification(): | |
| error = RuntimeError("unclassified failure") | |
| wrapper = ProviderRecoveryExhausted(error) | |
| error.__cause__ = wrapper | |
| assert ( | |
| _provider_query_failure_reason(wrapper) | |
| == "Could not load models. Check the provider's settings and retry." | |
| ) | |