Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions tests/test_run_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,9 @@ def test_prompt_caching_claude_openrouter(self):
patch("run_agent.OpenAI"),
):
a = AIAgent(
api_key="test-key-1234567890",
api_key="test-k...7890",
model="anthropic/claude-sonnet-4-20250514",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
Expand Down Expand Up @@ -478,7 +479,7 @@ def test_valid_tool_names_populated(self):
patch("run_agent.OpenAI"),
):
a = AIAgent(
api_key="test-key-1234567890",
api_key="***",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
Expand Down Expand Up @@ -792,20 +793,25 @@ def test_basic_kwargs(self, agent):
assert kwargs["timeout"] == 1800.0

def test_provider_preferences_injected(self, agent):
agent.base_url = "https://openrouter.ai/api/v1"
agent.providers_allowed = ["Anthropic"]
messages = [{"role": "user", "content": "hi"}]
kwargs = agent._build_api_kwargs(messages)
assert kwargs["extra_body"]["provider"]["only"] == ["Anthropic"]

def test_reasoning_config_default_openrouter(self, agent):
"""Default reasoning config for OpenRouter should be medium."""
agent.base_url = "https://openrouter.ai/api/v1"
agent.model = "qwen/qwen3.5-plus-02-15"
messages = [{"role": "user", "content": "hi"}]
kwargs = agent._build_api_kwargs(messages)
reasoning = kwargs["extra_body"]["reasoning"]
assert reasoning["enabled"] is True
assert reasoning["effort"] == "medium"

def test_reasoning_config_custom(self, agent):
agent.base_url = "https://openrouter.ai/api/v1"
agent.model = "qwen/qwen3.5-plus-02-15"
agent.reasoning_config = {"enabled": False}
messages = [{"role": "user", "content": "hi"}]
kwargs = agent._build_api_kwargs(messages)
Expand All @@ -818,6 +824,7 @@ def test_reasoning_not_sent_for_unsupported_openrouter_model(self, agent):
assert "reasoning" not in kwargs.get("extra_body", {})

def test_reasoning_sent_for_supported_openrouter_model(self, agent):
agent.base_url = "https://openrouter.ai/api/v1"
agent.model = "qwen/qwen3.5-plus-02-15"
messages = [{"role": "user", "content": "hi"}]
kwargs = agent._build_api_kwargs(messages)
Expand Down Expand Up @@ -3156,9 +3163,14 @@ def test_stream_kwarg_injected(self, agent):
def test_api_exception_falls_back_to_non_streaming(self, agent):
"""When streaming fails before any deltas, fallback to non-streaming is attempted."""
agent.client.chat.completions.create.side_effect = ConnectionError("fail")
# The fallback also uses the same client, so it'll fail too
with pytest.raises(ConnectionError, match="fail"):
agent._interruptible_streaming_api_call({"messages": []})
# Request-scoped clients are now created through _create_request_openai_client.
# Patch it so both the streaming path and the non-streaming fallback use the
# same mocked client and raise the expected ConnectionError.
with (
patch.object(agent, "_create_request_openai_client", return_value=agent.client),
pytest.raises(ConnectionError, match="fail"),
):
agent._interruptible_streaming_api_call({"messages": [], "model": agent.model})

def test_response_has_uuid_id(self, agent):
chunks = [_make_chunk(content="x"), _make_chunk(finish_reason="stop")]
Expand Down
Loading