from __future__ import annotations

import gc
import os
import time

import pytest
from inference.core.workflows.core_steps.common.query_language.introspection.core import \
    _ref_to_def_name


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_basic_ref_extraction():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test basic extraction of definition name from standard $ref format.'
    schema = {'$ref': '#/$defs/Foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_basic_ref_extraction', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_basic_ref_with_different_path():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction when ref uses different path structure.'
    schema = {'$ref': '#/definitions/User'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_basic_ref_with_different_path', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_basic_ref_with_nested_path():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction from deeply nested reference path.'
    schema = {'$ref': '#/$defs/models/entities/Product'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_basic_ref_with_nested_path', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_no_ref_returns_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that missing $ref key returns None.'
    schema = {'type': 'object', 'properties': {}}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_no_ref_returns_none', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_empty_schema_returns_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that empty schema dict returns None.'
    schema = {}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_empty_schema_returns_none', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_none_ref_value_returns_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that explicit None as $ref value returns None.'
    schema = {'$ref': None}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_none_ref_value_returns_none', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_custom_ref_key():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test using custom ref_key parameter.'
    schema = {'custom_ref': '#/$defs/CustomModel'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_custom_ref_key', '_ref_to_def_name', '2', codeflash_loop_index, schema, ref_key='custom_ref')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_custom_ref_key_not_present():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    "Test custom ref_key that doesn't exist in schema."
    schema = {'$ref': '#/$defs/Foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_custom_ref_key_not_present', '_ref_to_def_name', '2', codeflash_loop_index, schema, ref_key='nonexistent')
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_special_characters_in_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction when definition name contains allowed special characters.'
    schema = {'$ref': '#/$defs/User_Model_v2'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_special_characters_in_name', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_camel_case():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction of CamelCase definition names.'
    schema = {'$ref': '#/$defs/UserProfile'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_camel_case', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_single_element_ref():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref with just the definition name (no path separators).'
    schema = {'$ref': 'SimpleName'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_single_element_ref', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_ending_with_slash():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref path that ends with a slash.'
    schema = {'$ref': '#/$defs/Foo/'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_ending_with_slash', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_only_slashes():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref containing only forward slashes.'
    schema = {'$ref': '///'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_only_slashes', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_numbers_in_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test definition names containing numeric characters.'
    schema = {'$ref': '#/$defs/Model123'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_numbers_in_name', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_uppercase_letters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test fully uppercase definition names.'
    schema = {'$ref': '#/$defs/FOO'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_uppercase_letters', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_empty_string_ref():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test empty string as $ref value.'
    schema = {'$ref': ''}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_empty_string_ref', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_multiple_consecutive_slashes():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref with multiple consecutive forward slashes.'
    schema = {'$ref': '#//$defs//Foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_multiple_consecutive_slashes', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_schema_with_extra_keys():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    "Test that extra schema properties don't affect extraction."
    schema = {'$ref': '#/$defs/Target', 'type': 'object', 'description': 'Some object', 'properties': {'nested': {'type': 'string'}}}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_schema_with_extra_keys', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_url_like_format():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref with protocol-like format.'
    schema = {'$ref': 'http://example.com/schemas/Model'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_url_like_format', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_relative_path():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test relative path reference format.'
    schema = {'$ref': '../schemas/Base'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_relative_path', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_hash_fragment():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test reference using hash fragment identifier.'
    schema = {'$ref': '#Foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_hash_fragment', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_with_single_slash():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test reference with single forward slash.'
    schema = {'$ref': '/'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_single_slash', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_case_sensitive_definition_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that definition names preserve case sensitivity.'
    schema = {'$ref': '#/$defs/foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_case_sensitive_definition_name', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    schema_upper = {'$ref': '#/$defs/Foo'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_case_sensitive_definition_name', '_ref_to_def_name', '5', codeflash_loop_index, schema_upper)
    codeflash_output = codeflash_return_value
    result_upper = codeflash_output

def test_ref_with_hyphen_in_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test definition name containing hyphens.'
    schema = {'$ref': '#/$defs/my-model'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_with_hyphen_in_name', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_ref_value_type_coercion():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that ref_key parameter is used to look up value in dict.'
    schema = {'$ref': '#/$defs/Model1', 'other_ref': '#/$defs/Model2'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_value_type_coercion', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result_default = codeflash_output
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_ref_value_type_coercion', '_ref_to_def_name', '4', codeflash_loop_index, schema, ref_key='other_ref')
    codeflash_output = codeflash_return_value
    result_other = codeflash_output

def test_very_long_ref_path():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction from a very long reference path.'
    segments = ['#'] + ['segment'] * 100 + ['TargetDef']
    long_ref = '/'.join(segments)
    schema = {'$ref': long_ref}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_very_long_ref_path', '_ref_to_def_name', '4', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_very_long_definition_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction of extremely long definition name.'
    long_name = 'A' * 1000
    schema = {'$ref': f'#/$defs/{long_name}'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_very_long_definition_name', '_ref_to_def_name', '3', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_many_schemas_sequential():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test processing multiple schemas sequentially.'
    results = []
    for i in range(100):
        schema = {'$ref': f'#/$defs/Model{i}'}
        codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_many_schemas_sequential', '_ref_to_def_name', '2_1', codeflash_loop_index, schema)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        results.append(result)
    for i, result in enumerate(results):
        pass

def test_schemas_with_varying_path_depths():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test schemas with different nesting depths.'
    results = []
    for depth in range(1, 101):
        segments = ['#'] + ['level'] * depth + ['Target']
        ref = '/'.join(segments)
        schema = {'$ref': ref}
        codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_schemas_with_varying_path_depths', '_ref_to_def_name', '2_3', codeflash_loop_index, schema)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        results.append(result)

def test_large_schema_dict_with_many_keys():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extraction from schema with many extra properties.'
    schema = {'$ref': '#/$defs/TargetModel'}
    for i in range(500):
        schema[f'property_{i}'] = f'value_{i}'
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_large_schema_dict_with_many_keys', '_ref_to_def_name', '3', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_batch_processing_with_none_and_valid():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test batch processing of mixed valid and None refs.'
    results = []
    for i in range(100):
        if i % 2 == 0:
            schema = {'$ref': f'#/$defs/Model{i}'}
        else:
            schema = {}
        codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_batch_processing_with_none_and_valid', '_ref_to_def_name', '2_1', codeflash_loop_index, schema)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        results.append(result)
    for i, result in enumerate(results):
        if i % 2 == 0:
            pass
        else:
            pass

def test_unicode_characters_in_definition_name():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of unicode characters in definition names.'
    schema = {'$ref': '#/$defs/Model_日本語'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_unicode_characters_in_definition_name', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_refs_with_query_parameters_style():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test ref format with query-parameter-like syntax.'
    schema = {'$ref': '#/$defs/Model?version=2'}
    codeflash_return_value = codeflash_wrap(_ref_to_def_name, 'tests.inference.unit_tests.test__ref_to_def_name__unit_test_1', None, 'test_refs_with_query_parameters_style', '_ref_to_def_name', '2', codeflash_loop_index, schema)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_stability_of_extraction_multiple_calls():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that multiple calls on same input produce identical results.'
    schema = {'$ref': '#/$defs/ConsistentModel'}
    results = [_ref_to_def_name(schema) for _ in range(100)]