"""
E2E: Stories Feed Workflow
===========================
Tests the FULL production pipeline when the device shows a Story view.

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

import os

FIXTURES_DIR = os.path.join(os.path.dirname(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_stories_feed_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A Stories feed dump must be processable without crashes.
    """
    stories_xml = _load_fixture("stories_feed_dump.xml")
    device = make_real_device_with_xml([stories_xml, stories_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 Stories feed."
    # 🚨 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 Story!"

    assert "back" not in device.pressed_keys, "obstacle_guard false positive on Stories feed!"


def test_story_view_full_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A single story view (full-screen playback) must be processable.
    """
    story_xml = _load_fixture("story_view_full.xml")
    device = make_real_device_with_xml([story_xml, story_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 Story view."
    # 🚨 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 Story view!"
