|
| 1 | +"""Unit tests for post_hooks screenshot functions.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from post_hooks import _append_screenshots_to_pr, capture_pr_screenshots |
| 6 | + |
| 7 | + |
| 8 | +class TestCapturePrScreenshots: |
| 9 | + def test_returns_urls_on_success(self): |
| 10 | + with patch("browser.capture_screenshot", return_value="https://s3/img.png"): |
| 11 | + result = capture_pr_screenshots("https://github.com/owner/repo/pull/1", "task-1") |
| 12 | + assert result == ["https://s3/img.png"] |
| 13 | + |
| 14 | + def test_returns_empty_list_when_pr_url_empty(self): |
| 15 | + result = capture_pr_screenshots("", "task-1") |
| 16 | + assert result == [] |
| 17 | + |
| 18 | + def test_returns_empty_list_when_pr_url_not_github(self): |
| 19 | + result = capture_pr_screenshots("https://gitlab.com/owner/repo/pull/1", "task-1") |
| 20 | + assert result == [] |
| 21 | + |
| 22 | + def test_returns_empty_list_on_exception(self): |
| 23 | + with patch("browser.capture_screenshot", side_effect=RuntimeError("boom")): |
| 24 | + result = capture_pr_screenshots("https://github.com/owner/repo/pull/1", "task-1") |
| 25 | + assert result == [] |
| 26 | + |
| 27 | + |
| 28 | +class TestAppendScreenshotsToPr: |
| 29 | + def _make_mocks(self): |
| 30 | + config = MagicMock() |
| 31 | + config.repo_url = "https://github.com/owner/repo" |
| 32 | + setup = MagicMock() |
| 33 | + setup.branch = "bgagent/task-1" |
| 34 | + setup.repo_dir = "/tmp/repo" |
| 35 | + return config, setup |
| 36 | + |
| 37 | + def test_appends_screenshots_section(self): |
| 38 | + config, setup = self._make_mocks() |
| 39 | + view_result = MagicMock(returncode=0, stdout="## Summary\n\nSome PR body") |
| 40 | + edit_result = MagicMock(returncode=0, stderr="") |
| 41 | + with patch("post_hooks.subprocess.run", side_effect=[view_result, edit_result]) as mock_run: |
| 42 | + _append_screenshots_to_pr(config, setup, ["https://s3/img1.png"]) |
| 43 | + edit_call = mock_run.call_args_list[1] |
| 44 | + body_arg = edit_call[0][0][edit_call[0][0].index("--body") + 1] |
| 45 | + assert "## Screenshots" in body_arg |
| 46 | + assert "" in body_arg |
| 47 | + |
| 48 | + def test_replaces_existing_screenshots_section(self): |
| 49 | + config, setup = self._make_mocks() |
| 50 | + existing_body = "## Summary\n\nBody\n\n## Screenshots\n\n" |
| 51 | + view_result = MagicMock(returncode=0, stdout=existing_body) |
| 52 | + edit_result = MagicMock(returncode=0, stderr="") |
| 53 | + with patch("post_hooks.subprocess.run", side_effect=[view_result, edit_result]) as mock_run: |
| 54 | + _append_screenshots_to_pr(config, setup, ["https://s3/new.png"]) |
| 55 | + edit_call = mock_run.call_args_list[1] |
| 56 | + body_arg = edit_call[0][0][edit_call[0][0].index("--body") + 1] |
| 57 | + assert "" in body_arg |
| 58 | + assert "https://old.png" not in body_arg |
| 59 | + # Should only have one ## Screenshots section |
| 60 | + assert body_arg.count("## Screenshots") == 1 |
| 61 | + |
| 62 | + def test_handles_gh_pr_view_failure(self): |
| 63 | + config, setup = self._make_mocks() |
| 64 | + view_result = MagicMock(returncode=1, stdout="", stderr="not found") |
| 65 | + with patch("post_hooks.subprocess.run", return_value=view_result): |
| 66 | + # Should not raise |
| 67 | + _append_screenshots_to_pr(config, setup, ["https://s3/img.png"]) |
| 68 | + |
| 69 | + def test_handles_gh_pr_edit_failure(self): |
| 70 | + config, setup = self._make_mocks() |
| 71 | + view_result = MagicMock(returncode=0, stdout="## Summary\n\nBody") |
| 72 | + edit_result = MagicMock(returncode=1, stderr="permission denied") |
| 73 | + with patch("post_hooks.subprocess.run", side_effect=[view_result, edit_result]): |
| 74 | + # Should not raise |
| 75 | + _append_screenshots_to_pr(config, setup, ["https://s3/img.png"]) |
| 76 | + |
| 77 | + def test_does_nothing_when_urls_empty(self): |
| 78 | + config, setup = self._make_mocks() |
| 79 | + with patch("post_hooks.subprocess.run") as mock_run: |
| 80 | + _append_screenshots_to_pr(config, setup, []) |
| 81 | + mock_run.assert_not_called() |
0 commit comments