#!/usr/bin/env bash
# Run Playwright e2e tests in the same Docker image as CI (and as snapshot generation).
# Use when running locally on macOS/Windows so tests mirror CI; in CI or devcontainer use pnpm test:e2e directly.
set -e

SCRIPT_DIR="${BASH_SOURCE%/*}"
PROJECT_ROOT="${SCRIPT_DIR}/.."
cd "$PROJECT_ROOT"
PROJECT_ROOT="$(pwd)"

PLAYWRIGHT_IMAGE="${PLAYWRIGHT_IMAGE:-mcr.microsoft.com/playwright:v1.58.0-noble}"
echo "Running e2e tests in Docker image: $PLAYWRIGHT_IMAGE (same as CI)"
echo "Project root: $PROJECT_ROOT"
echo ""

docker run --rm \
  -v "$PROJECT_ROOT:/app" -w /app \
  -e CI=1 \
  "$PLAYWRIGHT_IMAGE" \
  bash -c '
    corepack enable && corepack prepare pnpm@10.28.2 --activate
    pnpm install --no-frozen-lockfile || pnpm install
    pnpm run build
    npx serve dist -p 4173 &
    sleep 2
    pnpm exec playwright test
    pnpm install --config.confirmModulesPurge=false
  '
