#!/usr/bin/env bash
# Restart the API (and, for the billing suite only, a second API with test Stripe settings) for CI.
# Called by scripts/e2e/run.mjs through E2E_RESTART with E2E_SUITE set.
# Test mail is intercepted into a private disposable mailbox, never API logs.
set -euo pipefail
test "${NODE_ENV:-}" = test
test "${E2E_ALLOW_MUTATIONS:-}" = true
test -n "${TEST_MAILBOX_PATH:-}"
if [ -f .test-api.pid ]; then kill "$(cat .test-api.pid)" 2>/dev/null || true; fi
if [ -f .test-billing-api.pid ]; then kill "$(cat .test-billing-api.pid)" 2>/dev/null || true; fi
sleep 1
nohup node --require ./scripts/e2e/test-mail.cjs apps/api/dist/main.js >> api.log 2>&1 &
echo "$!" > .test-api.pid
if [ "${E2E_SUITE:-}" = "billing" ]; then
  PORT=4100 \
  STRIPE_SECRET_KEY=sk_test_e2e_local_only STRIPE_WEBHOOK_SECRET=whsec_e2e_local_only \
  STRIPE_PRICE_PREMIUM=price_e2e_premium STRIPE_PRICE_FIRM=price_e2e_firm STRIPE_PRICE_FEATURED=price_e2e_featured \
  FEATURED_SLOTS_PER_PAGE=1 \
  nohup node --require ./scripts/e2e/test-mail.cjs apps/api/dist/main.js >> api-billing.log 2>&1 &
  echo "$!" > .test-billing-api.pid
fi
