{%- 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.

{%- set messagegroups = root.messagegroups %}
{%- set uses_cloudevents_message = cloudEvents.usesCloudEvents(root) %}

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

const MQTT_BROKER_URL = process.env.MQTT_BROKER_URL || 'mqtt://localhost:1883';

jest.setTimeout(60000);

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

describe('{{ test_class_name }}', () => {
    // Tests verify MQTT client class structure and method availability
    // For full integration testing with MQTT broker, configure MQTT_BROKER_URL environment variable
    
    {%- set message_list = messagegroup.messages.items() | list %}
    {%- for messageid, message in message_list[:3] %}
    {%- set messagename = messageid | strip_namespace | pascal %}
    {%- set body_type = util.body_type(data_project_name, root, message) %}
    
    test('should have {{ messagename }} MQTT client with handler and publish methods', () => {
        // Verify client class exists and has required methods
        expect({{ class_name }}).toBeDefined();
        expect({{ class_name }}.prototype.publish{{ messagename }}).toBeDefined();
        expect({{ class_name }}.prototype.subscribe).toBeDefined();
        expect({{ class_name }}.prototype.close).toBeDefined();
        
        // Verify handler property exists
        const client = new {{ class_name }}(MQTT_BROKER_URL);
        expect(client).toBeDefined();
        
        // Set a test handler to verify property exists
        {%- set isCloudEvent = cloudEvents.isCloudEvent(message) %}
        {%- if isCloudEvent %}
        client.{{ messagename }}Handler = async (cloudEvent: CloudEvent, data: {{ body_type }}) => {
            // Test handler
        };
        {%- else %}
        client.{{ messagename }}Handler = async (data: {{ body_type }}) => {
            // Test handler
        };
        {%- endif %}
        
        expect(client.{{ messagename }}Handler).toBeDefined();
        
        // Clean up
        client.close();
    });
    {%- endfor %}
});
{%- endfor %}
