from pathlib import Path

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


def helper_block():
    start = APP_JS.index("function vmixCountdownSyncCommands")
    end = APP_JS.index("function buildShortcutRuntimeContext", start)
    return APP_JS[start:end]


def test_pause_uses_pause_and_change_without_stop_reset():
    block = helper_block()
    pause_start = block.index('if (action === "pause" || action === "stop")')
    set_start = block.index('if (action === "set")', pause_start)
    pause_branch = block[pause_start:set_start]
    assert 'Function: "PauseCountdown"' in pause_branch
    assert 'Function: "ChangeCountdown"' in pause_branch
    assert 'Function: "StopCountdown"' not in pause_branch
    assert pause_branch.index('Function: "PauseCountdown"') < pause_branch.index('Function: "ChangeCountdown"')


def test_start_uses_change_then_start_without_stop_or_set_duration():
    block = helper_block()
    start_branch = block[block.index("// Start/Resume:"):]
    assert 'Function: "ChangeCountdown"' in start_branch
    assert 'Function: "StartCountdown"' in start_branch
    assert 'Function: "StopCountdown"' not in start_branch
    assert 'Function: "SetCountdown"' not in start_branch
    assert start_branch.index('Function: "ChangeCountdown"') < start_branch.index('Function: "StartCountdown"')


def test_set_only_changes_current_position_and_does_not_toggle_pause():
    block = helper_block()
    set_start = block.index('if (action === "set")')
    start_branch = block.index("// Start/Resume:", set_start)
    set_branch = block[set_start:start_branch]
    assert 'Function: "ChangeCountdown"' in set_branch
    assert 'Function: "PauseCountdown"' not in set_branch
    assert 'Function: "StopCountdown"' not in set_branch


def test_runtime_version():
    assert 'BUILD_VERSION = "2026.08.24.10"' in APP_PY
