This commit is contained in:
AI Christianson 2025-03-08 09:30:52 -05:00
parent b262305592
commit b9241780d0
1 changed files with 89 additions and 54 deletions

View File

@ -17,67 +17,102 @@ def test_show_thoughts_flag():
assert args.show_thoughts is True assert args.show_thoughts is True
@pytest.fixture(autouse=True) @pytest.fixture
def mock_config_repository(): def mock_config_repository():
"""Mock the ConfigRepository to avoid database operations during tests""" """Mock the ConfigRepository to avoid database operations during tests"""
with patch('ra_aid.database.repositories.config_repository.config_repo_var') as mock_repo_var: mock_repo = MagicMock()
# Setup a mock repository
mock_repo = MagicMock() # Create a dictionary to simulate config
config = {}
# Create a dictionary to simulate config
config = {} # Setup get method to return config values
def get_config(key, default=None):
# Setup get method to return config values return config.get(key, default)
def get_config(key, default=None): mock_repo.get.side_effect = get_config
return config.get(key, default)
mock_repo.get.side_effect = get_config # Setup set method to update config values
def set_config(key, value):
# Setup set method to update config values config[key] = value
def set_config(key, value): mock_repo.set.side_effect = set_config
config[key] = value
mock_repo.set.side_effect = set_config # Setup update method to update multiple config values
def update_config(config_dict):
# Setup update method to update multiple config values config.update(config_dict)
def update_config(config_dict): mock_repo.update.side_effect = update_config
config.update(config_dict)
mock_repo.update.side_effect = update_config # Setup get_all method to return the config dict
def get_all_config():
# Setup get_all method to return the config dict return config.copy()
def get_all_config(): mock_repo.get_all.side_effect = get_all_config
return config.copy()
mock_repo.get_all.side_effect = get_all_config return mock_repo
# Make the mock context var return our mock repo
mock_repo_var.get.return_value = mock_repo
yield mock_repo
def test_show_thoughts_config(mock_config_repository): def test_show_thoughts_config(mock_config_repository):
"""Test that the show_thoughts flag is correctly stored in config.""" """Test that the --show-thoughts flag is correctly stored in config."""
import sys import sys
from unittest.mock import patch
from ra_aid.__main__ import main # Create a mock parse_arguments function
def mock_parse_arguments(args=None):
# Create a mock arguments object with controlled values
mock_args = MagicMock()
mock_args.show_thoughts = "--show-thoughts" in sys.argv
# Explicitly set research_only and chat to False to avoid sys.exit(1)
mock_args.research_only = False
mock_args.chat = False
# Set message to a default value to avoid sys.exit(1) for missing message
mock_args.message = "test message"
mock_args.wipe_project_memory = False
mock_args.webui = False
return mock_args
# Reset mocks # Test with --show-thoughts flag
with patch.object(sys, "argv", ["ra-aid", "--show-thoughts"]):
with patch("ra_aid.__main__.parse_arguments", side_effect=mock_parse_arguments):
# Mock ConfigRepositoryManager to return our mock
with patch('ra_aid.database.repositories.config_repository.ConfigRepositoryManager.__enter__',
return_value=mock_config_repository):
# Mock the required dependencies to prevent actual execution
with patch("ra_aid.__main__.setup_logging"), \
patch("ra_aid.__main__.DatabaseManager"), \
patch("ra_aid.__main__.ensure_migrations_applied"), \
patch("ra_aid.__main__.check_dependencies"), \
patch("ra_aid.__main__.validate_environment", return_value=(True, [], True, [])), \
patch("ra_aid.__main__.build_status"), \
patch("ra_aid.__main__.console.print"), \
patch("ra_aid.__main__.initialize_llm"), \
patch("ra_aid.__main__.run_research_agent"):
# Run the main function
from ra_aid.__main__ import main
main()
# Verify that show_thoughts was set to True in config
mock_config_repository.set.assert_any_call("show_thoughts", True)
# Reset mock for second test
mock_config_repository.set.reset_mock() mock_config_repository.set.reset_mock()
# For testing, we need to patch ConfigRepositoryManager.__enter__ to return our mock # Test without --show-thoughts flag
with patch('ra_aid.database.repositories.config_repository.ConfigRepositoryManager.__enter__', return_value=mock_config_repository): with patch.object(sys, "argv", ["ra-aid"]):
# Test with --show-thoughts flag with patch("ra_aid.__main__.parse_arguments", side_effect=mock_parse_arguments):
with patch.object(sys, "argv", ["ra-aid", "-m", "test message", "--show-thoughts"]): # Mock ConfigRepositoryManager to return our mock
with patch("ra_aid.__main__.run_research_agent", return_value=None): with patch('ra_aid.database.repositories.config_repository.ConfigRepositoryManager.__enter__',
main() return_value=mock_config_repository):
# Verify the show_thoughts flag is set to True in config # Mock the required dependencies to prevent actual execution
mock_config_repository.set.assert_any_call("show_thoughts", True) with patch("ra_aid.__main__.setup_logging"), \
patch("ra_aid.__main__.DatabaseManager"), \
# Reset mocks patch("ra_aid.__main__.ensure_migrations_applied"), \
mock_config_repository.set.reset_mock() patch("ra_aid.__main__.check_dependencies"), \
patch("ra_aid.__main__.validate_environment", return_value=(True, [], True, [])), \
# Test without --show-thoughts flag (default: False) patch("ra_aid.__main__.build_status"), \
with patch.object(sys, "argv", ["ra-aid", "-m", "test message"]): patch("ra_aid.__main__.console.print"), \
with patch("ra_aid.__main__.run_research_agent", return_value=None): patch("ra_aid.__main__.initialize_llm"), \
main() patch("ra_aid.__main__.run_research_agent"):
# Verify the show_thoughts flag is set to False in config
mock_config_repository.set.assert_any_call("show_thoughts", False) # Run the main function
from ra_aid.__main__ import main
main()
# Verify that show_thoughts was set to False in config
mock_config_repository.set.assert_any_call("show_thoughts", False)