from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors import BehaviorContext
from GramAddict.core.behaviors.post_data_extraction import PostDataExtractionPlugin


@pytest.fixture
def post_data_extraction():
    return PostDataExtractionPlugin()


@pytest.fixture
def mock_context():
    session_state = MagicMock()
    session_state.job_target = "Feed"

    ctx = BehaviorContext(
        device=MagicMock(),
        configs=MagicMock(),
        session_state=session_state,
        cognitive_stack={},
        context_xml="<xml></xml>",
    )
    return ctx


class TestPostDataExtractionPlugin:
    @patch("GramAddict.core.behaviors.post_data_extraction.extract_post_content")
    def test_execute_success(self, mock_extract, post_data_extraction, mock_context):
        mock_extract.return_value = {"username": "test_user", "description": "hello"}

        result = post_data_extraction.execute(mock_context)

        assert result.executed is True
        assert result.should_skip is False
        assert mock_context.post_data == {"username": "test_user", "description": "hello"}
        assert mock_context.username == "test_user"

    @patch("GramAddict.core.behaviors.post_data_extraction.extract_post_content")
    def test_execute_failure(self, mock_extract, post_data_extraction, mock_context):
        mock_extract.return_value = None

        result = post_data_extraction.execute(mock_context)

        assert result.executed is False
