#!/usr/bin/env bash
# Single-shot health check for E2E endpoints. Returns 0 if both up.
set -u

PID=$(tasklist.exe 2>/dev/null | awk '/matrix_client_pc\.exe/ {print $2; exit}')
echo "[e2e] PID: ${PID:-(not running)}"

if [ -z "$PID" ]; then
  echo "[e2e] app not running"
  exit 1
fi

E2E_OK=0
CDP_OK=0
PING=$(curl -sS --max-time 1 http://127.0.0.1:8767/ping 2>/dev/null) && E2E_OK=1
CDP=$(curl -sS --max-time 1 http://127.0.0.1:9222/json/version 2>/dev/null) && CDP_OK=1

echo "[e2e] :8767 (E2E API) -> ${E2E_OK} ${PING:-no response}"
echo "[e2e] :9222 (CDP)     -> ${CDP_OK} $(echo "$CDP" | head -c 120)"

if [ $E2E_OK -eq 1 ] && [ $CDP_OK -eq 1 ]; then
  exit 0
fi
exit 1
