|
| 1 | +""" |
| 2 | +Integration test for main.py with BattleSnake configuration. |
| 3 | +
|
| 4 | +This test verifies that the main execution flow works without exceptions, |
| 5 | +using DeterministicModel instead of real LLM models. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import tempfile |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +import pytest |
| 13 | +import yaml |
| 14 | +from minisweagent.models.test_models import DeterministicModel |
| 15 | + |
| 16 | +from main import main |
| 17 | + |
| 18 | + |
| 19 | +def test_main_battlesnake_integration(): |
| 20 | + """ |
| 21 | + Integration test for main.py with configs/battlesnake.yaml. |
| 22 | + Success criterion: execution completes without exceptions. |
| 23 | + """ |
| 24 | + # Create a temporary config file with DeterministicModel settings |
| 25 | + config_path = "configs/battlesnake.yaml" |
| 26 | + |
| 27 | + # Read the original config |
| 28 | + with open(config_path, "r") as f: |
| 29 | + config = yaml.safe_load(f) |
| 30 | + |
| 31 | + # Create a temporary directory for test artifacts |
| 32 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 33 | + temp_config_path = os.path.join(temp_dir, "test_battlesnake.yaml") |
| 34 | + |
| 35 | + # Reduce rounds to 1 for faster testing |
| 36 | + config["game"]["rounds"] = 1 |
| 37 | + |
| 38 | + # Write the modified config |
| 39 | + with open(temp_config_path, "w") as f: |
| 40 | + yaml.dump(config, f) |
| 41 | + |
| 42 | + def mock_get_agent(original_get_agent): |
| 43 | + """Wrapper to replace agent models with DeterministicModel""" |
| 44 | + |
| 45 | + def wrapper(agent_config, game): |
| 46 | + agent = original_get_agent(agent_config, game) |
| 47 | + print("In wrapper, got agent of type ", type(agent)) |
| 48 | + |
| 49 | + # Replace model if the agent has one (specifically for MiniSWEAgent) |
| 50 | + if hasattr(agent, "agent") and hasattr(agent.agent, "model"): |
| 51 | + print(f"Replacing model for agent {agent.name}") |
| 52 | + # Create DeterministicModel with the specified command |
| 53 | + deterministic_model = DeterministicModel( |
| 54 | + outputs=[ |
| 55 | + "```bash\necho 'COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT'\n```" |
| 56 | + ] |
| 57 | + ) |
| 58 | + agent.agent.model = deterministic_model |
| 59 | + |
| 60 | + return agent |
| 61 | + |
| 62 | + return wrapper |
| 63 | + |
| 64 | + # Import the get_agent function and patch it where it's used in main |
| 65 | + from codeclash.agents import get_agent |
| 66 | + |
| 67 | + # Run the main function with cleanup enabled |
| 68 | + with patch("main.get_agent", side_effect=mock_get_agent(get_agent)): |
| 69 | + # This should complete without raising any exceptions |
| 70 | + main(temp_config_path, cleanup=True, push_agent=False) |
0 commit comments