@@ -410,3 +410,58 @@ def test_try_to_discard_when_no_discards_left(
410410 assert isinstance (response , dict )
411411 assert "error" in response
412412 assert "No discards left" in response ["error" ]
413+
414+
415+ class TestCashOut :
416+ """Tests for the cash_out API endpoint."""
417+
418+ @pytest .fixture (autouse = True )
419+ def setup_and_teardown (
420+ self , udp_client : socket .socket
421+ ) -> Generator [None , None , None ]:
422+ """Set up and tear down each test method."""
423+ # Start a run
424+ start_run_args = {
425+ "deck" : "Red Deck" ,
426+ "stake" : 1 ,
427+ "challenge" : None ,
428+ "seed" : "OOOO155" , # four of a kind in first hand
429+ }
430+ send_and_receive_api_message (udp_client , "start_run" , start_run_args )
431+
432+ # Select blind
433+ send_and_receive_api_message (
434+ udp_client , "skip_or_select_blind" , {"action" : "select" }
435+ )
436+
437+ # Play a winning hand (four of a kind) to reach shop
438+ game_state = send_and_receive_api_message (
439+ udp_client ,
440+ "play_hand_or_discard" ,
441+ {"action" : "play_hand" , "cards" : [0 , 1 , 2 , 3 ]},
442+ )
443+ assert game_state ["state" ] == State .ROUND_EVAL .value
444+ yield
445+ send_and_receive_api_message (udp_client , "go_to_menu" , {})
446+
447+ def test_cash_out_success (self , udp_client : socket .socket ) -> None :
448+ """Test successful cash out returns to shop state."""
449+ # Cash out should transition to shop state
450+ game_state = send_and_receive_api_message (udp_client , "cash_out" , {})
451+
452+ # Verify we're in shop state after cash out
453+ assert game_state ["state" ] == State .SHOP .value
454+
455+ def test_cash_out_invalid_state_error (self , udp_client : socket .socket ) -> None :
456+ """Test cash out returns error when not in shop state."""
457+ # Go to menu first to ensure we're not in shop state
458+ send_and_receive_api_message (udp_client , "go_to_menu" , {})
459+
460+ # Try to cash out when not in shop - should return error
461+ response = send_and_receive_api_message (udp_client , "cash_out" , {})
462+
463+ # Verify error response
464+ assert isinstance (response , dict )
465+ assert "error" in response
466+ assert "Cannot cash out when not in shop" in response ["error" ]
467+ assert "state" in response
0 commit comments