|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +from contextlib import AsyncExitStack |
| 5 | +from unittest.mock import AsyncMock, MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | +import pytest_asyncio |
| 9 | + |
| 10 | +from vllm.entrypoints.context import ConversationContext |
| 11 | +from vllm.entrypoints.openai.protocol import ResponsesRequest |
| 12 | +from vllm.entrypoints.openai.serving_responses import OpenAIServingResponses |
| 13 | +from vllm.entrypoints.tool_server import ToolServer |
| 14 | + |
| 15 | + |
| 16 | +class MockConversationContext(ConversationContext): |
| 17 | + """Mock conversation context for testing""" |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + self.init_tool_sessions_called = False |
| 21 | + self.init_tool_sessions_args = None |
| 22 | + self.init_tool_sessions_kwargs = None |
| 23 | + |
| 24 | + def append_output(self, output) -> None: |
| 25 | + pass |
| 26 | + |
| 27 | + async def call_tool(self): |
| 28 | + return [] |
| 29 | + |
| 30 | + def need_builtin_tool_call(self) -> bool: |
| 31 | + return False |
| 32 | + |
| 33 | + def render_for_completion(self): |
| 34 | + return [] |
| 35 | + |
| 36 | + async def init_tool_sessions(self, tool_server, exit_stack, request_id, |
| 37 | + mcp_tools): |
| 38 | + self.init_tool_sessions_called = True |
| 39 | + self.init_tool_sessions_args = (tool_server, exit_stack, request_id, |
| 40 | + mcp_tools) |
| 41 | + |
| 42 | + async def cleanup_session(self) -> None: |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def mock_serving_responses(): |
| 48 | + """Create a mock OpenAIServingResponses instance""" |
| 49 | + serving_responses = MagicMock(spec=OpenAIServingResponses) |
| 50 | + serving_responses.tool_server = MagicMock(spec=ToolServer) |
| 51 | + return serving_responses |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def mock_context(): |
| 56 | + """Create a mock conversation context""" |
| 57 | + return MockConversationContext() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def mock_exit_stack(): |
| 62 | + """Create a mock async exit stack""" |
| 63 | + return MagicMock(spec=AsyncExitStack) |
| 64 | + |
| 65 | + |
| 66 | +class TestInitializeToolSessions: |
| 67 | + """Test class for _initialize_tool_sessions method""" |
| 68 | + |
| 69 | + @pytest_asyncio.fixture |
| 70 | + async def serving_responses_instance(self): |
| 71 | + """Create a real OpenAIServingResponses instance for testing""" |
| 72 | + # Create minimal mocks for required dependencies |
| 73 | + engine_client = MagicMock() |
| 74 | + engine_client.get_model_config = AsyncMock() |
| 75 | + |
| 76 | + model_config = MagicMock() |
| 77 | + model_config.hf_config.model_type = "test" |
| 78 | + model_config.get_diff_sampling_param.return_value = {} |
| 79 | + |
| 80 | + models = MagicMock() |
| 81 | + |
| 82 | + tool_server = MagicMock(spec=ToolServer) |
| 83 | + |
| 84 | + # Create the actual instance |
| 85 | + instance = OpenAIServingResponses( |
| 86 | + engine_client=engine_client, |
| 87 | + model_config=model_config, |
| 88 | + models=models, |
| 89 | + request_logger=None, |
| 90 | + chat_template=None, |
| 91 | + chat_template_content_format="auto", |
| 92 | + tool_server=tool_server, |
| 93 | + ) |
| 94 | + |
| 95 | + return instance |
| 96 | + |
| 97 | + @pytest.mark.asyncio |
| 98 | + async def test_initialize_tool_sessions(self, serving_responses_instance, |
| 99 | + mock_context, mock_exit_stack): |
| 100 | + """Test that method works correctly with only MCP tools""" |
| 101 | + |
| 102 | + request = ResponsesRequest(input="test input", tools=[]) |
| 103 | + |
| 104 | + # Call the method |
| 105 | + await serving_responses_instance._initialize_tool_sessions( |
| 106 | + request, mock_context, mock_exit_stack) |
| 107 | + assert mock_context.init_tool_sessions_called is False |
| 108 | + |
| 109 | + # Create only MCP tools |
| 110 | + tools = [ |
| 111 | + { |
| 112 | + "type": "web_search_preview" |
| 113 | + }, |
| 114 | + { |
| 115 | + "type": "code_interpreter", |
| 116 | + "container": { |
| 117 | + "type": "auto" |
| 118 | + } |
| 119 | + }, |
| 120 | + ] |
| 121 | + |
| 122 | + request = ResponsesRequest(input="test input", tools=tools) |
| 123 | + |
| 124 | + # Call the method |
| 125 | + await serving_responses_instance._initialize_tool_sessions( |
| 126 | + request, mock_context, mock_exit_stack) |
| 127 | + |
| 128 | + # Verify that init_tool_sessions was called |
| 129 | + assert mock_context.init_tool_sessions_called |
0 commit comments