#!/usr/bin/env bash
set -euo pipefail

# Run Flutter tests twice in compact mode and summarize results
cd "$(dirname "$0")/.."

run_once() {
  local idx="$1"
  echo "[Run ${idx}] flutter test -r compact"
  if flutter test -r compact; then
    echo "[Run ${idx}] PASSED"
    return 0
  else
    echo "[Run ${idx}] FAILED"
    return 1
  fi
}

pass_count=0

run_once 1
status1=$?
if [ "$status1" -eq 0 ]; then pass_count=$((pass_count+1)); fi

run_once 2
status2=$?
if [ "$status2" -eq 0 ]; then pass_count=$((pass_count+1)); fi

echo "========================================"
echo "Summary: $pass_count/2 runs passed"
echo "Run 1: $( [ "$status1" -eq 0 ] && echo PASSED || echo FAILED )"
echo "Run 2: $( [ "$status2" -eq 0 ] && echo PASSED || echo FAILED )"
echo "========================================"

# Exit code 0 only if both runs passed
if [ "$pass_count" -eq 2 ]; then
  exit 0
else
  exit 1
fi
