#!/bin/bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # Test script for HeroDB - Redis-compatible database with redb backend # This script starts the server and runs comprehensive tests # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration DB_DIR="/tmp/test_db" PORT=6381 SERVER_PID="" # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Function to cleanup on exit cleanup() { if [ ! -z "$SERVER_PID" ]; then print_status "Stopping HeroDB server (PID: $SERVER_PID)..." kill $SERVER_PID 2>/dev/null || true wait $SERVER_PID 2>/dev/null || true fi # Clean up test database if [ -d "$DB_DIR" ]; then print_status "Cleaning up test database directory..." rm -rf "$DB_DIR" fi } # Set trap to cleanup on script exit trap cleanup EXIT # Function to wait for server to start wait_for_server() { local max_attempts=30 local attempt=1 print_status "Waiting for server to start on port $PORT..." while [ $attempt -le $max_attempts ]; do if nc -z localhost $PORT 2>/dev/null; then print_success "Server is ready!" return 0 fi echo -n "." sleep 1 attempt=$((attempt + 1)) done print_error "Server failed to start within $max_attempts seconds" return 1 } # Function to send Redis command and get response redis_cmd() { local cmd="$1" local expected="$2" print_status "Testing: $cmd" local result=$(echo "$cmd" | redis-cli -p $PORT --raw 2>/dev/null || echo "ERROR") if [ "$expected" != "" ] && [ "$result" != "$expected" ]; then print_error "Expected: '$expected', Got: '$result'" return 1 else print_success "✓ $cmd -> $result" return 0 fi } # Main execution main() { print_status "Starting HeroDB" # Build the project print_status "Building HeroDB..." if ! cargo build -p herodb --release; then print_error "Failed to build HeroDB" exit 1 fi # Create test database directory mkdir -p "$DB_DIR" # Start the server print_status "Starting HeroDB server..." ${SCRIPT_DIR}/target/release/herodb --dir "$DB_DIR" --port $PORT & SERVER_PID=$! # Wait for server to start if ! wait_for_server; then print_error "Failed to start server" exit 1 fi } # Check dependencies check_dependencies() { if ! command -v cargo &> /dev/null; then print_error "cargo is required but not installed" exit 1 fi if ! command -v nc &> /dev/null; then print_warning "netcat (nc) not found - some tests may not work properly" fi if ! command -v redis-cli &> /dev/null; then print_warning "redis-cli not found - using netcat fallback" fi } # Run dependency check and main function check_dependencies main "$@" tail -f /dev/null