Files

140 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Run the whole OHMJ test suite:
# 1. Backend functional tests (api/tests.php)
# 2. Backend code coverage (Xdebug, if available)
# 3. Frontend type/a11y check (svelte-check)
# 4. Frontend E2E smoke test (Chromium via CDP)
#
# Usage: ./test.sh
# Servers are started automatically if not already running, and stopped at the end.
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
API_DIR="$ROOT/api"
FRONT_DIR="$ROOT/partitions"
API_URL="http://localhost:8000"
FRONT_URL="http://localhost:5173"
COV_DIR="${TMPDIR:-/tmp}/ohmj_cov"
API_PID=""
FRONT_PID=""
STARTED_API=0
STARTED_FRONT=0
FAILED=0
cleanup() {
rm -f "${TMPDIR:-/tmp}"/rate_* 2>/dev/null || true
if [ "$STARTED_API" = "1" ] && [ -n "$API_PID" ]; then
kill -TERM "-$API_PID" 2>/dev/null || kill "$API_PID" 2>/dev/null || true
fi
if [ "$STARTED_FRONT" = "1" ] && [ -n "$FRONT_PID" ]; then
kill -TERM "-$FRONT_PID" 2>/dev/null || kill "$FRONT_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT
wait_http() {
local url="$1" tries="${2:-40}"
for _ in $(seq 1 "$tries"); do
if curl -s -m 2 -o /dev/null "$url"; then
return 0
fi
sleep 0.5
done
return 1
}
has_xdebug() {
php -d zend_extension=xdebug -r 'exit(function_exists("xdebug_start_code_coverage") ? 0 : 1);' 2>/dev/null
}
# Xdebug is present but commented in php.ini: load it explicitly via -d.
XDEBUG=0
if has_xdebug; then
XDEBUG=1
fi
echo "=== OHMJ test suite ==="
echo "Backend : php -c php-upload.ini -S localhost:8000 router.php"
echo "Frontend: npm run check && npm run test:e2e"
echo "Coverage: $([ "$XDEBUG" = "1" ] && echo 'Xdebug available' || echo 'Xdebug not found (skipped)')"
echo
# ---------- Backend + coverage ----------
rm -rf "$COV_DIR" 2>/dev/null || true
rm -f "${TMPDIR:-/tmp}"/rate_* 2>/dev/null || true
echo "--- [1/4] Backend tests ---"
if curl -s -m 2 -o /dev/null "$API_URL/login" -X POST -H 'Content-Type: application/json' -d '{}' 2>/dev/null; then
echo "API already running, reusing it (coverage may be skipped)."
else
if [ "$XDEBUG" = "1" ]; then
( cd "$API_DIR" && setsid php -d zend_extension=xdebug -d xdebug.mode=coverage \
-d auto_prepend_file="$API_DIR/coverage-prepend.php" \
-c php-upload.ini -S localhost:8000 router.php >/tmp/ohmj_api.log 2>&1 </dev/null & echo $! >/tmp/ohmj_api.pid )
else
( cd "$API_DIR" && setsid php -c php-upload.ini -S localhost:8000 router.php >/tmp/ohmj_api.log 2>&1 </dev/null & echo $! >/tmp/ohmj_api.pid )
fi
API_PID="$(cat /tmp/ohmj_api.pid)"
STARTED_API=1
wait_http "$API_URL/login" || { echo "API failed to start (see /tmp/ohmj_api.log)"; exit 1; }
fi
rm -f "${TMPDIR:-/tmp}"/rate_* 2>/dev/null || true
if ( cd "$API_DIR" && php tests.php ); then
echo "Backend tests: OK"
else
echo "Backend tests: FAILED"
FAILED=1
fi
echo
echo "--- [2/4] Backend coverage ---"
if [ "$XDEBUG" = "1" ] && [ "$STARTED_API" = "1" ]; then
rm -f "${TMPDIR:-/tmp}"/rate_* 2>/dev/null || true
( cd "$API_DIR" && php -d zend_extension=xdebug -d xdebug.mode=coverage \
-d auto_prepend_file="$API_DIR/coverage-prepend.php" tests.php >/dev/null 2>&1 )
( cd "$ROOT" && SHOW_MISSING=1 php api/coverage-report.php \
api/index.php api/router.php api/lib/Auth.php api/lib/ScoreScanner.php )
elif [ "$XDEBUG" != "1" ]; then
echo "Xdebug not available: run 'omarchy-pkg-install xdebug' to enable coverage."
else
echo "External API detected: coverage skipped (it needs the instrumented server)."
echo "Stop the running API and re-run ./test.sh to measure coverage."
fi
# ---------- Frontend ----------
echo
echo "--- [3/4] Frontend check (svelte-check) ---"
if ( cd "$FRONT_DIR" && npm run check ); then
echo "Frontend check: OK"
else
echo "Frontend check: FAILED"
FAILED=1
fi
echo
echo "--- [4/4] Frontend E2E smoke test ---"
if curl -s -m 2 -o /dev/null "$FRONT_URL/"; then
echo "Frontend already running, reusing it."
else
( cd "$FRONT_DIR" && setsid npm run dev -- --port 5173 >/tmp/ohmj_front.log 2>&1 </dev/null & echo $! >/tmp/ohmj_front.pid )
FRONT_PID="$(cat /tmp/ohmj_front.pid)"
STARTED_FRONT=1
wait_http "$FRONT_URL/" || { echo "Frontend failed to start (see /tmp/ohmj_front.log)"; exit 1; }
fi
if ( cd "$FRONT_DIR" && npm run test:e2e ); then
echo "E2E: OK"
else
echo "E2E: FAILED"
FAILED=1
fi
echo
if [ "$FAILED" = "1" ]; then
echo "=== RESULT: FAILURES ==="
exit 1
fi
echo "=== RESULT: ALL GREEN ==="