#!/usr/bin/env bash
# Run Go unit tests for the GoodMem CLI module.
# Usage:
#   ./run_tests.sh                # verbose run of all tests
#   ./run_tests.sh --no-cache     # disable cached results
#   ./run_tests.sh --quiet        # suppress -v output
#   ./run_tests.sh -- -run Filter # pass flags directly to go test

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"

GO_ARGS=()
TARGET_SPECIFIED=false
VERBOSE=true

while [ "$#" -gt 0 ]; do
  case "$1" in
    --no-cache)
      GO_ARGS+=("-count=1")
      shift
      ;;
    --quiet)
      VERBOSE=false
      shift
      ;;
    --verbose|-v)
      VERBOSE=true
      shift
      ;;
    --)
      shift
      while [ "$#" -gt 0 ]; do
        arg="$1"
        GO_ARGS+=("$arg")
        if [[ "$arg" != -* ]]; then
          TARGET_SPECIFIED=true
        fi
        shift
      done
      break
      ;;
    *)
      GO_ARGS+=("$1")
      if [[ "$1" != -* ]]; then
        TARGET_SPECIFIED=true
      fi
      shift
      ;;
  esac
done

if $VERBOSE; then
  GO_ARGS=("-v" "${GO_ARGS[@]}")
fi

if ! $TARGET_SPECIFIED; then
  GO_ARGS+=("./...")
fi

go test "${GO_ARGS[@]}"
