82 lines
1.6 KiB
Bash
Executable File
82 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# CRM backend smoke runner (repo-tracked)
|
|
# - builds backend
|
|
# - restarts PM2
|
|
# - optional seed
|
|
# - runs API smoke test
|
|
# - runs Jest tests
|
|
|
|
BACKEND_DIR="${BACKEND_DIR:-/opt/crm/backend}"
|
|
ENV_FILE="${ENV_FILE:-/opt/crm/.env}"
|
|
|
|
SEED=0
|
|
|
|
usage() {
|
|
cat <<'USAGE'
|
|
Usage: scripts/smoke_backend.sh [--seed]
|
|
|
|
Environment:
|
|
BACKEND_DIR Backend directory (default: /opt/crm/backend)
|
|
ENV_FILE Env file path (default: /opt/crm/.env)
|
|
|
|
Options:
|
|
--seed Run prisma/seed.ts before tests
|
|
USAGE
|
|
}
|
|
|
|
if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
|
|
usage
|
|
exit 0
|
|
fi
|
|
|
|
if [[ ${1:-} == "--seed" ]]; then
|
|
SEED=1
|
|
fi
|
|
|
|
if [[ ! -f "$ENV_FILE" ]]; then
|
|
echo "Missing env file: $ENV_FILE" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Export env vars for this script execution
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
. "$ENV_FILE"
|
|
set +a
|
|
|
|
BASE_URL="http://127.0.0.1:${PORT:-3000}"
|
|
|
|
echo "[smoke_backend] build"
|
|
( cd "$BACKEND_DIR" && npm run build )
|
|
|
|
echo "[smoke_backend] pm2 restart"
|
|
pm2 restart crm-backend
|
|
|
|
if [[ "$SEED" -eq 1 ]]; then
|
|
echo "[smoke_backend] seed"
|
|
( cd "$BACKEND_DIR" && npx ts-node prisma/seed.ts )
|
|
fi
|
|
|
|
echo "[smoke_backend] wait for /health"
|
|
for i in {1..30}; do
|
|
if curl -fsS "$BASE_URL/health" >/dev/null 2>&1; then
|
|
break
|
|
fi
|
|
sleep 1
|
|
if [[ "$i" -eq 30 ]]; then
|
|
echo "Backend did not become ready" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
echo "[smoke_backend] api smoke"
|
|
( cd "$BACKEND_DIR" && BASE_URL="$BASE_URL" ./test-api.sh )
|
|
|
|
echo "[smoke_backend] jest"
|
|
( cd "$BACKEND_DIR" && npm test )
|
|
( cd "$BACKEND_DIR" && npm run test:e2e )
|
|
|
|
echo "[smoke_backend] OK"
|