#!/usr/bin/env bash
# Run the WebKit smoke suite, upload artifacts (video/trace/screenshots/report)
# to MinIO, then exit with the suite's real result so a CD gate can act on it.
set -uo pipefail

STAMP="${RUN_ID:-$(date +%Y%m%d-%H%M%S)}"
DEST="e2e/${STAMP}"

echo "▶ Playwright WebKit smoke — BASE_DOMAIN=${BASE_DOMAIN:-riotpiao.com}"
npm test
RESULT=$?
echo "▶ suite exit=${RESULT}"

# Upload artifacts even on failure — a failed deploy is exactly when you want the video.
if [[ -n "${MINIO_ENDPOINT:-}" && -n "${MINIO_ACCESS_KEY:-}" ]]; then
  echo "▶ uploading artifacts → ${MINIO_ENDPOINT}/${MINIO_BUCKET:-e2e-artifacts}/${DEST}"
  mc alias set store "${MINIO_ENDPOINT}" "${MINIO_ACCESS_KEY}" "${MINIO_SECRET_KEY}" >/dev/null 2>&1
  mc mb --ignore-existing "store/${MINIO_BUCKET:-e2e-artifacts}" >/dev/null 2>&1
  mc cp --recursive test-results/       "store/${MINIO_BUCKET:-e2e-artifacts}/${DEST}/test-results/"       >/dev/null 2>&1 || true
  mc cp --recursive playwright-report/  "store/${MINIO_BUCKET:-e2e-artifacts}/${DEST}/playwright-report/"  >/dev/null 2>&1 || true
  echo "▶ artifacts uploaded under ${DEST}/"
else
  echo "▶ MINIO_ENDPOINT unset — skipping upload (artifacts in ./test-results)"
fi

exit ${RESULT}
