from __future__ import annotations

import gc
import inspect
import os
import time
from collections import defaultdict
from pathlib import Path

import dill as pickle
import pytest
from inference.core.workflows.core_steps.sinks.email_notification.v2 import (
    PARAMETER_REGEX, apply_operations_to_message_parameters,
    format_email_message)


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}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

class TestFormatEmailMessage:
    """Test suite for format_email_message function."""

    def test_simple_message_without_parameters(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test formatting a message with no parameters.'
        message = 'Hello World'
        message_parameters = {}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_simple_message_without_parameters', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_single_parameter_replacement(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test replacing a single parameter placeholder.'
        message = 'Hello {{ $parameters.name }}'
        message_parameters = {'name': 'Alice'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_single_parameter_replacement', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_multiple_different_parameters(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test replacing multiple different parameters in one message.'
        message = '{{ $parameters.greeting }} {{ $parameters.name }}'
        message_parameters = {'greeting': 'Hello', 'name': 'Bob'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_multiple_different_parameters', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_same_parameter_multiple_times(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter used multiple times in the same message.'
        message = '{{ $parameters.name }} likes {{ $parameters.name }}'
        message_parameters = {'name': 'Charlie'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_same_parameter_multiple_times', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_with_spaces_in_placeholder(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that placeholders with extra spaces are correctly matched.'
        message = 'Value: {{  $parameters.value  }}'
        message_parameters = {'value': '123'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_with_spaces_in_placeholder', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_not_in_message_ignored(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that parameters not used in message are ignored.'
        message = 'Name: {{ $parameters.name }}'
        message_parameters = {'name': 'David', 'unused': 'value', 'another': 42}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_not_in_message_ignored', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_numeric_parameter_converted_to_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that numeric parameters are converted to strings.'
        message = 'Count: {{ $parameters.count }}'
        message_parameters = {'count': 42}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_numeric_parameter_converted_to_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_float_parameter_converted_to_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that float parameters are converted to strings.'
        message = 'Price: {{ $parameters.price }}'
        message_parameters = {'price': 19.99}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_float_parameter_converted_to_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_boolean_parameter_converted_to_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that boolean parameters are converted to strings.'
        message = 'Active: {{ $parameters.is_active }}'
        message_parameters = {'is_active': True}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_boolean_parameter_converted_to_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_name_with_underscores(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter names containing underscores.'
        message = 'User: {{ $parameters.user_name }}'
        message_parameters = {'user_name': 'Eve'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_name_with_underscores', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_name_with_numbers(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter names containing numbers.'
        message = 'Item: {{ $parameters.item1 }}'
        message_parameters = {'item1': 'widget'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_name_with_numbers', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_empty_message_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test formatting an empty message.'
        message = ''
        message_parameters = {'name': 'Frank'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_empty_message_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_empty_parameters_dict(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test with empty parameters dictionary.'
        message = 'Message without parameters'
        message_parameters = {}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_empty_parameters_dict', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_unmatched_placeholder(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test message with placeholder for parameter not in dict.'
        message = 'Hello {{ $parameters.missing }}'
        message_parameters = {}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_unmatched_placeholder', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_placeholder_with_invalid_parameter_name(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that malformed placeholders are not replaced.'
        message = 'Value: {{ parameters.name }}'
        message_parameters = {'name': 'George'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_placeholder_with_invalid_parameter_name', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_placeholder_without_braces(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test text that looks like parameter reference but without braces.'
        message = 'Use $parameters.name here'
        message_parameters = {'name': 'Henry'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_placeholder_without_braces', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_string_parameter_with_special_characters(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value containing special characters.'
        message = 'Email: {{ $parameters.email }}'
        message_parameters = {'email': 'user+tag@example.com'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_string_parameter_with_special_characters', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_with_newlines(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value containing newline characters.'
        message = 'Description: {{ $parameters.desc }}'
        message_parameters = {'desc': 'Line 1\nLine 2'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_with_newlines', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_is_zero(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that parameter value of 0 is correctly converted.'
        message = 'Count: {{ $parameters.count }}'
        message_parameters = {'count': 0}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_is_zero', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_is_false(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that parameter value of False is correctly converted.'
        message = 'Active: {{ $parameters.active }}'
        message_parameters = {'active': False}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_is_false', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_is_empty_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter with empty string value.'
        message = 'Name: [{{ $parameters.name }}]'
        message_parameters = {'name': ''}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_is_empty_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_is_list(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value that is a list.'
        message = 'Items: {{ $parameters.items }}'
        message_parameters = {'items': [1, 2, 3]}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_is_list', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_is_dict(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value that is a dictionary.'
        message = 'Data: {{ $parameters.data }}'
        message_parameters = {'data': {'key': 'value'}}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_is_dict', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_multiple_placeholders_adjacent(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test multiple placeholders placed adjacent to each other.'
        message = '{{ $parameters.first }}{{ $parameters.second }}'
        message_parameters = {'first': 'Hello', 'second': 'World'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_multiple_placeholders_adjacent', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_placeholder_at_message_start(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test placeholder at the beginning of message.'
        message = '{{ $parameters.greeting }} there'
        message_parameters = {'greeting': 'Hi'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_placeholder_at_message_start', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_placeholder_at_message_end(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test placeholder at the end of message.'
        message = 'Thank you {{ $parameters.name }}'
        message_parameters = {'name': 'Iris'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_placeholder_at_message_end', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_only_placeholder_in_message(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test message that is only a placeholder.'
        message = '{{ $parameters.content }}'
        message_parameters = {'content': 'The actual content'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_only_placeholder_in_message', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_literal_braces(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        "Test message with literal braces that don't form valid placeholders."
        message = 'Use {curly} braces here'
        message_parameters = {}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_literal_braces', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_not_in_operations_dict(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test when parameter is in message but not in operations dict.'
        message = 'Name: {{ $parameters.name }}'
        message_parameters = {'name': 'Jack'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_not_in_operations_dict', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_case_sensitive_parameter_names(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that parameter names are case-sensitive.'
        message = '{{ $parameters.Name }}'
        message_parameters = {'name': 'Kevin'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_case_sensitive_parameter_names', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_with_very_long_name(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter with a very long name.'
        long_name = 'a' * 100
        message = f'Value: {{{{ $parameters.{long_name} }}}}'
        message_parameters = {long_name: 'found'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_with_very_long_name', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_unicode_characters(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test message containing unicode characters.'
        message = 'Hello {{ $parameters.name }} 你好'
        message_parameters = {'name': '林'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_unicode_characters', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_with_regex_special_chars(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value containing regex special characters.'
        message = 'Pattern: {{ $parameters.pattern }}'
        message_parameters = {'pattern': '.*+?[](){}|\\^$'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_with_regex_special_chars', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_value_containing_placeholder_like_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value that looks like a placeholder.'
        message = 'Template: {{ $parameters.template }}'
        message_parameters = {'template': '{{ $parameters.other }}'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_value_containing_placeholder_like_string', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_three_parameters_all_different(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test three different parameters in one message.'
        message = '{{ $parameters.a }} {{ $parameters.b }} {{ $parameters.c }}'
        message_parameters = {'a': 'X', 'b': 'Y', 'c': 'Z'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_three_parameters_all_different', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_name_with_leading_underscore(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter name starting with underscore.'
        message = 'Value: {{ $parameters._private }}'
        message_parameters = {'_private': 'secret'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_name_with_leading_underscore', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_many_different_parameters(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test message with 50 different parameters.'
        params_list = [f'p{i}' for i in range(50)]
        message = ' '.join([f'{{{{ $parameters.{p} }}}}' for p in params_list])
        message_parameters = {p: f'value_{i}' for i, p in enumerate(params_list)}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_many_different_parameters', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        expected = ' '.join([f'value_{i}' for i in range(50)])

    def test_same_parameter_repeated_many_times(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test same parameter used 100 times in message.'
        message = ' '.join(['{{ $parameters.value }}'] * 100)
        message_parameters = {'value': 'X'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_same_parameter_repeated_many_times', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        expected = ' '.join(['X'] * 100)

    def test_large_parameter_value_string(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test with parameter value containing very large string.'
        large_value = 'x' * 10000
        message = 'Data: {{ $parameters.data }}'
        message_parameters = {'data': large_value}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_large_parameter_value_string', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_many_parameters_with_no_usage(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test with 100 parameters but only a few used in message.'
        message_parameters = {f'unused_{i}': f'value_{i}' for i in range(97)}
        message_parameters.update({'used1': 'A', 'used2': 'B', 'used3': 'C'})
        message = 'Result: {{ $parameters.used1 }} {{ $parameters.used2 }} {{ $parameters.used3 }}'
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_many_parameters_with_no_usage', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_list_with_many_elements(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test parameter value that is a list with 500 elements.'
        large_list = list(range(500))
        message = 'Data: {{ $parameters.data }}'
        message_parameters = {'data': large_list}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_list_with_many_elements', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_many_non_parameter_parts(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test long message with placeholders interspersed throughout.'
        parts = []
        for i in range(20):
            parts.append(f'Text before param {i}: {{{{ $parameters.p{i} }}}}')
        message = ' '.join(parts)
        message_parameters = {f'p{i}': f'value{i}' for i in range(20)}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_many_non_parameter_parts', 'format_email_message', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_parameter_dict_with_many_items(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test with parameter dictionary containing 1000 items.'
        message_parameters = {f'param_{i}': f'val_{i}' for i in range(1000)}
        message = 'Use: {{ $parameters.param_0 }} and {{ $parameters.param_999 }}'
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_parameter_dict_with_many_items', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_message_with_many_operations_defined(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test with operation chains defined for many parameters.'
        message_parameters = {f'p{i}': f'value{i}' for i in range(50)}
        message_parameters_operations = {f'p{i}': [] for i in range(50)}
        message = ' '.join([f'{{{{ $parameters.p{i} }}}}' for i in range(50)])
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_message_with_many_operations_defined', 'format_email_message', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        expected = ' '.join([f'value{i}' for i in range(50)])

    def test_alternating_parameters_pattern(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test message with alternating parameter patterns.'
        message_parts = []
        for i in range(200):
            if i % 2 == 0:
                message_parts.append('{{ $parameters.even }}')
            else:
                message_parts.append('{{ $parameters.odd }}')
        message = ' '.join(message_parts)
        message_parameters = {'even': 'E', 'odd': 'O'}
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_alternating_parameters_pattern', 'format_email_message', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        expected_parts = []
        for i in range(200):
            if i % 2 == 0:
                expected_parts.append('E')
            else:
                expected_parts.append('O')

    def test_long_message_with_complex_text(self):
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test very long message with complex formatting.'
        message_template = '\n        Subject: Report for {{ $parameters.date }}\n        \n        Dear {{ $parameters.recipient }},\n        \n        We are pleased to present the following data:\n        ' + '\n        '.join([f'Item {i}: {{{{ $parameters.item_{i} }}}}' for i in range(30)]) + '\n        \n        Thank you,\n        {{ $parameters.sender }}\n        '
        message_parameters = {'date': '2024-01-01', 'recipient': 'Customer', 'sender': 'Support'}
        message_parameters.update({f'item_{i}': f'Value{i}' for i in range(30)})
        message_parameters_operations = {}
        _call__bound__arguments = inspect.signature(format_email_message).bind(message_template, message_parameters, message_parameters_operations)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(format_email_message, 'tests.inference.unit_tests.test_format_email_message__unit_test_1', 'TestFormatEmailMessage', 'test_long_message_with_complex_text', 'format_email_message', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output