""" Tests for Phase Transitions. Verifies that invariants hold and state is securely cleaned up when moving between phases. """ from engine.game.enums import Phase class TestPhaseTransitions: def test_main_to_live_set_clears_looked_cards(self, validated_game_state): """ Verify that transitioning from MAIN to LIVE_SET clears 'looked_cards'. Stale looked_cards can cause information leaks or illegal actions. """ gs = validated_game_state p = gs.players[0] # Setup: MAIN phase, looked_cards populated (simulating a "Look at top 3" ability) # We need to simulate P2 (last player) ending turn to trigger LIVE_SET gs.phase = Phase.MAIN gs.current_player = 1 # Player 1 (2nd player) gs.looked_cards = [101, 102, 103] # Action 0 in MAIN = End Main Phase gs = gs.step(0) # Assert assert gs.phase == Phase.LIVE_SET assert gs.looked_cards == [], "looked_cards should be cleared after MAIN phase" def test_live_result_to_active_clears_performance_data(self, validated_game_state): """ Verify that transitioning from LIVE_RESULT to ACTIVE (next turn) clears performance data. """ gs = validated_game_state p = gs.players[0] # Setup: LIVE_RESULT phase gs.phase = Phase.LIVE_RESULT # Dirty state that should be cleaned gs.performance_results = {"p1": 5000, "p2": 4000} p.performance_abilities_processed = True # Setup: Simulate the state where we are waiting for user confirmation # The engine requires the user to acknowledge results ("CONTINUE_LIVE_RESULT") # before transitioning to ACTIVE and clearing data. gs.pending_choices = [("CONTINUE_LIVE_RESULT", {"player_id": 0})] gs.current_player = 0 # Action 0 = Select first choice (Confirm) gs = gs.step(0) # Assert # Should be back to start of turn sequence (ACTIVE) # Note: Depending on engine logic, it might go to P2 LIVE_RESULT if multiplayer? # Assuming verified fixture has 2 players. # But if it's new turn, phase should be ACTIVE (Phase 1). assert gs.performance_results == {}, "performance_results should be cleared" assert gs.players[0].performance_abilities_processed is False, "performance processing flag should be reset" def test_turn_change_resets_temporary_flags(self, validated_game_state): """ Verify that 'members_played_this_turn' and other turn-scoped flags are reset. """ gs = validated_game_state p = gs.players[0] # Setup: P1 (next player) has dirty state that needs clearing gs.players[1].members_played_this_turn.fill(True) # Setup: Pending choice to confirm results and advance gs.pending_choices = [("CONTINUE_LIVE_RESULT", {"player_id": 0})] gs.current_player = 0 # Transition gs = gs.step(0) # Confirm -> _finish_live_result -> _do_active_phase (P1) # Check P1 (current player) assert not any(gs.players[1].members_played_this_turn), "P1's history should be cleared"