Skip to content

Commit 401aad7

Browse files
- add test coverage
1 parent 9fee3ea commit 401aad7

File tree

1 file changed

+112
-1
lines changed

1 file changed

+112
-1
lines changed

test/test_git_handler.py

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
# Import with testing=True
5151
from src.config import get_config, reset_config # noqa: E402
5252
from src import git_handler # noqa: E402
53+
from src.coding_agents import CodingAgents # noqa: E402
5354

5455

5556
class TestGitHandler(unittest.TestCase):
@@ -206,7 +207,8 @@ def test_create_issue_failure(self, mock_log, mock_ensure_label, mock_run_comman
206207
@patch('src.git_handler.ensure_label')
207208
@patch('src.git_handler.log')
208209
@patch('src.git_handler.debug_log')
209-
def test_reset_issue_success(self, mock_debug_log, mock_log, mock_ensure_label, mock_find_open_pr, mock_run_command, mock_check_issues):
210+
@patch('src.git_handler.config')
211+
def test_reset_issue_success(self, mock_config, mock_debug_log, mock_log, mock_ensure_label, mock_find_open_pr, mock_run_command, mock_check_issues):
210212
"""Test resetting a GitHub issue when successful"""
211213
# Setup
212214
issue_number = 42
@@ -215,6 +217,10 @@ def test_reset_issue_success(self, mock_debug_log, mock_log, mock_ensure_label,
215217
# Mock that no open PR exists
216218
mock_find_open_pr.return_value = None
217219
mock_check_issues.return_value = True
220+
221+
# Explicitly configure for SMARTFIX agent
222+
mock_config.CODING_AGENT = CodingAgents.SMARTFIX.name
223+
mock_config.GITHUB_REPOSITORY = 'mock/repo'
218224

219225
# Mock successful issue view with labels
220226
mock_run_command.side_effect = [
@@ -298,6 +304,111 @@ def test_reset_issue_with_open_pr(self, mock_log, mock_find_open_pr):
298304
"Cannot reset issue #42 because it has an open PR #123: https://github.com/mock/repo/pull/123",
299305
is_error=True
300306
)
307+
308+
@patch('src.git_handler.check_issues_enabled')
309+
@patch('src.git_handler.run_command')
310+
@patch('src.git_handler.find_open_pr_for_issue')
311+
@patch('src.git_handler.ensure_label')
312+
@patch('src.git_handler.log')
313+
@patch('src.git_handler.debug_log')
314+
@patch('src.git_handler.config')
315+
def test_reset_issue_claude_code(self, mock_config, mock_debug_log, mock_log, mock_ensure_label, mock_find_open_pr, mock_run_command, mock_check_issues):
316+
"""Test resetting a GitHub issue when using Claude Code agent"""
317+
# Setup
318+
issue_number = 42
319+
remediation_label = "smartfix-id:5678"
320+
321+
# Mock that no open PR exists
322+
mock_find_open_pr.return_value = None
323+
mock_check_issues.return_value = True
324+
325+
# Configure the mock to use CLAUDE_CODE
326+
mock_config.CODING_AGENT = CodingAgents.CLAUDE_CODE.name
327+
mock_config.GITHUB_REPOSITORY = 'mock/repo'
328+
329+
# Mock successful issue view with labels and other API calls
330+
mock_run_command.side_effect = [
331+
# First call - issue view response
332+
json.dumps({"labels": [{"name": "contrast-vuln-id:VULN-1234"}, {"name": "smartfix-id:OLD-REM"}]}),
333+
# Second call - remove label response
334+
"",
335+
# Third call - add label response
336+
"",
337+
# Fourth call - comment with @claude tag
338+
""
339+
]
340+
mock_ensure_label.return_value = True
341+
342+
# Execute
343+
result = git_handler.reset_issue(issue_number, remediation_label)
344+
345+
# Assert
346+
mock_check_issues.assert_called_once()
347+
self.assertEqual(mock_run_command.call_count, 4) # Should call run_command 4 times (view, remove label, add label, add comment)
348+
self.assertTrue(result)
349+
350+
# Check that Claude-specific logic was executed
351+
mock_debug_log.assert_any_call("CLAUDE_CODE agent detected need to add a comment and tag @claude for reprocessing")
352+
mock_log.assert_any_call(f"Added new comment tagging @claude to issue #{issue_number}")
353+
354+
# Verify the comment command
355+
comment_command_call = mock_run_command.call_args_list[3]
356+
comment_command = comment_command_call[0][0]
357+
358+
# Verify command structure
359+
self.assertEqual(comment_command[0], "gh")
360+
self.assertEqual(comment_command[1], "issue")
361+
self.assertEqual(comment_command[2], "comment")
362+
self.assertEqual(comment_command[5], str(issue_number))
363+
364+
# Verify comment body contains '@claude' and the remediation label
365+
comment_body = comment_command[-1]
366+
self.assertIn("@claude", comment_body)
367+
self.assertIn(remediation_label, comment_body)
368+
369+
@patch('src.git_handler.check_issues_enabled')
370+
@patch('src.git_handler.run_command')
371+
@patch('src.git_handler.find_open_pr_for_issue')
372+
@patch('src.git_handler.ensure_label')
373+
@patch('src.git_handler.log')
374+
@patch('src.git_handler.config')
375+
def test_reset_issue_claude_code_error(self, mock_config, mock_log, mock_ensure_label, mock_find_open_pr, mock_run_command, mock_check_issues):
376+
"""Test resetting a GitHub issue when using Claude Code agent but an error occurs"""
377+
# Setup
378+
issue_number = 42
379+
remediation_label = "smartfix-id:5678"
380+
381+
# Mock that no open PR exists
382+
mock_find_open_pr.return_value = None
383+
mock_check_issues.return_value = True
384+
385+
# Configure the mock to use CLAUDE_CODE
386+
mock_config.CODING_AGENT = CodingAgents.CLAUDE_CODE.name
387+
mock_config.GITHUB_REPOSITORY = 'mock/repo'
388+
389+
# Mock successful label operations but comment command fails
390+
mock_run_command.side_effect = [
391+
# First call - issue view response
392+
json.dumps({"labels": [{"name": "contrast-vuln-id:VULN-1234"}, {"name": "smartfix-id:OLD-REM"}]}),
393+
# Second call - remove label response
394+
"",
395+
# Third call - add label response
396+
"",
397+
# Fourth call - comment command fails
398+
Exception("Failed to comment")
399+
]
400+
mock_ensure_label.return_value = True
401+
402+
# Execute
403+
result = git_handler.reset_issue(issue_number, remediation_label)
404+
405+
# Assert
406+
mock_check_issues.assert_called_once()
407+
self.assertEqual(mock_run_command.call_count, 4) # Should still call run_command 4 times
408+
self.assertFalse(result) # Should return False due to the error
409+
410+
# Verify error was logged
411+
mock_log.assert_any_call(f"Failed to reset issue #{issue_number}: Failed to comment", is_error=True)
301412

302413
@patch('src.git_handler.run_command')
303414
@patch('src.git_handler.debug_log')

0 commit comments

Comments
 (0)