Skip to content

Commit ade21ef

Browse files
committed
test(lua.endpoints): make use of assert_gamestate_response in tests
1 parent 82b4a23 commit ade21ef

16 files changed

+278
-270
lines changed

tests/lua/endpoints/test_add.py

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import pytest
66

7-
from tests.lua.conftest import api, assert_error_response, load_fixture
7+
from tests.lua.conftest import (
8+
api,
9+
assert_error_response,
10+
assert_gamestate_response,
11+
load_fixture,
12+
)
813

914

1015
class TestAddEndpoint:
@@ -20,8 +25,9 @@ def test_add_joker(self, client: socket.socket) -> None:
2025
assert gamestate["state"] == "SELECTING_HAND"
2126
assert gamestate["jokers"]["count"] == 0
2227
response = api(client, "add", {"key": "j_joker"})
23-
assert response["result"]["jokers"]["count"] == 1
24-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
28+
after = assert_gamestate_response(response)
29+
assert after["jokers"]["count"] == 1
30+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
2531

2632
def test_add_consumable_tarot(self, client: socket.socket) -> None:
2733
"""Test adding a tarot consumable with valid key."""
@@ -33,8 +39,9 @@ def test_add_consumable_tarot(self, client: socket.socket) -> None:
3339
assert gamestate["state"] == "SELECTING_HAND"
3440
assert gamestate["consumables"]["count"] == 0
3541
response = api(client, "add", {"key": "c_fool"})
36-
assert response["result"]["consumables"]["count"] == 1
37-
assert response["result"]["consumables"]["cards"][0]["key"] == "c_fool"
42+
after = assert_gamestate_response(response)
43+
assert after["consumables"]["count"] == 1
44+
assert after["consumables"]["cards"][0]["key"] == "c_fool"
3845

3946
def test_add_consumable_planet(self, client: socket.socket) -> None:
4047
"""Test adding a planet consumable with valid key."""
@@ -46,8 +53,9 @@ def test_add_consumable_planet(self, client: socket.socket) -> None:
4653
assert gamestate["state"] == "SELECTING_HAND"
4754
assert gamestate["consumables"]["count"] == 0
4855
response = api(client, "add", {"key": "c_mercury"})
49-
assert response["result"]["consumables"]["count"] == 1
50-
assert response["result"]["consumables"]["cards"][0]["key"] == "c_mercury"
56+
after = assert_gamestate_response(response)
57+
assert after["consumables"]["count"] == 1
58+
assert after["consumables"]["cards"][0]["key"] == "c_mercury"
5159

5260
def test_add_consumable_spectral(self, client: socket.socket) -> None:
5361
"""Test adding a spectral consumable with valid key."""
@@ -59,8 +67,9 @@ def test_add_consumable_spectral(self, client: socket.socket) -> None:
5967
assert gamestate["state"] == "SELECTING_HAND"
6068
assert gamestate["consumables"]["count"] == 0
6169
response = api(client, "add", {"key": "c_familiar"})
62-
assert response["result"]["consumables"]["count"] == 1
63-
assert response["result"]["consumables"]["cards"][0]["key"] == "c_familiar"
70+
after = assert_gamestate_response(response)
71+
assert after["consumables"]["count"] == 1
72+
assert after["consumables"]["cards"][0]["key"] == "c_familiar"
6473

6574
def test_add_voucher(self, client: socket.socket) -> None:
6675
"""Test adding a voucher with valid key in SHOP state."""
@@ -72,8 +81,9 @@ def test_add_voucher(self, client: socket.socket) -> None:
7281
assert gamestate["state"] == "SHOP"
7382
assert gamestate["vouchers"]["count"] == 0
7483
response = api(client, "add", {"key": "v_overstock_norm"})
75-
assert response["result"]["vouchers"]["count"] == 1
76-
assert response["result"]["vouchers"]["cards"][0]["key"] == "v_overstock_norm"
84+
after = assert_gamestate_response(response)
85+
assert after["vouchers"]["count"] == 1
86+
assert after["vouchers"]["cards"][0]["key"] == "v_overstock_norm"
7787

