Files
whatsapp/test_sse.sh
T
2026-01-27 14:51:41 -05:00

134 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# Script de prueba para el sistema SSE
# Fecha: 27 de enero de 2026
echo "🧪 Testing SSE Real-time System"
echo "================================"
echo ""
# Colores para output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Base URL (cambiar según tu entorno)
BASE_URL="http://localhost"
echo "📝 Configuración:"
echo " Base URL: $BASE_URL"
echo ""
# Test 1: Verificar que sse_events.php es accesible
echo "1️⃣ Probando conexión SSE..."
timeout 3 curl -N -s "$BASE_URL/api/sse_events.php" 2>&1 | head -5 &
PID=$!
sleep 2
if kill -0 $PID 2>/dev/null; then
echo -e " ${GREEN}${NC} SSE endpoint responde"
kill $PID 2>/dev/null
else
echo -e " ${RED}${NC} SSE endpoint no responde"
fi
echo ""
# Test 2: Verificar que push_event.php es accesible
echo "2️⃣ Probando push interno..."
RESPONSE=$(curl -s -X POST "$BASE_URL/api/push_event.php" \
-H "Content-Type: application/json" \
-H "X-Push-Token: internal_push_secret_2026" \
-d '{
"event_type": "test",
"data": {"message": "Test from script"},
"target_user_id": "global"
}')
if echo "$RESPONSE" | grep -q "success"; then
echo -e " ${GREEN}${NC} Push endpoint funciona"
echo " Response: $RESPONSE"
else
echo -e " ${RED}${NC} Push endpoint falló"
echo " Response: $RESPONSE"
fi
echo ""
# Test 3: Verificar permisos en directorio uploads
echo "3️⃣ Verificando permisos de escritura..."
UPLOADS_DIR="../uploads"
if [ -w "$UPLOADS_DIR" ]; then
echo -e " ${GREEN}${NC} Directorio uploads tiene permisos de escritura"
else
echo -e " ${RED}${NC} Directorio uploads NO tiene permisos de escritura"
echo " Ejecuta: chmod 755 $UPLOADS_DIR"
fi
echo ""
# Test 4: Simular evento de mensaje nuevo
echo "4️⃣ Simulando mensaje nuevo..."
PUSH_RESPONSE=$(curl -s -X POST "$BASE_URL/api/push_event.php" \
-H "Content-Type: application/json" \
-H "X-Push-Token: internal_push_secret_2026" \
-d '{
"event_type": "new_message",
"data": {
"user_id": 999,
"phone_number": "573001234567",
"name": "Usuario Prueba",
"message": "Mensaje de prueba desde script",
"message_type": "text",
"timestamp": "'$(date '+%Y-%m-%d %H:%M:%S')'"
},
"target_user_id": "global"
}')
if echo "$PUSH_RESPONSE" | grep -q "success"; then
EVENT_ID=$(echo "$PUSH_RESPONSE" | grep -o '"event_id":"[^"]*"' | cut -d'"' -f4)
echo -e " ${GREEN}${NC} Evento enviado exitosamente"
echo " Event ID: $EVENT_ID"
else
echo -e " ${RED}${NC} Fallo al enviar evento"
fi
echo ""
# Test 5: Verificar que el evento se guardó
echo "5️⃣ Verificando archivo de eventos..."
EVENT_FILE="../uploads/events_global.json"
if [ -f "$EVENT_FILE" ]; then
echo -e " ${GREEN}${NC} Archivo de eventos existe"
echo " Últimos eventos guardados:"
cat "$EVENT_FILE" | python3 -m json.tool 2>/dev/null | tail -20 || cat "$EVENT_FILE"
else
echo -e " ${YELLOW}${NC} Archivo de eventos no encontrado"
echo " Esto es normal si es la primera ejecución"
fi
echo ""
# Test 6: Probar conexión SSE y recepción de evento
echo "6️⃣ Probando recepción de evento en SSE (5 segundos)..."
timeout 5 curl -N -s "$BASE_URL/api/sse_events.php" 2>&1 | while IFS= read -r line; do
if [[ "$line" == event:* ]]; then
echo -e " ${GREEN}${NC} Evento recibido: $line"
elif [[ "$line" == data:* ]]; then
echo " Data: $line"
fi
done
echo ""
# Resumen
echo "================================"
echo "✅ Pruebas completadas"
echo ""
echo "📌 Próximos pasos:"
echo " 1. Abre conversations.php en el navegador"
echo " 2. Abre DevTools → Console"
echo " 3. Deberías ver: '✅ SSE conectado'"
echo " 4. Envía un mensaje de prueba desde WhatsApp"
echo " 5. El mensaje debería aparecer en < 1 segundo"
echo ""
echo "🐛 Para debugging:"
echo " - Ver logs: tail -f /var/log/apache2/error.log"
echo " - Ver eventos: cat ../uploads/events_global.json"
echo " - Conectar manualmente: curl -N $BASE_URL/api/sse_events.php"
echo ""