Files
texas_hold_x/tests/test_server.py
T
mamamiyear c0bc5384f4 feat: add hand detail API and enrich hand summary fields
- HandSummary: add hole_cards, starting_stacks, ending_stacks, pot_contributions
- Engine: capture all players' hole cards (not just showdown), pre/post hand stacks, per-level pot contributions
- Server: new GET /game/<game_id>/hands/<hand_number> route
- Service: add get_hand_state() method
- Tests: add ServerTests for new endpoint, update existing tests
- Existing GET /game/<game_id> auto-inherits new fields via shared to_dict()
2026-05-23 22:11:45 +08:00

77 lines
2.5 KiB
Python

import json
import unittest
from threading import Thread
from urllib.request import Request, urlopen
from texas_holdem import server as poker_server
from texas_holdem.service import GameManager
class ServerTests(unittest.TestCase):
def setUp(self) -> None:
self.previous_manager = poker_server.MANAGER
poker_server.MANAGER = GameManager()
self.server = poker_server.create_server("127.0.0.1", 0)
self.thread = Thread(target=self.server.serve_forever, daemon=True)
self.thread.start()
host, port = self.server.server_address
self.base_url = f"http://{host}:{port}"
def tearDown(self) -> None:
self.server.shutdown()
self.server.server_close()
self.thread.join(timeout=2)
poker_server.MANAGER = self.previous_manager
def request_json(
self,
method: str,
path: str,
payload: dict[str, object] | None = None,
) -> dict[str, object]:
data = None
headers = {}
if payload is not None:
data = json.dumps(payload).encode("utf-8")
headers["Content-Type"] = "application/json"
request = Request(
f"{self.base_url}{path}",
data=data,
headers=headers,
method=method,
)
with urlopen(request, timeout=5) as response:
return json.loads(response.read().decode("utf-8"))
def test_get_hand_route_returns_expanded_hand_summary(self) -> None:
self.request_json(
"POST",
"/game",
{
"game_id": "route-demo",
"seed": 17,
"starting_stack": 200,
"small_blind": 5,
"big_blind": 10,
"players": [
{"id": "a", "type": "calling"},
{"id": "b", "type": "calling"},
],
},
)
self.request_json("POST", "/game/route-demo/hands", {"count": 1})
hand = self.request_json("GET", "/game/route-demo/hands/1")
game = self.request_json("GET", "/game/route-demo")
self.assertEqual(hand["hand_number"], 1)
self.assertEqual(set(hand["hole_cards"]), {"a", "b"})
self.assertEqual(hand["starting_stacks"], {"a": 200, "b": 200})
self.assertIn("ending_stacks", hand)
self.assertIn("pot_contributions", hand)
self.assertEqual(game["hands"][0], hand)
if __name__ == "__main__":
unittest.main()