|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
| 7 | +from agentkit.core.errors import AuthenticationError, LLMProviderError |
7 | 8 | from agentkit.core.types import Role |
8 | 9 | from agentkit.llm.base import Message |
9 | 10 | from agentkit.llm.gemini import GeminiClient |
@@ -52,6 +53,39 @@ async def test_gemini_text_chat_translation(mock_genai_client: MagicMock) -> Non |
52 | 53 | assert mock_genai_client.aio.models.generate_content.called |
53 | 54 |
|
54 | 55 |
|
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_gemini_classifies_structured_invalid_key_reason( |
| 58 | + mock_genai_client: MagicMock, |
| 59 | +) -> None: |
| 60 | + """Authentication classification reads provider error metadata, not display text.""" |
| 61 | + error = RuntimeError("request rejected") |
| 62 | + error.code = 400 # type: ignore[attr-defined] |
| 63 | + error.status_code = None # type: ignore[attr-defined] |
| 64 | + error.status = "INVALID_ARGUMENT" # type: ignore[attr-defined] |
| 65 | + error.details = {"error": {"reason": "API_KEY_INVALID"}} # type: ignore[attr-defined] |
| 66 | + mock_genai_client.aio.models.generate_content.side_effect = error |
| 67 | + adapter = GeminiClient(api_key="fake-key", client=mock_genai_client) |
| 68 | + |
| 69 | + with pytest.raises(AuthenticationError): |
| 70 | + await adapter.chat([Message.user("hello")]) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_gemini_status_code_falls_back_when_code_is_none( |
| 75 | + mock_genai_client: MagicMock, |
| 76 | +) -> None: |
| 77 | + """A present-but-empty code does not hide the alternate status_code field.""" |
| 78 | + error = RuntimeError("provider unavailable") |
| 79 | + error.code = None # type: ignore[attr-defined] |
| 80 | + error.status_code = 503 # type: ignore[attr-defined] |
| 81 | + mock_genai_client.aio.models.generate_content.side_effect = error |
| 82 | + adapter = GeminiClient(api_key="fake-key", client=mock_genai_client) |
| 83 | + |
| 84 | + with pytest.raises(LLMProviderError) as raised: |
| 85 | + await adapter.chat([Message.user("hello")]) |
| 86 | + assert raised.value.status_code == 503 |
| 87 | + |
| 88 | + |
55 | 89 | @pytest.mark.asyncio |
56 | 90 | async def test_gemini_tool_call_translation(mock_genai_client: MagicMock) -> None: |
57 | 91 | """Verify function calling translation and tool call extraction.""" |
|
0 commit comments