|
| 1 | +import pytest |
| 2 | + |
| 3 | +from kaggle_environments.envs.werewolf.game.consts import RoleConst |
| 4 | +from kaggle_environments.envs.werewolf.game.roles import ( |
| 5 | + create_players_from_agents_config, |
| 6 | + get_permutation, |
| 7 | + Player, |
| 8 | + Werewolf, |
| 9 | + Doctor, |
| 10 | + Seer, |
| 11 | + Villager, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def sample_agents_config(): |
| 17 | + """Provides a sample agent configuration list for testing.""" |
| 18 | + return [ |
| 19 | + {"id": "Player1", "agent_id": "random", "role": "Werewolf", "role_params": {}}, |
| 20 | + {"id": "Player2", "agent_id": "random", "role": "Doctor", "role_params": {"allow_self_save": True}}, |
| 21 | + {"id": "Player3", "agent_id": "random", "role": "Seer", "role_params": {}}, |
| 22 | + {"id": "Player4", "agent_id": "random", "role": "Villager", "role_params": {}}, |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +def test_create_players_from_agents_config_basic(sample_agents_config): |
| 27 | + """Tests basic player creation from a valid configuration.""" |
| 28 | + players = create_players_from_agents_config(sample_agents_config) |
| 29 | + |
| 30 | + assert isinstance(players, list) |
| 31 | + assert len(players) == len(sample_agents_config) |
| 32 | + assert all(isinstance(p, Player) for p in players) |
| 33 | + |
| 34 | + # Check player IDs |
| 35 | + assert [p.id for p in players] == ["Player1", "Player2", "Player3", "Player4"] |
| 36 | + |
| 37 | + # Check role assignment and types |
| 38 | + assert isinstance(players[0].role, Werewolf) |
| 39 | + assert players[0].role.name == RoleConst.WEREWOLF |
| 40 | + |
| 41 | + assert isinstance(players[1].role, Doctor) |
| 42 | + assert players[1].role.name == RoleConst.DOCTOR |
| 43 | + assert players[1].role.allow_self_save is True |
| 44 | + |
| 45 | + assert isinstance(players[2].role, Seer) |
| 46 | + assert players[2].role.name == RoleConst.SEER |
| 47 | + |
| 48 | + assert isinstance(players[3].role, Villager) |
| 49 | + assert players[3].role.name == RoleConst.VILLAGER |
| 50 | + |
| 51 | + |
| 52 | +def test_create_players_with_duplicate_ids_raises_error(sample_agents_config): |
| 53 | + """Tests that a ValueError is raised when duplicate agent IDs are provided.""" |
| 54 | + invalid_config = sample_agents_config + [{"id": "Player1", "agent_id": "random", "role": "Villager", "role_params": {}}] |
| 55 | + with pytest.raises(ValueError, match="Duplicate agent ids found: Player1"): |
| 56 | + create_players_from_agents_config(invalid_config) |
| 57 | + |
| 58 | + |
| 59 | +def test_get_permutation_is_deterministic(): |
| 60 | + """Tests that the get_permutation function is deterministic for the same seed.""" |
| 61 | + items = ["a", "b", "c", "d", "e"] |
| 62 | + seed1 = 123 |
| 63 | + seed2 = 456 |
| 64 | + |
| 65 | + permutation1_run1 = get_permutation(items, seed1) |
| 66 | + permutation1_run2 = get_permutation(items, seed1) |
| 67 | + permutation2 = get_permutation(items, seed2) |
| 68 | + |
| 69 | + assert permutation1_run1 == permutation1_run2 |
| 70 | + assert permutation1_run1 != permutation2 |
| 71 | + assert sorted(permutation1_run1) == sorted(items) # Ensure it's still a permutation |
| 72 | + |
| 73 | + |
| 74 | +def test_create_players_with_role_shuffling(sample_agents_config): |
| 75 | + """Tests that roles are shuffled deterministically when randomize_roles is True.""" |
| 76 | + seed = 10 |
| 77 | + # This is the expected order based on the LCG and Fisher-Yates implementation |
| 78 | + expected_permuted_roles = [ |
| 79 | + (RoleConst.WEREWOLF, {}), |
| 80 | + (RoleConst.SEER, {}), |
| 81 | + (RoleConst.DOCTOR, {"allow_self_save": True}), |
| 82 | + (RoleConst.VILLAGER, {}), |
| 83 | + ] |
| 84 | + |
| 85 | + players = create_players_from_agents_config(sample_agents_config, randomize_roles=True, seed=seed) |
| 86 | + |
| 87 | + # The roles should be assigned to the agents in the new permuted order |
| 88 | + for i, player in enumerate(players): |
| 89 | + expected_role_name, expected_role_params = expected_permuted_roles[i] |
| 90 | + assert player.role.name == expected_role_name |
| 91 | + # Check if role_params match, excluding defaults that might be added by pydantic |
| 92 | + for key, value in expected_role_params.items(): |
| 93 | + assert getattr(player.role, key) == value |
| 94 | + |
| 95 | + |
| 96 | +def test_create_players_no_shuffling_without_flag(sample_agents_config): |
| 97 | + """Tests that roles are not shuffled if randomize_roles is False, even with a seed.""" |
| 98 | + seed = 42 |
| 99 | + players = create_players_from_agents_config(sample_agents_config, randomize_roles=False, seed=seed) |
| 100 | + |
| 101 | + original_roles = [agent["role"] for agent in sample_agents_config] |
| 102 | + assigned_roles = [p.role.name for p in players] |
| 103 | + |
| 104 | + assert original_roles == assigned_roles |
0 commit comments