|
| 1 | +"""Unit tests for FiggieArena.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from codeclash.arenas.figgie.figgie import FiggieArena |
| 6 | + |
| 7 | +VALID_FIGGIE_BOT = """ |
| 8 | +def get_action(state): |
| 9 | + '''Make a trading decision based on game state.''' |
| 10 | + hand = state.get('hand', {}) |
| 11 | + offers = state.get('offers', {}) |
| 12 | + bids = state.get('bids', {}) |
| 13 | + position = state.get('position', 0) |
| 14 | +
|
| 15 | + # Simple strategy: try to sell non-goal suits |
| 16 | + for suit in ['spades', 'clubs', 'hearts', 'diamonds']: |
| 17 | + bid = bids.get(suit) |
| 18 | + if bid and bid.get('player') != position and hand.get(suit, 0) > 0: |
| 19 | + return {'type': 'sell', 'suit': suit} |
| 20 | +
|
| 21 | + return {'type': 'pass'} |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class TestFiggieValidation: |
| 26 | + """Tests for FiggieArena.validate_code()""" |
| 27 | + |
| 28 | + @pytest.fixture |
| 29 | + def arena(self, tmp_log_dir, minimal_config): |
| 30 | + """Create FiggieArena instance with mocked environment.""" |
| 31 | + config = minimal_config.copy() |
| 32 | + config["game"]["name"] = "Figgie" |
| 33 | + config["players"] = [ |
| 34 | + {"name": "p1", "agent": "dummy"}, |
| 35 | + {"name": "p2", "agent": "dummy"}, |
| 36 | + {"name": "p3", "agent": "dummy"}, |
| 37 | + {"name": "p4", "agent": "dummy"}, |
| 38 | + ] |
| 39 | + arena = FiggieArena.__new__(FiggieArena) |
| 40 | + arena.submission = "main.py" |
| 41 | + arena.log_local = tmp_log_dir |
| 42 | + return arena |
| 43 | + |
| 44 | + def test_valid_submission(self, arena, mock_player_factory): |
| 45 | + """Test that a valid Figgie bot passes validation.""" |
| 46 | + player = mock_player_factory( |
| 47 | + name="test_player", |
| 48 | + files={"main.py": VALID_FIGGIE_BOT}, |
| 49 | + command_outputs={ |
| 50 | + "ls": {"output": "main.py\n", "returncode": 0}, |
| 51 | + "cat main.py": {"output": VALID_FIGGIE_BOT, "returncode": 0}, |
| 52 | + }, |
| 53 | + ) |
| 54 | + is_valid, error = arena.validate_code(player) |
| 55 | + assert is_valid is True |
| 56 | + assert error is None |
| 57 | + |
| 58 | + def test_missing_file(self, arena, mock_player_factory): |
| 59 | + """Test that missing main.py fails validation.""" |
| 60 | + player = mock_player_factory( |
| 61 | + name="test_player", |
| 62 | + files={}, |
| 63 | + command_outputs={ |
| 64 | + "ls": {"output": "other.py\n", "returncode": 0}, |
| 65 | + }, |
| 66 | + ) |
| 67 | + is_valid, error = arena.validate_code(player) |
| 68 | + assert is_valid is False |
| 69 | + assert "main.py" in error |
| 70 | + |
| 71 | + def test_missing_get_action_function(self, arena, mock_player_factory): |
| 72 | + """Test that missing get_action function fails validation.""" |
| 73 | + bot_code = """ |
| 74 | +def make_move(game_state): |
| 75 | + '''Wrong function name.''' |
| 76 | + return {'type': 'pass'} |
| 77 | +""" |
| 78 | + player = mock_player_factory( |
| 79 | + name="test_player", |
| 80 | + files={"main.py": bot_code}, |
| 81 | + command_outputs={ |
| 82 | + "ls": {"output": "main.py\n", "returncode": 0}, |
| 83 | + "cat main.py": {"output": bot_code, "returncode": 0}, |
| 84 | + }, |
| 85 | + ) |
| 86 | + is_valid, error = arena.validate_code(player) |
| 87 | + assert is_valid is False |
| 88 | + assert "get_action" in error |
| 89 | + |
| 90 | + |
| 91 | +class TestFiggieRequirements: |
| 92 | + """Test Figgie-specific requirements.""" |
| 93 | + |
| 94 | + def test_rejects_invalid_player_count(self, minimal_config, tmp_log_dir): |
| 95 | + """Test that Figgie rejects invalid player counts (not 4 or 5).""" |
| 96 | + config = minimal_config.copy() |
| 97 | + config["game"]["name"] = "Figgie" |
| 98 | + config["players"] = [ |
| 99 | + {"name": "p1", "agent": "dummy"}, |
| 100 | + {"name": "p2", "agent": "dummy"}, |
| 101 | + ] |
| 102 | + |
| 103 | + with pytest.raises(ValueError, match="Figgie requires 4 or 5 players"): |
| 104 | + FiggieArena( |
| 105 | + config, |
| 106 | + tournament_id="test_tournament", |
| 107 | + local_output_dir=tmp_log_dir |
| 108 | + ) |
| 109 | + |
| 110 | + def test_rejects_6_players(self, minimal_config, tmp_log_dir): |
| 111 | + """Test that Figgie rejects 6 players.""" |
| 112 | + config = minimal_config.copy() |
| 113 | + config["game"]["name"] = "Figgie" |
| 114 | + config["players"] = [ |
| 115 | + {"name": f"p{i}", "agent": "dummy"} for i in range(6) |
| 116 | + ] |
| 117 | + |
| 118 | + with pytest.raises(ValueError, match="Figgie requires 4 or 5 players"): |
| 119 | + FiggieArena( |
| 120 | + config, |
| 121 | + tournament_id="test_tournament", |
| 122 | + local_output_dir=tmp_log_dir |
| 123 | + ) |
| 124 | + |
| 125 | + def test_accepts_4_or_5_players(self): |
| 126 | + """Test that Figgie accepts 4 or 5 players by checking class properties.""" |
| 127 | + assert FiggieArena.name == "Figgie" |
| 128 | + assert FiggieArena.submission == "main.py" |
| 129 | + # Description should mention both 4 and 5 players |
| 130 | + assert "4 or 5 players" in FiggieArena.description |
0 commit comments