from __future__ import annotations

import base64
import gc
import inspect
import os
import time
from pathlib import Path
from typing import List, Optional
from unittest.mock import MagicMock, Mock, patch

import dill as pickle
import pytest
from inference.core.managers.base import ModelManager
from inference.core.workflows.core_steps.common.entities import \
    StepExecutionMode
from inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1 import \
    Qwen35VLBlockV1
from inference.core.workflows.execution_engine.entities.base import (
    Batch, ImageParentMetadata, WorkflowImageData)
from inference_sdk import InferenceHTTPClient


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

def create_test_image_data(base64_image: str) -> WorkflowImageData:
    """Create a WorkflowImageData instance for testing."""
    parent_metadata = ImageParentMetadata(parent_id='test_image')
    return WorkflowImageData(parent_metadata=parent_metadata, base64_image=base64_image)

def create_test_batch(base64_images: List[str]) -> Batch[WorkflowImageData]:
    """Create a Batch of WorkflowImageData instances for testing."""
    images = [create_test_image_data(base64_img) for base64_img in base64_images]
    indices = [(i,) for i in range(len(images))]
    return Batch(content=images, indices=indices)

def test_run_remotely_basic_with_single_image():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with a single image and default prompts.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Test response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_basic_with_single_image', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_with_custom_prompt():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with custom prompt and system_prompt.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Custom response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt='What color is the car?', system_prompt='You are a helpful assistant.')
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_custom_prompt', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    call_args = mock_client.infer_lmm.call_args
    prompt_arg = call_args.kwargs.get('prompt') or call_args[0][2]

def test_run_remotely_multiple_images():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with multiple images in batch.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64_1 = base64.b64encode(b'image_1_data').decode('utf-8')
    test_base64_2 = base64.b64encode(b'image_2_data').decode('utf-8')
    test_base64_3 = base64.b64encode(b'image_3_data').decode('utf-8')
    images = create_test_batch([test_base64_1, test_base64_2, test_base64_3])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.side_effect = [{'response': 'Response 1'}, {'response': 'Response 2'}, {'response': 'Response 3'}]
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_multiple_images', 'run_remotely', '8_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_hosted_mode():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with hosted API target.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Hosted response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'hosted'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.HOSTED_CORE_MODEL_URL', 'https://api.roboflow.com'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_hosted_mode', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_model_id_in_path():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that model_id_in_path is set to True when calling infer_lmm.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Test'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_model_id_in_path', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    call_kwargs = mock_client.infer_lmm.call_args.kwargs

def test_run_remotely_response_without_response_key():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    "Test run_remotely when API response doesn't have 'response' key."
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'result': 'Alternative result'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_response_without_response_key', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_with_none_prompt_none_system_prompt():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely when both prompt and system_prompt are None.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Default response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_none_prompt_none_system_prompt', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    call_kwargs = mock_client.infer_lmm.call_args.kwargs
    prompt_arg = call_kwargs.get('prompt')

def test_run_remotely_with_empty_string_prompt():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with empty string prompt (falsy but not None).'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt='', system_prompt='')
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_empty_string_prompt', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    call_kwargs = mock_client.infer_lmm.call_args.kwargs
    prompt_arg = call_kwargs.get('prompt')

def test_run_remotely_empty_batch():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with an empty batch.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    images = Batch(content=[], indices=[])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_empty_batch', 'run_remotely', '5_2_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_with_special_characters_in_prompt():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with special characters and unicode in prompts.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    special_prompt = "What's <this> & that? [Test] {special} 你好"
    special_system = 'Be helpful! @#$%^&*()'
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=special_prompt, system_prompt=special_system)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_special_characters_in_prompt', 'run_remotely', '8_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    call_kwargs = mock_client.infer_lmm.call_args.kwargs
    prompt_arg = call_kwargs.get('prompt')

def test_run_remotely_with_none_api_key():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with None as API key.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key=None, step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_none_api_key', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    mock_client_class.assert_called()
    call_args = mock_client_class.call_args

