from GramAddict.core.telepathic_engine import TelepathicEngine


class TestTelepathicGuards:
    def setup_method(self):
        self.engine = TelepathicEngine()

    def test_strict_story_ring_guard(self):
        """
        TDD: Story rings MUST be physically near the top of the screen (y < 30%).
        Post profile headers that appear further down must be aggressively blocked
        when the intent is 'tap story ring avatar'.
        """
        intent = "tap story ring avatar"
        screen_height = 2400

        # Valid Story Ring (Top of screen, but below status bar)
        valid_story = {"resource_id": "reel_ring", "y": 300, "area": 100}
        assert self.engine._structural_sanity_check(valid_story, intent, screen_height) is True

        # Invalid Story Ring (Hallucination: Post profile header in the feed)
        invalid_story = {"resource_id": "row_feed_profile_header", "y": 800, "area": 100}
        assert self.engine._structural_sanity_check(invalid_story, intent, screen_height) is False

    def test_strict_button_guard(self):
        """
        TDD: When explicitly looking for a 'button', nodes that declare themselves
        as profiles (e.g. 'go to profile') must be blocked, to prevent accidental
        profile visits when clicking 'like'.
        """
        intent = "Heart like button for comment"
        screen_height = 2400

        # Valid Like Button
        valid_btn = {"resource_id": "like_button", "semantic_string": "Like", "y": 1000, "area": 100}
        assert self.engine._structural_sanity_check(valid_btn, intent, screen_height) is True

        # Invalid Profile Link masquerading as a match due to string proximity
        invalid_prof = {
            "resource_id": "username",
            "semantic_string": "Go to cayleighanddavid's profile",
            "y": 1000,
            "area": 100,
        }
        assert self.engine._structural_sanity_check(invalid_prof, intent, screen_height) is False

        # However, if the intent *is* profile, it should pass
        intent_prof = "go to profile"
        assert self.engine._structural_sanity_check(invalid_prof, intent_prof, screen_height) is True

    def test_like_semantic_verification(self):
        """
        TDD: Verify that 'unlike' is treated as a successful 'Like' action,
        because tapping 'Like' changes the state to 'Unlike' in English Instagram.
        """
        # Testing the specific regex logic inside verify_success
        import re

        xml_dump_success = '<node class="android.widget.ImageView" content-desc="Unlike" />'

        marker_found = re.search(r"\b(liked|unlike|gefällt mir nicht mehr|gefällt mir am)\b", xml_dump_success.lower())
        assert marker_found is not None

        xml_dump_fail = '<node class="android.widget.ImageView" content-desc="Like" />'
        marker_found_fail = re.search(
            r"\b(liked|unlike|gefällt mir nicht mehr|gefällt mir am)\b", xml_dump_fail.lower()
        )
        assert marker_found_fail is None
