from __future__ import annotations

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

import pytest
from inference.core.cache.air_gapped import _is_model_cached, _slugify_model_id


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 test_returns_false_when_no_cache_exists():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns False when neither cache layout exists.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_returns_false_when_no_cache_exists', '_is_model_cached', '1_0_0', codeflash_loop_index, 'nonexistent-model')
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_returns_true_with_traditional_layout():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns True when traditional cache layout exists with files.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_returns_true_with_traditional_layout', '_is_model_cached', '1_0_4', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_returns_true_with_inference_models_layout():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns True when inference-models cache layout exists with files.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_returns_true_with_inference_models_layout', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_returns_false_when_traditional_directory_is_empty():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns False when traditional cache directory exists but is empty.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_returns_false_when_traditional_directory_is_empty', '_is_model_cached', '1_0_3', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_returns_false_when_inference_models_directory_is_empty():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns False when inference-models cache directory exists but is empty.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_returns_false_when_inference_models_directory_is_empty', '_is_model_cached', '1_0_4', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_prefers_traditional_layout_over_inference_models():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that traditional layout is checked first and returns True if it has content.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            traditional_path = os.path.join(tmpdir, model_id)
            os.makedirs(traditional_path, exist_ok=True)
            with open(os.path.join(traditional_path, 'model.bin'), 'w') as f:
                f.write('test content')
            slug = _slugify_model_id(model_id)
            models_cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(models_cache_path, exist_ok=True)
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_prefers_traditional_layout_over_inference_models', '_is_model_cached', '1_0_7', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_falls_through_to_inference_models_when_traditional_empty():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that inference-models layout is checked when traditional layout is empty.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            traditional_path = os.path.join(tmpdir, model_id)
            os.makedirs(traditional_path, exist_ok=True)
            slug = _slugify_model_id(model_id)
            models_cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(models_cache_path, exist_ok=True)
            with open(os.path.join(models_cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_falls_through_to_inference_models_when_traditional_empty', '_is_model_cached', '1_0_7', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_model_id_containing_special_characters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with model_id that contains special characters that get slugified.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'model/with@special#chars!'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_model_id_containing_special_characters', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_very_long_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a very long model_id that gets truncated in slugify.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'a' * 100
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_very_long_model_id', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_empty_string_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with empty string as model_id.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = ''
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_empty_string_model_id', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_only_special_characters_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with model_id containing only special characters.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = '!@#$%^&*()'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_only_special_characters_model_id', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_unicode_characters_in_model_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with unicode characters in model_id.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'model-with-é-unicode'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_unicode_characters_in_model_id', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_cache_directory_is_file_not_directory():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that function returns False when cache path exists but is a file, not a directory.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            with open(cache_path, 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_cache_directory_is_file_not_directory', '_is_model_cached', '1_0_3', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_multiple_files_in_traditional_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with multiple files in traditional cache layout.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            for i in range(5):
                with open(os.path.join(cache_path, f'file{i}.bin'), 'w') as f:
                    f.write(f'content {i}')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_multiple_files_in_traditional_cache', '_is_model_cached', '1_0_4', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_multiple_subdirectories_in_inference_models_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with multiple subdirectories in inference-models cache layout.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            os.makedirs(cache_path, exist_ok=True)
            for i in range(3):
                subdir = os.path.join(cache_path, f'subdir{i}')
                os.makedirs(subdir, exist_ok=True)
                with open(os.path.join(subdir, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_multiple_subdirectories_in_inference_models_cache', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_symlink_to_cache_directory():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test behavior when cache path is a symlink to an existing directory with content.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            real_cache = os.path.join(tmpdir, 'real_cache')
            os.makedirs(real_cache, exist_ok=True)
            with open(os.path.join(real_cache, 'model.bin'), 'w') as f:
                f.write('test content')
            cache_path = os.path.join(tmpdir, model_id)
            os.symlink(real_cache, cache_path)
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_symlink_to_cache_directory', '_is_model_cached', '1_0_6', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_model_id_case_sensitivity():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that model_id comparison is case-sensitive (based on traditional path lookup).'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id_lower = 'test-model'
            cache_path = os.path.join(tmpdir, model_id_lower)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('test content')
            model_id_upper = 'TEST-MODEL'
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_model_id_case_sensitivity', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id_upper)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_directory_with_hidden_files_only():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that a directory with only hidden files is still considered cached.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, '.hidden'), 'w') as f:
                f.write('hidden content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_directory_with_hidden_files_only', '_is_model_cached', '1_0_4', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_many_other_models_in_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that function correctly identifies cache when many other models exist.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            for i in range(100):
                other_model_path = os.path.join(tmpdir, f'other-model-{i}')
                os.makedirs(other_model_path, exist_ok=True)
                with open(os.path.join(other_model_path, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            model_id = 'target-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                f.write('target content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_many_other_models_in_cache', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_many_files_in_single_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test performance with a cache directory containing many files.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            for i in range(500):
                with open(os.path.join(cache_path, f'file{i}.bin'), 'w') as f:
                    f.write(f'content {i}')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_many_files_in_single_cache', '_is_model_cached', '1_0_4', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_deeply_nested_cache_structure():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with deeply nested directory structure in cache.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            slug = _slugify_model_id(model_id)
            cache_path = os.path.join(tmpdir, 'models-cache', slug)
            nested_path = cache_path
            for i in range(10):
                nested_path = os.path.join(nested_path, f'level{i}')
            os.makedirs(nested_path, exist_ok=True)
            with open(os.path.join(nested_path, 'model.bin'), 'w') as f:
                f.write('test content')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_deeply_nested_cache_structure', '_is_model_cached', '1_0_7', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_large_file_in_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large file in cache directory.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            model_id = 'test-model'
            cache_path = os.path.join(tmpdir, model_id)
            os.makedirs(cache_path, exist_ok=True)
            large_file_path = os.path.join(cache_path, 'large_model.bin')
            with open(large_file_path, 'wb') as f:
                f.write(b'x' * (10 * 1024 * 1024))
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_large_file_in_cache', '_is_model_cached', '1_0_5', codeflash_loop_index, model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_many_models_in_inference_models_layout():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with many models in the inference-models cache layout.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            for i in range(100):
                model_id = f'model-{i}'
                slug = _slugify_model_id(model_id)
                cache_path = os.path.join(tmpdir, 'models-cache', slug)
                os.makedirs(cache_path, exist_ok=True)
                with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            target_model_id = 'model-50'
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_many_models_in_inference_models_layout', '_is_model_cached', '1_0_2', codeflash_loop_index, target_model_id)
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_mixed_traditional_and_inference_models_layouts():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test performance with both cache layouts populated with multiple models.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            for i in range(50):
                model_id = f'traditional-model-{i}'
                cache_path = os.path.join(tmpdir, model_id)
                os.makedirs(cache_path, exist_ok=True)
                with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            for i in range(50):
                model_id = f'inference-model-{i}'
                slug = _slugify_model_id(model_id)
                cache_path = os.path.join(tmpdir, 'models-cache', slug)
                os.makedirs(cache_path, exist_ok=True)
                with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_mixed_traditional_and_inference_models_layouts', '_is_model_cached', '1_0_2', codeflash_loop_index, 'traditional-model-25')
            codeflash_output = codeflash_return_value
            result1 = codeflash_output
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_mixed_traditional_and_inference_models_layouts', '_is_model_cached', '1_0_5', codeflash_loop_index, 'inference-model-75')
            codeflash_output = codeflash_return_value
            result2 = codeflash_output

def test_query_performance_with_empty_cache_dir():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test performance when querying for nonexistent model with many other cached models.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            for i in range(200):
                model_id = f'model-{i}'
                cache_path = os.path.join(tmpdir, model_id)
                os.makedirs(cache_path, exist_ok=True)
                with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                    f.write(f'content {i}')
            codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_query_performance_with_empty_cache_dir', '_is_model_cached', '1_0_1', codeflash_loop_index, 'nonexistent-model')
            codeflash_output = codeflash_return_value
            result = codeflash_output

def test_with_various_slugified_model_ids():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with multiple model IDs that produce different slugs.'
    with tempfile.TemporaryDirectory() as tmpdir:
        with mock.patch('inference.core.cache.air_gapped.MODEL_CACHE_DIR', tmpdir):
            test_models = ['simple-model', 'model_with_underscores', 'model-with-dashes', 'ModelWithCaps', 'model123', 'model-123-abc']
            for model_id in test_models:
                slug = _slugify_model_id(model_id)
                cache_path = os.path.join(tmpdir, 'models-cache', slug)
                os.makedirs(cache_path, exist_ok=True)
                with open(os.path.join(cache_path, 'model.bin'), 'w') as f:
                    f.write(f'content for {model_id}')
            for model_id in test_models:
                codeflash_return_value = codeflash_wrap(_is_model_cached, 'tests.inference.unit_tests.test__is_model_cached__unit_test_1', None, 'test_with_various_slugified_model_ids', '_is_model_cached', '1_0_2_0', codeflash_loop_index, model_id)
                codeflash_output = codeflash_return_value
                result = codeflash_output