from unittest.mock import patch

import pytest

from GramAddict.core.perception.action_memory import ActionMemory
from GramAddict.core.perception.spatial_parser import SpatialNode


@pytest.fixture
def memory():
    with patch("GramAddict.core.qdrant_memory.UIMemoryDB") as MockDB:
        mock_db = MockDB.return_value
        yield ActionMemory(ui_memory=mock_db)


def test_track_click_stores_memory(memory):
    node = SpatialNode(bounds=(0, 0, 10, 10), content_desc="Test Node")
    memory.track_click("tap test", node)

    assert memory._last_click_context is not None
    assert memory._last_click_context["intent"] == "tap test"


def test_confirm_click_boosts_confidence(memory):
    node = SpatialNode(bounds=(0, 0, 10, 10), content_desc="Test Node")
    memory.track_click("tap test", node)
    memory.confirm_click()

    memory.ui_memory.boost_confidence.assert_called_once()
    assert memory._last_click_context is None


def test_reject_click_decays_confidence(memory):
    node = SpatialNode(bounds=(0, 0, 10, 10), content_desc="Test Node")
    memory.track_click("tap test", node)
    memory.reject_click()

    memory.ui_memory.decay_confidence.assert_called_once()
    assert memory._last_click_context is None
