from core.languages.python.optimizer.diff_patches_utils.search_and_replace import apply_patches


def test_patches() -> None:
    patches = """<<<<<<< SEARCH
    total = 0
    for n in numbers:
        total += n
    mean = total / len(numbers)

    variance_sum = 0
    for n in numbers:
        variance_sum += (n - mean) ** 2
    variance = variance_sum / len(numbers)
=======
    mean = sum(numbers) / len(numbers)
    variance = sum((n - mean) ** 2 for n in numbers) / len(numbers)
>>>>>>> REPLACE

<<<<<<< SEARCH
    min_val = numbers[0]
    max_val = numbers[0]
    for n in numbers:
        if n < min_val:
            min_val = n
        if n > max_val:
            max_val = n
=======
    min_val = min(numbers)
    max_val = max(numbers)
>>>>>>> REPLACE"""

    original = """def compute_statistics(numbers):
    if not numbers:
        return None

    total = 0
    for n in numbers:
        total += n
    mean = total / len(numbers)

    variance_sum = 0
    for n in numbers:
        variance_sum += (n - mean) ** 2
    variance = variance_sum / len(numbers)

    print("Mean:", mean)
    print("Variance:", variance)

    min_val = numbers[0]
    max_val = numbers[0]
    for n in numbers:
        if n < min_val:
            min_val = n
        if n > max_val:
            max_val = n

    return {
        'mean': mean,
        'variance': variance,
        'min': min_val,
        'max': max_val
    }
"""
    expected = """def compute_statistics(numbers):
    if not numbers:
        return None

    mean = sum(numbers) / len(numbers)
    variance = sum((n - mean) ** 2 for n in numbers) / len(numbers)

    print("Mean:", mean)
    print("Variance:", variance)

    min_val = min(numbers)
    max_val = max(numbers)

    return {
        'mean': mean,
        'variance': variance,
        'min': min_val,
        'max': max_val
    }
"""
    new = apply_patches(patches, original)
    assert new == expected
