from pathlib import Path from unittest.mock import MagicMock, patch from aider_gitea import push_changes class TestIssueCommentOnFailure: def setup_method(self): self.cwd = Path('/tmp/test-repo') self.branch_name = 'issue-123-test-branch' self.issue_number = '123' self.issue_title = 'Test Issue' self.base_branch = 'main' self.gitea_client = MagicMock() self.owner = 'test-owner' self.repo = 'test-repo' @patch('aider_gitea.run_cmd', return_value=False) @patch('aider_gitea.has_commits_on_branch', return_value=True) @patch('aider_gitea.get_commit_messages', return_value=['Test commit']) def test_comment_on_push_failure( self, mock_get_commit_messages, mock_has_commits, mock_run_cmd, ): """Test that a comment is added to the issue when git push fails.""" result = push_changes( self.cwd, self.branch_name, self.issue_number, self.issue_title, self.base_branch, self.gitea_client, self.owner, self.repo, ) assert result is False self.gitea_client.create_issue_comment.assert_called_once() comment_body = self.gitea_client.create_issue_comment.call_args[1]['body'] assert 'Failed to push branch' in comment_body assert '❌ **Automated Solution Failed**' in comment_body @patch('aider_gitea.run_cmd', return_value=True) @patch('aider_gitea.has_commits_on_branch', return_value=True) @patch('aider_gitea.get_commit_messages', return_value=['Test commit']) def test_comment_on_pr_creation_failure( self, mock_get_commit_messages, mock_has_commits, mock_run_cmd, ): """Test that a comment is added to the issue when PR creation fails.""" self.gitea_client.create_pull_request.side_effect = Exception( 'PR creation failed', ) result = push_changes( self.cwd, self.branch_name, self.issue_number, self.issue_title, self.base_branch, self.gitea_client, self.owner, self.repo, ) assert result is False self.gitea_client.create_issue_comment.assert_called_once() comment_body = self.gitea_client.create_issue_comment.call_args[1]['body'] assert 'Failed to create pull request' in comment_body assert '❌ **Automated Solution Failed**' in comment_body assert self.branch_name in comment_body @patch('aider_gitea.run_cmd', return_value=True) @patch('aider_gitea.has_commits_on_branch', return_value=True) @patch('aider_gitea.get_commit_messages', return_value=['Test commit']) def test_comment_failure_handled( self, mock_get_commit_messages, mock_has_commits, mock_run_cmd, ): """Test that exceptions during commenting are properly handled.""" self.gitea_client.create_pull_request.side_effect = Exception( 'PR creation failed', ) self.gitea_client.create_issue_comment.side_effect = Exception( 'Comment creation failed', ) # This should not raise an exception result = push_changes( self.cwd, self.branch_name, self.issue_number, self.issue_title, self.base_branch, self.gitea_client, self.owner, self.repo, ) assert result is False