"""
E2E: Post Detail & Carousel Workflow
======================================
Tests the FULL production pipeline for post detail and carousel views.

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

import os

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


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


def test_post_detail_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A post detail page must be processable without crashes.
    """
    post_xml = _load_fixture("post_detail_real.xml")
    device = make_real_device_with_xml([post_xml, post_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on post detail."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the post!"

    assert "back" not in device.pressed_keys, "obstacle_guard false positive on post detail!"


def test_carousel_post_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A carousel post must be processable without crashes.
    """
    carousel_xml = _load_fixture("carousel_post_dump.xml")
    device = make_real_device_with_xml([carousel_xml, carousel_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on carousel post."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the carousel!"

    assert "back" not in device.pressed_keys, "obstacle_guard false positive on carousel post!"
