from GramAddict.core.telepathic_engine import TelepathicEngine


class TestVerifySuccessGridReels:
    """
    TDD Tests: Reproduces Bug 1 from the 2026-04-17 09:56 run.

    The Grid Fast-Path correctly clicks an explore grid item, the UI changes
    (a Reel opens), but verify_success() returns False because it only looks
    for row_feed_* markers which don't exist in Reel views.
    """

    def setup_method(self):
        self.engine = TelepathicEngine()
        # Simulate a click context so verify_success has something to check against
        TelepathicEngine._last_click_context = {
            "intent": "view a post",
            "semantic_string": "id context: 'image button'",
            "x": 178,
            "y": 558,
            "timestamp": 0,
        }

    def test_reel_view_accepted_as_valid_grid_result(self):
        """A Reel opening after a grid tap must be accepted as success."""
        # Simulate post-click XML containing Reel markers but NO feed markers
        reel_xml = """
        <hierarchy>
            <node resource-id="com.instagram.android:id/clips_viewer_view_pager" class="ViewPager" />
            <node resource-id="com.instagram.android:id/clips_media_component" class="FrameLayout" />
            <node content-desc="Like" resource-id="com.instagram.android:id/clips_like_button" />
            <node content-desc="Comment" resource-id="com.instagram.android:id/clips_comment_button" />
        </hierarchy>
        """
        result = self.engine.verify_success("view a post", reel_xml)
        assert result is True, "verify_success rejected a valid Reel view opened from grid tap"

    def test_normal_feed_post_still_accepted(self):
        """A normal feed post opening after a grid tap must still be accepted."""
        feed_xml = """
        <hierarchy>
            <node resource-id="com.instagram.android:id/row_feed_button_like" content-desc="Like" />
            <node resource-id="com.instagram.android:id/row_feed_button_comment" content-desc="Comment" />
            <node resource-id="com.instagram.android:id/row_feed_photo_profile_name" text="@testuser" />
        </hierarchy>
        """
        result = self.engine.verify_success("view a post", feed_xml)
        assert result is True, "verify_success rejected a valid feed post opened from grid tap"

    def test_explore_grid_still_visible_is_failure(self):
        """If the grid is still showing (no post opened), verify must fail."""
        explore_xml = """
        <hierarchy>
            <node resource-id="com.instagram.android:id/recycler_view" />
            <node resource-id="com.instagram.android:id/image_button" />
            <node resource-id="com.instagram.android:id/explore_action_bar" />
            <node text="Search" resource-id="com.instagram.android:id/action_bar_search_edit_text" />
        </hierarchy>
        """
        result = self.engine.verify_success("view a post", explore_xml)
        assert result is False, "verify_success should return False when grid is still visible"

    def test_profile_grid_reel_accepted(self):
        """Profile grid → Reel must also be accepted."""
        TelepathicEngine._last_click_context["intent"] = "view a post"
        reel_xml = """
        <hierarchy>
            <node resource-id="com.instagram.android:id/clips_viewer_view_pager" />
            <node resource-id="com.instagram.android:id/reel_viewer_subtitle" text="Audio" />
        </hierarchy>
        """
        result = self.engine.verify_success("view a post", reel_xml)
        assert result is True, "verify_success rejected a Reel opened from profile grid"
