from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
APP_JS = (ROOT / "ui_builder/static/app.js").read_text(encoding="utf-8")


def test_authoritative_strength_change_rebalances_penalty_targets():
    start = APP_JS.index("function hockeyStoreGameControl")
    end = APP_JS.index("async function hockeyLoadGameControl", start)
    block = APP_JS[start:end]
    assert "const strengthChanged" in block
    assert "hockeyStrengthMappingSignature(previousControl) !== hockeyStrengthMappingSignature(payload)" in block
    assert "rebalanceVmixPenaltyTargets({ force: true, hideUnused: true })" in block
    assert "Penalty strength rebalance error" in block


def test_penalty_plan_rebalances_after_strength_refresh_to_single_advantage_plate():
    start = APP_JS.index("function penaltyDisplayEntriesByTargetSide")
    end = APP_JS.index("function hockeyPenaltySideMappingDetail", start)
    block = APP_JS[start:end]
    assert 'penaltyLocalAdvantageSide(home.length, away.length)' in block
    assert 'transitionEntry = all[0]' in block
    assert 'routedToAdvantage: true' in block
    assert 'if (!advantageSide)' in block