7888
def test_add_playing_card(self, client: socket.socket) -> None:
7989
"""Test adding a playing card with valid key."""
@@ -85,8 +95,9 @@ def test_add_playing_card(self, client: socket.socket) -> None:
8595
assert gamestate["state"] == "SELECTING_HAND"
8696
assert gamestate["hand"]["count"] == 8
8797
response = api(client, "add", {"key": "H_A"})
88-
assert response["result"]["hand"]["count"] == 9
89-
assert response["result"]["hand"]["cards"][8]["key"] == "H_A"
98+
after = assert_gamestate_response(response)
99+
assert after["hand"]["count"] == 9
100+
assert after["hand"]["cards"][8]["key"] == "H_A"
90101

91102
def test_add_no_key_provided(self, client: socket.socket) -> None:
92103
"""Test add endpoint with no key parameter."""
@@ -205,9 +216,10 @@ def test_add_playing_card_with_seal(self, client: socket.socket, seal: str) -> N
205216
assert gamestate["state"] == "SELECTING_HAND"
206217
assert gamestate["hand"]["count"] == 8
207218
response = api(client, "add", {"key": "H_A", "seal": seal})
208-
assert response["result"]["hand"]["count"] == 9
209-
assert response["result"]["hand"]["cards"][8]["key"] == "H_A"
210-
assert response["result"]["hand"]["cards"][8]["modifier"]["seal"] == seal
219+
after = assert_gamestate_response(response)
220+
assert after["hand"]["count"] == 9
221+
assert after["hand"]["cards"][8]["key"] == "H_A"
222+
assert after["hand"]["cards"][8]["modifier"]["seal"] == seal
211223

212224
def test_add_playing_card_invalid_seal(self, client: socket.socket) -> None:
213225
"""Test adding a playing card with invalid seal value."""
@@ -258,11 +270,10 @@ def test_add_joker_with_edition(self, client: socket.socket, edition: str) -> No
258270
assert gamestate["state"] == "SHOP"
259271
assert gamestate["jokers"]["count"] == 0
260272
response = api(client, "add", {"key": "j_joker", "edition": edition})
261-
assert response["result"]["jokers"]["count"] == 1
262-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
263-
assert (
264-
response["result"]["jokers"]["cards"][0]["modifier"]["edition"] == edition
265-
)
273+
after = assert_gamestate_response(response)
274+
assert after["jokers"]["count"] == 1
275+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
276+
assert after["jokers"]["cards"][0]["modifier"]["edition"] == edition
266277

