161 lines
6.1 KiB
Bash
Executable File
161 lines
6.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# e2e_api.sh — Suite end-to-end de la API HTTP de navegator_dashboard.
|
|
#
|
|
# Asume que `navegator_dashboard.exe` esta corriendo con la API en 127.0.0.1:19333.
|
|
# Si no esta, lanza la app primero (no la mata al final — dejarla viva para iterar).
|
|
#
|
|
# Cubre:
|
|
# 1. /health responde OK
|
|
# 2. /browsers retorna JSON array
|
|
# 3. POST /spawn lanza chrome.exe + retorna {ok:true,pid,port}
|
|
# 4. CDP responde en el puerto retornado
|
|
# 5. cdp-cli puede navegar/evaluar sobre la instancia
|
|
# 6. /browsers ahora contiene la instancia (1 entrada por chrome master, no N)
|
|
# 7. POST /kill mata la instancia
|
|
# 8. CDP deja de responder
|
|
# 9. /browsers vuelve a no contenerla
|
|
#
|
|
# Exit 0 en exito, !=0 en fallo. Cada paso imprime "OK" o "FAIL: ..." a stderr.
|
|
|
|
set -euo pipefail
|
|
|
|
API="${NAVD_API:-http://127.0.0.1:19333}"
|
|
EXE="${NAVD_EXE:-/mnt/c/Users/lucas/Desktop/apps/navegator_dashboard/navegator_dashboard.exe}"
|
|
CDP_CLI="${NAVD_CDP_CLI:-/home/lucas/fn_registry/projects/osint_graph/apps/graph_explorer/cdp-cli/cdp-cli}"
|
|
|
|
PROFILE="e2e_$(date +%s)"
|
|
PORT=19299
|
|
|
|
failures=0
|
|
step() { echo; echo "==[ $1 ]==" >&2; }
|
|
ok() { echo " OK: $1" >&2; }
|
|
fail() { echo " FAIL: $1" >&2; failures=$((failures + 1)); }
|
|
|
|
# ---------------------------------------------------------
|
|
step "0. dashboard reachable"
|
|
# ---------------------------------------------------------
|
|
if ! curl -sf --max-time 2 "$API/health" >/dev/null 2>&1; then
|
|
if [[ -x "$EXE" ]]; then
|
|
echo " app no responde, lanzando $EXE..." >&2
|
|
powershell.exe -NoProfile -Command "Start-Process -FilePath '$(wslpath -w "$EXE" 2>/dev/null || echo "$EXE")'" >/dev/null 2>&1 || true
|
|
for _ in $(seq 1 20); do
|
|
if curl -sf --max-time 1 "$API/health" >/dev/null 2>&1; then break; fi
|
|
sleep 0.5
|
|
done
|
|
fi
|
|
fi
|
|
if curl -sf --max-time 2 "$API/health" >/dev/null 2>&1; then
|
|
ok "dashboard up"
|
|
else
|
|
fail "dashboard no responde en $API/health"
|
|
exit 1
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "1. /health"
|
|
# ---------------------------------------------------------
|
|
body="$(curl -sf --max-time 2 "$API/health")"
|
|
echo " body: $body" >&2
|
|
[[ "$body" == *'"ok":true'* ]] && ok "/health responde ok=true" || fail "/health body inesperado"
|
|
|
|
# ---------------------------------------------------------
|
|
step "2. /browsers (initial)"
|
|
# ---------------------------------------------------------
|
|
body="$(curl -sf --max-time 2 "$API/browsers")"
|
|
echo " body: $body" >&2
|
|
[[ "$body" == \[* ]] && ok "/browsers retorna array" || fail "/browsers no retorna JSON array"
|
|
|
|
# ---------------------------------------------------------
|
|
step "3. POST /spawn"
|
|
# ---------------------------------------------------------
|
|
body="$(curl -sf --max-time 10 -X POST "$API/spawn?profile=$PROFILE&port=$PORT")"
|
|
echo " body: $body" >&2
|
|
if [[ "$body" == *'"ok":true'* ]]; then
|
|
ok "/spawn launched"
|
|
SPAWNED_PID="$(echo "$body" | sed -nE 's/.*"pid":([0-9]+).*/\1/p')"
|
|
echo " pid=$SPAWNED_PID port=$PORT profile=$PROFILE" >&2
|
|
else
|
|
fail "/spawn fallo"
|
|
exit 1
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "4. CDP responde en puerto $PORT"
|
|
# ---------------------------------------------------------
|
|
ok_cdp=""
|
|
for _ in $(seq 1 20); do
|
|
if curl -sf --max-time 1 "http://127.0.0.1:$PORT/json/version" >/dev/null 2>&1; then
|
|
ok_cdp=1; break
|
|
fi
|
|
sleep 0.5
|
|
done
|
|
if [[ -n "$ok_cdp" ]]; then
|
|
browser="$(curl -sf "http://127.0.0.1:$PORT/json/version" 2>/dev/null | grep -i '"Browser"' | head -1 | tr -d '\r' || true)"
|
|
ok "CDP up —${browser}"
|
|
else
|
|
fail "CDP no responde en $PORT"
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "5. cdp-cli navigate + evaluate"
|
|
# ---------------------------------------------------------
|
|
if [[ -x "$CDP_CLI" ]]; then
|
|
"$CDP_CLI" navigate --port "$PORT" --url "data:text/html,<h1 id=t>e2e ok</h1>" >/dev/null 2>&1 \
|
|
&& ok "navigate OK" || fail "navigate fallo"
|
|
title="$("$CDP_CLI" evaluate --port "$PORT" --js "document.getElementById('t').innerText" 2>/dev/null || echo "")"
|
|
[[ "$title" == "e2e ok" ]] && ok "evaluate retorno texto correcto" || fail "evaluate retorno: '$title'"
|
|
else
|
|
fail "cdp-cli no encontrado en $CDP_CLI"
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "6. /browsers contiene instancia (filtro --type=)"
|
|
# ---------------------------------------------------------
|
|
sleep 2 # dar tiempo a que se note en el rescan async del dashboard
|
|
body="$(curl -sf --max-time 5 "$API/browsers")"
|
|
echo " body: $body" >&2
|
|
count_match="$(echo "$body" | grep -c "\"profile\":\"$PROFILE\"" || true)"
|
|
echo " matches profile $PROFILE: $count_match" >&2
|
|
if [[ "$count_match" == "1" ]]; then
|
|
ok "exactamente 1 entrada (master only, no renderers)"
|
|
elif [[ "$count_match" -ge "1" ]]; then
|
|
fail "esperaba 1, encontrado $count_match (filtro --type= roto?)"
|
|
else
|
|
fail "no encuentro el perfil $PROFILE en /browsers"
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "7. POST /kill"
|
|
# ---------------------------------------------------------
|
|
body="$(curl -sf --max-time 5 -X POST "$API/kill?profile=$PROFILE")"
|
|
echo " body: $body" >&2
|
|
[[ "$body" == *'"ok":true'* ]] && ok "/kill respondio ok" || fail "/kill fallo"
|
|
|
|
# ---------------------------------------------------------
|
|
step "8. CDP ya no responde"
|
|
# ---------------------------------------------------------
|
|
sleep 2
|
|
if curl -sf --max-time 1 "http://127.0.0.1:$PORT/json/version" >/dev/null 2>&1; then
|
|
fail "CDP sigue vivo en $PORT despues de kill"
|
|
else
|
|
ok "CDP muerto"
|
|
fi
|
|
|
|
# ---------------------------------------------------------
|
|
step "9. /browsers ya no contiene instancia"
|
|
# ---------------------------------------------------------
|
|
sleep 1
|
|
body="$(curl -sf --max-time 5 "$API/browsers")"
|
|
count_match="$(echo "$body" | grep -c "\"profile\":\"$PROFILE\"" || true)"
|
|
[[ "$count_match" == "0" ]] && ok "instancia desaparecida" || fail "/browsers sigue listando perfil $PROFILE ($count_match veces)"
|
|
|
|
# ---------------------------------------------------------
|
|
echo
|
|
if [[ $failures -eq 0 ]]; then
|
|
echo "===== e2e_api.sh: ALL PASS =====" >&2
|
|
exit 0
|
|
else
|
|
echo "===== e2e_api.sh: $failures FAILURES =====" >&2
|
|
exit 1
|
|
fi
|