Skip to content

Commit 6177566

Browse files
committed
test(api): add test for game over and no discards left
1 parent 0acf22f commit 6177566

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

tests/test_api_functions.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,27 @@ def test_play_hand(
299299
assert len(set(final_card_keys) - set(init_card_keys)) == expected_new_cards
300300
assert set(final_card_keys) & set(played_hand_keys) == set()
301301

302-
def test_play_hand_winning(
303-
self, udp_client: socket.socket, setup_and_teardown: dict
304-
) -> None:
302+
def test_play_hand_winning(self, udp_client: socket.socket) -> None:
305303
"""Test playing a winning hand (four of a kind)"""
306-
_ = setup_and_teardown
307304
play_hand_args = {"action": "play_hand", "cards": [0, 1, 2, 3]}
308305
game_state = send_and_receive_api_message(
309306
udp_client, "play_hand_or_discard", play_hand_args
310307
)
311308
assert game_state["state"] == State.ROUND_EVAL.value
312309

313-
def test_play_hand_invalid_cards(self, udp_client: socket.socket) -> None:
310+
def test_play_hands_losing(self, udp_client: socket.socket) -> None:
311+
"""Test playing a series of losing hands and reach Main menu again."""
312+
for _ in range(4):
313+
game_state = send_and_receive_api_message(
314+
udp_client,
315+
"play_hand_or_discard",
316+
{"action": "play_hand", "cards": [0]},
317+
)
318+
assert game_state["state"] == State.GAME_OVER.value
319+
320+
def test_play_hand_or_discard_invalid_cards(
321+
self, udp_client: socket.socket
322+
) -> None:
314323
"""Test playing a hand with invalid card indices returns error."""
315324
play_hand_args = {"action": "play_hand", "cards": [10, 11, 12, 13, 14]}
316325
response = send_and_receive_api_message(
@@ -376,3 +385,28 @@ def test_discard(
376385
)
377386
assert len(set(final_card_keys) - set(init_card_keys)) == expected_new_cards
378387
assert set(final_card_keys) & set(discarded_hand_keys) == set()
388+
389+
def test_try_to_discard_when_no_discards_left(
390+
self, udp_client: socket.socket
391+
) -> None:
392+
"""Test trying to discard when no discards are left."""
393+
for _ in range(4):
394+
game_state = send_and_receive_api_message(
395+
udp_client,
396+
"play_hand_or_discard",
397+
{"action": "discard", "cards": [0]},
398+
)
399+
assert game_state["state"] == State.SELECTING_HAND.value
400+
assert game_state["game"]["hands_played"] == 0
401+
assert game_state["game"]["current_round"]["discards_left"] == 0
402+
403+
response = send_and_receive_api_message(
404+
udp_client,
405+
"play_hand_or_discard",
406+
{"action": "discard", "cards": [0]},
407+
)
408+
409+
# Should receive error response for no discards left
410+
assert isinstance(response, dict)
411+
assert "error" in response
412+
assert "No discards left" in response["error"]

0 commit comments

Comments
 (0)