from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors import BehaviorContext
from GramAddict.core.behaviors.darwin_dwell import DarwinDwellPlugin


@pytest.fixture
def darwin_dwell():
    return DarwinDwellPlugin()


@pytest.fixture
def mock_context():
    ctx = BehaviorContext(
        device=MagicMock(),
        configs=MagicMock(),
        session_state=MagicMock(),
        cognitive_stack={
            "darwin": MagicMock(),
            "nav_graph": MagicMock(),
            "zero_latency_engine": MagicMock(),
            "resonance": MagicMock(),
        },
        context_xml="<xml></xml>",
        post_data={"description": "test desc"},
        username="test_user",
        shared_state={"res_score": 0.8},
    )
    return ctx


class TestDarwinDwellPlugin:
    def test_execute_with_darwin(self, darwin_dwell, mock_context):
        result = darwin_dwell.execute(mock_context)

        assert result.executed is True
        mock_darwin = mock_context.cognitive_stack["darwin"]
        mock_darwin.execute_micro_wobble.assert_called_once_with(mock_context.device)
        mock_darwin.execute_proof_of_resonance.assert_called_once()

    @patch("GramAddict.core.behaviors.darwin_dwell.sleep")
    def test_execute_without_darwin(self, mock_sleep, darwin_dwell, mock_context):
        mock_context.cognitive_stack.pop("darwin")

        result = darwin_dwell.execute(mock_context)

        assert result.executed is True
        mock_sleep.assert_called_once()
