#!/usr/bin/env bash
# Runs Playwright smoke tests inside the official Playwright container against
# the running PedScribe app. Usage: npm run e2e  (or ./scripts/e2e.sh)
set -euo pipefail

cd "$(dirname "$0")/.."

IMAGE="mcr.microsoft.com/playwright:v1.50.0-noble"

# --- PREFLIGHT: static reference linter ---
# Catches the class of bug where a JS file reaches for an id that no
# HTML element (or dynamic id assignment anywhere in the repo) ever
# produces — the lightbox + adminMilestones dead-code bugs were both
# this shape and both went undetected until someone tripped over them
# in the real app. Fails the build before tests even start.
echo "==> Static reference lint"
docker run --rm -v "$PWD:/work" -w /work node:20-alpine \
  node scripts/lint-references.js

# Attach the Playwright container to the same Docker network as the app so it
# can resolve pediatric-ai-scribe by service name. Internal container port is 3000.
NETWORK="${E2E_NETWORK:-ped-ai_default}"
BASE_URL="${BASE_URL:-http://pediatric-ai-scribe:3000}"

docker run --rm --ipc=host \
  --network="$NETWORK" \
  -v "$PWD/e2e":/work \
  -w /work \
  -e BASE_URL="$BASE_URL" \
  -e CI=true \
  "$IMAGE" \
  sh -c "npm install --no-audit --no-fund --silent && npx playwright test"
