#!/bin/bash
# ===============================================================================
# HomeAudit Test Script
# Tests the enhanced audit system functionality
# ===============================================================================

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_status() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[PASS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[FAIL]${NC} $1"
}

# Test counter
TESTS_PASSED=0
TESTS_FAILED=0

# Test function
run_test() {
    local test_name="$1"
    local test_command="$2"
    
    print_status "Running: $test_name"
    
    if eval "$test_command" >/dev/null 2>&1; then
        print_success "$test_name"
        ((TESTS_PASSED++))
    else
        print_error "$test_name"
        ((TESTS_FAILED++))
    fi
}

# Main test execution
main() {
    echo "========================================"
    echo "HomeAudit System Test Suite"
    echo "Testing Version 2.0 Enhancements"
    echo "========================================"
    echo

    # Test 1: Check if main audit script exists and is executable
    run_test "Audit script exists and is executable" \
        "[ -f linux_system_audit.sh ] && [ -x linux_system_audit.sh ]"

    # Test 2: Check if deployment script exists and is executable
    run_test "Deployment script exists and is executable" \
        "[ -f deploy_audit.sh ] && [ -x deploy_audit.sh ]"

    # Test 3: Check if Ansible playbook exists
    run_test "Ansible playbook exists" \
        "[ -f linux_audit_playbook.yml ]"

    # Test 4: Check if inventory file exists
    run_test "Inventory file exists" \
        "[ -f inventory.ini ]"

    # Test 5: Check if configuration file exists
    run_test "Configuration file exists" \
        "[ -f audit_config.yml ]"

    # Test 6: Test audit script syntax
    run_test "Audit script has valid bash syntax" \
        "bash -n linux_system_audit.sh"

    # Test 7: Test deployment script syntax
    run_test "Deployment script has valid bash syntax" \
        "bash -n deploy_audit.sh"

    # Test 8: Test Ansible playbook syntax
    run_test "Ansible playbook has valid YAML syntax" \
        "ansible-playbook --syntax-check linux_audit_playbook.yml >/dev/null 2>&1"

    # Test 9: Check for required functions in audit script
    run_test "Audit script contains enhanced error handling" \
        "grep -q 'error_handler' linux_system_audit.sh"

    # Test 10: Check for enhanced logging
    run_test "Audit script contains enhanced logging" \
        "grep -q 'log_info\|log_warn\|log_error' linux_system_audit.sh"

    # Test 11: Check for environment validation
    run_test "Audit script contains environment validation" \
        "grep -q 'validate_environment' linux_system_audit.sh"

    # Test 12: Check for enhanced security features
    run_test "Audit script contains enhanced security checks" \
        "grep -q 'dangerous_suid\|sensitive_patterns' linux_system_audit.sh"

    # Test 13: Check for JSON fallback functionality
    run_test "Audit script contains JSON fallback logic" \
        "grep -q 'jq not available' linux_system_audit.sh"

    # Test 14: Check for retry logic in playbook
    run_test "Ansible playbook contains retry logic" \
        "grep -q 'retries:' linux_audit_playbook.yml"

    # Test 15: Check for enhanced connectivity testing
    run_test "Deployment script contains enhanced connectivity testing" \
        "grep -q 'success_count.*total_hosts' deploy_audit.sh"

    # Test 16: Check for configuration file validation
    run_test "Configuration file has valid YAML structure" \
        "python3 -c 'import yaml; yaml.safe_load(open(\"audit_config.yml\"))' >/dev/null 2>&1"

    # Test 17: Check for version information
    run_test "Scripts contain version 2.0 information" \
        "grep -q 'Version.*2.0' linux_system_audit.sh deploy_audit.sh"

    # Test 18: Check for enhanced summary generation
    run_test "Audit script contains enhanced summary generation" \
        "grep -q 'create_enhanced_summary' linux_system_audit.sh"

    # Test 19: Check for archive verification
    run_test "Audit script contains archive verification" \
        "grep -q 'verify_integrity\|archive_size' linux_system_audit.sh"

    # Test 20: Check for modular architecture
    run_test "Audit script uses modular approach" \
        "grep -q 'modules=(' linux_system_audit.sh"

    echo
    echo "========================================"
    echo "Test Results Summary"
    echo "========================================"
    echo "Tests Passed: $TESTS_PASSED"
    echo "Tests Failed: $TESTS_FAILED"
    echo "Total Tests: $((TESTS_PASSED + TESTS_FAILED))"
    echo

    if [ $TESTS_FAILED -eq 0 ]; then
        print_success "All tests passed! HomeAudit v2.0 is ready for deployment."
        exit 0
    else
        print_error "Some tests failed. Please review the issues above."
        exit 1
    fi
}

# Run main function
main "$@"
