#!/bin/bash
# Test script for native libraries with Redis via docker-compose
# Usage: ./scripts/test-native-with-redis.sh

set -e

echo "=== FetchML Native Library Test with Redis ==="
echo ""

# Check if native libraries are built
if [ ! -f "native/build/libqueue_index.so" ] && [ ! -f "native/build/libqueue_index.dylib" ]; then
    echo "Building native libraries..."
    make native-build
fi

# Start Redis via docker-compose
echo "Starting Redis..."
cd deployments
docker-compose -f docker-compose.dev.yml up -d redis
cd ..

# Wait for Redis to be ready
echo "Waiting for Redis to be ready..."
sleep 2
until docker exec ml-experiments-redis redis-cli ping | grep -q PONG; do
    echo "  Redis not ready yet, waiting..."
    sleep 1
done
echo "  Redis is ready!"
echo ""

# Run tests with native_libs build tag
echo "Running tests with -tags native_libs..."
CGO_ENABLED=1 go test -tags native_libs -v ./tests/benchmarks/... 2>&1 | head -100

TEST_EXIT=${PIPESTATUS[0]}

echo ""
echo "=== Test Summary ==="
if [ $TEST_EXIT -eq 0 ]; then
    echo "✅ All tests passed with native libraries enabled"
else
    echo "❌ Tests failed with exit code: $TEST_EXIT"
fi

# Cleanup
echo ""
echo "Stopping Redis..."
cd deployments
docker-compose -f docker-compose.dev.yml stop redis
cd ..

exit $TEST_EXIT
