|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from mcp import Tool |
| 5 | + |
| 6 | +from nerve.tools.mcp.client import Client |
| 7 | +from nerve.tools.mcp.compiler import create_function_body |
| 8 | + |
| 9 | + |
| 10 | +class TestCreateFunctionBody(unittest.IsolatedAsyncioTestCase): |
| 11 | + async def test_create_function_body_with_string_argument(self) -> None: |
| 12 | + # Mock the client |
| 13 | + client = MagicMock(spec=Client) |
| 14 | + |
| 15 | + # Create a mock tool with a simple string argument |
| 16 | + mock_tool = Tool( |
| 17 | + name="hello_world", |
| 18 | + description="A simple hello world function", |
| 19 | + inputSchema={ |
| 20 | + "type": "object", |
| 21 | + "properties": {"name": {"type": "string", "description": "The name to greet"}}, |
| 22 | + "required": ["name"], |
| 23 | + }, |
| 24 | + ) |
| 25 | + |
| 26 | + func_body, type_defs = await create_function_body(client, mock_tool) |
| 27 | + |
| 28 | + self.assertIn( |
| 29 | + ''' |
| 30 | +async def hello_world(name: Annotated[str, "The name to greet"]) -> Any: |
| 31 | + """A simple hello world function""" |
| 32 | +'''.strip(), |
| 33 | + func_body, |
| 34 | + ) |
| 35 | + |
| 36 | + async def test_create_function_body_with_string_and_int_arguments(self) -> None: |
| 37 | + # Mock the client |
| 38 | + client = MagicMock(spec=Client) |
| 39 | + |
| 40 | + # Create a mock tool with string and int arguments |
| 41 | + mock_tool = Tool( |
| 42 | + name="calculate", |
| 43 | + description="A function that performs a calculation", |
| 44 | + inputSchema={ |
| 45 | + "type": "object", |
| 46 | + "properties": { |
| 47 | + "operation": {"type": "string", "description": "The operation to perform"}, |
| 48 | + "value": {"type": "integer", "description": "The value to calculate with"}, |
| 49 | + }, |
| 50 | + "required": ["operation", "value"], |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + func_body, type_defs = await create_function_body(client, mock_tool) |
| 55 | + |
| 56 | + self.assertIn( |
| 57 | + ''' |
| 58 | +async def calculate(operation: Annotated[str, "The operation to perform"], value: Annotated[int, "The value to calculate with"]) -> Any: |
| 59 | + """A function that performs a calculation""" |
| 60 | +'''.strip(), |
| 61 | + func_body, |
| 62 | + ) |
| 63 | + |
| 64 | + async def test_create_function_body_with_complex_nested_arguments(self) -> None: |
| 65 | + # Mock the client |
| 66 | + client = MagicMock(spec=Client) |
| 67 | + |
| 68 | + # Create a mock tool with complex nested arguments |
| 69 | + mock_tool = Tool( |
| 70 | + name="process_data", |
| 71 | + description="A function that processes complex data structures", |
| 72 | + inputSchema={ |
| 73 | + "type": "object", |
| 74 | + "properties": { |
| 75 | + "user": { |
| 76 | + "type": "object", |
| 77 | + "properties": { |
| 78 | + "name": {"type": "string", "description": "User's name"}, |
| 79 | + "age": {"type": "integer", "description": "User's age"}, |
| 80 | + "preferences": { |
| 81 | + "type": "array", |
| 82 | + "items": {"type": "string"}, |
| 83 | + "description": "User's preferences", |
| 84 | + }, |
| 85 | + }, |
| 86 | + "description": "User information", |
| 87 | + }, |
| 88 | + "settings": { |
| 89 | + "type": "object", |
| 90 | + "properties": { |
| 91 | + "enabled": {"type": "boolean", "description": "Whether processing is enabled"}, |
| 92 | + "options": { |
| 93 | + "type": "object", |
| 94 | + "properties": { |
| 95 | + "mode": {"type": "string", "description": "Processing mode"}, |
| 96 | + "priority": {"type": "integer", "description": "Processing priority"}, |
| 97 | + }, |
| 98 | + "description": "Processing options", |
| 99 | + }, |
| 100 | + }, |
| 101 | + "description": "Processing settings", |
| 102 | + }, |
| 103 | + }, |
| 104 | + "required": ["user", "settings"], |
| 105 | + }, |
| 106 | + ) |
| 107 | + |
| 108 | + func_body, type_defs = await create_function_body(client, mock_tool) |
| 109 | + |
| 110 | + print() |
| 111 | + print(func_body) |
| 112 | + print() |
| 113 | + |
| 114 | + self.assertIn( |
| 115 | + ''' |
| 116 | +async def process_data(user: Annotated[user_0, "User information"], settings: Annotated[settings_1, "Processing settings"]) -> Any: |
| 117 | + """A function that processes complex data structures""" |
| 118 | +'''.strip(), |
| 119 | + func_body, |
| 120 | + ) |
0 commit comments