|
6 | 6 | import time |
7 | 7 | from pathlib import Path |
8 | 8 |
|
| 9 | +import httpx2 |
9 | 10 | import pytest |
10 | 11 | import uvicorn |
11 | 12 | from fastapi import FastAPI, Request |
@@ -182,3 +183,49 @@ def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, c |
182 | 183 | assert '"RUN_FINISHED"' in response.text |
183 | 184 | assert '"RUN_ERROR"' not in response.text |
184 | 185 | assert seen == [expected] |
| 186 | + |
| 187 | + |
| 188 | +def test_an_anthropic_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch): |
| 189 | + seen = [] |
| 190 | + |
| 191 | + async def respond(transport, request): |
| 192 | + body = json.loads(request.content) |
| 193 | + seen.append((str(request.url), request.headers.get("x-api-key"), body["model"])) |
| 194 | + return httpx2.Response( |
| 195 | + 200, |
| 196 | + request=request, |
| 197 | + json={ |
| 198 | + "id": "msg", |
| 199 | + "type": "message", |
| 200 | + "role": "assistant", |
| 201 | + "model": body["model"], |
| 202 | + "content": [{"type": "text", "text": "hello"}], |
| 203 | + "stop_reason": "end_turn", |
| 204 | + "stop_sequence": None, |
| 205 | + "usage": {"input_tokens": 1, "output_tokens": 1}, |
| 206 | + }, |
| 207 | + ) |
| 208 | + |
| 209 | + # Keep the real AG2 and Anthropic clients; replace only the network transport. |
| 210 | + monkeypatch.setattr(httpx2.AsyncHTTPTransport, "handle_async_request", respond) |
| 211 | + monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) |
| 212 | + monkeypatch.setenv("BOT_PROVIDER", "anthropic") |
| 213 | + monkeypatch.setenv("BOT_MODEL", "claude-sonnet-4-5") |
| 214 | + monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key") |
| 215 | + monkeypatch.setenv("ANTHROPIC_BASE_URL", "") |
| 216 | + monkeypatch.setenv("OPENAI_API_KEY", "") |
| 217 | + monkeypatch.setenv("OPENAI_BASE_URL", "") |
| 218 | + monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False) |
| 219 | + |
| 220 | + from src import main |
| 221 | + |
| 222 | + main = importlib.reload(main) |
| 223 | + response = TestClient(main.app).post( |
| 224 | + "/", json=RUN, headers={"x-openbot-agent-token": TOKEN} |
| 225 | + ) |
| 226 | + |
| 227 | + assert seen == [("https://api.anthropic.com/v1/messages", "test-key", "claude-sonnet-4-5")] |
| 228 | + assert response.status_code == 200 |
| 229 | + assert '"RUN_FINISHED"' in response.text |
| 230 | + assert '"RUN_ERROR"' not in response.text |
| 231 | + assert "hello" in response.text |
0 commit comments