from __future__ import annotations

import gc
import os
import re
import time

import pytest
from inference.core.workflows.core_steps.models.foundation.easy_ocr.v1 import \
    BlockManifest
from inference.core.workflows.prototypes.block import WorkflowBlockManifest


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_expected_variants_basic():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_returns_expected_variants_basic', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    variants = codeflash_output
    expected = ['easy_ocr/english_g2', 'easy_ocr/japanese_g2', 'easy_ocr/kannada_g2', 'easy_ocr/korean_g2', 'easy_ocr/latin_g2', 'easy_ocr/telugu_g2', 'easy_ocr/zh_sim_g2']

def test_elements_are_strings_non_empty_and_formatted():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_elements_are_strings_non_empty_and_formatted', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    variants = codeflash_output
    pattern = re.compile('^[a-z0-9_]+/[a-z0-9_]+_g2$')
    for idx, item in enumerate(variants):
        pass

def test_uniqueness_and_length():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_uniqueness_and_length', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    variants = codeflash_output

def test_base_class_default_is_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(WorkflowBlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_base_class_default_is_none', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    base_default = codeflash_output

def test_mutating_returned_list_does_not_change_subsequent_results():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_mutating_returned_list_does_not_change_subsequent_results', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    first_call = codeflash_output
    mutated = list(first_call)
    mutated[0] = 'easy_ocr/modified_g2'
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_mutating_returned_list_does_not_change_subsequent_results', 'get_supported_model_variants', '4', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    second_call = codeflash_output
    expected_first_element = 'easy_ocr/english_g2'

def test_returned_elements_are_ascii_and_contain_no_none():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_returned_elements_are_ascii_and_contain_no_none', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    variants = codeflash_output
    for v in variants:
        pass

def test_idempotent_across_many_calls_and_performance_like_loop():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_idempotent_across_many_calls_and_performance_like_loop', 'get_supported_model_variants', '0', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    expected = codeflash_output
    for i in range(1000):
        codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_idempotent_across_many_calls_and_performance_like_loop', 'get_supported_model_variants', '2_0', codeflash_loop_index)
        codeflash_output = codeflash_return_value
        current = codeflash_output
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_idempotent_across_many_calls_and_performance_like_loop', 'get_supported_model_variants', '3', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    a = codeflash_output
    codeflash_return_value = codeflash_wrap(BlockManifest.get_supported_model_variants, 'tests.inference.unit_tests.test_get_supported_model_variants__unit_test_0', None, 'test_idempotent_across_many_calls_and_performance_like_loop', 'get_supported_model_variants', '5', codeflash_loop_index)
    codeflash_output = codeflash_return_value
    b = codeflash_output