Log Brave search responses
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user