from __future__ import annotations

import gc
import os
import shutil
import tempfile
import time
from unittest.mock import patch

import pytest
from inference.core.cache.air_gapped import is_block_cached, is_model_cached
from inference.core.cache.model_artifacts import (are_all_files_cached,
                                                  is_file_cached)


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}:{codeflash_duration}######!')
    if exception:
        raise exception
    return return_value

def _slugify_model_id(model_id: str) -> str:
    """Reproduce the slug used by inference-models for cache directory names."""
    import hashlib
    import re
    slug = re.sub('[^A-Za-z0-9_-]+', '-', model_id)
    slug = re.sub('[_-]{2,}', '-', slug)
    if not slug:
        slug = 'special-char-only-model-id'
    if len(slug) > 48:
        slug = slug[:48]
    digest = hashlib.blake2s(model_id.encode('utf-8'), digest_size=4).hexdigest()
    return f'{slug}-{digest}'

def test_is_block_cached_with_empty_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that an empty list returns False (no models to check).'
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_empty_list', 'is_block_cached', '1', codeflash_loop_index, [])
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_dict_no_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a dict without model_id returns False.'
    artifacts_spec = {'files': ['file1.txt', 'file2.txt']}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_dict_no_model_id', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_dict_no_files():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a dict without files key returns False.'
    artifacts_spec = {'model_id': 'test-model'}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_dict_no_files', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_dict_empty_files():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a dict with empty files list returns False.'
    artifacts_spec = {'model_id': 'test-model', 'files': []}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_dict_empty_files', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_dict_none_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a dict with None as model_id returns False.'
    artifacts_spec = {'model_id': None, 'files': ['file1.txt']}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_dict_none_model_id', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_unrecognized_type_string():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a string input returns False (unrecognized format).'
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_unrecognized_type_string', 'is_block_cached', '1', codeflash_loop_index, 'not-a-list-or-dict')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_unrecognized_type_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that None input returns False (unrecognized format).'
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_unrecognized_type_none', 'is_block_cached', '1', codeflash_loop_index, None)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_unrecognized_type_int():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that an integer input returns False (unrecognized format).'
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_unrecognized_type_int', 'is_block_cached', '1', codeflash_loop_index, 42)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_unrecognized_type_tuple():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a tuple input returns False (unrecognized format).'
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_unrecognized_type_tuple', 'is_block_cached', '1', codeflash_loop_index, ('model1', 'model2'))
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_with_dict_empty_model_id_string():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a dict with empty string as model_id returns False.'
    artifacts_spec = {'model_id': '', 'files': ['file1.txt']}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_with_dict_empty_model_id_string', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_list_with_single_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with single model_id when model is not cached.'
    with patch('is_model_cached', return_value=False):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_single_model_id', 'is_block_cached', '1_0', codeflash_loop_index, ['test-model'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_multiple_model_ids_none_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with multiple model_ids when none are cached.'
    with patch('is_model_cached', return_value=False):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_multiple_model_ids_none_cached', 'is_block_cached', '1_0', codeflash_loop_index, ['model1', 'model2', 'model3'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_multiple_model_ids_one_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with multiple model_ids when one is cached (any() logic).'

    def mock_is_model_cached(model_id):
        return model_id == 'model2'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_multiple_model_ids_one_cached', 'is_block_cached', '2_0', codeflash_loop_index, ['model1', 'model2', 'model3'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_multiple_model_ids_all_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with multiple model_ids when all are cached.'
    with patch('is_model_cached', return_value=True):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_multiple_model_ids_all_cached', 'is_block_cached', '1_0', codeflash_loop_index, ['model1', 'model2', 'model3'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_single_file():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with single file when all files are cached.'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': ['weights.bin']}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_single_file', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_multiple_files_all_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with multiple files when all are cached.'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': ['weights.bin', 'config.json', 'tokenizer.model']}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_multiple_files_all_cached', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_multiple_files_not_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with multiple files when not all are cached.'
    with patch('are_all_files_cached', return_value=False):
        artifacts_spec = {'model_id': 'test-model', 'files': ['weights.bin', 'config.json']}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_multiple_files_not_cached', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_special_characters_in_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with special characters in model_id.'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'user/model-v1.0@latest', 'files': ['weights.bin']}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_special_characters_in_model_id', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_special_characters_in_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with special characters in model_ids.'

    def mock_is_model_cached(model_id):
        return model_id == 'user/model@v1.0'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_special_characters_in_model_ids', 'is_block_cached', '2_0', codeflash_loop_index, ['org/model@v1', 'user/model@v1.0', 'other/model'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_extra_keys():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with extra keys (should be ignored).'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': ['weights.bin'], 'extra_key': 'extra_value', 'another_key': 123}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_extra_keys', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_false_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with False as model_id (falsy but not None).'
    artifacts_spec = {'model_id': False, 'files': ['file1.txt']}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_false_values', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_dict_with_zero_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with 0 as model_id (falsy value).'
    artifacts_spec = {'model_id': 0, 'files': ['file1.txt']}
    codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_zero_model_id', 'is_block_cached', '2', codeflash_loop_index, artifacts_spec)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_is_block_cached_list_with_empty_string_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with empty string model_id when model is not cached.'
    with patch('is_model_cached', return_value=False):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_empty_string_model_id', 'is_block_cached', '1_0', codeflash_loop_index, [''])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_unicode_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with unicode characters in model_ids.'

    def mock_is_model_cached(model_id):
        return model_id == 'model-🚀'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_unicode_model_ids', 'is_block_cached', '2_0', codeflash_loop_index, ['model-v1', 'model-🚀', 'model-v2'])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_many_model_ids_first_is_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 100 model_ids where first one is cached.'
    model_ids = [f'model-{i}' for i in range(100)]

    def mock_is_model_cached(model_id):
        return model_id == 'model-0'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_many_model_ids_first_is_cached', 'is_block_cached', '3_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_many_model_ids_last_is_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 100 model_ids where last one is cached.'
    model_ids = [f'model-{i}' for i in range(100)]

    def mock_is_model_cached(model_id):
        return model_id == 'model-99'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_many_model_ids_last_is_cached', 'is_block_cached', '3_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_many_model_ids_none_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 100 model_ids where none are cached.'
    model_ids = [f'model-{i}' for i in range(100)]
    with patch('is_model_cached', return_value=False):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_many_model_ids_none_cached', 'is_block_cached', '2_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_many_model_ids_middle_is_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 100 model_ids where middle one is cached.'
    model_ids = [f'model-{i}' for i in range(100)]

    def mock_is_model_cached(model_id):
        return model_id == 'model-50'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_many_model_ids_middle_is_cached', 'is_block_cached', '3_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_many_files_all_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with 100 files when all are cached.'
    files = [f'file-{i}.bin' for i in range(100)]
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': files}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_many_files_all_cached', 'is_block_cached', '2_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_many_files_not_cached():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with 100 files when not all are cached.'
    files = [f'file-{i}.bin' for i in range(100)]
    with patch('are_all_files_cached', return_value=False):
        artifacts_spec = {'model_id': 'test-model', 'files': files}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_many_files_not_cached', 'is_block_cached', '2_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_duplicate_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with duplicate model_ids.'
    model_ids = ['model-1'] * 100

    def mock_is_model_cached(model_id):
        return True
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_duplicate_model_ids', 'is_block_cached', '3_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_very_long_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with very long model_id strings.'
    long_model_id = 'model-' + 'x' * 500

    def mock_is_model_cached(model_id):
        return model_id == long_model_id
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_very_long_model_ids', 'is_block_cached', '3_0', codeflash_loop_index, [long_model_id])
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_very_long_file_names():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with very long file names.'
    long_file_name = 'very-long-file-name-' + 'x' * 500 + '.bin'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': [long_file_name]}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_very_long_file_names', 'is_block_cached', '2_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_1000_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 1000 model_ids for performance.'
    model_ids = [f'model-{i}' for i in range(1000)]
    with patch('is_model_cached', return_value=False):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_1000_model_ids', 'is_block_cached', '2_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_list_with_1000_model_ids_one_match():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test list with 1000 model_ids where one matches.'
    model_ids = [f'model-{i}' for i in range(1000)]

    def mock_is_model_cached(model_id):
        return model_id == 'model-500'
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_list_with_1000_model_ids_one_match', 'is_block_cached', '3_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_dict_with_1000_files():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with 1000 files for performance.'
    files = [f'file-{i}.bin' for i in range(1000)]
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': files}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_dict_with_1000_files', 'is_block_cached', '2_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_mixed_nested_structures():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test dict with nested list in extra keys (should be ignored).'
    with patch('are_all_files_cached', return_value=True):
        artifacts_spec = {'model_id': 'test-model', 'files': ['weights.bin'], 'nested_list': [1, 2, 3], 'nested_dict': {'key': 'value'}}
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_mixed_nested_structures', 'is_block_cached', '1_1', codeflash_loop_index, artifacts_spec)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_is_block_cached_performance_list_evaluation_order():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that list evaluation stops early (any() optimization).'
    call_count = [0]

    def mock_is_model_cached(model_id):
        call_count[0] += 1
        return model_id == 'model-0'
    model_ids = [f'model-{i}' for i in range(100)]
    with patch('is_model_cached', side_effect=mock_is_model_cached):
        codeflash_return_value = codeflash_wrap(is_block_cached, 'tests.inference.unit_tests.test_is_block_cached__unit_test_1', None, 'test_is_block_cached_performance_list_evaluation_order', 'is_block_cached', '4_0', codeflash_loop_index, model_ids)
        codeflash_output = codeflash_return_value
        result = codeflash_output