from pathlib import Path

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


def test_build_version_bumped():
    assert 'BUILD_VERSION = "2026.08.19.22"' in APP


def test_overlay_parser_accepts_legacy_function_strings():
    assert 'replace(/\\s+/g, "")' in JS
    assert 'normalized.match(/(?:^|[^a-z0-9])OverlayInput([1-4])' in JS


def test_sequence_onair_has_post_ack_reconcile_fallback():
    assert 'function reconcileShortcutSequenceOnAir' in JS
    assert 'shortcutSequenceOverlayIntent(sequence)' in JS
    assert 'reconcileShortcutSequenceOnAir(sequence, { wasOnAir });' in JS


def test_normal_overlay_in_latches_sequence_on_air():
    start = JS.index('function reconcileShortcutSequenceOnAir')
    snippet = JS[start:start + 2600]
    assert 'if (intent.action === "In") active = true;' in snippet
    assert 'else if (intent.action === "Out" || intent.action === "Off") active = false;' in snippet
    assert 'else if (intent.action === "toggle") active = !active;' in snippet


def test_ack_confirmed_owner_remains_primary_signal():
    start = JS.index('function reconcileShortcutSequenceOnAir')
    snippet = JS[start:start + 1400]
    assert 'if (sequenceStillOwnsRuntimeOverlay(id))' in snippet
    assert 'state.quickPanelOnAirSequences.add(id);' in snippet
