#!/usr/bin/env python3
"""Test the error handler to ensure it correctly detects error messages."""

import sys
import os
import re

# Add the bot directory to the path so we can import modules
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Directly implement the error detection function to avoid module dependencies
def is_error_response(response_text: str) -> bool:
    """
    Detect if a response text is an error message.
    
    Args:
        response_text: The response text to check
        
    Returns:
        bool: True if the response appears to be an error message
    """
    if not response_text or not isinstance(response_text, str):
        return False
    
    response_lower = response_text.lower().strip()
    
    # Common error patterns
    error_patterns = [
        r'^error:?\s*\d{3}',  # "Error: 502" or "Error 502"
        r'^error:?\s+',  # "Error: " or "Error "
        r'^\d{3}\s+error',  # "502 Error"
        r'^sorry,?\s+(there\s+was\s+)?an?\s+error',  # "Sorry, an error" or "Sorry, there was an error"
        r'^sorry,?\s+the\s+response\s+took\s+too\s+long',  # Timeout error
        r'connection\s+(refused|failed|error|timeout)',
        r'timed?\s*out',
        r'failed\s+to\s+(connect|respond|process)',
        r'service\s+unavailable',
        r'internal\s+server\s+error',
        r'bad\s+gateway',
        r'gateway\s+timeout',
    ]
    
    # Check if response matches any error pattern
    for pattern in error_patterns:
        if re.search(pattern, response_lower):
            return True
    
    # Check for HTTP status codes indicating errors
    if re.match(r'^\d{3}$', response_text.strip()):
        status_code = int(response_text.strip())
        if status_code >= 400:  # HTTP error codes
            return True
    
    return False

# Test cases
test_cases = [
    # Error responses (should return True)
    ("Error 502", True),
    ("Error: 502", True),
    ("Error: Bad Gateway", True),
    ("502 Error", True),
    ("Sorry, there was an error", True),
    ("Sorry, an error occurred", True),
    ("Sorry, the response took too long. Please try again.", True),
    ("Connection refused", True),
    ("Connection timeout", True),
    ("Timed out", True),
    ("Failed to connect", True),
    ("Service unavailable", True),
    ("Internal server error", True),
    ("Bad gateway", True),
    ("Gateway timeout", True),
    ("500", True),
    ("502", True),
    ("503", True),
    
    # Normal responses (should return False)
    ("Hi! How are you doing today?", False),
    ("I'm Hatsune Miku! *waves*", False),
    ("That's so cool! Tell me more!", False),
    ("Sorry to hear that!", False),
    ("I'm sorry, but I can't help with that.", False),
    ("200", False),
    ("304", False),
    ("The error in your code is...", False),
]

def run_tests():
    print("Testing error detection...")
    print("=" * 60)
    
    passed = 0
    failed = 0
    
    for text, expected in test_cases:
        result = is_error_response(text)
        status = "✓" if result == expected else "✗"
        
        if result == expected:
            passed += 1
        else:
            failed += 1
            print(f"{status} FAILED: '{text}' -> {result} (expected {expected})")
    
    print("=" * 60)
    print(f"Tests passed: {passed}/{len(test_cases)}")
    print(f"Tests failed: {failed}/{len(test_cases)}")
    
    if failed == 0:
        print("\n✓ All tests passed!")
    else:
        print(f"\n✗ {failed} test(s) failed")
    
    return failed == 0

if __name__ == "__main__":
    success = run_tests()
    exit(0 if success else 1)
