#!/usr/bin/env bash
# Guard local/remote keyboard and mouse routing before pushing input changes.
set -euo pipefail

ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)
source "${ROOT}/scripts/ci/headless_run.sh"
cd "$ROOT"

if [[ "${LESAVKA_ALLOW_DISRUPTIVE_INPUT_TESTS:-0}" != "1" ]]; then
  cat <<'MSG'
Skipping disruptive input transport tests.
These tests create virtual keyboards/mice and can emit events into the active
desktop. Run them only on an isolated worker/session with:

  LESAVKA_ALLOW_DISRUPTIVE_INPUT_TESTS=1 scripts/ci/input_transport_gate.sh
MSG
  exit 0
fi

INPUT_TESTS=(
  --test client_app_include_contract
  --test client_inputs_contract
  --test client_inputs_extra_contract
  --test client_inputs_routing_contract
  --test client_keyboard_activation_contract
  --test client_keyboard_paste_rpc_contract
  --test client_keyboard_process_contract
  --test client_keyboard_shift_contract
  --test client_keyboard_include_contract
  --test client_keyboard_include_extra_contract
  --test client_mouse_include_contract
  --test client_mouse_include_extra_contract
  --test client_mouse_uinput_contract
  --test server_gadget_include_contract
  --test server_main_binary_extra_contract
  --test server_runtime_smoke_contract
)

cargo fmt --all -- --check
cargo check -q -p lesavka_client --bin lesavka-client
cargo check -q -p lesavka_server --bin lesavka-server
lesavka_ci_run_headless cargo test -q -p lesavka_tests --features disruptive-input-tests "${INPUT_TESTS[@]}"
