#!/usr/bin/env bash
# gradle_unit_test — Corre unit tests JVM de un modulo Android (no requiere emulador)

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

gradle_unit_test() {
    local project_dir="$1"
    local module="${2:-app}"
    local variant="Debug"

    # Parsear flag opcional --variant (consumir project_dir y module primero)
    local nshift=$(( $# < 2 ? $# : 2 ))
    shift "$nshift"
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --variant)
                variant="$2"
                shift 2
                ;;
            *)
                shift
                ;;
        esac
    done

    if [[ -z "$project_dir" ]]; then
        echo "gradle_unit_test: project_dir es obligatorio" >&2
        return 1
    fi

    local task=":${module}:test${variant}UnitTest"
    local report="${project_dir}/${module}/build/reports/tests/test${variant}UnitTest/index.html"

    gradle_run "$project_dir" "$task"
    local exit_code=$?

    echo "REPORT: $report"
    return $exit_code
}
