Log Brave search responses

This commit is contained in:
2026-05-19 21:49:31 +02:00
parent 5ed1104c12
commit cc783ab8ed
2 changed files with 104 additions and 1 deletions

View File

@@ -105,6 +105,62 @@ class WebSearchTests(unittest.IsolatedAsyncioTestCase):
assert seen_headers is not None
self.assertEqual(seen_headers["x-subscription-token"], "test-key")
async def test_search_logs_success_response_summary(self) -> None:
async def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
request=request,
json={
"query": {"original": "python"},
"web": {
"results": [
{
"title": "Python",
"url": "https://www.python.org",
}
]
},
},
)
client = BraveWebSearchClient(
AIWebSearchConfig(api_key="test-key"),
transport=httpx.MockTransport(handler),
)
with self.assertLogs("app.ai.web_search", level="INFO") as captured:
await client.search({"query": "python"})
logs = "\n".join(captured.output)
self.assertIn("Brave web search response status=200", logs)
self.assertIn("results=1", logs)
async def test_search_logs_error_response_body(self) -> None:
async def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
422,
request=request,
json={
"error": {
"code": "VALIDATION",
"detail": "Unable to validate request parameter(s)",
}
},
)
client = BraveWebSearchClient(
AIWebSearchConfig(api_key="test-key"),
transport=httpx.MockTransport(handler),
)
with self.assertLogs("app.ai.web_search", level="WARNING") as captured:
with self.assertRaisesRegex(ValueError, "Unable to validate"):
await client.search({"query": "python"})
logs = "\n".join(captured.output)
self.assertIn("Brave web search error status=422", logs)
self.assertIn("VALIDATION", logs)
async def test_search_rejects_missing_subscription_token_before_request(self) -> None:
async def handler(request: httpx.Request) -> httpx.Response:
self.fail("request should not be sent without a subscription token")