from __future__ import annotations

import gc
import inspect
import os
import time
from pathlib import Path
from typing import Optional

import dill as pickle
import numpy as np
import pytest
from inference.core.workflows.execution_engine.entities.base import \
    WorkflowImageData


def codeflash_wrap(codeflash_wrapped, codeflash_test_module_name, codeflash_test_class_name, codeflash_test_name, codeflash_function_name, codeflash_line_id, codeflash_loop_index, *args, **kwargs):
    test_id = f'{codeflash_test_module_name}:{codeflash_test_class_name}:{codeflash_test_name}:{codeflash_line_id}:{codeflash_loop_index}'
    if not hasattr(codeflash_wrap, 'index'):
        codeflash_wrap.index = {}
    if test_id in codeflash_wrap.index:
        codeflash_wrap.index[test_id] += 1
    else:
        codeflash_wrap.index[test_id] = 0
    codeflash_test_index = codeflash_wrap.index[test_id]
    invocation_id = f'{codeflash_line_id}_{codeflash_test_index}'
    test_stdout_tag = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_loop_index}:{invocation_id}"
    print(f'!$######{test_stdout_tag}######$!')
    exception = None
    gc.disable()
    try:
        counter = time.perf_counter_ns()
        return_value = codeflash_wrapped(*args, **kwargs)
        codeflash_duration = time.perf_counter_ns() - counter
    except Exception as e:
        codeflash_duration = time.perf_counter_ns() - counter
        exception = e
    gc.enable()
    print(f'!######{test_stdout_tag}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

class ImageParentMetadata:
    """Real class for parent metadata."""

    def __init__(self, metadata_id: str, data: Optional[str]=None):
        self.metadata_id = metadata_id
        self.data = data

    def __eq__(self, other):
        if not isinstance(other, ImageParentMetadata):
            return False
        return self.metadata_id == other.metadata_id and self.data == other.data

    def __repr__(self):
        return f'ImageParentMetadata(metadata_id={self.metadata_id}, data={self.data})'

class VideoMetadata:
    """Real class for video metadata."""

    def __init__(self, frame_count: int, fps: float):
        self.frame_count = frame_count
        self.fps = fps

    def __eq__(self, other):
        if not isinstance(other, VideoMetadata):
            return False
        return self.frame_count == other.frame_count and self.fps == other.fps

    def __repr__(self):
        return f'VideoMetadata(frame_count={self.frame_count}, fps={self.fps})'

def test_copy_and_replace_no_changes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that copy_and_replace without kwargs creates an identical copy.'
    parent_metadata = ImageParentMetadata('parent_1', 'parent_data')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_no_changes', 'copy_and_replace', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_image_reference_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing only the image_reference.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='original_ref')
    new_ref = 'new_ref_456'
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_reference=new_ref)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_image_reference_only', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_base64_image():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing with base64_image.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    numpy_arr = np.array([[1, 2], [3, 4]])
    original = WorkflowImageData(parent_metadata=parent_metadata, numpy_image=numpy_arr)
    base64_str = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, base64_image=base64_str)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_base64_image', 'copy_and_replace', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_numpy_image():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing with numpy_image.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    new_numpy = np.array([[5, 6, 7], [8, 9, 10]])
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=new_numpy)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_numpy_image', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_video_metadata():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing video_metadata.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    video_meta = VideoMetadata(frame_count=30, fps=24.0)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, video_metadata=video_meta)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_video_metadata', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_image_name():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing image_name.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    new_name = 'my_image.jpg'
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name=new_name)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_image_name', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_parent_metadata_updates_ancestor():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that replacing parent_metadata also updates workflow_root_ancestor_metadata when they are the same.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    new_parent = ImageParentMetadata('parent_2', 'data2')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, parent_metadata=new_parent)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_parent_metadata_updates_ancestor', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_multiple_image_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that when multiple image fields are provided, all are set (used for validation).'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, base64_image='base64_data')
    new_ref = 'new_ref'
    new_numpy = np.array([[1, 2], [3, 4]])
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_reference=new_ref, numpy_image=new_numpy)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_multiple_image_fields', 'copy_and_replace', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_none_numpy_image():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test explicitly setting numpy_image to None.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    numpy_arr = np.array([[1, 2], [3, 4]])
    original = WorkflowImageData(parent_metadata=parent_metadata, numpy_image=numpy_arr)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=None, image_reference='ref_123')
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_none_numpy_image', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_empty_string_image_reference():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test setting image_reference to empty string.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_reference='')
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_empty_string_image_reference', 'copy_and_replace', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_empty_string_base64():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test setting base64_image to empty string.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, base64_image='')
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_empty_string_base64', 'copy_and_replace', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_workflow_root_ancestor_metadata_with_different_parent():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing workflow_root_ancestor_metadata when it differs from parent.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    ancestor_metadata = ImageParentMetadata('ancestor_1', 'ancestor_data')
    original = WorkflowImageData(parent_metadata=parent_metadata, workflow_root_ancestor_metadata=ancestor_metadata, image_reference='ref_123')
    new_ancestor = ImageParentMetadata('ancestor_2', 'new_ancestor_data')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, workflow_root_ancestor_metadata=new_ancestor)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_workflow_root_ancestor_metadata_with_different_parent', 'copy_and_replace', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_both_parent_and_ancestor_metadata():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test replacing both parent_metadata and workflow_root_ancestor_metadata.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    ancestor_metadata = ImageParentMetadata('ancestor_1', 'ancestor_data')
    original = WorkflowImageData(parent_metadata=parent_metadata, workflow_root_ancestor_metadata=ancestor_metadata, image_reference='ref_123')
    new_parent = ImageParentMetadata('parent_2', 'data2')
    new_ancestor = ImageParentMetadata('ancestor_2', 'data3')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, parent_metadata=new_parent, workflow_root_ancestor_metadata=new_ancestor)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_both_parent_and_ancestor_metadata', 'copy_and_replace', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_workflow_ancestor_updates_parent():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that replacing workflow_root_ancestor_metadata can update parent when they are same.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    new_ancestor = ImageParentMetadata('ancestor_new', 'ancestor_data')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, workflow_root_ancestor_metadata=new_ancestor)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_workflow_ancestor_updates_parent', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_video_metadata_to_none():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test removing video_metadata by setting to None.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    video_meta = VideoMetadata(frame_count=30, fps=24.0)
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123', video_metadata=video_meta)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, video_metadata=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_video_metadata_to_none', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_image_name_to_none():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test removing image_name by setting to None.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123', image_name='original_name.jpg')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_image_name_to_none', 'copy_and_replace', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_zero_array():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test setting numpy_image to a zero array.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    zero_array = np.zeros((10, 10, 3), dtype=np.uint8)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=zero_array)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_zero_array', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_large_base64_string():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a very large base64_image string.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    large_base64 = 'A' * 100000
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, base64_image=large_base64)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_large_base64_string', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_image_reference_with_special_characters():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test image_reference with special characters and unicode.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='original')
    special_ref = "ref_with_special_chars_!@#$%^&*()_+-=[]{}|;':,.<>?/"
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_reference=special_ref)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_image_reference_with_special_characters', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_image_name_with_various_extensions():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test image_name with different file extensions.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    extensions = ['image.jpg', 'photo.png', 'picture.gif', 'graphic.bmp', 'image.svg']
    for ext in extensions:
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name=ext)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_image_name_with_various_extensions', 'copy_and_replace', '4_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        copied = codeflash_output

