#!/bin/bash # Regime Detection gRPC Endpoint Integration Test Script # # This script tests Wave D regime detection endpoints: # 1. Start Trading Service and API Gateway # 2. Test gRPC endpoints with grpcurl # 3. Test TLI commands # 4. Cleanup # # Usage: # ./scripts/test_regime_endpoints.sh set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" cd "$PROJECT_ROOT" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}======================================${NC}" echo -e "${BLUE}Regime Detection Endpoint Integration Test${NC}" echo -e "${BLUE}======================================${NC}" echo "" # Step 1: Check prerequisites echo -e "${YELLOW}[1/6] Checking prerequisites...${NC}" if ! command -v grpcurl &> /dev/null; then echo -e "${RED}✗ grpcurl not found. Install with: go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest${NC}" exit 1 fi if ! command -v tli &> /dev/null; then echo -e "${YELLOW}⚠ tli not found. Building TLI...${NC}" cargo build -p tli --release export PATH="$PROJECT_ROOT/target/release:$PATH" fi echo -e "${GREEN}✓ Prerequisites OK${NC}" echo "" # Step 2: Check if services are already running echo -e "${YELLOW}[2/6] Checking service status...${NC}" TRADING_SERVICE_RUNNING=false API_GATEWAY_RUNNING=false if lsof -i :50052 &> /dev/null; then echo -e "${GREEN}✓ Trading Service already running on port 50052${NC}" TRADING_SERVICE_RUNNING=true else echo -e "${YELLOW}⚠ Trading Service not running${NC}" fi if lsof -i :50051 &> /dev/null; then echo -e "${GREEN}✓ API Gateway already running on port 50051${NC}" API_GATEWAY_RUNNING=true else echo -e "${YELLOW}⚠ API Gateway not running${NC}" fi # Step 3: Start services if not running if [ "$TRADING_SERVICE_RUNNING" = false ] || [ "$API_GATEWAY_RUNNING" = false ]; then echo "" echo -e "${YELLOW}[3/6] Starting services...${NC}" if [ "$TRADING_SERVICE_RUNNING" = false ]; then echo -e "${BLUE}Starting Trading Service...${NC}" cargo run -p trading_service --bin trading_service --release > /tmp/trading_service.log 2>&1 & TRADING_SERVICE_PID=$! echo "Trading Service PID: $TRADING_SERVICE_PID" sleep 8 fi if [ "$API_GATEWAY_RUNNING" = false ]; then echo -e "${BLUE}Starting API Gateway...${NC}" cargo run -p api_gateway --release > /tmp/api_gateway.log 2>&1 & API_GATEWAY_PID=$! echo "API Gateway PID: $API_GATEWAY_PID" sleep 8 fi echo -e "${GREEN}✓ Services started${NC}" else echo -e "${GREEN}✓ Using existing services${NC}" fi echo "" # Step 4: Test gRPC endpoints with grpcurl echo -e "${YELLOW}[4/6] Testing gRPC endpoints...${NC}" echo "" # Test GetRegimeState echo -e "${BLUE}Testing GetRegimeState for ES.FUT...${NC}" REGIME_STATE=$(grpcurl -plaintext \ -d '{"symbol":"ES.FUT"}' \ localhost:50052 \ foxhunt.trading.TradingService/GetRegimeState 2>&1) if [ $? -eq 0 ]; then echo -e "${GREEN}✓ GetRegimeState succeeded${NC}" echo "$REGIME_STATE" | jq '.' 2>/dev/null || echo "$REGIME_STATE" else echo -e "${RED}✗ GetRegimeState failed${NC}" echo "$REGIME_STATE" fi echo "" # Test GetRegimeTransitions echo -e "${BLUE}Testing GetRegimeTransitions for ES.FUT (limit=10)...${NC}" TRANSITIONS=$(grpcurl -plaintext \ -d '{"symbol":"ES.FUT","limit":10}' \ localhost:50052 \ foxhunt.trading.TradingService/GetRegimeTransitions 2>&1) if [ $? -eq 0 ]; then echo -e "${GREEN}✓ GetRegimeTransitions succeeded${NC}" echo "$TRANSITIONS" | jq '.' 2>/dev/null || echo "$TRANSITIONS" else echo -e "${RED}✗ GetRegimeTransitions failed${NC}" echo "$TRANSITIONS" fi echo "" # Test via API Gateway (port 50051) echo -e "${BLUE}Testing GetRegimeState via API Gateway (port 50051)...${NC}" GATEWAY_REGIME=$(grpcurl -plaintext \ -d '{"symbol":"NQ.FUT"}' \ localhost:50051 \ foxhunt.trading.TradingService/GetRegimeState 2>&1) if [ $? -eq 0 ]; then echo -e "${GREEN}✓ API Gateway proxy succeeded${NC}" echo "$GATEWAY_REGIME" | jq '.' 2>/dev/null || echo "$GATEWAY_REGIME" else echo -e "${RED}✗ API Gateway proxy failed${NC}" echo "$GATEWAY_REGIME" fi echo "" # Step 5: Test TLI commands echo -e "${YELLOW}[5/6] Testing TLI commands...${NC}" echo "" # Note: TLI commands require authentication, so we'll provide instructions echo -e "${BLUE}To test TLI commands, run:${NC}" echo "" echo -e " ${GREEN}# View current regime state${NC}" echo -e " tli trade ml regime --symbol ES.FUT" echo "" echo -e " ${GREEN}# View regime transitions${NC}" echo -e " tli trade ml transitions --symbol ES.FUT --limit 20" echo "" echo -e " ${GREEN}# View regime state for multiple symbols${NC}" echo -e " tli trade ml regime --symbol NQ.FUT" echo -e " tli trade ml regime --symbol CL.FUT" echo "" echo -e "${YELLOW}Note: TLI commands require authentication. Run 'tli auth login' first if needed.${NC}" echo "" # Step 6: Cleanup echo -e "${YELLOW}[6/6] Cleanup...${NC}" if [ -n "$TRADING_SERVICE_PID" ]; then echo "Stopping Trading Service (PID: $TRADING_SERVICE_PID)..." kill $TRADING_SERVICE_PID 2>/dev/null || true fi if [ -n "$API_GATEWAY_PID" ]; then echo "Stopping API Gateway (PID: $API_GATEWAY_PID)..." kill $API_GATEWAY_PID 2>/dev/null || true fi echo -e "${GREEN}✓ Cleanup complete${NC}" echo "" echo -e "${BLUE}======================================${NC}" echo -e "${GREEN}✓ Integration test complete${NC}" echo -e "${BLUE}======================================${NC}" echo "" echo -e "${YELLOW}Next steps:${NC}" echo "1. Review logs: tail -f /tmp/trading_service.log /tmp/api_gateway.log" echo "2. Run integration tests: cargo test -p trading_service --test regime_grpc_integration_test -- --ignored" echo "3. Test TLI commands (see above)" echo ""