{%- import "cloudevents.jinja.include" as cloudEvents -%}
{%- import "util.jinja.include" as util -%}
// This code was generated by the xRegistry tool.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.

import { EventHubConsumerClient, EventHubProducerClient, ReceivedEventData, EventData } from '@azure/event-hubs';
import { ContainerClient } from '@azure/storage-blob';
{%- set messagegroups = root.messagegroups %}
{%- set uses_cloudevents_message = cloudEvents.usesCloudEvents(root) %}

import { EventHubsProcessor, {% for messagegroupid, messagegroup in messagegroups.items() -%}{%- set class_name = (messagegroupid | pascal | strip_namespace) + "EventDispatcher" %}{{ class_name }}{% if not loop.last %}, {% endif %}{% endfor %} } from '../src';
{%- if uses_cloudevents_message %}
import { CloudEvent } from 'cloudevents';
{%- endif %}

// Import data types
import * as {{ data_project_name }} from '../../{{ data_project_name }}/dist/index.js';

const EVENTHUB_CONNECTION_STRING = process.env.EVENTHUB_CONNECTION_STRING || 
    'Endpoint=sb://testlocal;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=dGVzdGtleQ==;EntityPath=test-hub';
const EVENTHUB_NAME = 'test-hub';
const CONSUMER_GROUP = '$Default';
const BLOB_STORAGE_CONNECTION_STRING = process.env.BLOB_STORAGE_CONNECTION_STRING || 
    'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=dGVzdGtleQ==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;';
const BLOB_CONTAINER_NAME = 'eventhub-checkpoints';

jest.setTimeout(10000);

{%- for messagegroupid, messagegroup in messagegroups.items() %}
{%- set pascal_group_name = messagegroupid | pascal %}
{%- set class_name = (pascal_group_name | strip_namespace) + "EventDispatcher" %}
{%- set test_class_name = (project_name | strip_dots | pascal) + (pascal_group_name | strip_dots) + "Tests" %}

describe('{{ test_class_name }}', () => {
    // Tests verify dispatcher instantiation and handler assignment
    // For full integration testing with Event Hubs emulator, configure EVENTHUB_CONNECTION_STRING environment variable
    
    {%- set message_list = messagegroup.messages.items() | list %}
    {%- for messageid, message in message_list[:3] %}
    {%- set messagename = messageid | strip_namespace | pascal %}
    {%- set message_body_type = util.body_type(data_project_name, root, message) %}
    {%- set isCloudEvent = cloudEvents.isCloudEvent(message) %}
    
    test('should instantiate {{ messagename }} dispatcher and set handler', async () => {
        const dispatcher = new {{ class_name }}();
        const consumerClient = new EventHubConsumerClient(
            CONSUMER_GROUP,
            EVENTHUB_CONNECTION_STRING,
            EVENTHUB_NAME
        );
        const processor = new EventHubsProcessor(consumerClient);
        
        dispatcher.{{ messagename }}Handler = async (eventData: ReceivedEventData{% if isCloudEvent %}, cloudEvent: CloudEvent{% endif %}, data: {{ message_body_type }}) => {
            // Handler implementation
        };
        
        // Verify dispatcher and handler can be configured
        expect(dispatcher).toBeDefined();
        expect(dispatcher.{{ messagename }}Handler).toBeDefined();
        expect(processor).toBeDefined();
        
        await consumerClient.close();
    });
    {%- endfor %}
});
{%- endfor %}