def test_copy_and_replace_preserves_parent_metadata_object_identity():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that parent_metadata object identity is preserved when not replaced.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name='new_name.jpg')
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_preserves_parent_metadata_object_identity', 'copy_and_replace', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_numpy_array_dtype_uint8():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with uint8 numpy array (typical for images).'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    image_array = np.array([[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 255]]], dtype=np.uint8)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=image_array)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_numpy_array_dtype_uint8', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_numpy_array_dtype_float32():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with float32 numpy array.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    image_array = np.array([[[0.5, 0.2], [0.8, 0.9]], [[0.1, 0.3], [0.7, 0.4]]], dtype=np.float32)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=image_array)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_numpy_array_dtype_float32', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_large_numpy_array_1000x1000():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large numpy array (1000x1000 pixels, 3 channels).'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    large_array = np.random.randint(0, 256, (1000, 1000, 3), dtype=np.uint8)
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, numpy_image=large_array)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_large_numpy_array_1000x1000', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_many_sequential_copies():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating 1000 sequential copies with different updates.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_0')
    for i in range(1, 100):
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, image_name=f'image_{i}.jpg')
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_many_sequential_copies', 'copy_and_replace', '3_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output

def test_copy_and_replace_chain_with_metadata_updates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test chaining copy_and_replace with metadata updates.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    for i in range(100):
        if i % 2 == 0:
            new_parent = ImageParentMetadata(f'parent_{i}', f'data_{i}')
            _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, parent_metadata=new_parent)
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_chain_with_metadata_updates', 'copy_and_replace', '3_0_1', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            current = codeflash_output
        else:
            _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, image_name=f'image_{i}.jpg')
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_chain_with_metadata_updates', 'copy_and_replace', '3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            current = codeflash_output

