#!/bin/bash
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$HOME/.nvm/versions/node/$(ls $HOME/.nvm/versions/node/ 2>/dev/null | tail -1)/bin"

cd /Users/gbpark/ERP-node

# Node 경로 찾기
NODE_BIN=""
if command -v node &>/dev/null; then
  NODE_BIN=$(command -v node)
elif [ -f "$HOME/.nvm/nvm.sh" ]; then
  source "$HOME/.nvm/nvm.sh"
  NODE_BIN=$(command -v node)
fi

if [ -z "$NODE_BIN" ]; then
  echo "BROWSER_TEST_RESULT: FAIL - node not found"
  exit 1
fi

echo "Using node: $NODE_BIN"

# playwright가 루트 node_modules에 있으므로 그걸로 실행
PLAYWRIGHT_BIN="/Users/gbpark/ERP-node/node_modules/.bin/playwright"

if [ -f "$PLAYWRIGHT_BIN" ]; then
  echo "playwright binary found: $PLAYWRIGHT_BIN"
  "$PLAYWRIGHT_BIN" test .agent-pipeline/browser-tests/e2e-test.spec.ts \
    --config=.agent-pipeline/browser-tests/playwright.config.ts \
    --reporter=line
  EXIT=$?
  if [ $EXIT -eq 0 ]; then
    echo "BROWSER_TEST_RESULT: PASS"
  else
    echo "BROWSER_TEST_RESULT: FAIL - test failed with exit code $EXIT"
  fi
else
  echo "playwright binary not found, falling back to mjs runner"
  $NODE_BIN /Users/gbpark/ERP-node/run-e2e-runtime-test.mjs
fi
