#!/bin/bash
# Run CLI integration tests against a specific binary
#
# Usage:
#   ./scripts/test_cli_binary.sh <path-to-wasot-binary>
#   ./scripts/test_cli_binary.sh <path-to-wasot-binary> <filter>
#
# Examples:
#   ./scripts/test_cli_binary.sh ./target/release/wasot
#   ./scripts/test_cli_binary.sh /usr/local/bin/wasot
#   ./scripts/test_cli_binary.sh ./target/release/wasot selector
#
# The binary path is passed via WASOT_TEST_BINARY environment variable.

set -e

if [ -z "$1" ]; then
    echo "Usage: $0 <path-to-wasot-binary> [filter]"
    echo ""
    echo "Examples:"
    echo "  $0 ./target/release/wasot"
    echo "  $0 /usr/local/bin/wasot"
    echo "  $0 ./target/release/wasot selector"
    exit 1
fi

BINARY_PATH="$1"
FILTER="${2:-}"

# Verify binary exists and is executable
if [ ! -x "$BINARY_PATH" ]; then
    echo "❌ Error: Binary not found or not executable: $BINARY_PATH"
    exit 1
fi

# Get absolute path
BINARY_PATH="$(realpath "$BINARY_PATH")"

echo "🧪 Running CLI integration tests..."
echo "   Binary: $BINARY_PATH"

if [ -n "$FILTER" ]; then
    echo "   Filter: $FILTER"
    WASOT_TEST_BINARY="$BINARY_PATH" cargo test --test cli_tests "$FILTER" -- --nocapture
else
    WASOT_TEST_BINARY="$BINARY_PATH" cargo test --test cli_tests
fi

echo "✅ CLI tests completed!"
