#!/bin/bash

# Script to test different combinations to find test interactions
# Usage: ./test_combinations.sh [runs]

RUNS=${1:-20}

echo "Testing different test combinations to find interactions..."
echo "Runs per combination: $RUNS"
echo "Started at: $(date)"
echo

test_combination() {
    local name="$1"
    local cmd="$2"
    local runs="$3"
    
    echo "=== Testing: $name ==="
    echo "Command: $cmd"
    
    local success=0
    local fail=0
    local timeout=0
    
    for i in $(seq 1 $runs); do
        echo -n "  Run $i/$runs: "
        
        timeout 60s bash -c "$cmd" >/dev/null 2>&1
        case $? in
            0) success=$((success + 1)); echo "PASS" ;;
            124) timeout=$((timeout + 1)); echo "TIMEOUT" ;;
            *) fail=$((fail + 1)); echo "FAIL" ;;
        esac
    done
    
    echo "  Results: $success PASS, $fail FAIL, $timeout TIMEOUT"
    echo "  Success rate: $(echo "scale=1; $success * 100 / $runs" | bc -l)%"
    echo
}

# Test various combinations that might cause the benchmark to fail
test_combination "Unit tests + Benchmark" \
    "cargo test --lib --quiet && cargo test --test submit_to_main_thread_benchmark --quiet" \
    $RUNS

test_combination "Executor bug test + Benchmark" \
    "cargo test --test executor_bug_test --quiet && cargo test --test submit_to_main_thread_benchmark --quiet" \
    $RUNS

test_combination "Platform tests + Benchmark" \
    "cargo test --test platform_coalesced_keyboard_test --quiet && cargo test --test platform_coalesced_mouse_test --quiet && cargo test --test submit_to_main_thread_benchmark --quiet" \
    $RUNS

test_combination "All integration tests" \
    "cargo test --tests --quiet" \
    $RUNS

test_combination "Doctests + Integration tests" \
    "cargo test --doc --quiet && cargo test --tests --quiet" \
    $RUNS

test_combination "Full test suite (like original)" \
    "cargo test --quiet" \
    $RUNS

echo "Finished at: $(date)"