from pathlib import Path

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


def _block(start: str, end: str) -> str:
    return APP_JS.split(start, 1)[1].split(end, 1)[0]


def test_pause_stop_sends_only_pause_only_command():
    helper = _block("function vmixCountdownSyncCommands", "function buildShortcutRuntimeContext")
    branch = helper.split('if (action === "stop" || action === "pause")', 1)[1].split('if (action === "set")', 1)[0]
    assert 'Function: "SuspendCountdown"' in branch
    assert 'Function: "StopCountdown"' not in branch
    assert 'Function: "SetCountdown"' not in branch


def test_explicit_edit_still_can_seed_stopped_clock():
    helper = _block("function vmixCountdownSyncCommands", "function buildShortcutRuntimeContext")
    branch = helper.split('if (action === "set")', 1)[1].split('return [', 2)[1]
    assert 'Function: "StopCountdown"' in branch
    assert 'Function: "SetCountdown"' in branch
    sync = _block("async function syncActiveVmixGameCountdown", "function penaltyMirrorKey")
    assert 'timerState.running ? "start" : "set"' in sync


def test_penalty_pause_preserves_native_vmix_frozen_value():
    rebalance = _block("async function rebalanceVmixPenaltyTargets", "function finishActionMatchesSource")
    assert "preservePausedCountdown = false" in rebalance
    assert "if (!preservePausedCountdown || assignmentChanged)" in rebalance
    control = _block("function controlHockeyPenalty", "function formatHockeyPenaltyTime")
    assert 'preservePausedCountdown: command === "pause"' in control


def test_build104_runtime_version():
    assert 'BUILD_VERSION = "2026.08.24.7"' in APP
