|
| 1 | +""" |
| 2 | +Unit tests for BattleCodeArena. |
| 3 | +
|
| 4 | +Tests validate_code() and get_results() methods without requiring Docker. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from codeclash.arenas.arena import RoundStats |
| 10 | +from codeclash.arenas.battlecode.battlecode import BC_FOLDER, BC_LOG, BC_TIE, BattleCodeArena |
| 11 | + |
| 12 | +from .conftest import MockPlayer |
| 13 | + |
| 14 | +VALID_BOT_PY = """ |
| 15 | +from battlecode25.stubs import * |
| 16 | +
|
| 17 | +def turn(): |
| 18 | + # Simple bot that does nothing |
| 19 | + pass |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +class TestBattleCodeValidation: |
| 24 | + """Tests for BattleCodeArena.validate_code()""" |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def arena(self, tmp_log_dir, minimal_config): |
| 28 | + """Create BattleCodeArena instance with mocked environment.""" |
| 29 | + arena = BattleCodeArena.__new__(BattleCodeArena) |
| 30 | + arena.submission = f"src/{BC_FOLDER}" |
| 31 | + arena.log_local = tmp_log_dir |
| 32 | + arena.run_cmd_round = "python run.py run" |
| 33 | + return arena |
| 34 | + |
| 35 | + def test_valid_submission(self, arena, mock_player_factory): |
| 36 | + """Test that a valid bot.py passes validation.""" |
| 37 | + player = mock_player_factory( |
| 38 | + name="test_player", |
| 39 | + files={ |
| 40 | + f"src/{BC_FOLDER}/bot.py": VALID_BOT_PY, |
| 41 | + }, |
| 42 | + command_outputs={ |
| 43 | + "ls src": {"output": f"{BC_FOLDER}\n", "returncode": 0}, |
| 44 | + f"ls src/{BC_FOLDER}": {"output": "bot.py\n__init__.py\n", "returncode": 0}, |
| 45 | + f"cat src/{BC_FOLDER}/bot.py": {"output": VALID_BOT_PY, "returncode": 0}, |
| 46 | + }, |
| 47 | + ) |
| 48 | + is_valid, error = arena.validate_code(player) |
| 49 | + assert is_valid is True |
| 50 | + assert error is None |
| 51 | + |
| 52 | + def test_missing_mysubmission_directory(self, arena, mock_player_factory): |
| 53 | + """Test that missing src/mysubmission/ fails validation.""" |
| 54 | + player = mock_player_factory( |
| 55 | + name="test_player", |
| 56 | + files={}, |
| 57 | + command_outputs={ |
| 58 | + "ls src": {"output": "otherpackage\n", "returncode": 0}, |
| 59 | + }, |
| 60 | + ) |
| 61 | + is_valid, error = arena.validate_code(player) |
| 62 | + assert is_valid is False |
| 63 | + assert BC_FOLDER in error |
| 64 | + |
| 65 | + def test_missing_bot_file(self, arena, mock_player_factory): |
| 66 | + """Test that missing bot.py fails validation.""" |
| 67 | + player = mock_player_factory( |
| 68 | + name="test_player", |
| 69 | + files={ |
| 70 | + f"src/{BC_FOLDER}/__init__.py": "", |
| 71 | + }, |
| 72 | + command_outputs={ |
| 73 | + "ls src": {"output": f"{BC_FOLDER}\n", "returncode": 0}, |
| 74 | + f"ls src/{BC_FOLDER}": {"output": "__init__.py\n", "returncode": 0}, |
| 75 | + }, |
| 76 | + ) |
| 77 | + is_valid, error = arena.validate_code(player) |
| 78 | + assert is_valid is False |
| 79 | + assert "bot.py" in error |
| 80 | + |
| 81 | + def test_missing_turn_function(self, arena, mock_player_factory): |
| 82 | + """Test that bot.py without turn() function fails validation.""" |
| 83 | + invalid_bot = """ |
| 84 | +from battlecode25.stubs import * |
| 85 | +
|
| 86 | +def setup(): |
| 87 | + pass |
| 88 | +
|
| 89 | +def run(): |
| 90 | + pass |
| 91 | +""" |
| 92 | + player = mock_player_factory( |
| 93 | + name="test_player", |
| 94 | + files={ |
| 95 | + f"src/{BC_FOLDER}/bot.py": invalid_bot, |
| 96 | + }, |
| 97 | + command_outputs={ |
| 98 | + "ls src": {"output": f"{BC_FOLDER}\n", "returncode": 0}, |
| 99 | + f"ls src/{BC_FOLDER}": {"output": "bot.py\n", "returncode": 0}, |
| 100 | + f"cat src/{BC_FOLDER}/bot.py": {"output": invalid_bot, "returncode": 0}, |
| 101 | + }, |
| 102 | + ) |
| 103 | + is_valid, error = arena.validate_code(player) |
| 104 | + assert is_valid is False |
| 105 | + assert "turn()" in error |
| 106 | + |
| 107 | + |
| 108 | +class TestBattleCodeResults: |
| 109 | + """Tests for BattleCodeArena.get_results()""" |
| 110 | + |
| 111 | + @pytest.fixture |
| 112 | + def arena(self, tmp_log_dir, minimal_config): |
| 113 | + """Create BattleCodeArena instance.""" |
| 114 | + config = minimal_config.copy() |
| 115 | + config["game"]["name"] = "BattleCode" |
| 116 | + config["game"]["sims_per_round"] = 3 |
| 117 | + arena = BattleCodeArena.__new__(BattleCodeArena) |
| 118 | + arena.submission = f"src/{BC_FOLDER}" |
| 119 | + arena.log_local = tmp_log_dir |
| 120 | + arena.config = config |
| 121 | + arena.logger = type("Logger", (), {"debug": lambda self, msg: None, "info": lambda self, msg: None})() |
| 122 | + return arena |
| 123 | + |
| 124 | + def _create_sim_log(self, round_dir, idx: int, winner_key: str, is_coin_flip: bool = False): |
| 125 | + """ |
| 126 | + Create a simulation log file. |
| 127 | +
|
| 128 | + Args: |
| 129 | + winner_key: "A" or "B" to indicate which player won |
| 130 | + is_coin_flip: If True, sets reason to coin flip (arbitrary win) |
| 131 | + """ |
| 132 | + log_file = round_dir / BC_LOG.format(idx=idx) |
| 133 | + reason = BC_TIE if is_coin_flip else "Reason: Team won by controlling more territory." |
| 134 | + # The log format has winner info in third-to-last line |
| 135 | + log_file.write_text( |
| 136 | + f"""Round starting... |
| 137 | +Turn 100... |
| 138 | +Turn 200... |
| 139 | +Winner: Team ({winner_key}) wins (game over) |
| 140 | +{reason} |
| 141 | +Final stats |
| 142 | +""" |
| 143 | + ) |
| 144 | + |
| 145 | + def test_parse_results_player_a_wins(self, arena, tmp_log_dir): |
| 146 | + """Test parsing results when player A (first player) wins.""" |
| 147 | + round_dir = tmp_log_dir / "rounds" / "1" |
| 148 | + round_dir.mkdir(parents=True) |
| 149 | + |
| 150 | + # A wins 2 games, B wins 1 |
| 151 | + self._create_sim_log(round_dir, 0, "A") |
| 152 | + self._create_sim_log(round_dir, 1, "A") |
| 153 | + self._create_sim_log(round_dir, 2, "B") |
| 154 | + |
| 155 | + agents = [MockPlayer("Alice"), MockPlayer("Bob")] |
| 156 | + stats = RoundStats(round_num=1, agents=agents) |
| 157 | + |
| 158 | + arena.get_results(agents, round_num=1, stats=stats) |
| 159 | + |
| 160 | + assert stats.winner == "Alice" |
| 161 | + assert stats.scores["Alice"] == 2 |
| 162 | + assert stats.scores["Bob"] == 1 |
| 163 | + |
| 164 | + def test_parse_results_player_b_wins(self, arena, tmp_log_dir): |
| 165 | + """Test parsing results when player B (second player) wins.""" |
| 166 | + round_dir = tmp_log_dir / "rounds" / "1" |
| 167 | + round_dir.mkdir(parents=True) |
| 168 | + |
| 169 | + # A wins 1 game, B wins 2 |
| 170 | + self._create_sim_log(round_dir, 0, "B") |
| 171 | + self._create_sim_log(round_dir, 1, "B") |
| 172 | + self._create_sim_log(round_dir, 2, "A") |
| 173 | + |
| 174 | + agents = [MockPlayer("Alice"), MockPlayer("Bob")] |
| 175 | + stats = RoundStats(round_num=1, agents=agents) |
| 176 | + |
| 177 | + arena.get_results(agents, round_num=1, stats=stats) |
| 178 | + |
| 179 | + assert stats.winner == "Bob" |
| 180 | + assert stats.scores["Alice"] == 1 |
| 181 | + assert stats.scores["Bob"] == 2 |
| 182 | + |
| 183 | + def test_parse_results_with_coin_flips(self, arena, tmp_log_dir): |
| 184 | + """Test parsing results where some wins are coin flips (don't count).""" |
| 185 | + round_dir = tmp_log_dir / "rounds" / "1" |
| 186 | + round_dir.mkdir(parents=True) |
| 187 | + |
| 188 | + # Coin flip wins should be treated as ties |
| 189 | + self._create_sim_log(round_dir, 0, "A") |
| 190 | + self._create_sim_log(round_dir, 1, "A", is_coin_flip=True) # Doesn't count |
| 191 | + self._create_sim_log(round_dir, 2, "B") |
| 192 | + |
| 193 | + agents = [MockPlayer("Alice"), MockPlayer("Bob")] |
| 194 | + stats = RoundStats(round_num=1, agents=agents) |
| 195 | + |
| 196 | + arena.get_results(agents, round_num=1, stats=stats) |
| 197 | + |
| 198 | + # Only non-coin-flip wins count |
| 199 | + assert stats.scores["Alice"] == 1 |
| 200 | + assert stats.scores["Bob"] == 1 |
| 201 | + |
| 202 | + |
| 203 | +class TestBattleCodeConfig: |
| 204 | + """Tests for BattleCodeArena configuration and properties.""" |
| 205 | + |
| 206 | + def test_arena_name(self): |
| 207 | + """Test that arena has correct name.""" |
| 208 | + assert BattleCodeArena.name == "BattleCode" |
| 209 | + |
| 210 | + def test_submission_path(self): |
| 211 | + """Test that submission path is correct.""" |
| 212 | + assert BattleCodeArena.submission == f"src/{BC_FOLDER}" |
| 213 | + |
| 214 | + def test_bc_folder_name(self): |
| 215 | + """Test that BC folder name is mysubmission.""" |
| 216 | + assert BC_FOLDER == "mysubmission" |
| 217 | + |
| 218 | + def test_default_args(self): |
| 219 | + """Test default arguments.""" |
| 220 | + assert BattleCodeArena.default_args.get("maps") == "quack" |
| 221 | + |
| 222 | + def test_description_mentions_python(self): |
| 223 | + """Test that description mentions Python as the language.""" |
| 224 | + assert "python" in BattleCodeArena.description.lower() |
0 commit comments