"""
E2E: Normal Feed Post Workflow
===============================
Tests the FULL production pipeline when the device shows a normal
Instagram post. The pipeline must run without crashes and process
the post normally.

Mock: ONLY the device (XML dumps).
Real: Cognitive stack, PluginRegistry, SessionState, Config.
"""

NORMAL_POST_XML = """<?xml version="1.0" encoding="utf-8"?>
<hierarchy rotation="0">
    <node class="android.widget.FrameLayout" package="com.instagram.android"
          bounds="[0,0][1080,2400]">
        <node resource-id="com.instagram.android:id/action_bar_container"
              class="android.widget.FrameLayout" bounds="[0,0][1080,170]">
            <node text="Instagram" class="android.widget.TextView" />
        </node>
        <node resource-id="com.instagram.android:id/row_feed_photo_profile_name"
              class="android.widget.TextView" text="photographer_jane"
              clickable="true" bounds="[160,300][600,340]" />
        <node resource-id="com.instagram.android:id/row_feed_photo_imageview"
              class="android.widget.ImageView"
              content-desc="Photo by photographer_jane. A beautiful sunset."
              bounds="[0,350][1080,1350]" />
        <node resource-id="com.instagram.android:id/row_feed_button_like"
              class="android.widget.ImageView" clickable="true"
              content-desc="Like" bounds="[30,1360][120,1450]" />
        <node resource-id="com.instagram.android:id/row_feed_button_comment"
              class="android.widget.ImageView" clickable="true"
              content-desc="Comment" bounds="[150,1360][240,1450]" />
        <node resource-id="com.instagram.android:id/row_feed_textview_likes"
              class="android.widget.TextView" text="1,234 likes"
              bounds="[30,1460][400,1500]" />
        <node resource-id="com.instagram.android:id/tab_bar" bounds="[0,2300][1080,2400]">
            <node resource-id="com.instagram.android:id/feed_tab" content-desc="Home"
                  selected="true" clickable="true" bounds="[0,2300][216,2400]" />
        </node>
    </node>
</hierarchy>
"""


def test_normal_post_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A normal Instagram post must be processable by the full pipeline.
    If any plugin has a broken import, missing attribute, or crashes
    on valid XML, this test catches it.
    """
    device = make_real_device_with_xml([NORMAL_POST_XML, NORMAL_POST_XML])
    results, ctx = e2e_workflow_ctx(device)

    # Multiple plugins must have run
    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, (
        f"Only {executed_count} plugin(s) executed on a normal feed post. "
        "The pipeline is broken — plugins are crashing silently."
    )

    # No false positives: obstacle_guard must NOT have pressed BACK
    assert "back" not in device.pressed_keys, "obstacle_guard pressed BACK on a normal Instagram post — false positive!"

    # No CONTEXT_LOST
    for result in results:
        assert (
            result.metadata.get("return_code") != "CONTEXT_LOST"
        ), "Plugin returned CONTEXT_LOST on a normal feed post!"

    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    # The pipeline must have ACTUALLY interacted with the device (swipe or tap)
    assert len(device.clicks) > 0 or len(device.swipes) > 0, (
        "LIE DETECTED: The pipeline claimed success (executed=True) but did "
        "not perform a single click or swipe on the device!"
    )

    # Check if the interaction was correctly targeted
    # On a normal feed post, it should eventually like, comment, or swipe.
    # The Like button is at bounds="[30,1360][120,1450]", center ~ (75, 1405)
    liked = any(70 <= c[0] <= 80 and 1400 <= c[1] <= 1410 for c in device.clicks)
    swiped = len(device.swipes) > 0

    assert liked or swiped, (
        f"LIE DETECTED: The bot interacted, but NOT with the like button or by swiping! "
        f"Clicks: {device.clicks}, Swipes: {device.swipes}"
    )
