Skip to content

Commit

Permalink
Fix test_function_calling.py to use correct litellm types
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Jan 27, 2025
1 parent 8954f4e commit 9a90439
Showing 1 changed file with 72 additions and 78 deletions.
150 changes: 72 additions & 78 deletions tests/unit/test_function_calling.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
"""Test function calling module."""

import json
import pytest
from unittest.mock import Mock

import pytest
from litellm import ModelResponse

from openhands.agenthub.codeact_agent.function_calling import response_to_actions
from openhands.core.exceptions import FunctionCallValidationError
from openhands.events.action import (
BrowseInteractiveAction,
BrowseURLAction,
CmdRunAction,
IPythonRunCellAction,
FileEditAction,
FileReadAction,
BrowseInteractiveAction,
BrowseURLAction,
IPythonRunCellAction,
)
from openhands.events.event import FileEditSource, FileReadSource


def create_mock_response(function_name: str, arguments: dict) -> ModelResponse:
"""Helper function to create a mock response with a tool call."""
return ModelResponse(
id="mock-id",
id='mock-id',
choices=[
Mock(
message=Mock(
tool_calls=[
Mock(
function=Mock(
name=function_name,
arguments=json.dumps(arguments)
),
id="mock-tool-call-id"
)
{
'message': {
'tool_calls': [
{
'function': {
'name': function_name,
'arguments': json.dumps(arguments),
},
'id': 'mock-tool-call-id',
'type': 'function',
}
],
content=None
)
)
]
'content': None,
'role': 'assistant',
},
'index': 0,
'finish_reason': 'tool_calls',
}
],
)


def test_execute_bash_valid():
"""Test execute_bash with valid arguments."""
response = create_mock_response("execute_bash", {"command": "ls", "is_input": "false"})
response = create_mock_response(
'execute_bash', {'command': 'ls', 'is_input': 'false'}
)
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], CmdRunAction)
assert actions[0].command == "ls"
assert actions[0].command == 'ls'
assert actions[0].is_input is False


def test_execute_bash_missing_command():
"""Test execute_bash with missing command argument."""
response = create_mock_response("execute_bash", {"is_input": "false"})
response = create_mock_response('execute_bash', {'is_input': 'false'})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "command"' in str(exc_info.value)


def test_execute_ipython_cell_valid():
"""Test execute_ipython_cell with valid arguments."""
response = create_mock_response("execute_ipython_cell", {"code": "print('hello')"})
response = create_mock_response('execute_ipython_cell', {'code': "print('hello')"})
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], IPythonRunCellAction)
Expand All @@ -71,7 +76,7 @@ def test_execute_ipython_cell_valid():

def test_execute_ipython_cell_missing_code():
"""Test execute_ipython_cell with missing code argument."""
response = create_mock_response("execute_ipython_cell", {})
response = create_mock_response('execute_ipython_cell', {})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "code"' in str(exc_info.value)
Expand All @@ -80,33 +85,28 @@ def test_execute_ipython_cell_missing_code():
def test_edit_file_valid():
"""Test edit_file with valid arguments."""
response = create_mock_response(
"edit_file",
{
"path": "/path/to/file",
"content": "file content",
"start": 1,
"end": 10
}
'edit_file',
{'path': '/path/to/file', 'content': 'file content', 'start': 1, 'end': 10},
)
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], FileEditAction)
assert actions[0].path == "/path/to/file"
assert actions[0].content == "file content"
assert actions[0].path == '/path/to/file'
assert actions[0].content == 'file content'
assert actions[0].start == 1
assert actions[0].end == 10


def test_edit_file_missing_required():
"""Test edit_file with missing required arguments."""
# Missing path
response = create_mock_response("edit_file", {"content": "content"})
response = create_mock_response('edit_file', {'content': 'content'})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "path"' in str(exc_info.value)

