Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrabach committed Feb 19, 2025
1 parent 9293e52 commit fd09a7a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions libraries/python/mcp-extensions/tests/test_tool_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from unittest.mock import AsyncMock, MagicMock ## This suites and ensure!Cl=Success

import pytest
from mcp_extensions._tool_utils import (
convert_tools_to_openai_tools,
execute_tool_with_notifications,
send_tool_call_progress,
)


def test_convert_tools_to_openai_tools_empty():
result = convert_tools_to_openai_tools([])
assert result is None


# Test: Notification handling in execute_tool_with_notifications
@pytest.mark.asyncio
async def test_execute_tool_with_notification_handling():
mock_session = AsyncMock()
mock_session.incoming_messages = AsyncMock(return_value=[])
mock_tool_call_function = AsyncMock(return_value="result")
mock_handler = AsyncMock()

await execute_tool_with_notifications(
session=mock_session,
tool_call_function=mock_tool_call_function,
notification_handler=mock_handler,
)

mock_handler.assert_not_called()
mock_tool_call_function.assert_awaited_once()


# Test: send_tool_call_progress
@pytest.mark.asyncio
async def test_send_tool_call_progress():
mock_context = AsyncMock()
message = "Progress update"
data = {"step": 1}

await send_tool_call_progress(mock_context, message, data)

# Ensure the log message was sent properly
mock_context.session.send_log_message.assert_called_once_with(
level="info",
data=message,
)


# Test: execute_tool_with_notifications
@pytest.mark.asyncio
async def test_execute_tool_with_notifications():
mock_session = AsyncMock()
mock_tool_call_function = AsyncMock(return_value="result")
mock_notification_handler = AsyncMock()

result = await execute_tool_with_notifications(
session=mock_session,
tool_call_function=mock_tool_call_function,
notification_handler=mock_notification_handler,
)

assert result == "result"
mock_tool_call_function.assert_awaited_once()
mock_notification_handler.assert_not_called()


# Test: convert_tools_to_openai_tools
def test_convert_tools_to_openai_tools():
mock_tool = MagicMock()
mock_tool.name = "test_tool"
mock_tool.inputSchema = {"type": "object", "properties": {}}
mock_tool.description = "A test tool."

result = convert_tools_to_openai_tools([mock_tool])

assert result is not None and len(result) == 1
assert result[0]["function"]["name"] == "test_tool"
assert "description" in result[0]["function"] and result[0]["function"]["description"] == "A test tool."
assert "parameters" in result[0]["function"] and result[0]["function"]["parameters"] == {
"type": "object",
"properties": {},
}

0 comments on commit fd09a7a

Please sign in to comment.