from unittest.mock import patch, mock_open import pytest from scripts.agent import ( run_bash_command, run_file_read, run_repo_ast_parser, process_query, ) def test_run_bash_command_exception(): with patch("subprocess.run", side_effect=Exception("Subprocess failure")): result = run_bash_command("echo test") assert "Error executing bash command: Subprocess failure" in result def test_run_file_read_exception(): with patch("os.path.exists", return_value=True), \ patch("builtins.open", side_effect=Exception("File access denied")): result = run_file_read("some_file.py") assert "Error reading file: File access denied" in result def test_run_repo_ast_parser_imports_and_classes(): code_content = """ from module_a import something class SampleClass: pass """ with patch("os.path.exists", return_value=True), \ patch("builtins.open", mock_open(read_data=code_content)): result = run_repo_ast_parser("dummy.py") assert "module_a" in result assert "SampleClass" in result def test_process_query_scripts_branch(): with patch("scripts.agent.run_pytest_suite", return_value="OK") as mock_suite: res = process_query("run pytest coverage scripts") mock_suite.assert_called_once() _, kwargs = mock_suite.call_args assert kwargs.get("cov_module") == "scripts"