# Missing content
response = create_mock_response("edit_file", {"path": "/path/to/file"})
response = create_mock_response('edit_file', {'path': '/path/to/file'})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "content"' in str(exc_info.value)
Expand All @@ -116,56 +116,49 @@ def test_str_replace_editor_valid():
"""Test str_replace_editor with valid arguments."""
# Test view command
response = create_mock_response(
"str_replace_editor",
{
"command": "view",
"path": "/path/to/file"
}
'str_replace_editor', {'command': 'view', 'path': '/path/to/file'}
)
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], FileReadAction)
assert actions[0].path == "/path/to/file"
assert actions[0].path == '/path/to/file'
assert actions[0].impl_source == FileReadSource.OH_ACI

# Test other commands
response = create_mock_response(
"str_replace_editor",
'str_replace_editor',
{
"command": "str_replace",
"path": "/path/to/file",
"old_str": "old",
"new_str": "new"
}
'command': 'str_replace',
'path': '/path/to/file',
'old_str': 'old',
'new_str': 'new',
},
)
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], FileEditAction)
assert actions[0].path == "/path/to/file"
assert actions[0].path == '/path/to/file'
assert actions[0].impl_source == FileEditSource.OH_ACI


def test_str_replace_editor_missing_required():
"""Test str_replace_editor with missing required arguments."""
# Missing command
response = create_mock_response("str_replace_editor", {"path": "/path/to/file"})
response = create_mock_response('str_replace_editor', {'path': '/path/to/file'})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "command"' in str(exc_info.value)

# Missing path
response = create_mock_response("str_replace_editor", {"command": "view"})
response = create_mock_response('str_replace_editor', {'command': 'view'})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "path"' in str(exc_info.value)


def test_browser_valid():
"""Test browser with valid arguments."""
response = create_mock_response(
"browser",
{"code": "click('button-1')"}
)
response = create_mock_response('browser', {'code': "click('button-1')"})
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], BrowseInteractiveAction)
Expand All @@ -174,27 +167,24 @@ def test_browser_valid():

def test_browser_missing_code():
"""Test browser with missing code argument."""
response = create_mock_response("browser", {})
response = create_mock_response('browser', {})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "code"' in str(exc_info.value)


def test_web_read_valid():
"""Test web_read with valid arguments."""
response = create_mock_response(
"web_read",
{"url": "https://example.com"}
)
response = create_mock_response('web_read', {'url': 'https://example.com'})
actions = response_to_actions(response)
assert len(actions) == 1
assert isinstance(actions[0], BrowseURLAction)
assert actions[0].url == "https://example.com"
assert actions[0].url == 'https://example.com'


def test_web_read_missing_url():
"""Test web_read with missing url argument."""
response = create_mock_response("web_read", {})
response = create_mock_response('web_read', {})
with pytest.raises(FunctionCallValidationError) as exc_info:
response_to_actions(response)
assert 'Missing required argument "url"' in str(exc_info.value)
Expand All @@ -203,24 +193,28 @@ def test_web_read_missing_url():
def test_invalid_json_arguments():
"""Test handling of invalid JSON in arguments."""
response = ModelResponse(
id="mock-id",
id='mock-id',
choices=[
Mock(
message=Mock(
tool_calls=[
Mock(
function=Mock(
name="execute_bash",
arguments="invalid json"
),
id="mock-tool-call-id"
)
{
'message': {
'tool_calls': [
{
'function': {
'name': 'execute_bash',
'arguments': 'invalid json',
},
'id': 'mock-tool-call-id',
'type': 'function',
}
],
content=None
)
)
]
'content': None,
'role': 'assistant',
},
'index': 0,
'finish_reason': 'tool_calls',
}
],
)
with pytest.raises(RuntimeError) as exc_info:
response_to_actions(response)
assert "Failed to parse tool call arguments" in str(exc_info.value)
assert 'Failed to parse tool call arguments' in str(exc_info.value)

0 comments on commit 9a90439

Please sign in to comment.