|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from openhands.core.config import LLMConfig
|
|
from openhands.events.action.message import MessageAction
|
|
from openhands.llm import LLM
|
|
from openhands.resolver.github_issue import GithubIssue
|
|
from openhands.resolver.issue_definitions import IssueHandler, PRHandler
|
|
|
|
|
|
def test_guess_success_multiline_explanation():
|
|
|
|
issue = GithubIssue(
|
|
owner='test',
|
|
repo='test',
|
|
number=1,
|
|
title='Test Issue',
|
|
body='Test body',
|
|
thread_comments=None,
|
|
review_comments=None,
|
|
)
|
|
history = [MessageAction(content='Test message')]
|
|
llm_config = LLMConfig(model='test', api_key='test')
|
|
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.choices = [
|
|
MagicMock(
|
|
message=MagicMock(
|
|
content="""--- success
|
|
true
|
|
|
|
--- explanation
|
|
The PR successfully addressed the issue by:
|
|
- Fixed bug A
|
|
- Added test B
|
|
- Updated documentation C
|
|
|
|
Automatic fix generated by OpenHands π"""
|
|
)
|
|
)
|
|
]
|
|
|
|
|
|
with patch.object(LLM, 'completion', return_value=mock_response) as mock_completion:
|
|
|
|
handler = IssueHandler('test', 'test', 'test', llm_config)
|
|
|
|
|
|
success, _, explanation = handler.guess_success(issue, history)
|
|
|
|
|
|
assert success is True
|
|
assert 'The PR successfully addressed the issue by:' in explanation
|
|
assert 'Fixed bug A' in explanation
|
|
assert 'Added test B' in explanation
|
|
assert 'Updated documentation C' in explanation
|
|
assert 'Automatic fix generated by OpenHands' in explanation
|
|
|
|
|
|
mock_completion.assert_called_once()
|
|
|
|
|
|
def test_pr_handler_guess_success_with_thread_comments():
|
|
|
|
llm_config = LLMConfig(model='test', api_key='test')
|
|
handler = PRHandler('test-owner', 'test-repo', 'test-token', llm_config)
|
|
|
|
|
|
issue = GithubIssue(
|
|
owner='test-owner',
|
|
repo='test-repo',
|
|
number=1,
|
|
title='Test PR',
|
|
body='Test Body',
|
|
thread_comments=['First comment', 'Second comment'],
|
|
closing_issues=['Issue description'],
|
|
review_comments=None,
|
|
thread_ids=None,
|
|
head_branch='test-branch',
|
|
)
|
|
|
|
|
|
history = [MessageAction(content='Fixed the issue by implementing X and Y')]
|
|
|
|
|
|
llm_config = LLMConfig(model='test-model', api_key='test-key')
|
|
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.choices = [
|
|
MagicMock(
|
|
message=MagicMock(
|
|
content="""--- success
|
|
true
|
|
|
|
--- explanation
|
|
The changes successfully address the feedback."""
|
|
)
|
|
)
|
|
]
|
|
|
|
|
|
with patch.object(LLM, 'completion', return_value=mock_response):
|
|
success, success_list, explanation = handler.guess_success(issue, history)
|
|
|
|
|
|
assert success is True
|
|
assert success_list == [True]
|
|
assert 'successfully address' in explanation
|
|
assert len(json.loads(explanation)) == 1
|
|
|
|
|
|
def test_pr_handler_guess_success_only_review_comments():
|
|
|
|
llm_config = LLMConfig(model='test', api_key='test')
|
|
handler = PRHandler('test-owner', 'test-repo', 'test-token', llm_config)
|
|
|
|
|
|
issue = GithubIssue(
|
|
owner='test-owner',
|
|
repo='test-repo',
|
|
number=1,
|
|
title='Test PR',
|
|
body='Test Body',
|
|
thread_comments=None,
|
|
closing_issues=['Issue description'],
|
|
review_comments=['Please fix the formatting', 'Add more tests'],
|
|
thread_ids=None,
|
|
head_branch='test-branch',
|
|
)
|
|
|
|
|
|
history = [MessageAction(content='Fixed the formatting and added more tests')]
|
|
|
|
|
|
llm_config = LLMConfig(model='test-model', api_key='test-key')
|
|
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.choices = [
|
|
MagicMock(
|
|
message=MagicMock(
|
|
content="""--- success
|
|
true
|
|
|
|
--- explanation
|
|
The changes successfully address the review comments."""
|
|
)
|
|
)
|
|
]
|
|
|
|
|
|
with patch.object(LLM, 'completion', return_value=mock_response):
|
|
success, success_list, explanation = handler.guess_success(issue, history)
|
|
|
|
|
|
assert success is True
|
|
assert success_list == [True]
|
|
assert (
|
|
'["The changes successfully address the review comments."]' in explanation
|
|
)
|
|
|
|
|
|
def test_pr_handler_guess_success_no_comments():
|
|
|
|
llm_config = LLMConfig(model='test', api_key='test')
|
|
handler = PRHandler('test-owner', 'test-repo', 'test-token', llm_config)
|
|
|
|
|
|
issue = GithubIssue(
|
|
owner='test-owner',
|
|
repo='test-repo',
|
|
number=1,
|
|
title='Test PR',
|
|
body='Test Body',
|
|
thread_comments=None,
|
|
closing_issues=['Issue description'],
|
|
review_comments=None,
|
|
thread_ids=None,
|
|
head_branch='test-branch',
|
|
)
|
|
|
|
|
|
history = [MessageAction(content='Fixed the issue')]
|
|
|
|
|
|
llm_config = LLMConfig(model='test-model', api_key='test-key')
|
|
|
|
|
|
success, success_list, explanation = handler.guess_success(issue, history)
|
|
assert success is False
|
|
assert success_list is None
|
|
assert explanation == 'No feedback was found to process'
|
|
|