from __future__ import annotations from unittest.mock import AsyncMock, MagicMock import pytest from yuxi.channel.application.service.config_service import ConfigService from yuxi.channel.domain.middleware.configurable import Configurable from yuxi.channel.domain.service.pipeline import Pipeline class TestConfigService: @pytest.fixture def mock_config_reload(self): return AsyncMock() @pytest.fixture def mock_pipeline(self): pipeline = MagicMock(spec=Pipeline) pipeline.middlewares = [] return pipeline @pytest.fixture def mock_channel_config(self): config = AsyncMock() config.on_config_updated.return_value = ["auth_token"] return config @pytest.fixture def config_service(self, mock_config_reload, mock_pipeline, mock_channel_config): return ConfigService( config_data={"auth": {"token": "old"}}, config_reload=mock_config_reload, pipeline=mock_pipeline, channel_config=mock_channel_config, ) @pytest.mark.asyncio async def test_reload(self, config_service, mock_config_reload): mock_config_reload.reload.return_value = {"auth": {"token": "new"}} updated, config_hash = await config_service.reload() assert "auth_token" in updated assert config_hash is not None assert len(config_hash) == 8 @pytest.mark.asyncio async def test_reload_no_change(self, config_service, mock_config_reload): mock_config_reload.reload.return_value = {} updated, config_hash = await config_service.reload() assert updated == ["auth_token"] @pytest.mark.asyncio async def test_reload_with_configurable_middleware(self, config_service, mock_pipeline): mock_mw = MagicMock(spec=Configurable) mock_mw.on_config_updated.return_value = ["keyword_filter"] mock_pipeline.middlewares = [mock_mw] mock_config_reload = AsyncMock() mock_config_reload.reload.return_value = {"keyword_blocklist": ["bad"]} service = ConfigService( config_data={}, config_reload=mock_config_reload, pipeline=mock_pipeline, ) updated, _ = await service.reload() assert "keyword_filter" in updated def test_config_property(self, config_service): assert config_service.config == {"auth": {"token": "old"}} def test_update_configurable_middlewares(self, config_service, mock_pipeline): mock_mw = MagicMock(spec=Configurable) mock_mw.on_config_updated.return_value = ["test_mw"] mock_pipeline.middlewares = [mock_mw] updated = config_service._update_configurable_middlewares() assert "test_mw" in updated def test_update_configurable_middlewares_no_configurable(self, config_service, mock_pipeline): mock_pipeline.middlewares = [MagicMock()] updated = config_service._update_configurable_middlewares() assert updated == []