"""
E2E: Stuck on Other Profile Regression
========================================
PRODUCTION BUG 2026-04-30:
Bot landed on OTHER_PROFILE screen during a ReelsFeed interaction session.
The pipeline kept cycling through plugins that all returned "Cannot X on
other_profile" without ever pressing BACK or returning CONTEXT_LOST.

Pattern observed:
- likes plugin: "Cannot 'tap like button' on other_profile"
- repost plugin: "Cannot 'share to story' on other_profile"
- follow plugin: "Cannot 'tap Follow button' on other_profile"
- profile_visit: Keeps trying to visit the same profile
- 35x snapping attempts on the same post

Root cause: The screen was classified as NORMAL (score 0.96) even though
the bot was on OTHER_PROFILE. The SAE only classified the raw visual
state, not the screen's structural type. The obstacle_guard only checks
SituationType, not ScreenType mismatches.

This test verifies that when ALL interaction plugins fail on a screen,
the pipeline must signal a context problem rather than silently loop.

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

import os

E2E_FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixtures")


def _load_e2e_fixture(name):
    with open(os.path.join(E2E_FIXTURES_DIR, name), "r", encoding="utf-8") as f:
        return f.read()


def test_other_profile_does_not_infinite_loop(make_real_device_with_xml, e2e_workflow_ctx):
    """
    REGRESSION: When stuck on OTHER_PROFILE, the pipeline must not
    silently succeed with 0 interactions and 0 errors — it must signal
    that something is wrong (via metadata or should_skip).

    The pipeline MUST NOT:
    - Perform interactions on a profile page as if it were a feed post
    - Silently succeed while doing nothing useful
    """
    profile_xml = _load_e2e_fixture("other_profile_real.xml")
    device = make_real_device_with_xml([profile_xml, profile_xml, profile_xml])
    results, ctx = e2e_workflow_ctx(device)

    # The pipeline ran — check what happened
    executed_plugins = [r for r in results if r.executed]

    # KEY ASSERTION: The bot must NOT perform like/follow/comment
    # interactions against a profile page as if it were a feed post.
    # If interaction count > 0, the bot is hallucinating feed elements
    # on a profile page.
    interaction_results = [r for r in results if r.interactions > 0]
    hallucinated_interactions = [
        r
        for r in interaction_results
        if r.metadata.get("action") in ("tap like button", "tap follow button", "share to story")
    ]

    # Verify the pipeline didn't crash silently
    assert len(executed_plugins) >= 1, "No plugins executed at all — the pipeline is dead!"

    # Count the snapping attempts as a proxy for "stuck in loop"
    # In the real bug, we saw 35 snapping attempts
    snap_swipes = len(device.swipes)
    assert snap_swipes <= 10, (
        f"Perfect snapping performed {snap_swipes} swipes — "
        "this is the 'stuck on other_profile' infinite snapping loop!"
    )


def test_other_profile_preserves_no_ad_false_positive(make_real_device_with_xml, e2e_workflow_ctx):
    """
    REGRESSION: The VLM identified a regular profile as an "ad" because
    the profile bio/grid collage was misclassified. The resonance evaluator
    must not false-positive on real profiles.

    This tests that ad_guard doesn't fire on a real user profile.
    """
    profile_xml = _load_e2e_fixture("other_profile_real.xml")
    device = make_real_device_with_xml([profile_xml, profile_xml])
    results, ctx = e2e_workflow_ctx(device)

    # ad_guard must NOT fire on a real user profile
    ad_guard_results = [r for r in results if r.metadata.get("plugin") == "ad_guard" and r.should_skip]
    assert len(ad_guard_results) == 0, (
        "ad_guard false-positive on a real user profile! "
        "This was a production bug where profile grid collages were "
        "misclassified as ads."
    )
