import pytest

from kurier.core.basekurier import BaseKurier
from kurier.core.exceptions import KurierPublishError
from kurier.hermes import Hermes

Hermes.is_enabled = False


class BrokenBus(BaseKurier):
    def __init__(self, name: str):
        super().__init__(name)
        self._register()

    def _publish_to_bus(self, json_message, meta_data):
        raise RuntimeError("Broker connection failed")

    def _run_consumer_loop(self):
        pass

    def close(self):
        pass

    @classmethod
    def get(cls, name):
        pass

    @classmethod
    def start_consumer(cls, name):
        pass


@pytest.mark.asyncio
async def test_publish_raises_exception():
    BaseKurier._registry.clear()
    BrokenBus._registry.clear()
    bus = BrokenBus("broken")
    with pytest.raises(KurierPublishError) as excinfo:
        await bus.publish_async("message")
    assert "Broker connection failed" in str(excinfo.value)
