"""
E2E: Instagram Modal Workflow
===============================
Tests the FULL production pipeline when Instagram shows a modal overlay
(survey, "Not Now" prompt, content creation flow).

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

INSTAGRAM_SURVEY_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/dialog_container"
              class="android.widget.FrameLayout" bounds="[100,600][980,1800]">
            <node text="How are you enjoying Instagram?"
                  class="android.widget.TextView"
                  bounds="[150,650][930,750]" />
            <node text="Not Now"
                  class="android.widget.Button" clickable="true"
                  bounds="[150,1600][500,1700]" />
            <node text="Rate Now"
                  class="android.widget.Button" clickable="true"
                  bounds="[550,1600][930,1700]" />
        </node>
    </node>
</hierarchy>
"""

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/row_feed_button_like"
              class="android.widget.ImageView" clickable="true"
              content-desc="Like" bounds="[30,1360][120,1450]" />
    </node>
</hierarchy>
"""


def test_instagram_survey_modal_terminates_chain(make_real_device_with_xml, e2e_workflow_ctx):
    """
    An Instagram survey modal ("How are you enjoying Instagram?")
    must be detected and dismissed. The chain must skip interactions.
    """

    device = make_real_device_with_xml([INSTAGRAM_SURVEY_XML, NORMAL_POST_XML])
    results, ctx = e2e_workflow_ctx(device, context_xml=INSTAGRAM_SURVEY_XML)

    guard_fired = any(r.executed and r.should_skip for r in results)
    assert guard_fired, "obstacle_guard did not fire on Instagram survey modal!"

    total_interactions = sum(r.interactions for r in results)
    assert total_interactions == 0, f"{total_interactions} interaction(s) performed on a survey modal!"

    assert "back" in device.pressed_keys, "obstacle_guard did not press BACK on survey modal!"
