#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# shellcheck source-path=SCRIPTDIR
source "$SCRIPT_DIR/style.sh"

pushd e2e
# Avoid mapfile that is not available on bash 3.2 for compatibility with macOS
SLOW_FILES=()
while IFS= read -r line; do
  SLOW_FILES+=("$line")
done < <(fd -tf -g "test_*_slow" | sort)
FAST_FILES=()
while IFS= read -r line; do
  FAST_FILES+=("$line")
done < <(fd -tf -g "test_*" | grep -v "_slow$" | sort)
popd
FILES=("${FAST_FILES[@]}" "${SLOW_FILES[@]}")
test_count=0
skipped_count=0
status=0

summary "Test" "Duration" "Result"
summary "---" "---" "---"

for index in "${!FILES[@]}"; do
  TEST_NAME="${FILES[$index]}"

  # split tests into tranches to reduce test time
  if [[ -n ${TEST_TRANCHE_COUNT:-} ]]; then
    if [[ $((index % TEST_TRANCHE_COUNT)) -ne $TEST_TRANCHE ]]; then
      continue
    fi
  fi

  # Skip slow tests unless TEST_ALL == 1
  if [[ ${TEST_ALL:-0} != 1 && $TEST_NAME == *_slow ]]; then
    #    title="E2E test $TEST_NAME skipped" file="e2e/$TEST_NAME" warn "slow tests are disabled"
    skipped_count=$((skipped_count + 1))
    summary "$TEST_NAME" "-" ":zap:"
    continue
  fi

  mise x -- wait-for-gh-rate-limit
  # Actually run the test
  "$ROOT/e2e/run_test" "$TEST_NAME" # fail fast for now
  # if ! "$ROOT/e2e/run_test" "$TEST_NAME"; then
  #   status=1
  # fi
  test_count=$((test_count + 1))
done

echo "E2E: ran $test_count tests, skipped $skipped_count tests" >&2

exit "$status"
