package org.raddatz.familienarchiv.notification;

import org.junit.jupiter.api.Test;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

class SseEmitterRegistryTest {

    private final SseEmitterRegistry registry = new SseEmitterRegistry();

    @Test
    void register_returnsEmitter() {
        SseEmitter emitter = registry.register(UUID.randomUUID());

        assertThat(emitter).isNotNull();
    }

    @Test
    void send_doesNothing_whenNoEmitterRegistered() {
        assertThatCode(() -> registry.send(UUID.randomUUID(), "data"))
                .doesNotThrowAnyException();
    }

    @Test
    void register_replacesExistingEmitter_forSameUser() {
        UUID userId = UUID.randomUUID();

        SseEmitter first = registry.register(userId);
        SseEmitter second = registry.register(userId);

        assertThat(first).isNotSameAs(second);
    }

    @Test
    void send_doesNotThrow_whenEmitterRegistered_andSendFails() {
        // Registering an emitter without an active HTTP connection causes IOException on send
        UUID userId = UUID.randomUUID();
        registry.register(userId);

        // Must not propagate the IOException — it's caught and the emitter is removed
        assertThatCode(() -> registry.send(userId, "data")).doesNotThrowAnyException();
    }
}
