from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors import BehaviorContext
from GramAddict.core.behaviors.close_friends_guard import CloseFriendsGuardPlugin


@pytest.fixture
def cf_guard():
    plugin = CloseFriendsGuardPlugin()
    return plugin


@pytest.fixture
def mock_context():
    device = MagicMock()
    configs = MagicMock()
    session_state = MagicMock()

    ctx = BehaviorContext(device=device, configs=configs, session_state=session_state, cognitive_stack={})
    return ctx


class TestCloseFriendsGuardPlugin:
    def test_can_activate(self, cf_guard, mock_context):
        mock_context.context_xml = "<xml>enge freunde</xml>"
        assert cf_guard.can_activate(mock_context) is True

        mock_context.context_xml = "<xml>regular post</xml>"
        assert cf_guard.can_activate(mock_context) is False

        cf_guard._enabled = False
        assert cf_guard.can_activate(mock_context) is False

    @patch("GramAddict.core.behaviors.close_friends_guard.sleep")
    @patch("GramAddict.core.behaviors.close_friends_guard.humanized_scroll")
    def test_execute_has_badge(self, mock_scroll, mock_sleep, cf_guard, mock_context):
        mock_context.device.dump_hierarchy.return_value = "<xml>post enge freunde badge</xml>"

        result = cf_guard.execute(mock_context)

        assert result.executed is True
        mock_scroll.assert_called_once_with(mock_context.device, is_skip=True)
        mock_sleep.assert_called_once()
