"""
Unit tests for SafeCodeExecutor
"""

import sys
sys.path.insert(0, "..")

from safe_code_executor import SafeCodeExecutor


def test_basic_python_execution():
    """Test basic Python execution"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="print('Hello, World!')"
    )
    
    assert result["status"] == "succeeded"
    assert "Hello, World!" in result["stdout"]
    print("✓ Basic execution works")


def test_json_transform():
    """Test JSON transformation"""
    executor = SafeCodeExecutor()
    
    code = '''
import json
data = {"a": 1, "b": 2, "c": 3}
result = {"sum": data["a"] + data["b"] + data["c"]}
print(json.dumps(result))
'''
    
    result = executor.execute(language="python", code=code)
    
    assert result["status"] == "succeeded"
    assert result["result_json"]["sum"] == 6
    print("✓ JSON transform works")


def test_regex_parse():
    """Test regex parsing"""
    executor = SafeCodeExecutor()
    
    code = '''
import re
text = "Email: test@example.com"
match = re.search(r'[\w.-]+@[\w.-]+', text)
print(match.group() if match else "None")
'''
    
    result = executor.execute(language="python", code=code)
    
    assert result["status"] == "succeeded"
    assert "test@example.com" in result["stdout"]
    print("✓ Regex parse works")


def test_validation_blocks_os_import():
    """Test that os import is blocked"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="import os; print(os.getcwd())"
    )
    
    assert result["status"] == "failed"
    assert "import os" in result["error"] or "os" in result["error"]
    print("✓ OS import blocked")


def test_validation_blocks_subprocess():
    """Test that subprocess is blocked"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="import subprocess; subprocess.run(['ls'])"
    )
    
    assert result["status"] == "failed"
    print("✓ Subprocess blocked")


def test_validation_blocks_socket():
    """Test that socket is blocked"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="import socket; s = socket.socket()"
    )
    
    assert result["status"] == "failed"
    print("✓ Socket blocked")


def test_validation_blocks_eval():
    """Test that eval is blocked"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="eval('1+1')"
    )
    
    assert result["status"] == "failed"
    print("✓ Eval blocked")


def test_validation_blocks_file_io():
    """Test that file I/O is blocked"""
    executor = SafeCodeExecutor()
    
    result = executor.execute(
        language="python",
        code="open('/etc/passwd').read()"
    )
    
    assert result["status"] == "failed"
    print("✓ File I/O blocked")


def test_math_operations():
    """Test math operations work"""
    executor = SafeCodeExecutor()
    
    code = '''
import math
print(math.sqrt(16))
print(math.pi)
'''
    
    result = executor.execute(language="python", code=code)
    
    assert result["status"] == "succeeded"
    print("✓ Math operations work")


def test_statistics():
    """Test statistics module"""
    executor = SafeCodeExecutor()
    
    code = '''
import statistics
data = [1, 2, 3, 4, 5]
print(statistics.mean(data))
print(statistics.median(data))
'''
    
    result = executor.execute(language="python", code=code)
    
    assert result["status"] == "succeeded"
    print("✓ Statistics module works")


if __name__ == "__main__":
    print("=== Running Unit Tests ===\n")
    
    test_basic_python_execution()
    test_json_transform()
    test_regex_parse()
    test_validation_blocks_os_import()
    test_validation_blocks_subprocess()
    test_validation_blocks_socket()
    test_validation_blocks_eval()
    test_validation_blocks_file_io()
    test_math_operations()
    test_statistics()
    
    print("\n✅ All unit tests passed!")
