# backend/tests/test_scoring.py import pytest from httpx import AsyncClient pytestmark = pytest.mark.anyio async def test_scoring_flow( client: AsyncClient, auth_headers, valid_tournament_payload ): # 1. Create Tournament res = await client.post( "/tournaments", json=valid_tournament_payload, headers=auth_headers ) t_id = res.json()["id"] t_code = valid_tournament_payload["code"] # 2. Get Bracket Nodes (Updated Endpoint) bracket_res = await client.get(f"/tournaments/{t_id}/bracket") nodes = bracket_res.json() # Find active node active_node = next(n for n in nodes if n.get("match") is not None) match_data = active_node["match"] match_id = match_data["id"] next_node_id = active_node["winner_next_node_id"] # 3. Report Score score_payload = { "id": match_id, "code": t_code, "sets": [{"p1": 21, "p2": 19}, {"p1": 21, "p2": 15}], } report_res = await client.post( f"/tournaments/{t_id}/matches/{match_id}/score", json=score_payload ) assert report_res.status_code == 200 # 4. Verify Winner Advanced (Fetch bracket again) updated_res = await client.get(f"/tournaments/{t_id}/bracket") updated_nodes = updated_res.json() target_node = next(n for n in updated_nodes if n["id"] == next_node_id) winner_id = match_data["p1_team"]["id"] p1_in_target = target_node["p1_team"]["id"] if target_node["p1_team"] else None p2_in_target = target_node["p2_team"]["id"] if target_node["p2_team"] else None assert winner_id in [p1_in_target, p2_in_target] # 5. Test Invalid Code bad_payload = score_payload.copy() bad_payload["code"] = "WRONG" bad_res = await client.post( f"/tournaments/{t_id}/matches/{match_id}/score", json=bad_payload ) assert bad_res.status_code == 403 async def test_clear_score(client: AsyncClient, auth_headers, valid_tournament_payload): # Setup res = await client.post( "/tournaments", json=valid_tournament_payload, headers=auth_headers ) t_id = res.json()["id"] # Get active match bracket = (await client.get(f"/tournaments/{t_id}/bracket")).json() active_node = next(n for n in bracket if n.get("match")) match_id = active_node["match"]["id"] score_payload = {"id": match_id, "code": "1234", "sets": [{"p1": 25, "p2": 0}]} await client.post( f"/tournaments/{t_id}/matches/{match_id}/score", json=score_payload ) # Verify Finished check_res = await client.get(f"/tournaments/{t_id}/matches/{match_id}") assert check_res.json()["status"] == "Finished" # Action: Clear Score clear_res = await client.delete( f"/tournaments/{t_id}/matches/{match_id}/score", headers=auth_headers ) assert clear_res.status_code == 200 # Verify Reset final_res = await client.get(f"/tournaments/{t_id}/matches/{match_id}") data = final_res.json() assert data["status"] == "Pending" assert data["winner_team_id"] is None assert len(data["sets"]) == 0