"""
TDD Tests: Zero-Maintenance String Compliance

These tests enforce that no hardcoded German/localized strings
exist in navigation-critical code paths. The bot must rely
exclusively on structural resource_id patterns, never on
localized UI text that changes with device language.
"""

import re


# ══════════════════════════════════════════════════════════
# 1. TOGGLE_INTENT_MARKERS must be language-agnostic
# ══════════════════════════════════════════════════════════


class TestToggleIntentMarkersAreLanguageAgnostic:
    """Ensure TOGGLE_INTENT_MARKERS contains zero localized strings."""

    GERMAN_STRINGS = [
        "gefällt",
        "gefolgt",
        "abonnieren",
        "speichern",
        "gespeichert",
        "antworten",
        "kommentar",
        "beitrag",
    ]

    def test_no_german_strings_in_toggle_markers(self):
        from GramAddict.core.perception.action_memory import TOGGLE_INTENT_MARKERS

        for intent_key, markers in TOGGLE_INTENT_MARKERS.items():
            for marker in markers:
                assert marker.lower() not in [
                    g.lower() for g in self.GERMAN_STRINGS
                ], f"TOGGLE_INTENT_MARKERS['{intent_key}'] contains German string '{marker}'!"

    def test_markers_only_contain_english_or_resource_id_patterns(self):
        """All markers must be English words or resource_id fragments."""
        from GramAddict.core.perception.action_memory import TOGGLE_INTENT_MARKERS

        allowed_pattern = re.compile(r"^[a-z_]+$")
        for intent_key, markers in TOGGLE_INTENT_MARKERS.items():
            for marker in markers:
                assert allowed_pattern.match(
                    marker
                ), f"Marker '{marker}' in '{intent_key}' contains non-ASCII or non-ID characters!"

    def test_intent_match_rejects_reel_message_composer(self):
        """The semantic guard must reject reel message composer for 'like' intent."""
        from GramAddict.core.perception.action_memory import _intent_matches_node

        # This was the exact production failure: VLM picked the message composer
        semantic = "text: 'Send message', desc: '', id: 'com.instagram.android:id/reel_viewer_message_composer_text'"
        assert _intent_matches_node("tap like button", semantic) is False

    def test_intent_match_accepts_real_like_button(self):
        """The semantic guard must accept a real like button by resource_id."""
        from GramAddict.core.perception.action_memory import _intent_matches_node

        semantic = "text: '', desc: 'Like', id: 'com.instagram.android:id/row_feed_button_like'"
        assert _intent_matches_node("tap like button", semantic) is True

    def test_intent_match_accepts_like_button_by_id_only(self):
        """Even without text/desc, resource_id containing 'like' is enough."""
        from GramAddict.core.perception.action_memory import _intent_matches_node

        semantic = "text: '', desc: '', id: 'com.instagram.android:id/row_feed_button_like'"
        assert _intent_matches_node("tap like button", semantic) is True


# ══════════════════════════════════════════════════════════
# 2. TelepathicEngine Following Guard must be structural
# ══════════════════════════════════════════════════════════


class TestTelepathicEngineFollowingGuardIsStructural:
    """The 'already followed' guard must not rely on German strings."""

    def test_no_german_in_following_guard_source(self):
        """Scan telepathic_engine.py for any German follow-state strings."""
        import inspect

        from GramAddict.core.telepathic_engine import TelepathicEngine

        source = inspect.getsource(TelepathicEngine)
        german_terms = ["gefolgt", "angefragt", "abonniert", "abonnieren"]
        for term in german_terms:
            assert (
                term not in source
            ), f"TelepathicEngine source contains German string '{term}'!"


# ══════════════════════════════════════════════════════════
# 3. DarwinEngine comment detection must be structural
# ══════════════════════════════════════════════════════════


class TestDarwinEngineCommentDetectionIsStructural:
    """The _has_comments heuristic must not rely on German strings."""

    def test_no_german_in_has_comments_source(self):
        import inspect

        from GramAddict.core.darwin_engine import DarwinEngine

        source = inspect.getsource(DarwinEngine._has_comments)
        german_terms = ["kommentar", "ansehen"]
        for term in german_terms:
            assert (
                term not in source
            ), f"DarwinEngine._has_comments contains German string '{term}'!"


# ══════════════════════════════════════════════════════════
# 4. ResonanceEngine comment filtering must be structural
# ══════════════════════════════════════════════════════════


class TestResonanceEngineCommentFilteringIsStructural:
    """Comment extraction blocked_exact list must not contain German strings."""

    def test_no_german_in_resonance_source(self):
        import inspect

        from GramAddict.core.resonance_engine import ResonanceEngine

        source = inspect.getsource(ResonanceEngine.extract_and_learn_comments)
        german_terms = [
            "antworten",
            "gefällt mir",
            "antworten ansehen",
            "übersetzung anzeigen",
            "antworten verbergen",
            "alle kommentare ansehen",
            "absenden",
            "gehe zu",
            "tippe auf",
            "aktionen für diesen beitrag",
        ]
        for term in german_terms:
            assert (
                term not in source
            ), f"ResonanceEngine.extract_and_learn_comments contains German '{term}'!"
