#!/usr/bin/env bash

# Test script for configuration tool
# Tests decision logic tree and validation

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"


log_success "Testing Configuration Tool..."

# Test 1: Python syntax
log_warn "Test 1: Python syntax check"
python3 -m py_compile scripts/configure-network.py scripts/configure-network-validation.py
if [ $? -eq 0 ]; then
    log_success "✓ Python syntax is valid"
else
    log_error "✗ Python syntax errors found"
    exit 1
fi

# Test 2: Import validation
log_warn "Test 2: Import validation"
python3 -c "import sys; sys.path.insert(0, 'scripts'); from configure_network_validation import ConfigurationValidator; print('✓ Validation module imports successfully')"
if [ $? -eq 0 ]; then
    log_success "✓ Validation module imports successfully"
else
    log_error "✗ Validation module import failed"
    exit 1
fi

# Test 3: Validation logic
log_warn "Test 3: Validation logic"
python3 << 'EOF'
import sys
sys.path.insert(0, 'scripts')
from configure_network_validation import ConfigurationValidator
from pathlib import Path

# Test configuration
test_config = {
    'genesis': {
        'chainId': 138,
        'blockPeriodSeconds': 2,
        'epochLength': 30000,
        'requestTimeoutSeconds': 10,
        'gasLimit': '0x1c9c380',
    },
    'validators': ['0x' + '0' * 40],
    'network': {
        'clusterName': 'test-cluster',
        'resourceGroup': 'test-rg',
        'location': 'eastus',
        'vnetAddressSpace': '10.0.0.0/16',
        'subnets': {
            'validators': '10.0.1.0/24',
            'sentries': '10.0.2.0/24',
            'rpc': '10.0.3.0/24',
            'aks': '10.0.4.0/24',
        }
    },
    'nodes': {
        'validatorCount': 1,
        'sentryCount': 0,
        'rpcCount': 0,
    },
    'ports': {
        'p2p': 30303,
        'rpcHttp': 8545,
        'rpcWs': 8546,
        'metrics': 9545,
    },
    'besu': {
        'validators': {'rpcHttpEnabled': False},
        'sentries': {'rpcHttpEnabled': True},
        'rpc': {'rpcHttpEnabled': True, 'p2pEnabled': False},
    },
    'deployment': {
        'type': 'aks',
        'aksEnabled': True,
        'vmEnabled': False,
    },
    'alloc': {},
}

validator = ConfigurationValidator(test_config, Path('.'))
is_valid, errors, warnings = validator.validate_all()

print(f"Validation result: {'Valid' if is_valid else 'Invalid'}")
print(f"Errors: {len(errors)}")
print(f"Warnings: {len(warnings)}")

if errors:
    print("Errors:")
    for error in errors:
        print(f"  - {error}")

if warnings:
    print("Warnings:")
    for warning in warnings:
        print(f"  - {warning}")

if len(warnings) > 0:
    print("✓ Validation logic working (warnings expected for test config)")
else:
    print("⚠ No warnings generated (may indicate validation issue)")
EOF

if [ $? -eq 0 ]; then
    log_success "✓ Validation logic test passed"
else
    log_error "✗ Validation logic test failed"
    exit 1
fi

log_success "All tests passed!"

