from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors.repost import RepostPlugin


@pytest.fixture
def repost_plugin():
    return RepostPlugin()


@pytest.fixture
def mock_context():
    ctx = MagicMock()
    ctx.configs = MagicMock()
    ctx.configs.get_plugin_config.return_value = {"percentage": 100}
    ctx.shared_state = {"res_score": 1.0}
    ctx.context_xml = "<xml></xml>"
    ctx.device = MagicMock()

    mock_nav = MagicMock()
    mock_nav.do.return_value = True
    ctx.cognitive_stack = {"nav_graph": mock_nav}
    return ctx


class TestRepostPlugin:
    @patch("GramAddict.core.behaviors.repost.random.random", return_value=0.1)
    def test_can_activate_enabled(self, mock_random, repost_plugin, mock_context):
        assert repost_plugin.can_activate(mock_context) is True

    def test_can_activate_disabled_via_config(self, repost_plugin, mock_context):
        mock_context.configs.get_plugin_config.return_value = {"percentage": 0}
        assert repost_plugin.can_activate(mock_context) is False

    def test_execute_success(self, repost_plugin, mock_context):
        result = repost_plugin.execute(mock_context)

        assert result.executed is True
        assert result.interactions == 1

        mock_context.cognitive_stack["nav_graph"].do.assert_called_once_with("share to story")

    def test_execute_fails_no_add_to_story(self, repost_plugin, mock_context):
        mock_context.cognitive_stack["nav_graph"].do.return_value = False

        result = repost_plugin.execute(mock_context)

        assert result.executed is False