267278
@pytest.mark.parametrize("edition", ["HOLO", "FOIL", "POLYCHROME", "NEGATIVE"])
268279
def test_add_playing_card_with_edition(
@@ -277,9 +288,10 @@ def test_add_playing_card_with_edition(
277288
assert gamestate["state"] == "SELECTING_HAND"
278289
assert gamestate["hand"]["count"] == 8
279290
response = api(client, "add", {"key": "H_A", "edition": edition})
280-
assert response["result"]["hand"]["count"] == 9
281-
assert response["result"]["hand"]["cards"][8]["key"] == "H_A"
282-
assert response["result"]["hand"]["cards"][8]["modifier"]["edition"] == edition
291+
after = assert_gamestate_response(response)
292+
assert after["hand"]["count"] == 9
293+
assert after["hand"]["cards"][8]["key"] == "H_A"
294+
assert after["hand"]["cards"][8]["modifier"]["edition"] == edition
283295

284296
def test_add_consumable_with_negative_edition(self, client: socket.socket) -> None:
285297
"""Test adding a consumable with NEGATIVE edition (only valid edition for consumables)."""
@@ -291,12 +303,10 @@ def test_add_consumable_with_negative_edition(self, client: socket.socket) -> No
291303
assert gamestate["state"] == "SHOP"
292304
assert gamestate["consumables"]["count"] == 0
293305
response = api(client, "add", {"key": "c_fool", "edition": "NEGATIVE"})
294-
assert response["result"]["consumables"]["count"] == 1
295-
assert response["result"]["consumables"]["cards"][0]["key"] == "c_fool"
296-
assert (
297-
response["result"]["consumables"]["cards"][0]["modifier"]["edition"]
298-
== "NEGATIVE"
299-
)
306+
after = assert_gamestate_response(response)
307+
assert after["consumables"]["count"] == 1
308+
assert after["consumables"]["cards"][0]["key"] == "c_fool"
309+
assert after["consumables"]["cards"][0]["modifier"]["edition"] == "NEGATIVE"
300310

301311
@pytest.mark.parametrize("edition", ["HOLO", "FOIL", "POLYCHROME"])
302312
def test_add_consumable_with_non_negative_edition_fails(
@@ -367,12 +377,10 @@ def test_add_playing_card_with_enhancement(
367377
assert gamestate["state"] == "SELECTING_HAND"
368378
assert gamestate["hand"]["count"] == 8
369379
response = api(client, "add", {"key": "H_A", "enhancement": enhancement})
370-
assert response["result"]["hand"]["count"] == 9
371-
assert response["result"]["hand"]["cards"][8]["key"] == "H_A"
372-
assert (
373-
response["result"]["hand"]["cards"][8]["modifier"]["enhancement"]
374-
== enhancement
375-
)
380+
after = assert_gamestate_response(response)
381+
assert after["hand"]["count"] == 9
382+
assert after["hand"]["cards"][8]["key"] == "H_A"
383+
assert after["hand"]["cards"][8]["modifier"]["enhancement"] == enhancement
376384

377385
def test_add_playing_card_invalid_enhancement(self, client: socket.socket) -> None:
378386
"""Test adding a playing card with invalid enhancement value."""
@@ -423,9 +431,10 @@ def test_add_joker_with_eternal(self, client: socket.socket) -> None:
423431
assert gamestate["state"] == "SHOP"
424432
assert gamestate["jokers"]["count"] == 0
425433
response = api(client, "add", {"key": "j_joker", "eternal": True})
426-
assert response["result"]["jokers"]["count"] == 1
427-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
428-
assert response["result"]["jokers"]["cards"][0]["modifier"]["eternal"] is True
434+
after = assert_gamestate_response(response)
435+
assert after["jokers"]["count"] == 1
436+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
437+
assert after["jokers"]["cards"][0]["modifier"]["eternal"] is True
429438

430439
@pytest.mark.parametrize("key", ["c_fool", "v_overstock_norm"])
431440
def test_add_non_joker_with_eternal_fails(
@@ -473,11 +482,10 @@ def test_add_joker_with_perishable(
473482
assert gamestate["state"] == "SHOP"
474483
assert gamestate["jokers"]["count"] == 0
475484
response = api(client, "add", {"key": "j_joker", "perishable": rounds})
476-
assert response["result"]["jokers"]["count"] == 1
477-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
478-
assert (
479-
response["result"]["jokers"]["cards"][0]["modifier"]["perishable"] == rounds
480-
)
485+
after = assert_gamestate_response(response)
486+
assert after["jokers"]["count"] == 1
487+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
488+
assert after["jokers"]["cards"][0]["modifier"]["perishable"] == rounds
481489

482490
def test_add_joker_with_eternal_and_perishable(self, client: socket.socket) -> None:
483491
"""Test adding a joker with both eternal and perishable stickers."""
@@ -491,10 +499,11 @@ def test_add_joker_with_eternal_and_perishable(self, client: socket.socket) -> N
491499
response = api(
492500
client, "add", {"key": "j_joker", "eternal": True, "perishable": 5}
493501
)
494-
assert response["result"]["jokers"]["count"] == 1
495-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
496-
assert response["result"]["jokers"]["cards"][0]["modifier"]["eternal"] is True
497-
assert response["result"]["jokers"]["cards"][0]["modifier"]["perishable"] == 5
502+
after = assert_gamestate_response(response)
503+
assert after["jokers"]["count"] == 1
504+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
505+
assert after["jokers"]["cards"][0]["modifier"]["eternal"] is True
506+
assert after["jokers"]["cards"][0]["modifier"]["perishable"] == 5
498507

499508
@pytest.mark.parametrize("invalid_value", [0, -1])
500509
def test_add_joker_with_perishable_invalid_integer_fails(
@@ -580,9 +589,10 @@ def test_add_joker_with_rental(self, client: socket.socket) -> None:
580589
assert gamestate["state"] == "SHOP"
581590
assert gamestate["jokers"]["count"] == 0
582591
response = api(client, "add", {"key": "j_joker", "rental": True})
583-
assert response["result"]["jokers"]["count"] == 1
584-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
585-
assert response["result"]["jokers"]["cards"][0]["modifier"]["rental"] is True
592+
after = assert_gamestate_response(response)
593+
assert after["jokers"]["count"] == 1
594+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
595+
assert after["jokers"]["cards"][0]["modifier"]["rental"] is True
586596

587597
@pytest.mark.parametrize("key", ["c_fool", "v_overstock_norm"])
588598
def test_add_non_joker_with_rental_fails(
@@ -614,10 +624,11 @@ def test_add_joker_with_rental_and_eternal(self, client: socket.socket) -> None:
614624
response = api(
615625
client, "add", {"key": "j_joker", "rental": True, "eternal": True}
616626
)
617-
assert response["result"]["jokers"]["count"] == 1
618-
assert response["result"]["jokers"]["cards"][0]["key"] == "j_joker"
619-
assert response["result"]["jokers"]["cards"][0]["modifier"]["rental"] is True
620-
assert response["result"]["jokers"]["cards"][0]["modifier"]["eternal"] is True
627+
after = assert_gamestate_response(response)
628+
assert after["jokers"]["count"] == 1
629+
assert after["jokers"]["cards"][0]["key"] == "j_joker"
630+
assert after["jokers"]["cards"][0]["modifier"]["rental"] is True
631+
assert after["jokers"]["cards"][0]["modifier"]["eternal"] is True
621632

622633
def test_add_playing_card_with_rental_fails(self, client: socket.socket) -> None:
623634
"""Test that rental cannot be applied to playing cards."""

tests/lua/endpoints/test_buy.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import pytest
66

7-
from tests.lua.conftest import api, assert_error_response, load_fixture
7+
from tests.lua.conftest import (
8+
api,
9+
assert_error_response,
10+
assert_gamestate_response,
11+
load_fixture,
12+
)
813

914

1015
class TestBuyEndpoint:
@@ -128,15 +133,17 @@ def test_buy_joker_success(self, client: socket.socket) -> None:
128133
assert gamestate["state"] == "SHOP"
129134
assert gamestate["shop"]["cards"][0]["set"] == "JOKER"
130135
response = api(client, "buy", {"card": 0})
131-
assert response["result"]["jokers"]["cards"][0]["set"] == "JOKER"
136+
gamestate = assert_gamestate_response(response)
137+
assert gamestate["jokers"]["cards"][0]["set"] == "JOKER"
132138

133139
def test_buy_consumable_success(self, client: socket.socket) -> None:
134140
"""Test buying a consumable card (Planet/Tarot/Spectral) from shop."""
135141
gamestate = load_fixture(client, "buy", "state-SHOP--shop.cards[1].set-PLANET")
136142
assert gamestate["state"] == "SHOP"
137143
assert gamestate["shop"]["cards"][1]["set"] == "PLANET"
138144
response = api(client, "buy", {"card": 1})
139-
assert response["result"]["consumables"]["cards"][0]["set"] == "PLANET"
145+
gamestate = assert_gamestate_response(response)
146+
assert gamestate["consumables"]["cards"][0]["set"] == "PLANET"
140147

141148
def test_buy_voucher_success(self, client: socket.socket) -> None:
142149
"""Test buying a voucher from shop."""
@@ -146,8 +153,9 @@ def test_buy_voucher_success(self, client: socket.socket) -> None:
146153
assert gamestate["state"] == "SHOP"
147154
assert gamestate["vouchers"]["cards"][0]["set"] == "VOUCHER"
148155
response = api(client, "buy", {"voucher": 0})
149-
assert response["result"]["used_vouchers"] is not None
150-
assert len(response["result"]["used_vouchers"]) > 0
156+
gamestate = assert_gamestate_response(response)
157+
assert gamestate["used_vouchers"] is not None
158+
assert len(gamestate["used_vouchers"]) > 0
151159

152160
def test_buy_packs_success(self, client: socket.socket) -> None:
153161
"""Test buying a pack from shop."""
@@ -160,8 +168,9 @@ def test_buy_packs_success(self, client: socket.socket) -> None:
160168
assert gamestate["packs"]["cards"][0]["label"] == "Buffoon Pack"
161169
assert gamestate["packs"]["cards"][1]["label"] == "Standard Pack"
162170
response = api(client, "buy", {"pack": 0})
163-
assert response["result"]["pack"] is not None
164-
assert len(response["result"]["pack"]["cards"]) > 0
171+
gamestate = assert_gamestate_response(response)
172+
assert gamestate["pack"] is not None
173+
assert len(gamestate["pack"]["cards"]) > 0
165174

166175

167176
class TestBuyEndpointValidation:

tests/lua/endpoints/test_cash_out.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
"""Tests for src/lua/endpoints/cash_out.lua"""
22

33
import socket
4-
from typing import Any
54

6-
from tests.lua.conftest import api, assert_error_response, load_fixture
7-
8-
9-
def verify_cash_out_response(response: dict[str, Any]) -> None:
10-
"""Verify that cash_out response has expected fields."""
11-
# Verify state field - should transition to SHOP after cashing out
12-
assert "state" in response["result"]
13-
assert isinstance(response["result"]["state"], str)
14-
assert response["result"]["state"] == "SHOP"
15-
16-
# Verify shop field exists
17-
assert "shop" in response["result"]
18-
assert isinstance(response["result"]["shop"], dict)
5+
from tests.lua.conftest import (
6+
api,
7+
assert_error_response,
8+
assert_gamestate_response,
9+
load_fixture,
10+
)
1911

2012

2113
class TestCashOutEndpoint:
@@ -26,8 +18,7 @@ def test_cash_out_from_ROUND_EVAL(self, client: socket.socket) -> None:
2618
gamestate = load_fixture(client, "cash_out", "state-ROUND_EVAL")
2719
assert gamestate["state"] == "ROUND_EVAL"
2820
response = api(client, "cash_out", {})
29-
verify_cash_out_response(response)
30-
assert response["result"]["state"] == "SHOP"
21+
assert_gamestate_response(response, state="SHOP")
3122

3223

3324
class TestCashOutEndpointStateRequirements:

tests/lua/endpoints/test_discard.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import socket
44

5-
from tests.lua.conftest import api, assert_error_response, load_fixture
5+
from tests.lua.conftest import (
6+
api,
7+
assert_error_response,
8+
assert_gamestate_response,
9+
load_fixture,
10+
)
611

712

813
class TestDiscardEndpoint:
@@ -53,25 +58,19 @@ def test_discard_no_discards_left(self, client: socket.socket) -> None:
5358

5459
def test_discard_valid_single_card(self, client: socket.socket) -> None:
5560
"""Test discard endpoint with valid single card."""
56-
gamestate = load_fixture(client, "discard", "state-SELECTING_HAND")
57-
assert gamestate["state"] == "SELECTING_HAND"
61+
before = load_fixture(client, "discard", "state-SELECTING_HAND")
62+
assert before["state"] == "SELECTING_HAND"
5863
response = api(client, "discard", {"cards": [0]})
59-
assert response["result"]["state"] == "SELECTING_HAND"
60-
assert (
61-
response["result"]["round"]["discards_left"]
62-
== gamestate["round"]["discards_left"] - 1
63-
)
64+
after = assert_gamestate_response(response, state="SELECTING_HAND")
65+
assert after["round"]["discards_left"] == before["round"]["discards_left"] - 1
6466

6567
def test_discard_valid_multiple_cards(self, client: socket.socket) -> None:
6668
"""Test discard endpoint with valid multiple cards."""
67-
gamestate = load_fixture(client, "discard", "state-SELECTING_HAND")
68-
assert gamestate["state"] == "SELECTING_HAND"
69+
before = load_fixture(client, "discard", "state-SELECTING_HAND")
70+
assert before["state"] == "SELECTING_HAND"
6971
response = api(client, "discard", {"cards": [1, 2, 3]})
70-
assert response["result"]["state"] == "SELECTING_HAND"
71-
assert (
72-
response["result"]["round"]["discards_left"]
73-
== gamestate["round"]["discards_left"] - 1
74-
)
72+
after = assert_gamestate_response(response, state="SELECTING_HAND")
73+
assert after["round"]["discards_left"] == before["round"]["discards_left"] - 1
7574

7675

7776
class TestDiscardEndpointValidation:

0 commit comments

Comments
 (0)