|
6 | 6 | import time |
7 | 7 | from pathlib import Path |
8 | 8 |
|
| 9 | +import httpx |
9 | 10 | import pytest |
10 | 11 | import uvicorn |
11 | 12 | from fastapi import FastAPI, Request |
@@ -178,3 +179,41 @@ def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, c |
178 | 179 | assert '"RUN_ERROR"' not in response.text |
179 | 180 | assert "hello" in response.text |
180 | 181 | assert seen == [expected] |
| 182 | + |
| 183 | + |
| 184 | +def test_an_anthropic_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch): |
| 185 | + seen = [] |
| 186 | + provider_seen = [] |
| 187 | + provider_app = _provider_app(provider_seen) |
| 188 | + |
| 189 | + async def respond(transport, request): |
| 190 | + seen.append( |
| 191 | + (request.url.scheme, request.url.host, request.url.path, request.headers.get("x-api-key")) |
| 192 | + ) |
| 193 | + async with httpx.ASGITransport(app=provider_app) as local_provider: |
| 194 | + return await local_provider.handle_async_request(request) |
| 195 | + |
| 196 | + # Keep the real framework and Anthropic clients; replace only the network transport. |
| 197 | + monkeypatch.setattr(httpx.AsyncHTTPTransport, "handle_async_request", respond) |
| 198 | + monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) |
| 199 | + monkeypatch.setenv("BOT_PROVIDER", "anthropic") |
| 200 | + monkeypatch.setenv("BOT_MODEL", "claude-sonnet-4-5") |
| 201 | + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key") |
| 202 | + monkeypatch.setenv("ANTHROPIC_BASE_URL", "") |
| 203 | + monkeypatch.setenv("OPENAI_API_KEY", "") |
| 204 | + monkeypatch.setenv("OPENAI_BASE_URL", "") |
| 205 | + monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False) |
| 206 | + |
| 207 | + from src import main |
| 208 | + |
| 209 | + main = importlib.reload(main) |
| 210 | + response = TestClient(main.app).post( |
| 211 | + "/", json=RUN, headers={"x-openbot-agent-token": TOKEN} |
| 212 | + ) |
| 213 | + |
| 214 | + assert seen == [("https", "api.anthropic.com", "/v1/messages", "test-key")] |
| 215 | + assert provider_seen == [("anthropic", "claude-sonnet-4-5")] |
| 216 | + assert response.status_code == 200 |
| 217 | + assert '"RUN_FINISHED"' in response.text |
| 218 | + assert '"RUN_ERROR"' not in response.text |
| 219 | + assert "hello" in response.text |
0 commit comments