def test_copy_and_replace_with_very_large_base64_string_1mb():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a 1MB base64 string.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    base64_1mb = 'B' * 1000000
    _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, base64_image=base64_1mb)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_very_large_base64_string_1mb', 'copy_and_replace', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    copied = codeflash_output

def test_copy_and_replace_with_multiple_video_metadata_updates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test updating video_metadata 100 times.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    video_meta = VideoMetadata(frame_count=30, fps=24.0)
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123', video_metadata=video_meta)
    for i in range(100):
        new_video = VideoMetadata(frame_count=30 + i, fps=24.0 + i * 0.1)
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, video_metadata=new_video)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_multiple_video_metadata_updates', 'copy_and_replace', '4_1', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output

def test_copy_and_replace_with_large_metadata_id_chains():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with 1000 different metadata objects in a chain.'
    parent_metadata = ImageParentMetadata('parent_0', 'data_0')
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    for i in range(1, 101):
        new_parent = ImageParentMetadata(f'parent_{i}', f'data_{i}')
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, parent_metadata=new_parent)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_large_metadata_id_chains', 'copy_and_replace', '3_1', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output

def test_copy_and_replace_with_alternating_image_types():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test alternating between numpy, base64, and reference 100 times.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_0')
    for i in range(100):
        if i % 3 == 0:
            numpy_arr = np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8)
            _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, numpy_image=numpy_arr)
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_alternating_image_types', 'copy_and_replace', '3_0_1', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            current = codeflash_output
        elif i % 3 == 1:
            _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, base64_image=f'base64_data_{i}')
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_alternating_image_types', 'copy_and_replace', '3_0_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            current = codeflash_output
        else:
            _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, image_reference=f'ref_{i}')
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_alternating_image_types', 'copy_and_replace', '3_0_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            current = codeflash_output

def test_copy_and_replace_preserves_unchanged_large_numpy_array():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a large numpy array reference is preserved when not modified.'
    large_array = np.random.randint(0, 256, (500, 500, 3), dtype=np.uint8)
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, numpy_image=large_array)
    for _ in range(100):
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name=f'image_{_}.jpg')
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_preserves_unchanged_large_numpy_array', 'copy_and_replace', '4_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output

def test_copy_and_replace_rapid_succession_100_copies():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test creating 100 copies in rapid succession.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    original = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    copies = []
    for i in range(100):
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(original, image_name=f'copy_{i}.jpg')
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_rapid_succession_100_copies', 'copy_and_replace', '4_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        copy = codeflash_output
        copies.append(copy)
    for i, copy in enumerate(copies):
        pass

def test_copy_and_replace_with_deeply_nested_chain():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test 500-level deep copy chain.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_123')
    for i in range(500):
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, image_name=f'image_{i}.jpg' if i % 2 == 0 else None)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_deeply_nested_chain', 'copy_and_replace', '3_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output

def test_copy_and_replace_with_all_kwargs_simultaneously_100_times():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test updating all possible kwargs 100 times.'
    parent_metadata = ImageParentMetadata('parent_1', 'data1')
    video_meta = VideoMetadata(frame_count=30, fps=24.0)
    current = WorkflowImageData(parent_metadata=parent_metadata, image_reference='ref_0', video_metadata=video_meta, image_name='image_0.jpg')
    for i in range(1, 100):
        new_parent = ImageParentMetadata(f'parent_{i}', f'data_{i}')
        new_video = VideoMetadata(frame_count=30 + i, fps=24.0 + i)
        _call__bound__arguments = inspect.signature(WorkflowImageData.copy_and_replace).bind(current, parent_metadata=new_parent, image_reference=f'ref_{i}', video_metadata=new_video, image_name=f'image_{i}.jpg')
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(WorkflowImageData.copy_and_replace, 'tests.inference.unit_tests.test_copy_and_replace__unit_test_1', None, 'test_copy_and_replace_with_all_kwargs_simultaneously_100_times', 'copy_and_replace', '4_2', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        current = codeflash_output