"""验证BrandCitationLLMService不再返回Mock数据，而是抛出明确错误

迁移自 test_llm_adapter_no_mock.py，原测试验证 LLMAdapter 不返回 Mock 数据。
现在验证 BrandCitationLLMService (System 1) 的相同行为。
"""
import pytest
from unittest.mock import AsyncMock, patch, MagicMock

from app.services.llm.brand_citation_service import BrandCitationLLMService
from app.services.llm.base import LLMError


class TestBrandCitationLLMServiceNoMock:
    """验证BrandCitationLLMService不返回Mock数据，而是抛出明确错误"""

    @pytest.fixture
    def service(self):
        return BrandCitationLLMService()

    @pytest.mark.asyncio
    async def test_enable_llm_false_raises_error(self, service):
        """ENABLE_LLM=False时必须抛出LLMError，而非返回Mock数据"""
        with patch('app.services.llm.brand_citation_service.settings') as mock_settings:
            mock_settings.ENABLE_LLM = False

            with pytest.raises(LLMError) as exc_info:
                await service.query_brand_citation(
                    keyword="AI搜索",
                    brand_name="测试品牌",
                    brand_aliases=["别名1"],
                )

            error_msg = str(exc_info.value)
            assert "ENABLE_LLM" in error_msg
            assert "未启用" in error_msg

    @pytest.mark.asyncio
    async def test_enable_llm_true_calls_provider(self, service):
        """ENABLE_LLM=True时正常调用Provider"""
        mock_provider = AsyncMock()
        mock_provider.chat.return_value = MagicMock(
            content='{"cited": true, "position": 1, "citation_text": "测试引用", "sentiment": "positive", "confidence": 0.95}'
        )

        with patch.object(service, '_get_provider', return_value=mock_provider):
            with patch('app.services.llm.brand_citation_service.settings') as mock_settings:
                mock_settings.ENABLE_LLM = True

                result = await service.query_brand_citation(
                    keyword="AI搜索",
                    brand_name="测试品牌",
                    brand_aliases=[],
                )

                assert result.cited is True
                assert result.position == 1
                assert result.sentiment == "positive"

    def test_get_mock_result_method_removed(self):
        """_get_mock_result方法必须已被删除（旧LLMAdapter遗留）"""
        assert not hasattr(BrandCitationLLMService, "_get_mock_result"), (
            "_get_mock_result方法仍然存在，必须删除"
        )

    @pytest.mark.asyncio
    async def test_error_message_user_friendly(self, service):
        """错误信息必须对用户友好，包含配置指引"""
        with patch('app.services.llm.brand_citation_service.settings') as mock_settings:
            mock_settings.ENABLE_LLM = False

            with pytest.raises(LLMError) as exc_info:
                await service.query_brand_citation(
                    keyword="AI搜索",
                    brand_name="测试品牌",
                    brand_aliases=[],
                )

            error_msg = str(exc_info.value)
            assert "ENABLE_LLM" in error_msg
