|
1 | 1 | """Tests for provider-agnostic LLM auth-error detection and typing.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +from functools import partial |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
6 | 7 | import botocore.exceptions |
7 | 8 | import httpx |
8 | 9 | import openai |
9 | 10 | import pytest |
| 11 | +from langchain_openai import ChatOpenAI |
10 | 12 |
|
11 | 13 | from agents.llm_config import CODEBOARDING_KEY_TAIL, OIDC_RELAY_PLACEHOLDER_KEY, current_provider_key_context |
12 | 14 | from agents.llm_errors import ( |
|
18 | 20 | is_transient_llm_error, |
19 | 21 | llm_failure_properties, |
20 | 22 | raise_if_auth_error, |
| 23 | + raise_if_codeboarding_quota_spent, |
21 | 24 | retry_after_seconds, |
22 | 25 | ) |
23 | 26 |
|
@@ -388,3 +391,59 @@ def test_reads_retry_after_http_date(self): |
388 | 391 | def test_no_retry_after_when_absent_unreadable_or_past(self, error: Exception): |
389 | 392 | with patch("agents.llm_errors.time.time", return_value=1792567660.0): |
390 | 393 | assert retry_after_seconds(error) is None |
| 394 | + |
| 395 | + |
| 396 | +_HOSTED_ENV = {"OPENROUTER_API_KEY": OIDC_RELAY_PLACEHOLDER_KEY, "OPENROUTER_BASE_URL": "http://127.0.0.1:4000/v1"} |
| 397 | +_SPENT_QUOTA = httpx.Response( |
| 398 | + 402, json={"error": {"type": "gha_proxy", "message": "Resource exhausted: token limit reached"}} |
| 399 | +) |
| 400 | + |
| 401 | + |
| 402 | +def _check_quota(env: dict[str, str], answer: httpx.Response | Exception, requests: list[httpx.Request]) -> None: |
| 403 | + def proxy(request: httpx.Request) -> httpx.Response: |
| 404 | + requests.append(request) |
| 405 | + if isinstance(answer, Exception): |
| 406 | + raise answer |
| 407 | + return answer |
| 408 | + |
| 409 | + with ( |
| 410 | + httpx.Client(transport=httpx.MockTransport(proxy)) as client, |
| 411 | + patch.dict(os.environ, env, clear=True), |
| 412 | + patch("agents.llm_errors.ChatOpenAI", partial(ChatOpenAI, http_client=client)), |
| 413 | + ): |
| 414 | + raise_if_codeboarding_quota_spent() |
| 415 | + |
| 416 | + |
| 417 | +class TestQuotaCheckBeforeTheRun: |
| 418 | + def test_a_spent_quota_raises_the_runtime_error(self): |
| 419 | + requests: list[httpx.Request] = [] |
| 420 | + with pytest.raises(LLMQuotaError) as caught: |
| 421 | + _check_quota(_HOSTED_ENV, _SPENT_QUOTA, requests) |
| 422 | + |
| 423 | + assert caught.value.is_codeboarding_quota |
| 424 | + assert "https://codeboarding.org/pricing" in str(caught.value) |
| 425 | + assert [(request.method, str(request.url)) for request in requests] == [ |
| 426 | + ("GET", "http://127.0.0.1:4000/v1/models") |
| 427 | + ] |
| 428 | + assert requests[0].headers["authorization"] == f"Bearer {OIDC_RELAY_PLACEHOLDER_KEY}" |
| 429 | + |
| 430 | + @pytest.mark.parametrize( |
| 431 | + "answer", |
| 432 | + [ |
| 433 | + httpx.Response(200, json={"data": []}), |
| 434 | + # OpenRouter itself out of credits: a CodeBoarding service issue, not the user's quota. |
| 435 | + httpx.Response(402, json={"error": {"code": 402, "message": "Insufficient credits"}}), |
| 436 | + httpx.Response(500, json={"error": {"type": "gha_proxy", "message": "Upstream unavailable."}}), |
| 437 | + httpx.ConnectError("relay is down"), |
| 438 | + httpx.ReadTimeout("proxy is slow"), |
| 439 | + ], |
| 440 | + ) |
| 441 | + def test_any_other_answer_lets_the_run_go_on(self, answer: httpx.Response | Exception): |
| 442 | + requests: list[httpx.Request] = [] |
| 443 | + _check_quota(_HOSTED_ENV, answer, requests) |
| 444 | + assert len(requests) == 1 |
| 445 | + |
| 446 | + def test_an_own_key_is_not_checked(self): |
| 447 | + requests: list[httpx.Request] = [] |
| 448 | + _check_quota({"OPENROUTER_API_KEY": "sk-or-own-1234"}, _SPENT_QUOTA, requests) |
| 449 | + assert requests == [] |
0 commit comments