|
8 | 8 | import pytest |
9 | 9 | from fastapi import Request, Response |
10 | 10 | from fastapi.testclient import TestClient |
11 | | -from pytest_mock import MockerFixture |
| 11 | +from llama_stack_api.openai_responses import OpenAIResponseObject |
| 12 | +from llama_stack_client.types import VersionInfo |
| 13 | +from pydantic_ai.messages import ( |
| 14 | + ModelMessage, |
| 15 | + ModelRequest, |
| 16 | + ModelResponse, |
| 17 | + NativeToolCallPart, |
| 18 | + NativeToolReturnPart, |
| 19 | + TextPart, |
| 20 | + ToolCallPart, |
| 21 | + ToolReturnPart, |
| 22 | +) |
| 23 | +from pydantic_ai.native_tools import FileSearchTool, MCPServerTool |
| 24 | +from pydantic_ai.run import AgentRunResult |
| 25 | +from pydantic_ai.usage import RunUsage |
| 26 | +from pytest_mock import AsyncMockType, MockerFixture |
12 | 27 | from sqlalchemy import create_engine |
13 | 28 | from sqlalchemy.engine import Engine |
14 | 29 | from sqlalchemy.orm import Session, sessionmaker |
@@ -70,9 +85,6 @@ def create_mock_llm_response( # pylint: disable=too-many-arguments,too-many-pos |
70 | 85 | Returns: |
71 | 86 | Mock LLM response object with the specified configuration. |
72 | 87 | """ |
73 | | - # pylint: disable=import-outside-toplevel |
74 | | - from llama_stack_api.openai_responses import OpenAIResponseObject |
75 | | - |
76 | 88 | mock_response = mocker.MagicMock(spec=OpenAIResponseObject) |
77 | 89 | mock_response.id = "response-123" |
78 | 90 |
|
@@ -154,6 +166,187 @@ def create_mock_tool_call( |
154 | 166 | return mock_tool_call |
155 | 167 |
|
156 | 168 |
|
| 169 | +def create_agent_run_result( # pylint: disable=too-many-arguments,too-many-positional-arguments |
| 170 | + mocker: MockerFixture, |
| 171 | + *, |
| 172 | + content: str = "This is a test response about Ansible.", |
| 173 | + response_id: str = "response-123", |
| 174 | + input_tokens: int = 10, |
| 175 | + output_tokens: int = 5, |
| 176 | + model_response: ModelResponse | None = None, |
| 177 | + new_messages: list[ModelMessage] | None = None, |
| 178 | +) -> AgentRunResult[str]: |
| 179 | + """Create a mock AgentRunResult wired for retrieve_agent_response. |
| 180 | +
|
| 181 | + Uses real pydantic-ai message types so build_turn_summary_from_agent_run |
| 182 | + exercises the same path as production agent runs. |
| 183 | +
|
| 184 | + Args: |
| 185 | + mocker: pytest-mock fixture. |
| 186 | + content: Assistant text content for the run. |
| 187 | + response_id: Provider response identifier. |
| 188 | + input_tokens: Input token count for the run. |
| 189 | + output_tokens: Output token count for the run. |
| 190 | + model_response: Optional pre-built ModelResponse. |
| 191 | + new_messages: Optional message sequence returned by new_messages(). |
| 192 | +
|
| 193 | + Returns: |
| 194 | + Mock AgentRunResult compatible with build_turn_summary_from_agent_run. |
| 195 | + """ |
| 196 | + if model_response is None: |
| 197 | + parts = [TextPart(content)] if content else [] |
| 198 | + model_response = ModelResponse( |
| 199 | + parts=parts, |
| 200 | + finish_reason="stop", |
| 201 | + provider_response_id=response_id, |
| 202 | + ) |
| 203 | + |
| 204 | + messages = new_messages if new_messages is not None else [model_response] |
| 205 | + run_result = mocker.MagicMock(spec=AgentRunResult) |
| 206 | + run_result.response = model_response |
| 207 | + run_result.usage = RunUsage( |
| 208 | + input_tokens=input_tokens, |
| 209 | + output_tokens=output_tokens, |
| 210 | + requests=1, |
| 211 | + ) |
| 212 | + run_result.new_messages.return_value = messages |
| 213 | + return run_result |
| 214 | + |
| 215 | + |
| 216 | +def create_file_search_agent_run_result( # pylint: disable=too-many-arguments,too-many-positional-arguments |
| 217 | + mocker: MockerFixture, |
| 218 | + *, |
| 219 | + content: str, |
| 220 | + response_id: str = "response-tool-rag", |
| 221 | + queries: Optional[list[str]] = None, |
| 222 | + results: Optional[list[dict[str, Any]]] = None, |
| 223 | + input_tokens: int = 10, |
| 224 | + output_tokens: int = 5, |
| 225 | +) -> AgentRunResult[str]: |
| 226 | + """Create an AgentRunResult containing a native file_search tool call.""" |
| 227 | + call = NativeToolCallPart( |
| 228 | + tool_name=FileSearchTool.kind, |
| 229 | + args={"queries": queries or ["test query"]}, |
| 230 | + tool_call_id="call-fs-1", |
| 231 | + ) |
| 232 | + return_part = NativeToolReturnPart( |
| 233 | + tool_name=FileSearchTool.kind, |
| 234 | + tool_call_id="call-fs-1", |
| 235 | + content={ |
| 236 | + "status": "success", |
| 237 | + "results": results or [], |
| 238 | + }, |
| 239 | + ) |
| 240 | + model_response = ModelResponse( |
| 241 | + parts=[call, return_part, TextPart(content)], |
| 242 | + finish_reason="stop", |
| 243 | + provider_response_id=response_id, |
| 244 | + ) |
| 245 | + return create_agent_run_result( |
| 246 | + mocker, |
| 247 | + content=content, |
| 248 | + response_id=response_id, |
| 249 | + input_tokens=input_tokens, |
| 250 | + output_tokens=output_tokens, |
| 251 | + model_response=model_response, |
| 252 | + ) |
| 253 | + |
| 254 | + |
| 255 | +def create_mcp_list_tools_agent_run_result( # pylint: disable=too-many-arguments,too-many-positional-arguments |
| 256 | + mocker: MockerFixture, |
| 257 | + *, |
| 258 | + content: str, |
| 259 | + response_id: str = "response-mcplist", |
| 260 | + server_label: str = "kubernetes-server", |
| 261 | + tools: Optional[list[dict[str, Any]]] = None, |
| 262 | + input_tokens: int = 15, |
| 263 | + output_tokens: int = 20, |
| 264 | +) -> AgentRunResult[str]: |
| 265 | + """Create an AgentRunResult containing an MCP list-tools native tool call.""" |
| 266 | + call = NativeToolCallPart( |
| 267 | + tool_name=f"{MCPServerTool.kind}:{server_label}", |
| 268 | + args={"action": "list_tools"}, |
| 269 | + tool_call_id="mcplist-101", |
| 270 | + ) |
| 271 | + return_part = NativeToolReturnPart( |
| 272 | + tool_name=f"{MCPServerTool.kind}:{server_label}", |
| 273 | + tool_call_id="mcplist-101", |
| 274 | + content={"tools": tools or []}, |
| 275 | + ) |
| 276 | + model_response = ModelResponse( |
| 277 | + parts=[call, return_part, TextPart(content)], |
| 278 | + finish_reason="stop", |
| 279 | + provider_response_id=response_id, |
| 280 | + ) |
| 281 | + return create_agent_run_result( |
| 282 | + mocker, |
| 283 | + content=content, |
| 284 | + response_id=response_id, |
| 285 | + input_tokens=input_tokens, |
| 286 | + output_tokens=output_tokens, |
| 287 | + model_response=model_response, |
| 288 | + ) |
| 289 | + |
| 290 | + |
| 291 | +def create_multi_tool_agent_run_result( |
| 292 | + mocker: MockerFixture, |
| 293 | + *, |
| 294 | + content: str = "Based on documentation and calculations...", |
| 295 | + response_id: str = "response-multi", |
| 296 | + input_tokens: int = 40, |
| 297 | + output_tokens: int = 60, |
| 298 | +) -> AgentRunResult[str]: |
| 299 | + """Create an AgentRunResult with file_search and function tool calls.""" |
| 300 | + file_search_call = NativeToolCallPart( |
| 301 | + tool_name=FileSearchTool.kind, |
| 302 | + args={"queries": ["Kubernetes deployment"]}, |
| 303 | + tool_call_id="search-1", |
| 304 | + ) |
| 305 | + file_search_return = NativeToolReturnPart( |
| 306 | + tool_name=FileSearchTool.kind, |
| 307 | + tool_call_id="search-1", |
| 308 | + content={"status": "success", "results": []}, |
| 309 | + ) |
| 310 | + function_call = ToolCallPart( |
| 311 | + tool_name="calculate", |
| 312 | + args={"operation": "sum"}, |
| 313 | + tool_call_id="func-2", |
| 314 | + ) |
| 315 | + function_return = ToolReturnPart( |
| 316 | + tool_name="calculate", |
| 317 | + content={"result": 2}, |
| 318 | + tool_call_id="func-2", |
| 319 | + ) |
| 320 | + model_response = ModelResponse( |
| 321 | + parts=[ |
| 322 | + file_search_call, |
| 323 | + file_search_return, |
| 324 | + function_call, |
| 325 | + TextPart(content), |
| 326 | + ], |
| 327 | + finish_reason="stop", |
| 328 | + provider_response_id=response_id, |
| 329 | + ) |
| 330 | + return create_agent_run_result( |
| 331 | + mocker, |
| 332 | + content=content, |
| 333 | + response_id=response_id, |
| 334 | + input_tokens=input_tokens, |
| 335 | + output_tokens=output_tokens, |
| 336 | + model_response=model_response, |
| 337 | + new_messages=[model_response, ModelRequest(parts=[function_return])], |
| 338 | + ) |
| 339 | + |
| 340 | + |
| 341 | +def set_query_agent_run( |
| 342 | + mock_query_agent: AsyncMockType, |
| 343 | + mocker: MockerFixture, |
| 344 | + **kwargs: Any, |
| 345 | +) -> None: |
| 346 | + """Configure mock agent.run return value for /query integration tests.""" |
| 347 | + mock_query_agent.run.return_value = create_agent_run_result(mocker, **kwargs) |
| 348 | + |
| 349 | + |
157 | 350 | # ========================================== |
158 | 351 | # Fixtures |
159 | 352 | # ========================================== |
@@ -448,10 +641,6 @@ def mock_llama_stack_client_fixture( |
448 | 641 | Yields: |
449 | 642 | mock_client: The mocked Llama Stack client instance. |
450 | 643 | """ |
451 | | - # pylint: disable=import-outside-toplevel |
452 | | - from llama_stack_api.openai_responses import OpenAIResponseObject |
453 | | - from llama_stack_client.types import VersionInfo |
454 | | - |
455 | 644 | # Patch AsyncLlamaStackClientHolder at multiple import locations |
456 | 645 | # This ensures the mock is active both during app startup (app.main) |
457 | 646 | # and during endpoint execution (query, conversations_v1, responses, etc.) |
@@ -514,3 +703,15 @@ def mock_llama_stack_client_fixture( |
514 | 703 | mock_holder_instance.get_client.return_value = mock_client |
515 | 704 |
|
516 | 705 | yield mock_client |
| 706 | + |
| 707 | + |
| 708 | +@pytest.fixture(name="mock_query_agent") |
| 709 | +def mock_query_agent_fixture(mocker: MockerFixture) -> Any: |
| 710 | + """Patch build_agent for /query and return the mock agent.""" |
| 711 | + mock_agent = mocker.AsyncMock() |
| 712 | + mock_agent.run = mocker.AsyncMock(return_value=create_agent_run_result(mocker)) |
| 713 | + mock_agent.build_agent_mock = mocker.patch( |
| 714 | + "utils.agents.query.build_agent", |
| 715 | + return_value=mock_agent, |
| 716 | + ) |
| 717 | + return mock_agent |
0 commit comments