from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors.post_interaction import PostInteractionPlugin


@pytest.fixture
def post_interaction_plugin():
    return PostInteractionPlugin()


@pytest.fixture
def mock_context():
    ctx = MagicMock()
    ctx.shared_state = {"session_outcomes": ["like", "comment"]}
    ctx.device = MagicMock()
    ctx.post_data = {"id": "123"}

    # Mock cognitive stack
    telemetry = MagicMock()
    ctx.cognitive_stack = {"telemetry": telemetry}
    return ctx


class TestPostInteractionPlugin:
    def test_can_activate_enabled(self, post_interaction_plugin, mock_context):
        assert post_interaction_plugin.can_activate(mock_context) is True

    @patch("GramAddict.core.behaviors.post_interaction.humanized_scroll")
    @patch("GramAddict.core.behaviors.post_interaction.sleep")
    def test_execute_success(self, mock_sleep, mock_scroll, post_interaction_plugin, mock_context):
        result = post_interaction_plugin.execute(mock_context)

        assert result.executed is True
        assert result.should_skip is True  # Signals the end of processing for this post

        # Telemetry should be called
        mock_context.cognitive_stack["telemetry"].log_post_interaction.assert_called_once_with(
            {"id": "123"}, ["like", "comment"]
        )

        # Scroll should be called
        mock_scroll.assert_called_once_with(mock_context.device)
