#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

if [ -z "$1" ]; then
  echo "Usage: ./run-module.sh <module>"
  echo ""
  echo "Available modules:"
  echo "  auth          - 认证授权域"
  echo "  space         - 空间与项目域"
  echo "  asset         - 设备与资产域"
  echo "  ops           - 运营与服务域"
  echo "  finance       - 财务与收费域"
  echo "  cross-domain  - 跨域流程"
  exit 1
fi

MODULE="$1"

cd "$SCRIPT_DIR"

if [ ! -f "playwright.config.ts" ]; then
  echo "ERROR: playwright.config.ts not found in $SCRIPT_DIR"
  exit 1
fi

if [ ! -d "node_modules" ]; then
  echo "Installing dependencies..."
  npm install
  npx playwright install chromium
fi

echo "Running $MODULE E2E tests..."
npx playwright test --project="$MODULE" --reporter=list

EXIT_CODE=$?

echo ""
echo "=========================================="
echo "  $MODULE E2E Tests Complete (exit code: $EXIT_CODE)"
echo "=========================================="

exit $EXIT_CODE