def test_run_remotely_api_response_with_string_response():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test when API response value is a simple string instead of dict.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Just a string response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_api_response_with_string_response', 'run_remotely', '6_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_with_very_long_prompt():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with very long prompt strings.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    test_base64 = base64.b64encode(b'fake_image_data').decode('utf-8')
    images = create_test_batch([test_base64])
    long_prompt = 'Describe ' + 'very ' * 500 + 'carefully'
    long_system = 'You are ' + 'helpful ' * 500 + 'assistant'
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.return_value = {'response': 'Response'}
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=long_prompt, system_prompt=long_system)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_very_long_prompt', 'run_remotely', '8_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_with_large_batch_of_images():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with a large batch of 100 images.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    num_images = 100
    test_base64_images = [base64.b64encode(f'image_{i}_data'.encode()).decode('utf-8') for i in range(num_images)]
    images = create_test_batch(test_base64_images)
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.side_effect = [{'response': f'Response {i}'} for i in range(num_images)]
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_large_batch_of_images', 'run_remotely', '7_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    for i, pred in enumerate(result):
        pass

def test_run_remotely_with_many_large_base64_images():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with 50 images with large base64 strings.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    num_images = 50
    test_base64_images = [base64.b64encode(b'x' * 10000 + f'image_{i}'.encode()).decode('utf-8') for i in range(num_images)]
    images = create_test_batch(test_base64_images)
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.side_effect = [{'response': f'Response {i}'} for i in range(num_images)]
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt='Describe the image', system_prompt='You are a vision model')
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_many_large_base64_images', 'run_remotely', '7_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output

def test_run_remotely_preserves_order_for_many_images():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that run_remotely preserves order of predictions for many images.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    num_images = 75
    test_base64_images = [base64.b64encode(f'image_{i}'.encode()).decode('utf-8') for i in range(num_images)]
    images = create_test_batch(test_base64_images)
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        responses = [{'response': f'ImageResponse_{i}'} for i in range(num_images)]
        mock_client.infer_lmm.side_effect = responses
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_preserves_order_for_many_images', 'run_remotely', '7_4_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    for i in range(num_images):
        pass

def test_run_remotely_with_varying_response_formats_at_scale():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test run_remotely with 100 images having varying response formats.'
    mock_model_registry = Mock()
    model_manager = ModelManager(model_registry=mock_model_registry)
    block = Qwen35VLBlockV1(model_manager=model_manager, api_key='test_key', step_execution_mode=StepExecutionMode.REMOTE)
    num_images = 100
    test_base64_images = [base64.b64encode(f'image_{i}'.encode()).decode('utf-8') for i in range(num_images)]
    images = create_test_batch(test_base64_images)

    def create_response(idx):
        if idx % 5 == 0:
            return {'response': f'Response {idx}'}
        elif idx % 5 == 1:
            return {'result': f'Result {idx}', 'data': f'Data {idx}'}
        elif idx % 5 == 2:
            return {'response': f'Just text {idx}'}
        elif idx % 5 == 3:
            return {'response': {'text': f'Nested {idx}'}}
        else:
            return {'response': [f'Item {idx}', f'Item2 {idx}']}
    with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.InferenceHTTPClient') as mock_client_class:
        mock_client = Mock(spec=InferenceHTTPClient)
        mock_client_class.return_value = mock_client
        mock_client.infer_lmm.side_effect = [create_response(i) for i in range(num_images)]
        with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.WORKFLOWS_REMOTE_API_TARGET', 'local'):
            with patch('inference.core.workflows.core_steps.models.foundation.qwen3_5vl.v1.LOCAL_INFERENCE_API_URL', 'http://localhost:9001'):
                _call__bound__arguments = inspect.signature(block.run_remotely).bind(images=images, model_version='qwen3-vl', prompt=None, system_prompt=None)
                _call__bound__arguments.apply_defaults()
                codeflash_return_value = codeflash_wrap(block.run_remotely, 'tests.inference.unit_tests.test_run_remotely__unit_test_1', None, 'test_run_remotely_with_varying_response_formats_at_scale', 'run_remotely', '8_3_0_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
                codeflash_output = codeflash_return_value
                result = codeflash_output
    for i, pred in enumerate(result):
        response = create_response(i)
        expected_output = response.get('response', response)