#!/usr/bin/env bash
# gradle_instrumented_test — corre instrumented tests Compose en emulador/device Android conectado
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/gradle_run.sh"
source "$SCRIPT_DIR/adb_wsl.sh"

gradle_instrumented_test() {
    local project_dir="${1:?project_dir required}"
    local module="${2:-app}"

    # Verificar device o emulador conectado
    local devices
    devices=$(adb_run devices | tail -n +2 | grep -E "(emulator|device)$" || true)
    if [[ -z "$devices" ]]; then
        echo "no Android device/emulator connected. Run android_emulator_start first." >&2
        return 3
    fi

    local exit_code=0
    gradle_run "$project_dir" ":${module}:connectedDebugAndroidTest" || exit_code=$?

    echo "REPORT: ${project_dir}/${module}/build/reports/androidTests/connected/index.html"

    return "$exit_code"
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
    gradle_instrumented_test "$@"
fi
