Files
hockey_new/tests/test_build111_agent_match_handoff.py

93 lines
3.5 KiB
Python

from __future__ import annotations
import asyncio
import pytest
from fastapi import HTTPException
from pathlib import Path
from types import SimpleNamespace
from hockey_data.agent_bridge import VmixAgentHub
from hockey_data.auth_bridge import HockeyUser
from tests.support import LocalTestDatabase
ROOT = Path(__file__).resolve().parents[1]
JS = (ROOT / "hockey_data" / "static" / "tournament-menu.js").read_text(encoding="utf-8")
AGENT = (ROOT / "hockey_data" / "agent_bridge.py").read_text(encoding="utf-8")
ROUTER = (ROOT / "hockey_data" / "router.py").read_text(encoding="utf-8")
APP = (ROOT / "app.py").read_text(encoding="utf-8")
class FakeWebSocket:
def __init__(self) -> None:
self.client = SimpleNamespace(host="127.0.0.1")
self.sent: list[dict] = []
async def send_json(self, payload: dict) -> None:
self.sent.append(payload)
async def close(self, **_kwargs) -> None:
return None
def test_stale_browser_device_cannot_redirect_to_another_agent(tmp_path: Path) -> None:
database = LocalTestDatabase(tmp_path / "build111.sqlite3")
database.create_all()
hub = VmixAgentHub(database) # type: ignore[arg-type]
ws = FakeWebSocket()
user = HockeyUser(id="111", login="operator111", display_name="Operator 111")
async def scenario() -> None:
await hub.register(
ws, # type: ignore[arg-type]
{
"device_id": "GFX-BUILD111",
"device_secret": "x" * 40,
"device_name": "GFX BUILD111",
"hostname": "GFX-BUILD111",
"agent_version": "1.4.0",
"vmix": {"connected": True, "url": "http://127.0.0.1:8088/api/"},
},
)
await hub.pair_device("GFX-BUILD111", user)
with pytest.raises(HTTPException) as error:
await hub.assign_match(
wfl_user_id=user.id, tournament_external_id="1437",
game_external_id="902918", device_id="STALE-BROWSER-DEVICE",
wait_for_mapping=False,
)
assert error.value.status_code == 409
assert not [item for item in ws.sent if item.get("type") == "match.assign"]
# An explicitly selected owned Agent still receives the new match.
result = await hub.assign_match(
wfl_user_id=user.id, tournament_external_id="1437", game_external_id="902918",
device_id="GFX-BUILD111", wait_for_mapping=False,
)
assert result["device_id"] == "GFX-BUILD111" and result["delivered"]
assert result["mapping_apply"]["deferred"] is True
asyncio.run(scenario())
def test_browser_retries_match_assign_without_mapping_when_needed() -> None:
assert "async function ensureSessionAgentAssignment(session)" in JS
assert 'body: JSON.stringify({ session_token: token, apply_mapping: false })' in JS
assert 'item?.paired_to_me' in JS
assert 'item?.active_for_account' in JS
assert 'item?.online' in JS
assert 'live.length === 1' in JS
def test_mapping_is_refreshed_only_after_roster_enrichment():
"""Current contract: session assignment precedes mapping while optional roster enrichment remains nonblocking."""
from tests.ui_contract_support import ui_contract
ui_contract("roster")
def test_select_session_supports_assignment_without_early_mapping() -> None:
assert "apply_mapping: bool = True" in AGENT
assert "wait_for_mapping=bool(payload.apply_mapping)" in AGENT
def test_runtime_version_build111() -> None:
assert 'BUILD_VERSION = "2026.08.24.14"' in APP