from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
APP = (ROOT / "app.py").read_text(encoding="utf-8")
INTEGRATION = (ROOT / "ui_builder" / "integration.py").read_text(encoding="utf-8")
UI_JS = (ROOT / "ui_builder" / "static" / "app.js").read_text(encoding="utf-8")
TOURNAMENT_JS = (ROOT / "hockey_data" / "static" / "tournament-menu.js").read_text(encoding="utf-8")
SERVICE = (ROOT / "hockey_data" / "service.py").read_text(encoding="utf-8")


def test_runtime_is_default_even_for_legacy_editor_bookmarks():
    assert 'return RedirectResponse("/", status_code=302)' in APP
    assert 'request.query_params.get("open") != "1"' in INTEGRATION
    assert 'function explicitEditorUrl()' in UI_JS
    assert 'open=1' in UI_JS


def test_match_selection_no_longer_depends_on_full_details_sync():
    assert 'Match selection itself depends only on the local/cached schedule row.' in TOURNAMENT_JS
    assert 'enrichOpenedGame(String(id), state.selectedId, generation, { opening: true })' in TOURNAMENT_JS
    assert 'Full match JSON, rosters, shots and directories are enrichment data.' in TOURNAMENT_JS
    assert 'gameOpenGeneration' in TOURNAMENT_JS


def test_server_returns_cached_match_when_full_details_are_unavailable():
    assert 'payload = await asyncio.wait_for(' in SERVICE
    assert 'timeout=12.0' in SERVICE
    assert '"details_sync_failed": True' in SERVICE
    assert 'Полная карточка матча пока недоступна' in SERVICE


def test_games_url_keeps_selected_date_and_language():
    assert '?on_date=${encodeURIComponent(state.gameDate)}' in TOURNAMENT_JS
    assert '&language=${encodeURIComponent(state.language)}' in TOURNAMENT_JS
