test_resolve_issues.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. import os
  2. import tempfile
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. import pytest
  5. from openhands.core.config import LLMConfig
  6. from openhands.events.action import CmdRunAction
  7. from openhands.events.observation import CmdOutputObservation, NullObservation
  8. from openhands.llm.llm import LLM
  9. from openhands.resolver.github_issue import GithubIssue, ReviewThread
  10. from openhands.resolver.issue_definitions import IssueHandler, PRHandler
  11. from openhands.resolver.resolve_issue import (
  12. complete_runtime,
  13. initialize_runtime,
  14. process_issue,
  15. )
  16. from openhands.resolver.resolver_output import ResolverOutput
  17. @pytest.fixture
  18. def mock_output_dir():
  19. with tempfile.TemporaryDirectory() as temp_dir:
  20. repo_path = os.path.join(temp_dir, 'repo')
  21. # Initialize a GitHub repo in "repo" and add a commit with "README.md"
  22. os.makedirs(repo_path)
  23. os.system(f'git init {repo_path}')
  24. readme_path = os.path.join(repo_path, 'README.md')
  25. with open(readme_path, 'w') as f:
  26. f.write('hello world')
  27. os.system(f'git -C {repo_path} add README.md')
  28. os.system(f"git -C {repo_path} commit -m 'Initial commit'")
  29. yield temp_dir
  30. @pytest.fixture
  31. def mock_subprocess():
  32. with patch('subprocess.check_output') as mock_check_output:
  33. yield mock_check_output
  34. @pytest.fixture
  35. def mock_os():
  36. with patch('os.system') as mock_system, patch('os.path.join') as mock_join:
  37. yield mock_system, mock_join
  38. @pytest.fixture
  39. def mock_prompt_template():
  40. return 'Issue: {{ body }}\n\nPlease fix this issue.'
  41. @pytest.fixture
  42. def mock_followup_prompt_template():
  43. return 'Issue context: {{ issues }}\n\nReview comments: {{ review_comments }}\n\nReview threads: {{ review_threads }}\n\nFiles: {{ files }}\n\nThread comments: {{ thread_context }}\n\nPlease fix this issue.'
  44. def create_cmd_output(exit_code: int, content: str, command_id: int, command: str):
  45. return CmdOutputObservation(
  46. exit_code=exit_code, content=content, command_id=command_id, command=command
  47. )
  48. def test_initialize_runtime():
  49. mock_runtime = MagicMock()
  50. mock_runtime.run_action.side_effect = [
  51. create_cmd_output(
  52. exit_code=0, content='', command_id=1, command='cd /workspace'
  53. ),
  54. create_cmd_output(
  55. exit_code=0,
  56. content='',
  57. command_id=2,
  58. command='git config --global core.pager ""',
  59. ),
  60. ]
  61. initialize_runtime(mock_runtime)
  62. assert mock_runtime.run_action.call_count == 2
  63. mock_runtime.run_action.assert_any_call(CmdRunAction(command='cd /workspace'))
  64. mock_runtime.run_action.assert_any_call(
  65. CmdRunAction(command='git config --global core.pager ""')
  66. )
  67. @pytest.mark.asyncio
  68. async def test_resolve_issue_no_issues_found():
  69. from openhands.resolver.resolve_issue import resolve_issue
  70. # Mock dependencies
  71. mock_handler = MagicMock()
  72. mock_handler.get_converted_issues.return_value = [] # Return empty list
  73. with patch(
  74. 'openhands.resolver.resolve_issue.issue_handler_factory',
  75. return_value=mock_handler,
  76. ):
  77. with pytest.raises(ValueError) as exc_info:
  78. await resolve_issue(
  79. owner='test-owner',
  80. repo='test-repo',
  81. token='test-token',
  82. username='test-user',
  83. max_iterations=5,
  84. output_dir='/tmp',
  85. llm_config=LLMConfig(model='test', api_key='test'),
  86. runtime_container_image='test-image',
  87. prompt_template='test-template',
  88. issue_type='pr',
  89. repo_instruction=None,
  90. issue_number=5432,
  91. comment_id=None,
  92. )
  93. assert 'No issues found for issue number 5432' in str(exc_info.value)
  94. assert 'test-owner/test-repo' in str(exc_info.value)
  95. assert 'exists in the repository' in str(exc_info.value)
  96. assert 'correct permissions' in str(exc_info.value)
  97. def test_download_issues_from_github():
  98. llm_config = LLMConfig(model='test', api_key='test')
  99. handler = IssueHandler('owner', 'repo', 'token', llm_config)
  100. mock_issues_response = MagicMock()
  101. mock_issues_response.json.side_effect = [
  102. [
  103. {'number': 1, 'title': 'Issue 1', 'body': 'This is an issue'},
  104. {
  105. 'number': 2,
  106. 'title': 'PR 1',
  107. 'body': 'This is a pull request',
  108. 'pull_request': {},
  109. },
  110. {'number': 3, 'title': 'Issue 2', 'body': 'This is another issue'},
  111. ],
  112. None,
  113. ]
  114. mock_issues_response.raise_for_status = MagicMock()
  115. mock_comments_response = MagicMock()
  116. mock_comments_response.json.return_value = []
  117. mock_comments_response.raise_for_status = MagicMock()
  118. def get_mock_response(url, *args, **kwargs):
  119. if '/comments' in url:
  120. return mock_comments_response
  121. return mock_issues_response
  122. with patch('requests.get', side_effect=get_mock_response):
  123. issues = handler.get_converted_issues(issue_numbers=[1, 3])
  124. assert len(issues) == 2
  125. assert handler.issue_type == 'issue'
  126. assert all(isinstance(issue, GithubIssue) for issue in issues)
  127. assert [issue.number for issue in issues] == [1, 3]
  128. assert [issue.title for issue in issues] == ['Issue 1', 'Issue 2']
  129. assert [issue.review_comments for issue in issues] == [None, None]
  130. assert [issue.closing_issues for issue in issues] == [None, None]
  131. assert [issue.thread_ids for issue in issues] == [None, None]
  132. def test_download_pr_from_github():
  133. llm_config = LLMConfig(model='test', api_key='test')
  134. handler = PRHandler('owner', 'repo', 'token', llm_config)
  135. mock_pr_response = MagicMock()
  136. mock_pr_response.json.side_effect = [
  137. [
  138. {
  139. 'number': 1,
  140. 'title': 'PR 1',
  141. 'body': 'This is a pull request',
  142. 'head': {'ref': 'b1'},
  143. },
  144. {
  145. 'number': 2,
  146. 'title': 'My PR',
  147. 'body': 'This is another pull request',
  148. 'head': {'ref': 'b2'},
  149. },
  150. {'number': 3, 'title': 'PR 3', 'body': 'Final PR', 'head': {'ref': 'b3'}},
  151. ],
  152. None,
  153. ]
  154. mock_pr_response.raise_for_status = MagicMock()
  155. # Mock for PR comments response
  156. mock_comments_response = MagicMock()
  157. mock_comments_response.json.return_value = [] # No PR comments
  158. mock_comments_response.raise_for_status = MagicMock()
  159. # Mock for GraphQL request (for download_pr_metadata)
  160. mock_graphql_response = MagicMock()
  161. mock_graphql_response.json.side_effect = lambda: {
  162. 'data': {
  163. 'repository': {
  164. 'pullRequest': {
  165. 'closingIssuesReferences': {
  166. 'edges': [
  167. {'node': {'body': 'Issue 1 body', 'number': 1}},
  168. {'node': {'body': 'Issue 2 body', 'number': 2}},
  169. ]
  170. },
  171. 'reviewThreads': {
  172. 'edges': [
  173. {
  174. 'node': {
  175. 'isResolved': False,
  176. 'id': '1',
  177. 'comments': {
  178. 'nodes': [
  179. {
  180. 'body': 'Unresolved comment 1',
  181. 'path': '/frontend/header.tsx',
  182. },
  183. {'body': 'Follow up thread'},
  184. ]
  185. },
  186. }
  187. },
  188. {
  189. 'node': {
  190. 'isResolved': True,
  191. 'id': '2',
  192. 'comments': {
  193. 'nodes': [
  194. {
  195. 'body': 'Resolved comment 1',
  196. 'path': '/some/file.py',
  197. }
  198. ]
  199. },
  200. }
  201. },
  202. {
  203. 'node': {
  204. 'isResolved': False,
  205. 'id': '3',
  206. 'comments': {
  207. 'nodes': [
  208. {
  209. 'body': 'Unresolved comment 3',
  210. 'path': '/another/file.py',
  211. }
  212. ]
  213. },
  214. }
  215. },
  216. ]
  217. },
  218. }
  219. }
  220. }
  221. }
  222. mock_graphql_response.raise_for_status = MagicMock()
  223. def get_mock_response(url, *args, **kwargs):
  224. if '/comments' in url:
  225. return mock_comments_response
  226. return mock_pr_response
  227. with patch('requests.get', side_effect=get_mock_response):
  228. with patch('requests.post', return_value=mock_graphql_response):
  229. issues = handler.get_converted_issues(issue_numbers=[1, 2, 3])
  230. assert len(issues) == 3
  231. assert handler.issue_type == 'pr'
  232. assert all(isinstance(issue, GithubIssue) for issue in issues)
  233. assert [issue.number for issue in issues] == [1, 2, 3]
  234. assert [issue.title for issue in issues] == ['PR 1', 'My PR', 'PR 3']
  235. assert [issue.head_branch for issue in issues] == ['b1', 'b2', 'b3']
  236. assert len(issues[0].review_threads) == 2 # Only unresolved threads
  237. assert (
  238. issues[0].review_threads[0].comment
  239. == 'Unresolved comment 1\n---\nlatest feedback:\nFollow up thread\n'
  240. )
  241. assert issues[0].review_threads[0].files == ['/frontend/header.tsx']
  242. assert (
  243. issues[0].review_threads[1].comment
  244. == 'latest feedback:\nUnresolved comment 3\n'
  245. )
  246. assert issues[0].review_threads[1].files == ['/another/file.py']
  247. assert issues[0].closing_issues == ['Issue 1 body', 'Issue 2 body']
  248. assert issues[0].thread_ids == ['1', '3']
  249. @pytest.mark.asyncio
  250. async def test_complete_runtime():
  251. mock_runtime = MagicMock()
  252. mock_runtime.run_action.side_effect = [
  253. create_cmd_output(
  254. exit_code=0, content='', command_id=1, command='cd /workspace'
  255. ),
  256. create_cmd_output(
  257. exit_code=0,
  258. content='',
  259. command_id=2,
  260. command='git config --global core.pager ""',
  261. ),
  262. create_cmd_output(
  263. exit_code=0,
  264. content='',
  265. command_id=3,
  266. command='git config --global --add safe.directory /workspace',
  267. ),
  268. create_cmd_output(
  269. exit_code=0,
  270. content='',
  271. command_id=4,
  272. command='git diff base_commit_hash fix',
  273. ),
  274. create_cmd_output(
  275. exit_code=0, content='git diff content', command_id=5, command='git apply'
  276. ),
  277. ]
  278. result = await complete_runtime(mock_runtime, 'base_commit_hash')
  279. assert result == {'git_patch': 'git diff content'}
  280. assert mock_runtime.run_action.call_count == 5
  281. @pytest.mark.asyncio
  282. async def test_process_issue(mock_output_dir, mock_prompt_template):
  283. # Mock dependencies
  284. mock_create_runtime = MagicMock()
  285. mock_initialize_runtime = AsyncMock()
  286. mock_run_controller = AsyncMock()
  287. mock_complete_runtime = AsyncMock()
  288. handler_instance = MagicMock()
  289. # Set up test data
  290. issue = GithubIssue(
  291. owner='test_owner',
  292. repo='test_repo',
  293. number=1,
  294. title='Test Issue',
  295. body='This is a test issue',
  296. )
  297. base_commit = 'abcdef1234567890'
  298. repo_instruction = 'Resolve this repo'
  299. max_iterations = 5
  300. llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  301. runtime_container_image = 'test_image:latest'
  302. # Test cases for different scenarios
  303. test_cases = [
  304. {
  305. 'name': 'successful_run',
  306. 'run_controller_return': MagicMock(
  307. history=[NullObservation(content='')],
  308. metrics=MagicMock(
  309. get=MagicMock(return_value={'test_result': 'passed'})
  310. ),
  311. last_error=None,
  312. ),
  313. 'run_controller_raises': None,
  314. 'expected_success': True,
  315. 'expected_error': None,
  316. 'expected_explanation': 'Issue resolved successfully',
  317. },
  318. {
  319. 'name': 'value_error',
  320. 'run_controller_return': None,
  321. 'run_controller_raises': ValueError('Test value error'),
  322. 'expected_success': False,
  323. 'expected_error': 'Agent failed to run or crashed',
  324. 'expected_explanation': 'Agent failed to run',
  325. },
  326. {
  327. 'name': 'runtime_error',
  328. 'run_controller_return': None,
  329. 'run_controller_raises': RuntimeError('Test runtime error'),
  330. 'expected_success': False,
  331. 'expected_error': 'Agent failed to run or crashed',
  332. 'expected_explanation': 'Agent failed to run',
  333. },
  334. {
  335. 'name': 'json_decode_error',
  336. 'run_controller_return': MagicMock(
  337. history=[NullObservation(content='')],
  338. metrics=MagicMock(
  339. get=MagicMock(return_value={'test_result': 'passed'})
  340. ),
  341. last_error=None,
  342. ),
  343. 'run_controller_raises': None,
  344. 'expected_success': True,
  345. 'expected_error': None,
  346. 'expected_explanation': 'Non-JSON explanation',
  347. 'is_pr': True,
  348. 'comment_success': [
  349. True,
  350. False,
  351. ], # To trigger the PR success logging code path
  352. },
  353. ]
  354. for test_case in test_cases:
  355. # Reset mocks
  356. mock_create_runtime.reset_mock()
  357. mock_initialize_runtime.reset_mock()
  358. mock_run_controller.reset_mock()
  359. mock_complete_runtime.reset_mock()
  360. handler_instance.reset_mock()
  361. # Mock return values
  362. mock_create_runtime.return_value = MagicMock(connect=AsyncMock())
  363. if test_case['run_controller_raises']:
  364. mock_run_controller.side_effect = test_case['run_controller_raises']
  365. else:
  366. mock_run_controller.return_value = test_case['run_controller_return']
  367. mock_run_controller.side_effect = None
  368. mock_complete_runtime.return_value = {'git_patch': 'test patch'}
  369. handler_instance.guess_success.return_value = (
  370. test_case['expected_success'],
  371. test_case.get('comment_success', None),
  372. test_case['expected_explanation'],
  373. )
  374. handler_instance.get_instruction.return_value = ('Test instruction', [])
  375. handler_instance.issue_type = 'pr' if test_case.get('is_pr', False) else 'issue'
  376. with (
  377. patch(
  378. 'openhands.resolver.resolve_issue.create_runtime', mock_create_runtime
  379. ),
  380. patch(
  381. 'openhands.resolver.resolve_issue.initialize_runtime',
  382. mock_initialize_runtime,
  383. ),
  384. patch(
  385. 'openhands.resolver.resolve_issue.run_controller', mock_run_controller
  386. ),
  387. patch(
  388. 'openhands.resolver.resolve_issue.complete_runtime',
  389. mock_complete_runtime,
  390. ),
  391. patch('openhands.resolver.resolve_issue.logger'),
  392. ):
  393. # Call the function
  394. result = await process_issue(
  395. issue,
  396. base_commit,
  397. max_iterations,
  398. llm_config,
  399. mock_output_dir,
  400. runtime_container_image,
  401. mock_prompt_template,
  402. handler_instance,
  403. repo_instruction,
  404. reset_logger=False,
  405. )
  406. # Assert the result
  407. expected_issue_type = 'pr' if test_case.get('is_pr', False) else 'issue'
  408. assert handler_instance.issue_type == expected_issue_type
  409. assert isinstance(result, ResolverOutput)
  410. assert result.issue == issue
  411. assert result.base_commit == base_commit
  412. assert result.git_patch == 'test patch'
  413. assert result.success == test_case['expected_success']
  414. assert result.success_explanation == test_case['expected_explanation']
  415. assert result.error == test_case['expected_error']
  416. # Assert that the mocked functions were called
  417. mock_create_runtime.assert_called_once()
  418. mock_initialize_runtime.assert_called_once()
  419. mock_run_controller.assert_called_once()
  420. mock_complete_runtime.assert_called_once()
  421. # Assert that guess_success was called only for successful runs
  422. if test_case['expected_success']:
  423. handler_instance.guess_success.assert_called_once()
  424. else:
  425. handler_instance.guess_success.assert_not_called()
  426. def test_get_instruction(mock_prompt_template, mock_followup_prompt_template):
  427. issue = GithubIssue(
  428. owner='test_owner',
  429. repo='test_repo',
  430. number=123,
  431. title='Test Issue',
  432. body='This is a test issue refer to image ![First Image](https://sampleimage.com/image1.png)',
  433. )
  434. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  435. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  436. instruction, images_urls = issue_handler.get_instruction(
  437. issue, mock_prompt_template, None
  438. )
  439. expected_instruction = 'Issue: Test Issue\n\nThis is a test issue refer to image ![First Image](https://sampleimage.com/image1.png)\n\nPlease fix this issue.'
  440. assert images_urls == ['https://sampleimage.com/image1.png']
  441. assert issue_handler.issue_type == 'issue'
  442. assert instruction == expected_instruction
  443. issue = GithubIssue(
  444. owner='test_owner',
  445. repo='test_repo',
  446. number=123,
  447. title='Test Issue',
  448. body='This is a test issue',
  449. closing_issues=['Issue 1 fix the type'],
  450. review_threads=[
  451. ReviewThread(
  452. comment="There is still a typo 'pthon' instead of 'python'", files=[]
  453. )
  454. ],
  455. thread_comments=[
  456. "I've left review comments, please address them",
  457. 'This is a valid concern.',
  458. ],
  459. )
  460. pr_handler = PRHandler('owner', 'repo', 'token', mock_llm_config)
  461. instruction, images_urls = pr_handler.get_instruction(
  462. issue, mock_followup_prompt_template, None
  463. )
  464. expected_instruction = "Issue context: [\n \"Issue 1 fix the type\"\n]\n\nReview comments: None\n\nReview threads: [\n \"There is still a typo 'pthon' instead of 'python'\"\n]\n\nFiles: []\n\nThread comments: I've left review comments, please address them\n---\nThis is a valid concern.\n\nPlease fix this issue."
  465. assert images_urls == []
  466. assert pr_handler.issue_type == 'pr'
  467. assert instruction == expected_instruction
  468. def test_file_instruction():
  469. issue = GithubIssue(
  470. owner='test_owner',
  471. repo='test_repo',
  472. number=123,
  473. title='Test Issue',
  474. body='This is a test issue ![image](https://sampleimage.com/sample.png)',
  475. )
  476. # load prompt from openhands/resolver/prompts/resolve/basic.jinja
  477. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  478. prompt = f.read()
  479. # Test without thread comments
  480. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  481. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  482. instruction, images_urls = issue_handler.get_instruction(issue, prompt, None)
  483. expected_instruction = """Please fix the following issue for the repository in /workspace.
  484. An environment has been set up for you to start working. You may assume all necessary tools are installed.
  485. # Problem Statement
  486. Test Issue
  487. This is a test issue ![image](https://sampleimage.com/sample.png)
  488. IMPORTANT: You should ONLY interact with the environment provided to you AND NEVER ASK FOR HUMAN HELP.
  489. You SHOULD INCLUDE PROPER INDENTATION in your edit commands.
  490. When you think you have fixed the issue through code changes, please finish the interaction."""
  491. assert instruction == expected_instruction
  492. assert images_urls == ['https://sampleimage.com/sample.png']
  493. def test_file_instruction_with_repo_instruction():
  494. issue = GithubIssue(
  495. owner='test_owner',
  496. repo='test_repo',
  497. number=123,
  498. title='Test Issue',
  499. body='This is a test issue',
  500. )
  501. # load prompt from openhands/resolver/prompts/resolve/basic.jinja
  502. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  503. prompt = f.read()
  504. # load repo instruction from openhands/resolver/prompts/repo_instructions/all-hands-ai___openhands-resolver.txt
  505. with open(
  506. 'openhands/resolver/prompts/repo_instructions/all-hands-ai___openhands-resolver.txt',
  507. 'r',
  508. ) as f:
  509. repo_instruction = f.read()
  510. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  511. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  512. instruction, image_urls = issue_handler.get_instruction(
  513. issue, prompt, repo_instruction
  514. )
  515. expected_instruction = """Please fix the following issue for the repository in /workspace.
  516. An environment has been set up for you to start working. You may assume all necessary tools are installed.
  517. # Problem Statement
  518. Test Issue
  519. This is a test issue
  520. IMPORTANT: You should ONLY interact with the environment provided to you AND NEVER ASK FOR HUMAN HELP.
  521. You SHOULD INCLUDE PROPER INDENTATION in your edit commands.
  522. Some basic information about this repository:
  523. This is a Python repo for openhands-resolver, a library that attempts to resolve github issues with the AI agent OpenHands.
  524. - Setup: `poetry install --with test --with dev`
  525. - Testing: `poetry run pytest tests/test_*.py`
  526. When you think you have fixed the issue through code changes, please finish the interaction."""
  527. assert instruction == expected_instruction
  528. assert issue_handler.issue_type == 'issue'
  529. assert image_urls == []
  530. def test_guess_success():
  531. mock_issue = GithubIssue(
  532. owner='test_owner',
  533. repo='test_repo',
  534. number=1,
  535. title='Test Issue',
  536. body='This is a test issue',
  537. )
  538. mock_history = [
  539. create_cmd_output(
  540. exit_code=0, content='', command_id=1, command='cd /workspace'
  541. )
  542. ]
  543. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  544. mock_completion_response = MagicMock()
  545. mock_completion_response.choices = [
  546. MagicMock(
  547. message=MagicMock(
  548. content='--- success\ntrue\n--- explanation\nIssue resolved successfully'
  549. )
  550. )
  551. ]
  552. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  553. with patch.object(
  554. LLM, 'completion', MagicMock(return_value=mock_completion_response)
  555. ):
  556. success, comment_success, explanation = issue_handler.guess_success(
  557. mock_issue, mock_history
  558. )
  559. assert issue_handler.issue_type == 'issue'
  560. assert comment_success is None
  561. assert success
  562. assert explanation == 'Issue resolved successfully'
  563. def test_guess_success_with_thread_comments():
  564. mock_issue = GithubIssue(
  565. owner='test_owner',
  566. repo='test_repo',
  567. number=1,
  568. title='Test Issue',
  569. body='This is a test issue',
  570. thread_comments=[
  571. 'First comment',
  572. 'Second comment',
  573. 'latest feedback:\nPlease add tests',
  574. ],
  575. )
  576. mock_history = [MagicMock(message='I have added tests for this case')]
  577. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  578. mock_completion_response = MagicMock()
  579. mock_completion_response.choices = [
  580. MagicMock(
  581. message=MagicMock(
  582. content='--- success\ntrue\n--- explanation\nTests have been added to verify thread comments handling'
  583. )
  584. )
  585. ]
  586. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  587. with patch.object(
  588. LLM, 'completion', MagicMock(return_value=mock_completion_response)
  589. ):
  590. success, comment_success, explanation = issue_handler.guess_success(
  591. mock_issue, mock_history
  592. )
  593. assert issue_handler.issue_type == 'issue'
  594. assert comment_success is None
  595. assert success
  596. assert 'Tests have been added' in explanation
  597. def test_instruction_with_thread_comments():
  598. # Create an issue with thread comments
  599. issue = GithubIssue(
  600. owner='test_owner',
  601. repo='test_repo',
  602. number=123,
  603. title='Test Issue',
  604. body='This is a test issue',
  605. thread_comments=[
  606. 'First comment',
  607. 'Second comment',
  608. 'latest feedback:\nPlease add tests',
  609. ],
  610. )
  611. # Load the basic prompt template
  612. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  613. prompt = f.read()
  614. llm_config = LLMConfig(model='test', api_key='test')
  615. issue_handler = IssueHandler('owner', 'repo', 'token', llm_config)
  616. instruction, images_urls = issue_handler.get_instruction(issue, prompt, None)
  617. # Verify that thread comments are included in the instruction
  618. assert 'First comment' in instruction
  619. assert 'Second comment' in instruction
  620. assert 'Please add tests' in instruction
  621. assert 'Issue Thread Comments:' in instruction
  622. assert images_urls == []
  623. def test_guess_success_failure():
  624. mock_issue = GithubIssue(
  625. owner='test_owner',
  626. repo='test_repo',
  627. number=1,
  628. title='Test Issue',
  629. body='This is a test issue',
  630. thread_comments=[
  631. 'First comment',
  632. 'Second comment',
  633. 'latest feedback:\nPlease add tests',
  634. ],
  635. )
  636. mock_history = [MagicMock(message='I have added tests for this case')]
  637. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  638. mock_completion_response = MagicMock()
  639. mock_completion_response.choices = [
  640. MagicMock(
  641. message=MagicMock(
  642. content='--- success\ntrue\n--- explanation\nTests have been added to verify thread comments handling'
  643. )
  644. )
  645. ]
  646. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  647. with patch.object(
  648. LLM, 'completion', MagicMock(return_value=mock_completion_response)
  649. ):
  650. success, comment_success, explanation = issue_handler.guess_success(
  651. mock_issue, mock_history
  652. )
  653. assert issue_handler.issue_type == 'issue'
  654. assert comment_success is None
  655. assert success
  656. assert 'Tests have been added' in explanation
  657. def test_guess_success_negative_case():
  658. mock_issue = GithubIssue(
  659. owner='test_owner',
  660. repo='test_repo',
  661. number=1,
  662. title='Test Issue',
  663. body='This is a test issue',
  664. )
  665. mock_history = [
  666. create_cmd_output(
  667. exit_code=0, content='', command_id=1, command='cd /workspace'
  668. )
  669. ]
  670. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  671. mock_completion_response = MagicMock()
  672. mock_completion_response.choices = [
  673. MagicMock(
  674. message=MagicMock(
  675. content='--- success\nfalse\n--- explanation\nIssue not resolved'
  676. )
  677. )
  678. ]
  679. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  680. with patch.object(
  681. LLM, 'completion', MagicMock(return_value=mock_completion_response)
  682. ):
  683. success, comment_success, explanation = issue_handler.guess_success(
  684. mock_issue, mock_history
  685. )
  686. assert issue_handler.issue_type == 'issue'
  687. assert comment_success is None
  688. assert not success
  689. assert explanation == 'Issue not resolved'
  690. def test_guess_success_invalid_output():
  691. mock_issue = GithubIssue(
  692. owner='test_owner',
  693. repo='test_repo',
  694. number=1,
  695. title='Test Issue',
  696. body='This is a test issue',
  697. )
  698. mock_history = [
  699. create_cmd_output(
  700. exit_code=0, content='', command_id=1, command='cd /workspace'
  701. )
  702. ]
  703. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  704. mock_completion_response = MagicMock()
  705. mock_completion_response.choices = [
  706. MagicMock(message=MagicMock(content='This is not a valid output'))
  707. ]
  708. issue_handler = IssueHandler('owner', 'repo', 'token', mock_llm_config)
  709. with patch.object(
  710. LLM, 'completion', MagicMock(return_value=mock_completion_response)
  711. ):
  712. success, comment_success, explanation = issue_handler.guess_success(
  713. mock_issue, mock_history
  714. )
  715. assert issue_handler.issue_type == 'issue'
  716. assert comment_success is None
  717. assert not success
  718. assert (
  719. explanation
  720. == 'Failed to decode answer from LLM response: This is not a valid output'
  721. )
  722. def test_download_pr_with_review_comments():
  723. llm_config = LLMConfig(model='test', api_key='test')
  724. handler = PRHandler('owner', 'repo', 'token', llm_config)
  725. mock_pr_response = MagicMock()
  726. mock_pr_response.json.side_effect = [
  727. [
  728. {
  729. 'number': 1,
  730. 'title': 'PR 1',
  731. 'body': 'This is a pull request',
  732. 'head': {'ref': 'b1'},
  733. },
  734. ],
  735. None,
  736. ]
  737. mock_pr_response.raise_for_status = MagicMock()
  738. # Mock for PR comments response
  739. mock_comments_response = MagicMock()
  740. mock_comments_response.json.return_value = [] # No PR comments
  741. mock_comments_response.raise_for_status = MagicMock()
  742. # Mock for GraphQL request with review comments but no threads
  743. mock_graphql_response = MagicMock()
  744. mock_graphql_response.json.side_effect = lambda: {
  745. 'data': {
  746. 'repository': {
  747. 'pullRequest': {
  748. 'closingIssuesReferences': {'edges': []},
  749. 'reviews': {
  750. 'nodes': [
  751. {'body': 'Please fix this typo'},
  752. {'body': 'Add more tests'},
  753. ]
  754. },
  755. }
  756. }
  757. }
  758. }
  759. mock_graphql_response.raise_for_status = MagicMock()
  760. def get_mock_response(url, *args, **kwargs):
  761. if '/comments' in url:
  762. return mock_comments_response
  763. return mock_pr_response
  764. with patch('requests.get', side_effect=get_mock_response):
  765. with patch('requests.post', return_value=mock_graphql_response):
  766. issues = handler.get_converted_issues(issue_numbers=[1])
  767. assert len(issues) == 1
  768. assert handler.issue_type == 'pr'
  769. assert isinstance(issues[0], GithubIssue)
  770. assert issues[0].number == 1
  771. assert issues[0].title == 'PR 1'
  772. assert issues[0].head_branch == 'b1'
  773. # Verify review comments are set but threads are empty
  774. assert len(issues[0].review_comments) == 2
  775. assert issues[0].review_comments[0] == 'Please fix this typo'
  776. assert issues[0].review_comments[1] == 'Add more tests'
  777. assert not issues[0].review_threads
  778. assert not issues[0].closing_issues
  779. assert not issues[0].thread_ids
  780. def test_download_issue_with_specific_comment():
  781. llm_config = LLMConfig(model='test', api_key='test')
  782. handler = IssueHandler('owner', 'repo', 'token', llm_config)
  783. # Define the specific comment_id to filter
  784. specific_comment_id = 101
  785. # Mock issue and comment responses
  786. mock_issue_response = MagicMock()
  787. mock_issue_response.json.side_effect = [
  788. [
  789. {'number': 1, 'title': 'Issue 1', 'body': 'This is an issue'},
  790. ],
  791. None,
  792. ]
  793. mock_issue_response.raise_for_status = MagicMock()
  794. mock_comments_response = MagicMock()
  795. mock_comments_response.json.return_value = [
  796. {
  797. 'id': specific_comment_id,
  798. 'body': 'Specific comment body',
  799. 'issue_url': 'https://api.github.com/repos/owner/repo/issues/1',
  800. },
  801. {
  802. 'id': 102,
  803. 'body': 'Another comment body',
  804. 'issue_url': 'https://api.github.com/repos/owner/repo/issues/2',
  805. },
  806. ]
  807. mock_comments_response.raise_for_status = MagicMock()
  808. def get_mock_response(url, *args, **kwargs):
  809. if '/comments' in url:
  810. return mock_comments_response
  811. return mock_issue_response
  812. with patch('requests.get', side_effect=get_mock_response):
  813. issues = handler.get_converted_issues(
  814. issue_numbers=[1], comment_id=specific_comment_id
  815. )
  816. assert len(issues) == 1
  817. assert issues[0].number == 1
  818. assert issues[0].title == 'Issue 1'
  819. assert issues[0].thread_comments == ['Specific comment body']
  820. if __name__ == '__main__':
  821. pytest.main()