Spaces:
Configuration error
Configuration error
File size: 19,630 Bytes
c903325 69373e6 1d4d416 c903325 ad0932c c903325 1d4d416 bf013e2 1d4d416 c903325 1d4d416 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | """
Unit tests for pycatan.game_manager module.
Tests the GameManager class and its basic functionality.
"""
import pytest
from unittest.mock import Mock, patch
import uuid
from pycatan.management.actions import Action, ActionType, ActionResult, GameState, GamePhase
from pycatan.players.user import create_test_user, UserInputError
from pycatan.management.game_manager import GameManager
from pycatan.core.card import ResCard
class TestGameManagerInitialization:
"""Test GameManager initialization and basic properties."""
def test_gamemanager_creation_basic(self):
"""Test basic GameManager creation."""
users = [create_test_user("Alice", 0), create_test_user("Bob", 1)]
gm = GameManager(users)
assert gm.num_players == 2
assert len(gm.users) == 2
assert gm.current_player_id == 0
assert not gm.is_running
assert not gm.is_paused
assert gm.game_id is not None
assert len(gm.game_id) > 0
def test_gamemanager_creation_with_config(self):
"""Test GameManager creation with custom config."""
users = [create_test_user("Charlie", 0)]
config = {"board_type": "custom", "victory_points": 15}
gm = GameManager(users, config)
assert gm.config == config
assert gm.num_players == 1
def test_gamemanager_invalid_users(self):
"""Test GameManager creation with invalid users."""
# Empty users list
with pytest.raises(ValueError, match="User list cannot be empty"):
GameManager([])
# Duplicate user IDs
users = [create_test_user("Alice", 0), create_test_user("Bob", 0)]
with pytest.raises(ValueError, match="All user IDs must be unique"):
GameManager(users)
def test_gamemanager_properties(self):
"""Test GameManager properties."""
users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1),
create_test_user("Charlie", 2)
]
gm = GameManager(users)
assert gm.current_user == users[0] # First user is current
assert gm.current_user.name == "Alice"
# Check game state
state = gm.get_full_state()
assert state.game_id == gm.game_id
assert state.current_player == 0
assert state.turn_number == 0
assert state.game_phase == GamePhase.SETUP_FIRST_ROUND
class TestGameManagerFlow:
"""Test basic game flow operations."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1)
]
self.gm = GameManager(self.users)
def test_start_game(self):
"""Test starting a game."""
assert not self.gm.is_running
result = self.gm.start_game()
assert result is True
assert self.gm.is_running
assert not self.gm.is_paused
# Can't start again
result = self.gm.start_game()
assert result is False
def test_pause_resume_game(self):
"""Test pausing and resuming a game."""
# Can't pause when not running
assert not self.gm.pause_game()
self.gm.start_game()
# Can pause when running
assert self.gm.pause_game()
assert self.gm.is_paused
# Can't pause when already paused
assert not self.gm.pause_game()
# Can resume when paused
assert self.gm.resume_game()
assert not self.gm.is_paused
# Can't resume when not paused
assert not self.gm.resume_game()
def test_end_game(self):
"""Test ending a game."""
# Can't end when not running
assert not self.gm.end_game()
self.gm.start_game()
# Can end when running
assert self.gm.end_game()
assert not self.gm.is_running
assert not self.gm.is_paused
def test_post_game_prompts_require_end_game_after_winner(self):
"""After a win, each player gets one final END_GAME prompt."""
for user in self.users:
user.set_next_action(Action(ActionType.END_GAME, user.user_id, {}))
events = []
for user in self.users:
user.notify_game_event = lambda event_type, message, affected_players=None: events.append(
(event_type, message)
)
self.gm.start_game()
self.gm.game.players[0].victory_points = 5
assert self.gm._check_game_end_conditions()
self.gm._handle_post_game_reactions()
assert self.gm._post_game_enders == {0, 1}
assert self.users[0].last_input_call["allowed_actions"] == ["END_GAME"]
assert "You won" in self.users[0].last_input_call["prompt_message"]
assert "You lost" in self.users[1].last_input_call["prompt_message"]
assert any(event_type == "end_game" for event_type, _message in events)
class TestGameManagerActions:
"""Test action execution and handling."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1)
]
self.gm = GameManager(self.users)
def test_trade_propose_records_pending_trade_and_accepts(self):
alice = create_test_user("Alice", 0)
bob = create_test_user("Bob", 1)
trade_offers = []
trade_responses = []
for user in [alice, bob]:
user.notify_trade_offer = lambda trade_id, proposer, target, offer, request: trade_offers.append(
(trade_id, proposer, target, offer, request)
)
user.notify_trade_response = lambda trade_id, status, responder: trade_responses.append(
(trade_id, status, responder)
)
bob.set_next_action(Action(ActionType.TRADE_ACCEPT, 1, {}))
gm = GameManager([alice, bob], random_seed=0)
gm.game.players[0].cards = [ResCard.Sheep]
gm.game.players[1].cards = [ResCard.Wood]
action = Action(
ActionType.TRADE_PROPOSE,
0,
{"target_player": 1, "offer": {"sheep": 1}, "request": {"wood": 1}},
)
result = gm._execute_trade_propose(action)
assert result.success
assert ResCard.Wood in gm.game.players[0].cards
assert ResCard.Sheep in gm.game.players[1].cards
assert action.parameters["trade_status"] == "accepted"
assert action.parameters["trade_id"].startswith("trade_")
assert "Trade offer" in bob.last_input_call["prompt_message"]
assert bob.last_input_call["allowed_actions"] == ["TRADE_ACCEPT", "TRADE_REJECT"]
assert trade_offers
assert trade_responses[-1][1] == "accepted"
assert gm._current_game_state.pending_trades == []
def test_trade_propose_rejects_invalid_target_id(self):
gm = GameManager(self.users, random_seed=0)
action = Action(
ActionType.TRADE_PROPOSE,
0,
{"target_player": -1, "offer": {"sheep": 1}, "request": {"wood": 1}},
)
result = gm._execute_trade_propose(action)
assert not result.success
assert result.status_code == "INVALID_PLAYER_ID"
def test_trade_propose_prompts_target_to_reject_when_target_lacks_cards(self):
alice = create_test_user("Alice", 0)
bob = create_test_user("Bob", 1)
trade_responses = []
for user in [alice, bob]:
user.notify_trade_offer = lambda *args: None
user.notify_trade_response = lambda trade_id, status, responder: trade_responses.append(
(trade_id, status, responder)
)
bob.set_next_action(Action(ActionType.TRADE_REJECT, 1, {}))
gm = GameManager([alice, bob], random_seed=0)
gm.game.players[0].cards = [ResCard.Sheep]
gm.game.players[1].cards = []
action = Action(
ActionType.TRADE_PROPOSE,
0,
{"target_player": 1, "offer": {"sheep": 1}, "request": {"wood": 1}},
)
result = gm._execute_trade_propose(action)
assert not result.success
assert result.status_code == "INSUFFICIENT_RESOURCES"
assert "doesn't have the required cards" in result.error_message
assert "You do not have the requested cards" in bob.last_input_call["prompt_message"]
assert bob.last_input_call["allowed_actions"] == ["TRADE_REJECT"]
assert action.parameters["trade_status"] == "rejected"
assert trade_responses[-1][1] == "rejected"
assert gm._current_game_state.pending_trades == []
def test_execute_action_rejects_action_not_allowed_in_phase(self):
"""Test that actions outside the current phase do not execute."""
self.gm.start_game()
result = self.gm.execute_action(Action(ActionType.END_TURN, player_id=0))
assert result.success is False
assert result.status_code == "ACTION_NOT_ALLOWED"
assert "PLACE_STARTING_SETTLEMENT" in result.error_message
def test_get_full_state_includes_current_dice_roll(self):
"""Test that AI-facing game state keeps the current turn dice roll."""
self.gm._current_game_state.dice_rolled = (5, 3)
state = self.gm.get_full_state()
assert state.dice_rolled == (5, 3)
def test_distribution_summary_uses_player_names_and_counts(self):
"""Test resource distribution summary is useful for AI prompts."""
summary = self.gm._format_distribution_summary(
{
"Player 1": ["sheep", "wheat", "wheat"],
"Player 2": ["ore"],
}
)
assert "Alice +1 sheep, 2 wheat" in summary
assert "Bob +1 ore" in summary
def test_execute_action_game_not_running(self):
"""Test executing action when game is not running."""
action = Action(ActionType.END_TURN, player_id=0)
result = self.gm.execute_action(action)
assert not result.success
assert "Game is not running" in result.error_message
assert result.status_code == "GAME_NOT_RUNNING"
def test_execute_action_wrong_turn(self):
"""Test executing action on wrong player's turn."""
self.gm.start_game()
action = Action(ActionType.END_TURN, player_id=1) # Bob's action on Alice's turn
result = self.gm.execute_action(action)
assert not result.success
assert "Not player 1's turn" in result.error_message
assert result.status_code == "NOT_YOUR_TURN"
def test_execute_action_end_turn(self):
"""Test executing end turn action."""
self.gm.start_game()
action = Action(ActionType.END_TURN, player_id=0)
result = self.gm.execute_action(action)
assert result.success
assert self.gm.current_player_id == 1 # Switched to next player
assert 0 in result.affected_players
assert 1 in result.affected_players
def test_execute_action_implemented_validation(self):
"""Test that building actions now work and validate properly."""
self.gm.start_game()
# Force game phase to NORMAL_PLAY to test resource validation
self.gm._current_game_state.game_phase = GamePhase.NORMAL_PLAY
# Test settlement building without starting mode (should fail due to cards)
action = Action(ActionType.BUILD_SETTLEMENT, player_id=0, parameters={'point_coords': (1, 1)})
result = self.gm.execute_action(action)
assert not result.success
assert "not enough cards" in result.error_message.lower()
assert result.status_code == "INSUFFICIENT_RESOURCES"
# Test settlement building with starting mode (should succeed)
# Note: Even in NORMAL_PLAY, if we explicitly pass is_starting=True, it should work (legacy/testing support)
action_starting = Action(ActionType.BUILD_SETTLEMENT, player_id=0, parameters={'point_coords': [0, 0], 'is_starting': True})
result_starting = self.gm.execute_action(action_starting)
assert result_starting.success
assert result_starting.updated_state is not None
def test_execute_action_setup_phase_inference(self):
"""Test that setup phase automatically infers is_starting=True."""
self.gm.start_game()
# Should be in SETUP_FIRST_ROUND by default
assert self.gm._current_game_state.game_phase == GamePhase.SETUP_FIRST_ROUND
# Test settlement building WITHOUT explicit is_starting=True
# This should now SUCCEED because GameManager infers it from the phase
action = Action(ActionType.BUILD_SETTLEMENT, player_id=0, parameters={'point_coords': [0, 0]})
result = self.gm.execute_action(action)
assert result.success
assert result.status_code == "ALL_GOOD"
def test_action_history(self):
"""Test that actions are recorded in history."""
self.gm.start_game()
action1 = Action(ActionType.END_TURN, player_id=0)
action2 = Action(ActionType.END_TURN, player_id=1)
self.gm.execute_action(action1)
self.gm.execute_action(action2)
history = self.gm.get_action_history()
assert len(history) == 2
assert history[0] == action1
assert history[1] == action2
class TestGameManagerUserInteraction:
"""Test user interaction and input handling."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1)
]
self.gm = GameManager(self.users)
def test_request_user_input_valid(self):
"""Test requesting input from a valid user."""
user = self.users[0]
expected_action = Action(ActionType.BUILD_SETTLEMENT, 0, {'point_coords': (1, 1)})
user.set_next_action(expected_action)
result = self.gm.request_user_input(0, "Build something")
assert result == expected_action
assert user.last_input_call is not None
assert user.last_input_call['prompt_message'] == "Build something"
def test_request_user_input_invalid_id(self):
"""Test requesting input with invalid user ID."""
with pytest.raises(UserInputError, match="Invalid user ID"):
self.gm.request_user_input(999, "Test")
def test_request_user_input_inactive_user(self):
"""Test requesting input from inactive user."""
self.users[0].set_active(False)
with pytest.raises(UserInputError, match="User 0 is not active"):
self.gm.request_user_input(0, "Test")
def test_get_user_by_id(self):
"""Test getting user by ID."""
user = self.gm.get_user_by_id(0)
assert user == self.users[0]
user = self.gm.get_user_by_id(1)
assert user == self.users[1]
user = self.gm.get_user_by_id(999)
assert user is None
class TestGameManagerTurnManagement:
"""Test turn management and player rotation."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1),
create_test_user("Charlie", 2)
]
self.gm = GameManager(self.users)
self.gm.start_game()
def test_turn_rotation(self):
"""Test that turns rotate correctly between players."""
# Start with player 0
assert self.gm.current_player_id == 0
assert self.gm.current_user.name == "Alice"
# End turn -> player 1
self.gm.execute_action(Action(ActionType.END_TURN, 0))
assert self.gm.current_player_id == 1
assert self.gm.current_user.name == "Bob"
# End turn -> player 2
self.gm.execute_action(Action(ActionType.END_TURN, 1))
assert self.gm.current_player_id == 2
assert self.gm.current_user.name == "Charlie"
# End turn -> back to player 0
self.gm.execute_action(Action(ActionType.END_TURN, 2))
assert self.gm.current_player_id == 0
assert self.gm.current_user.name == "Alice"
def test_turn_number_increments(self):
"""Test that turn number increments correctly."""
initial_turn = self.gm.get_full_state().turn_number
# Execute a few end turn actions
for player_id in [0, 1, 2]:
self.gm.execute_action(Action(ActionType.END_TURN, player_id))
final_turn = self.gm.get_full_state().turn_number
assert final_turn == initial_turn + 3
class TestGameManagerStringRepresentation:
"""Test string representations of GameManager."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [create_test_user("Alice", 0)]
self.gm = GameManager(self.users)
def test_str_representation(self):
"""Test string representation."""
str_repr = str(self.gm)
assert "GameManager" in str_repr
assert "players=1" in str_repr
assert "status=stopped" in str_repr
self.gm.start_game()
str_repr = str(self.gm)
assert "status=running" in str_repr
self.gm.pause_game()
str_repr = str(self.gm)
assert "status=paused" in str_repr
def test_repr_representation(self):
"""Test detailed representation."""
repr_str = repr(self.gm)
assert "GameManager" in repr_str
assert "players=1" in repr_str
assert "current_player=0" in repr_str
assert "turn=0" in repr_str
assert "running=False" in repr_str
assert "paused=False" in repr_str
class TestGameManagerIntegration:
"""Integration tests with actual game components."""
def setup_method(self):
"""Set up test GameManager for each test."""
self.users = [
create_test_user("Alice", 0),
create_test_user("Bob", 1)
]
self.gm = GameManager(self.users)
def test_integration_with_game_class(self):
"""Test that GameManager properly initializes Game class."""
# GameManager should have created a Game instance
assert self.gm.game is not None
assert hasattr(self.gm.game, 'add_settlement') # Verify it's the right type
# Game should be initialized with correct number of players
assert len(self.gm.game.players) == 2
def test_game_state_consistency(self):
"""Test that game state remains consistent."""
self.gm.start_game()
state = self.gm.get_full_state()
assert state.game_id == self.gm.game_id
assert state.current_player == self.gm.current_player_id
initial_turn = state.turn_number
# After ending a turn, state should update
self.gm.execute_action(Action(ActionType.END_TURN, 0))
new_state = self.gm.get_full_state()
assert new_state.current_player == 1
assert new_state.turn_number == initial_turn + 1
if __name__ == '__main__':
pytest.main([__file__, '-v'])
|