diff --git a/backend/espn/logic.py b/backend/espn/logic.py index 99c9c3c..2caa2ed 100644 --- a/backend/espn/logic.py +++ b/backend/espn/logic.py @@ -62,6 +62,23 @@ def _members_by_id(payload: dict) -> dict: return members +# Per-team waiver priority lives on the team object in ESPN's payload +# (it is state, not a setting, so it is NOT in the acquisitionSettings +# block). No recorded payload in this repo carries it yet, so the read +# is defensive: the canonical key first, then the plausible variants, +# each accepted only as a plain int. Anything else degrades to None — +# the rank is ESPN state and is never derived from standings/record. +_WAIVER_RANK_KEYS = ("waiverRank", "currentWaiverRank", "waiver_rank") + + +def _waiver_rank(team: dict): + for key in _WAIVER_RANK_KEYS: + value = team.get(key) + if isinstance(value, int) and not isinstance(value, bool): + return value + return None + + def _slim_team(team: dict, members: dict) -> dict: record = ((team.get("record") or {}).get("overall")) or {} streak_raw = record.get("streak") or {} @@ -84,6 +101,7 @@ def _slim_team(team: dict, members: dict) -> dict: "points_for": record.get("pointsFor"), "points_against": record.get("pointsAgainst"), "streak": streak, + "waiver_rank": _waiver_rank(team), } diff --git a/backend/tests/test_espn_logic.py b/backend/tests/test_espn_logic.py index 74d97ce..238030c 100644 --- a/backend/tests/test_espn_logic.py +++ b/backend/tests/test_espn_logic.py @@ -314,6 +314,32 @@ def test_get_league_acquisition_settings_absent(client): } +def _payload_with_teams(teams): + return dict(LEAGUE_PAYLOAD, teams=teams) + + +def test_get_league_waiver_rank_parsed(): + teams = [dict(t) for t in LEAGUE_PAYLOAD["teams"]] + teams[0]["waiverRank"] = 2 + teams[1]["currentWaiverRank"] = 1 + # teams[2] carries no waiver field at all + res = logic.get_league(FakeClient(payload=_payload_with_teams(teams))) + assert [t["waiver_rank"] for t in res["teams"]] == [2, 1, None] + # pre-existing keys are untouched + alpha = res["teams"][0] + assert alpha["name"] == "Alpha" + assert alpha["wins"] == 8 + assert alpha["streak"] == "W2" + + +def test_get_league_waiver_rank_absent_is_none(client): + # the default fixture carries no per-team waiver field: the rank + # degrades to None on every team instead of disappearing (and is + # never invented from standings or record) + res = logic.get_league(client) + assert [t["waiver_rank"] for t in res["teams"]] == [None, None, None] + + def test_get_standings_order_and_rank(client): res = logic.get_standings(client) assert res["count"] == 3