from __future__ import annotations

import gc
import os
import time

import pytest
from inference.core.workflows.core_steps.trackers._base import InstanceCache


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_record_instance_returns_false_on_first_appearance_and_true_on_reappearance():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    cache = InstanceCache(3)
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_record_instance_returns_false_on_first_appearance_and_true_on_reappearance', 'record_instance', '1', codeflash_loop_index, 1)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_record_instance_returns_false_on_first_appearance_and_true_on_reappearance', 'record_instance', '2', codeflash_loop_index, 1)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_record_instance_returns_false_on_first_appearance_and_true_on_reappearance', 'record_instance', '3', codeflash_loop_index, 2)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_record_instance_returns_false_on_first_appearance_and_true_on_reappearance', 'record_instance', '4', codeflash_loop_index, 2)
    codeflash_output = codeflash_return_value

def test_fifo_eviction_behaviour_with_small_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    cache = InstanceCache(2)
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '1', codeflash_loop_index, 10)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '2', codeflash_loop_index, 20)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '3', codeflash_loop_index, 10)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '4', codeflash_loop_index, 30)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '5', codeflash_loop_index, 10)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_fifo_eviction_behaviour_with_small_cache', 'record_instance', '6', codeflash_loop_index, 20)
    codeflash_output = codeflash_return_value

def test_size_clamped_to_minimum_one_when_constructed_with_non_positive_size():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    cache_zero = InstanceCache(0)
    cache_neg = InstanceCache(-5)
    for cache in (cache_zero, cache_neg):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_size_clamped_to_minimum_one_when_constructed_with_non_positive_size', 'record_instance', '2_0', codeflash_loop_index, 1)
        codeflash_output = codeflash_return_value
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_size_clamped_to_minimum_one_when_constructed_with_non_positive_size', 'record_instance', '2_2', codeflash_loop_index, 2)
        codeflash_output = codeflash_return_value
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_size_clamped_to_minimum_one_when_constructed_with_non_positive_size', 'record_instance', '2_4', codeflash_loop_index, 1)
        codeflash_output = codeflash_return_value
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_size_clamped_to_minimum_one_when_constructed_with_non_positive_size', 'record_instance', '2_6', codeflash_loop_index, 2)
        codeflash_output = codeflash_return_value

def test_various_tracker_id_values_are_supported_and_treated_as_keys():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    cache = InstanceCache(4)
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '1', codeflash_loop_index, -1)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '2', codeflash_loop_index, -1)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '3', codeflash_loop_index, 0)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '4', codeflash_loop_index, 0)
    codeflash_output = codeflash_return_value
    big = 10 ** 12
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '6', codeflash_loop_index, big)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '7', codeflash_loop_index, big)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '8', codeflash_loop_index, None)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '9', codeflash_loop_index, None)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '10', codeflash_loop_index, 'tracker-A')
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_various_tracker_id_values_are_supported_and_treated_as_keys', 'record_instance', '11', codeflash_loop_index, 'tracker-A')
    codeflash_output = codeflash_return_value

def test_large_scale_fill_and_reappearance_in_large_cache():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    size = 1000
    cache = InstanceCache(size)
    for i in range(size):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_fill_and_reappearance_in_large_cache', 'record_instance', '2_0', codeflash_loop_index, i)
        codeflash_output = codeflash_return_value
    for i in range(size):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_fill_and_reappearance_in_large_cache', 'record_instance', '3_0', codeflash_loop_index, i)
        codeflash_output = codeflash_return_value

def test_large_scale_eviction_after_overflow_of_capacity():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    size = 1000
    cache = InstanceCache(size)
    for i in range(size):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '2_0', codeflash_loop_index, i)
        codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '3', codeflash_loop_index, size)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '4', codeflash_loop_index, 0)
    codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '5', codeflash_loop_index, size)
    codeflash_output = codeflash_return_value
    for i in range(size + 1, size + 1000):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '6_0', codeflash_loop_index, i)
        codeflash_output = codeflash_return_value
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_large_scale_eviction_after_overflow_of_capacity', 'record_instance', '7', codeflash_loop_index, 1)
    codeflash_output = codeflash_return_value

def test_repeated_same_id_many_times_remains_stable():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    cache = InstanceCache(5)
    codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_repeated_same_id_many_times_remains_stable', 'record_instance', '1', codeflash_loop_index, 42)
    codeflash_output = codeflash_return_value
    for _ in range(1000):
        codeflash_return_value = codeflash_wrap(cache.record_instance, 'tests.inference.unit_tests.test_record_instance__unit_test_0', None, 'test_repeated_same_id_many_times_remains_stable', 'record_instance', '2_0', codeflash_loop_index, 42)
        codeflash_output = codeflash_return_value