"""
E2E: Ad Skip Workflow
=====================
Tests the feed loop logic to ensure it rapidly skips ads without triggering deep evaluation
or full interaction cycles, and delegates scrolling properly to the orchestrator via skip_type='fast'.
"""

import pytest


@pytest.mark.live_llm
def test_workflow_rapidly_skips_ads(make_real_device_with_image, e2e_configs, e2e_workflow_ctx):
    """
    TDD Test: The full plugin registry must process an ad post and return a 'fast' skip type.
    This guarantees that the orchestrator (bot_flow) will perform a low-latency skip.
    """
    xml_path = "tests/fixtures/home_feed_with_ad.xml"
    jpg_path = "tests/fixtures/home_feed_with_ad.jpg"

    with open(xml_path, "r", encoding="utf-8") as f:
        xml = f.read()

    device = make_real_device_with_image(jpg_path, xml)

    # e2e_workflow_ctx builds the real BehaviorContext + runs execute_all()
    results, ctx = e2e_workflow_ctx(device)

    # Find the result that triggered the skip
    skip_triggered = False
    skip_type = "normal"

    for result in results:
        if result.executed and result.should_skip:
            skip_triggered = True
            skip_type = getattr(result, "skip_type", "normal")
            break

    assert skip_triggered is True, "Pipeline failed to trigger a skip for an ad!"
    assert skip_type in ["fast", "double_fast"], f"Expected 'fast' skip for an ad, got '{skip_type}'."

    # We also assert that the orchestrator loop would catch this.
    # The ad_guard itself must NOT have scrolled, it just signals.
    # Our mocked device records swipes and clicks.
    # AdGuard should NOT have generated a swipe in the plugin!
    assert len(device.swipes) == 0, "AdGuard plugin scrolled the device! It should delegate to the orchestrator."
