package org.dromara.mqtt;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Tag;
import org.springframework.boot.context.event.ApplicationReadyEvent;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@Tag("dev")
class MqttPendingCommandCleanupListenerTest {

    @Test
    void onApplicationReadyClearsPendingCommands() {
        MqttCommandAckService service = mock(MqttCommandAckService.class);
        when(service.clearPendingCommands()).thenReturn(3);
        MqttPendingCommandCleanupListener listener = new MqttPendingCommandCleanupListener(service);

        listener.onApplicationReady(mock(ApplicationReadyEvent.class));

        verify(service).clearPendingCommands();
    }

    @Test
    void onApplicationReadyDoesNotThrowWhenCleanupFails() {
        MqttCommandAckService service = mock(MqttCommandAckService.class);
        when(service.clearPendingCommands()).thenThrow(new IllegalStateException("redis unavailable"));
        MqttPendingCommandCleanupListener listener = new MqttPendingCommandCleanupListener(service);

        assertThatCode(() -> listener.onApplicationReady(mock(ApplicationReadyEvent.class)))
            .doesNotThrowAnyException();
    }
}
