Coverage for notifications/audience.py: 93.33%
22 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
1"""Who a channel may reach (SB-1602). Pure functions, no I/O.
3Player channels are for squads old enough to use the platform. An age group that
4can't be read as "U<number>" never qualifies: this fails closed.
5"""
7from __future__ import annotations
9import re
10from typing import TYPE_CHECKING
12if TYPE_CHECKING:
13 from notifications.adapters.base import ChannelAdapter
15MIN_PLAYER_AGE = 14
17_AGE_GROUP = re.compile(r"^U(\d+)$")
20def age_number(age_group_name: str | None) -> int | None:
21 """14 for "U14"; None for anything else, including a test age group."""
22 match = _AGE_GROUP.match(age_group_name or "")
23 return int(match.group(1)) if match else None
26def players_allowed_at(age_group_name: str | None) -> bool:
27 """Whether a match at this age group may reach a player channel."""
28 age = age_number(age_group_name)
29 return age is not None and age >= MIN_PLAYER_AGE
32def player_channel_problem(adapter: ChannelAdapter, age_group_name: str | None) -> str | None:
33 """Why this platform can't host this player channel, or None if it can.
35 `age_group_name` is the squad's, or None for a club channel, which spans
36 every age group from MIN_PLAYER_AGE up and so must suit the youngest.
37 """
38 if adapter.min_player_age is None:
39 return f"{adapter.label} can't be used for player channels"
40 if age_group_name is None:
41 youngest = MIN_PLAYER_AGE
42 else:
43 youngest = age_number(age_group_name)
44 if not players_allowed_at(age_group_name) or youngest is None:
45 return f"Player channels are only available from U{MIN_PLAYER_AGE}"
46 if youngest < adapter.min_player_age: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 return f"{adapter.label} player channels are only available from U{adapter.min_player_age}"
48 return None