test_resolve_issues.py 31 KB

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