Skip to content

Commit 4bc1b61

Browse files
committed
feat: added tests and gamestate for reroll shop
1 parent 845799c commit 4bc1b61

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

docs/protocol-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ The `shop` function supports multiple in-shop actions. Use the `action` field in
143143
| `reroll` | Spend dollars to refresh the shop offer (cost shown in-game). ||
144144

145145
!!! note "Future actions"
146-
Additional shop actions such as `buy_and_use_card`, `open_pack`, and `redeem_voucher` will be added in a future release.
146+
Additional shop actions `buy_and_use_card`, `open_pack`, and `redeem_voucher` will be added soon.
147147

148148
### Errors
149149

src/lua/utils.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ function utils.get_game_state()
110110
-- "free_rerolls": int, (default 0) -- Number of free rerolls in the shop?
111111
hands_left = G.GAME.current_round.hands_left, -- Number of hands left for this round
112112
hands_played = G.GAME.current_round.hands_played, -- Number of hands played in this round
113+
114+
-- Reroll information (used in shop state)
115+
reroll_cost = G.GAME.current_round.reroll_cost, -- Current cost for a shop reroll
116+
free_rerolls = G.GAME.current_round.free_rerolls, -- Free rerolls remaining this round
113117
-- "idol_card": { -- what's a idol card?? maybe some random used by some joker/effect? idk
114118
-- "rank": "Ace" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "Jack" | "Queen" | "King",
115119
-- "suit": "Spades" | "Hearts" | "Diamonds" | "Clubs",

tests/lua/endpoints/test_shop.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,57 @@ def test_shop_buy_card(self, tcp_client: socket.socket) -> None:
244244
assert purchase_response["game"]["dollars"] == 4
245245
assert purchase_response["jokers"]["cards"][0]["cost"] == 6
246246

247+
# ------------------------------------------------------------------
248+
# reroll shop
249+
# ------------------------------------------------------------------
250+
251+
def test_shop_reroll_success(self, tcp_client: socket.socket) -> None:
252+
"""Successful reroll keeps us in shop and updates cards / dollars."""
253+
254+
# Capture shop state before reroll
255+
before_state = send_and_receive_api_message(tcp_client, "get_game_state", {})
256+
assert before_state["state"] == State.SHOP.value
257+
before_keys = [
258+
c["config"]["center_key"] for c in before_state["shop_jokers"]["cards"]
259+
]
260+
dollars_before = before_state["game"]["dollars"]
261+
reroll_cost = before_state["game"]["current_round"]["reroll_cost"]
262+
263+
# Perform the reroll
264+
after_state = send_and_receive_api_message(
265+
tcp_client, "shop", {"action": "reroll"}
266+
)
267+
268+
# verify state
269+
assert after_state["state"] == State.SHOP.value
270+
assert after_state["game"]["dollars"] == dollars_before - reroll_cost
271+
after_keys = [
272+
c["config"]["center_key"] for c in after_state["shop_jokers"]["cards"]
273+
]
274+
assert before_keys != after_keys
275+
276+
def test_shop_reroll_insufficient_dollars(self, tcp_client: socket.socket) -> None:
277+
"""Repeated rerolls eventually raise INVALID_ACTION when too expensive."""
278+
279+
# Perform rerolls until an error is returned or a reasonable max tries reached
280+
max_attempts = 10
281+
for _ in range(max_attempts):
282+
response = send_and_receive_api_message(
283+
tcp_client, "shop", {"action": "reroll"}
284+
)
285+
286+
# Break when error encountered and validate
287+
if "error" in response:
288+
assert_error_response(
289+
response,
290+
"Not enough dollars to reroll",
291+
["dollars", "reroll_cost"],
292+
ErrorCode.INVALID_ACTION.value,
293+
)
294+
break
295+
else:
296+
pytest.fail("Rerolls did not exhaust dollars within expected attempts")
297+
247298
# ------------------------------------------------------------------
248299
# buy_card validation / error scenarios
249300
# ------------------------------------------------------------------

0 commit comments

Comments
 (0)