#!/bin/bash set -e # AgentHub J10 — 2-Agent Smoke Test # Tests WebSocket messaging between two agents with persistence verification # Usage: ./smoke-lan-2-agents.sh # Example: ./smoke-lan-2-agents.sh 192.168.1.100:3000 AGENTHUB_HOST="${1:-127.0.0.1:3000}" BASE_URL="http://${AGENTHUB_HOST}" echo "🚀 AgentHub J10 Smoke Test — 2 Agents" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "Target: $BASE_URL" echo "" # Color output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color step() { echo -e "${YELLOW}▶ $1${NC}" } success() { echo -e "${GREEN}✓ $1${NC}" } error() { echo -e "${RED}✗ $1${NC}" exit 1 } # Step 1 — Health check step "Step 1/9: Health check" HEALTH=$(curl -s "${BASE_URL}/healthz" || echo '{"status":"error"}') if echo "$HEALTH" | grep -q '"status":"ok"'; then success "AgentHub is healthy" else error "AgentHub healthz failed: $HEALTH" fi echo "" # Step 2 — Create Agent 1 step "Step 2/9: Create TestAgent1" AGENT1_RESP=$(curl -s -X POST "${BASE_URL}/api/agents" \ -H "Content-Type: application/json" \ -d '{"name":"TestAgent1","capabilities":["chat"]}') AGENT1_ID=$(echo "$AGENT1_RESP" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$AGENT1_ID" ]; then error "Failed to create Agent1: $AGENT1_RESP" fi success "Agent1 created: $AGENT1_ID" echo "" # Step 3 — Create Agent 2 step "Step 3/9: Create TestAgent2" AGENT2_RESP=$(curl -s -X POST "${BASE_URL}/api/agents" \ -H "Content-Type: application/json" \ -d '{"name":"TestAgent2","capabilities":["chat"]}') AGENT2_ID=$(echo "$AGENT2_RESP" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$AGENT2_ID" ]; then error "Failed to create Agent2: $AGENT2_RESP" fi success "Agent2 created: $AGENT2_ID" echo "" # Step 4 — Generate API token for Agent 1 step "Step 4/9: Generate API token for Agent1" TOKEN1_RESP=$(curl -s -X POST "${BASE_URL}/api/tokens" \ -H "Content-Type: application/json" \ -d "{\"agentId\":\"$AGENT1_ID\",\"name\":\"test-token-1\"}") TOKEN1=$(echo "$TOKEN1_RESP" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) if [ -z "$TOKEN1" ]; then error "Failed to generate token for Agent1: $TOKEN1_RESP" fi success "Token1 generated: ${TOKEN1:0:12}..." echo "" # Step 5 — Generate API token for Agent 2 step "Step 5/9: Generate API token for Agent2" TOKEN2_RESP=$(curl -s -X POST "${BASE_URL}/api/tokens" \ -H "Content-Type: application/json" \ -d "{\"agentId\":\"$AGENT2_ID\",\"name\":\"test-token-2\"}") TOKEN2=$(echo "$TOKEN2_RESP" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) if [ -z "$TOKEN2" ]; then error "Failed to generate token for Agent2: $TOKEN2_RESP" fi success "Token2 generated: ${TOKEN2:0:12}..." echo "" # Step 6 — Exchange tokens for JWTs step "Step 6/9: Exchange tokens for JWTs" JWT1_RESP=$(curl -s -X POST "${BASE_URL}/api/sessions" \ -H "Content-Type: application/json" \ -d "{\"apiToken\":\"$TOKEN1\"}") JWT1=$(echo "$JWT1_RESP" | grep -o '"jwt":"[^"]*"' | cut -d'"' -f4) if [ -z "$JWT1" ]; then error "Failed to get JWT for Agent1: $JWT1_RESP" fi success "JWT1 obtained: ${JWT1:0:20}..." JWT2_RESP=$(curl -s -X POST "${BASE_URL}/api/sessions" \ -H "Content-Type: application/json" \ -d "{\"apiToken\":\"$TOKEN2\"}") JWT2=$(echo "$JWT2_RESP" | grep -o '"jwt":"[^"]*"' | cut -d'"' -f4) if [ -z "$JWT2" ]; then error "Failed to get JWT for Agent2: $JWT2_RESP" fi success "JWT2 obtained: ${JWT2:0:20}..." echo "" # Step 7 — Create test room step "Step 7/9: Create test room" ROOM_RESP=$(curl -s -X POST "${BASE_URL}/api/rooms" \ -H "Authorization: Bearer $JWT1" \ -H "Content-Type: application/json" \ -d "{\"name\":\"smoke-test-room-$(date +%s)\",\"createdByAgentId\":\"$AGENT1_ID\"}") ROOM_ID=$(echo "$ROOM_RESP" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$ROOM_ID" ]; then error "Failed to create room: $ROOM_RESP" fi success "Room created: $ROOM_ID" echo "" # Step 8 — WebSocket test instructions step "Step 8/9: WebSocket test (manual verification required)" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "REST API setup complete. For WebSocket test, use wscat or equivalent:" echo "" echo "# Terminal 1 — Agent 1" echo "wscat -c \"ws://${AGENTHUB_HOST}/agents?token=${JWT1}\"" echo "# Send: {\"type\":\"room:join\",\"roomId\":\"$ROOM_ID\"}" echo "# Send: {\"type\":\"message:send\",\"roomId\":\"$ROOM_ID\",\"body\":\"Hello from Agent 1\"}" echo "" echo "# Terminal 2 — Agent 2" echo "wscat -c \"ws://${AGENTHUB_HOST}/agents?token=${JWT2}\"" echo "# Send: {\"type\":\"room:join\",\"roomId\":\"$ROOM_ID\"}" echo "# Verify: Receives message from Agent 1" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" echo "Or use the companion Node.js test script:" echo " node test/smoke-lan-2-agents-ws.js $AGENTHUB_HOST $JWT1 $JWT2 $ROOM_ID" echo "" # Step 9 — Verify persistence via REST step "Step 9/9: Verify message persistence (after WebSocket test)" echo "" echo "After sending messages via WebSocket, verify persistence:" echo "" echo "curl -s \"${BASE_URL}/api/rooms/${ROOM_ID}/messages\" \\" echo " -H \"Authorization: Bearer $JWT1\" | grep -o '\"body\":\"[^\"]*\"'" echo "" echo "Expected output: \"body\":\"Hello from Agent 1\"" echo "" success "Setup complete! Proceed with WebSocket test." echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "Test summary:" echo " Agent 1 ID: $AGENT1_ID" echo " Agent 2 ID: $AGENT2_ID" echo " Room ID: $ROOM_ID" echo " JWT1: ${JWT1:0:30}..." echo " JWT2: ${JWT2:0:30}..." echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"