"""
TDD: Perception Module Tests.

Tests the extracted feed analysis functions in isolation.
"""


class TestFeedMarkers:
    """FEED_MARKERS must correctly identify feed presence."""

    def test_feed_markers_is_list(self):
        """FEED_MARKERS must be a list."""
        from GramAddict.core.perception.feed_analysis import FEED_MARKERS

        assert isinstance(FEED_MARKERS, list)
        assert len(FEED_MARKERS) >= 4

    def test_has_feed_markers_detects_feed(self):
        """has_feed_markers must return True when markers are present."""
        from GramAddict.core.perception.feed_analysis import has_feed_markers

        xml = '<hierarchy><node resource-id="row_feed_photo_profile_name"/></hierarchy>'
        assert has_feed_markers(xml) is True

    def test_has_feed_markers_detects_reels(self):
        """has_feed_markers must detect Reels markers."""
        from GramAddict.core.perception.feed_analysis import has_feed_markers

        xml = '<hierarchy><node resource-id="clips_media_component"/></hierarchy>'
        assert has_feed_markers(xml) is True

    def test_has_feed_markers_rejects_empty(self):
        """has_feed_markers must return False on empty/unrelated XML."""
        from GramAddict.core.perception.feed_analysis import has_feed_markers

        assert has_feed_markers("") is False
        assert has_feed_markers("<hierarchy/>") is False


class TestCarouselDetection:
    """Carousel detection must use Instagram-specific resource IDs."""

    def test_detects_carousel_indicator(self):
        """Must detect carousel_page_indicator."""
        from GramAddict.core.perception.feed_analysis import has_carousel_in_view

        xml = '<node resource-id="com.instagram.android:id/carousel_page_indicator"/>'
        assert has_carousel_in_view(xml) is True

    def test_detects_carousel_group(self):
        """Must detect carousel_media_group."""
        from GramAddict.core.perception.feed_analysis import has_carousel_in_view

        xml = '<node resource-id="com.instagram.android:id/carousel_media_group"/>'
        assert has_carousel_in_view(xml) is True

    def test_rejects_non_carousel(self):
        """Must not false-positive on non-carousel XML."""
        from GramAddict.core.perception.feed_analysis import has_carousel_in_view

        xml = '<node resource-id="com.instagram.android:id/action_bar_button"/>'
        assert has_carousel_in_view(xml) is False


class TestExtractPostContent:
    """Post content extraction must return valid dict structure."""

    def test_returns_dict_with_required_keys(self):
        """Must always return dict with username, description, caption."""
        from unittest.mock import MagicMock, patch

        from GramAddict.core.perception.feed_analysis import extract_post_content

        with patch("GramAddict.core.telepathic_engine.TelepathicEngine.get_instance") as mock:
            instance = MagicMock()
            instance.find_best_node.return_value = None
            mock.return_value = instance

            result = extract_post_content("<hierarchy/>")
            assert "username" in result
            assert "description" in result
            assert "caption" in result

    def test_handles_garbage_xml_gracefully(self):
        """Must not crash on corrupted XML."""
        from unittest.mock import MagicMock, patch

        from GramAddict.core.perception.feed_analysis import extract_post_content

        with patch("GramAddict.core.telepathic_engine.TelepathicEngine.get_instance") as mock:
            instance = MagicMock()
            instance.find_best_node.return_value = None
            mock.return_value = instance

            result = extract_post_content("garbage<<>>not xml at all")
            assert isinstance(result, dict)
            assert result["username"] == ""


class TestBackwardCompatibility:
    """bot_flow.py re-exports must work unchanged."""

    def test_feed_markers_importable_from_bot_flow(self):
        """FEED_MARKERS must be importable from bot_flow for existing tests."""
        from GramAddict.core.bot_flow import FEED_MARKERS

        assert isinstance(FEED_MARKERS, list)
        assert len(FEED_MARKERS) >= 4

    def test_has_carousel_importable_from_bot_flow(self):
        """has_carousel_in_view must be importable from bot_flow."""
        from GramAddict.core.bot_flow import has_carousel_in_view

        assert callable(has_carousel_in_view)

    def test_extract_post_content_importable_from_bot_flow(self):
        """_extract_post_content must be importable from bot_flow."""
        from GramAddict.core.bot_flow import _extract_post_content

        assert callable(_extract_post_content)
