"""Tests for the Fienta ticket parsing that feeds team-role sync. Covers the risky bit: turning raw Fienta ticket JSON into a reliable {discord identity -> team} + {team -> game} mapping, including which ticket types count as team members and how the game is detected. """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) import config # noqa: E402 from core import fienta # noqa: E402 def _ticket(ttype: str, *, team="", discord="", discord_id="", nick=""): return { "order_id": 1, "rows": [{ "ticket_type": {"title": ttype}, "attendee": { fienta._F_TEAM_NAME: team, fienta._F_DISCORD_USERNAME: discord, fienta._F_DISCORD_USERID: discord_id, fienta._F_DISCORD_USERNAME.replace("discord", "x"): "", "nickname_134815": nick, }, }], } CS2 = "Counter-Strike 2 Tournament - competitor ticket" LOL = "League of Legends Tournament - competitor ticket" def test_parse_maps_username_and_userid_to_team(): fienta.parse_tickets([ _ticket(CS2, team="KONE", discord="ar7enchik", discord_id="123"), ]) assert fienta.get_team_for_username("ar7enchik") == "KONE" assert fienta.get_team_for_username("AR7ENCHIK") == "KONE" # case-insensitive assert fienta.get_team_for_userid(123) == "KONE" assert fienta.get_team_game("KONE") == "CS2" def test_parse_detects_game_from_ticket_type(): fienta.parse_tickets([ _ticket(CS2, team="KONE", discord="a"), _ticket(LOL, team="Ööbik", discord="b"), ]) assert fienta.get_team_game("KONE") == "CS2" assert fienta.get_team_game("Ööbik") == "LoL" def test_parse_excludes_non_player_ticket_types(): fienta.parse_tickets([ _ticket("Visitor's Ticket", team="", discord=""), _ticket("Early Bird - visitor ticket", team="X", discord="ghost"), _ticket("Counter-Strike 2 Tournament Waiting List", team="WL", discord="waiter"), _ticket("LAN area - Access Ticket", team="", discord=""), _ticket(CS2, team="KONE", discord="real"), ]) assert fienta.all_team_names() == {"KONE"} assert fienta.get_team_for_username("ghost") is None assert fienta.get_team_for_username("waiter") is None assert fienta.get_team_for_username("real") == "KONE" def test_parse_includes_coach_and_substitute(): fienta.parse_tickets([ _ticket("Counter-Strike 2 Coach/Manager - competitor ticket", team="KONE", discord="coach"), _ticket("CS2 Substitute Player - competitor ticket", team="KONE", discord="sub"), ]) assert fienta.get_team_for_username("coach") == "KONE" assert fienta.get_team_for_username("sub") == "KONE" def test_parse_skips_tickets_without_team_or_discord(): fienta.parse_tickets([ _ticket(CS2, team="", discord="noteam"), # no team -> skipped _ticket(CS2, team="KONE", discord=""), # team but no discord -> team known, no user ]) assert fienta.get_team_for_username("noteam") is None assert "KONE" in fienta.all_team_names() def test_get_team_dividers_maps_via_game(monkeypatch): monkeypatch.setattr(config, "TEAM_DIVIDERS", {"cs2_2026": 100, "lol_2026": 200}) fienta.parse_tickets([ _ticket(CS2, team="KONE", discord="a"), _ticket(LOL, team="Ööbik", discord="b"), ]) assert fienta.get_team_dividers() == {"KONE": 100, "Ööbik": 200} def test_strips_leading_at_from_discord_username(): fienta.parse_tickets([_ticket(CS2, team="KONE", discord="@handle")]) assert fienta.get_team_for_username("handle") == "KONE"