import unittest

from GramAddict.core.telepathic_engine import TelepathicEngine


class TestPositionRejection(unittest.TestCase):
    def test_repro_following_button_rejection_fix(self):
        """
        VERIFICATION TEST: Verifies that a node at y=2182 (on screen height 2424)
        is NOT rejected anymore by the refined structural guard.
        """
        # Instantiate directly to bypass the autouse 'telepathic_mock' conftest fixture
        # which replaces TelepathicEngine.get_instance with MockTelepathicEngine.
        # _structural_sanity_check is a real method on TelepathicEngine, not on the mock.
        engine = TelepathicEngine()

        # This was the problematic node from the logs
        node = {
            "semantic_string": "description: '2.270following', id context: 'profile header following stacked familiar'",
            "x": 800,
            "y": 2182,
            "resource_id": "com.instagram.android:id/profile_header_following_stacked_familiar",
            "area": 5000,  # Normal button size
        }

        # Test 1: Intent is 'tap following list' (Should pass due to keyword and threshold)
        passed_keyword = engine._structural_sanity_check(node, "tap following list", screen_height=2424)
        print(f"\n[DEBUG] Intent: 'tap following list', Passed: {passed_keyword}")

        # Test 2: Intent is something else, but it's a 'safe' ID (Following)
        passed_id = engine._structural_sanity_check(node, "some other intent", screen_height=2424)
        print(f"\n[DEBUG] Intent: 'some other intent', Passed: {passed_id}")

        self.assertTrue(passed_keyword, "Following button should be allowed for following intent")
        self.assertTrue(passed_id, "Following button should be allowed due to safe ID bypass")
        print("\n[V] VERIFICATION SUCCESSFUL: Position Rejection Fix confirmed.")


if __name__ == "__main__":
    unittest.main()
