#!/usr/bin/env bash
# Scope: universal | Runs test suite before push
# MORPH-SPEC Pre-Push Hook
# Ensures all tests pass before pushing to remote

echo "🧪 Running test suite before push..."
echo ""

# Detect project type and run appropriate tests
if [[ -f *.csproj ]] || find . -maxdepth 2 -name "*.csproj" 2>/dev/null | grep -q .; then
  # .NET project detected
  echo "📦 Detected .NET project"

  if ! dotnet test --verbosity minimal --no-build --no-restore 2>/dev/null; then
    echo ""
    echo "❌ .NET tests failed!"
    echo ""
    echo "Fix failing tests before pushing."
    echo "Override with: git push --no-verify"
    exit 1
  fi

elif [[ -f package.json ]]; then
  # Node.js project detected
  echo "📦 Detected Node.js project"

  if ! npm test 2>/dev/null; then
    echo ""
    echo "❌ Node.js tests failed!"
    echo ""
    echo "Fix failing tests before pushing."
    echo "Override with: git push --no-verify"
    exit 1
  fi

else
  echo "⚠️  No recognized test framework found"
  echo "   Skipping test execution"
  exit 0
fi

echo ""
echo "✅ All tests passed - safe to push"
exit 0
