import pytest
from unittest.mock import AsyncMock
from krrkt.engine.deep_layer import DeepLayer


class TestDeepLayer:
    @pytest.mark.asyncio
    async def test_returns_findings_list(self):
        mock_provider = AsyncMock()
        mock_provider.analyze.return_value = [
            {"start": 0, "end": 5, "word": "Очень", "rule": "Усилитель",
             "weight": 100, "style": "red", "tab": "red",
             "description": "Замените фактами", "section": "усилитель",
             "source": "deep"}
        ]
        layer = DeepLayer(provider=mock_provider)
        findings = await layer.analyze("Очень хороший текст.", already_found=[])
        assert len(findings) == 1
        assert findings[0]["source"] == "deep"

    @pytest.mark.asyncio
    async def test_passes_already_found(self):
        mock_provider = AsyncMock()
        mock_provider.analyze.return_value = []
        layer = DeepLayer(provider=mock_provider)
        already = [{"start": 0, "end": 5, "word": "Очень", "rule": "Усилитель"}]
        await layer.analyze("Очень хороший текст.", already_found=already)
        call_args = mock_provider.analyze.call_args
        assert call_args[0][2] == already  # third positional arg is already_found

    @pytest.mark.asyncio
    async def test_empty_text(self):
        mock_provider = AsyncMock()
        mock_provider.analyze.return_value = []
        layer = DeepLayer(provider=mock_provider)
        findings = await layer.analyze("", already_found=[])
        assert findings == []

    @pytest.mark.asyncio
    async def test_none_already_found(self):
        mock_provider = AsyncMock()
        mock_provider.analyze.return_value = []
        layer = DeepLayer(provider=mock_provider)
        await layer.analyze("Текст для проверки.")
        call_args = mock_provider.analyze.call_args
        assert call_args[0][2] == []  # defaults to empty list
