#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
php "$ROOT/tests/notifications_collaboration_unit.php"
php "$ROOT/tests/notifications_collaboration_static.php"
if command -v tsc >/dev/null 2>&1; then
  tsc -p "$ROOT/tests/tsconfig.sprint22.json"
  echo "PASS sprint22_typescript_compile"
else
  echo "SKIP sprint22_typescript_compile (tsc unavailable)"
fi
PORT="${BCOS_TEST_SMTP_PORT:-25257}"
OUT="${TMPDIR:-/tmp}/bcos-smtp-test-$$.eml"
READY="${TMPDIR:-/tmp}/bcos-smtp-ready-$$"
rm -f "$OUT" "$READY"
python3 "$ROOT/tools/test_smtp_server.py" "$PORT" "$OUT" "$READY" >/dev/null 2>&1 &
SERVER_PID=$!
cleanup(){ kill "$SERVER_PID" >/dev/null 2>&1 || true; rm -f "$READY"; }
trap cleanup EXIT
for _ in $(seq 1 50); do [[ -f "$READY" ]] && break; sleep .1; done
[[ -f "$READY" ]] || { echo "FAIL smtp_test_server_ready" >&2; exit 1; }
php "$ROOT/tests/smtp_transport_integration.php" "$PORT"
wait "$SERVER_PID"
grep -q 'Integration passed' "$OUT"
echo "PASS smtp_message_captured"
rm -f "$OUT" "$READY"
trap - EXIT
