"""
Tests for PostDataExtractionPlugin
==================================
Ensures that data extraction robustly extracts the author username
from different UI layouts (Home Feed, Reels Feed, etc.) without failing.
"""

from GramAddict.core.behaviors.post_data_extraction import PostDataExtractionPlugin
from GramAddict.core.behaviors import BehaviorContext
from tests.e2e.conftest import load_fixture_xml
import pytest

def test_extract_username_from_home_feed(
    make_real_device_with_xml, e2e_configs, e2e_session, e2e_cognitive_stack_factory
):
    plugin = PostDataExtractionPlugin()
    xml = load_fixture_xml("home_feed_real.xml")
    
    device = make_real_device_with_xml(xml)
    
    ctx = BehaviorContext(
        device=device,
        configs=e2e_configs,
        session_state=e2e_session,
        cognitive_stack=e2e_cognitive_stack_factory(device),
        context_xml=xml,
        post_data={},
        username="",
    )
    
    result = plugin.execute(ctx)
    assert result.executed is True
    assert ctx.username != ""
    assert ctx.username is not None
    assert ctx.post_data.get("username_missing") is not True

def test_extract_username_from_reels_feed(
    make_real_device_with_xml, e2e_configs, e2e_session, e2e_cognitive_stack_factory
):
    plugin = PostDataExtractionPlugin()
    xml = load_fixture_xml("reels_feed_real.xml")
    
    device = make_real_device_with_xml(xml)
    
    ctx = BehaviorContext(
        device=device,
        configs=e2e_configs,
        session_state=e2e_session,
        cognitive_stack=e2e_cognitive_stack_factory(device),
        context_xml=xml,
        post_data={},
        username="",
    )
    
    result = plugin.execute(ctx)
    assert result.executed is True, "PostDataExtraction failed to execute on Reels Feed"
    assert ctx.username != "", "Username extracted from Reels Feed is empty!"
    assert ctx.username is not None
    assert ctx.post_data.get("username_missing") is not True


