from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.situational_awareness import SituationalAwarenessEngine, SituationType


@pytest.fixture
def sae():
    device = MagicMock()
    device.app_id = "com.instagram.android"
    device.deviceV2.info = {"screenOn": True}
    return SituationalAwarenessEngine(device)


def test_perceive_normal_with_unknown_keyboard(sae):
    # XML contains Instagram and some unknown keyboard
    xml = """
    <hierarchy>
        <node package="com.instagram.android" resource-id="com.instagram.android:id/button" />
        <node package="com.unknown.keyboard" resource-id="com.unknown.keyboard:id/key" />
    </hierarchy>
    """

    # We shouldn't call LLM for foreign app
    with patch("GramAddict.core.llm_provider.query_telepathic_llm") as mock_llm:
        # Let's mock ScreenMemoryDB to return NORMAL
        with patch("GramAddict.core.qdrant_memory.ScreenMemoryDB.get_screen_type", return_value="NORMAL"):
            res = sae.perceive(xml)
            assert res == SituationType.NORMAL
            # The LLM for foreign app should NOT have been called.
            mock_llm.assert_not_called()
