from __future__ import annotations from unittest.mock import AsyncMock, MagicMock, patch import pytest from yuxi.channel.startup import get_container, init_channel, register_channel_middleware, shutdown_channel class TestStartupModule: @pytest.fixture(autouse=True) def reset_container(self): import yuxi.channel.startup as startup startup._container = None yield startup._container = None @pytest.mark.asyncio async def test_init_channel(self): mock_container = MagicMock() with patch("yuxi.channel.startup.ChannelContainerFactory") as mock_factory: mock_factory.create = AsyncMock(return_value=mock_container) result = await init_channel(redis_url="redis://localhost") assert result is mock_container mock_factory.create.assert_awaited_once() @pytest.mark.asyncio async def test_shutdown_channel_with_container(self): mock_container = AsyncMock() await shutdown_channel(mock_container) mock_container.shutdown.assert_awaited_once() @pytest.mark.asyncio async def test_shutdown_channel_with_global(self): import yuxi.channel.startup as startup mock_container = AsyncMock() startup._container = mock_container await shutdown_channel() mock_container.shutdown.assert_awaited_once() assert startup._container is None @pytest.mark.asyncio async def test_shutdown_channel_none(self): result = await shutdown_channel(None) assert result is None def test_get_container_initial(self): result = get_container() assert result is None def test_get_container_after_init(self): import yuxi.channel.startup as startup mock_container = MagicMock() startup._container = mock_container result = get_container() assert result is mock_container def test_register_channel_middleware(self): import yuxi.channel.startup as startup mock_container = MagicMock() startup._container = mock_container app = MagicMock() app.state = MagicMock() with patch("yuxi.channel.startup.setup_channel") as mock_setup: register_channel_middleware(app) mock_setup.assert_called_once_with(app, mock_container) def test_register_channel_middleware_no_container(self): app = MagicMock() with patch("yuxi.channel.startup.setup_channel") as mock_setup: register_channel_middleware(app) mock_setup.assert_not_called()