from GramAddict.core.telepathic_engine import TelepathicEngine


def test_structural_guard_rejects_own_story_for_post_username():
    """
    TDD Test: Reproduces the bug where Telepathic Engine might select the user's
    OWN profile picture ("Your Story" in the Home Feed tray) when the intent
    is to tap the post author's username.
    """
    engine = TelepathicEngine()
    screen_height = 2400

    # Mock node representing the user's "Your Story" circle at the top
    # It contains "story" or "your story", has low Y (top of screen)
    your_story_node = {
        "semantic_string": "description: 'Your Story', id context: 'row feed photo profile imageview'",
        "y": 250,  # Top story tray
        "class_name": "android.widget.ImageView",
    }

    # Intent
    intent = "tap post username"

    # Expected behavior: Structural sanity check must REJECT this node to prevent
    # clicking our own story/profile
    is_valid = engine._structural_sanity_check(your_story_node, intent, screen_height)

    assert is_valid is False, "Structural Guard failed to reject 'Your Story' when looking for 'post username'."


def test_structural_guard_accepts_actual_post_username():
    engine = TelepathicEngine()
    screen_height = 2400

    actual_post_node = {
        "semantic_string": "text: 'estherabad9', id context: 'row feed photo profile name'",
        "y": 1200,  # Middle of screen (feed post header)
        "area": 5000,
        "class_name": "android.widget.TextView",
    }

    intent = "tap post username"

    is_valid = engine._structural_sanity_check(actual_post_node, intent, screen_height)

    assert is_valid is True, "Structural Guard incorrectly rejected the actual post username."


def test_structural_guard_rejects_own_username_story():
    """
    TDD Test: Reproduces 2026-04-16 23:18 bug where bot selected 'marisaundmarc's story'
    instead of an unseen story from ANOTHER user.
    """
    engine = TelepathicEngine()
    screen_height = 2400

    # Simulate current user is marisaundmarc
    engine._get_current_username = lambda: "marisaundmarc"

    # Mock node representing the user's OWN story, which contains their username
    own_story_node = {
        "semantic_string": "description: 'marisaundmarc\\'s story, 0 of 27, Unseen.', id context: 'avatar image view'",
        "y": 250,  # Top story tray
        "class_name": "android.widget.ImageView",
    }

    intent = "profile picture avatar story ring"

    # Should reject the user's own profile because clicking it means we edit/view our own story
    # instead of doing interactions with prospects.
    is_valid = engine._structural_sanity_check(own_story_node, intent, screen_height)

    assert is_valid is False, "Structural Guard failed to reject the bot's OWN username story."


def test_structural_reels_first_grid_item_y_coords():
    """
    TDD Test: Reels viewer layout has grid items that are structurally valid.
    Ensures that relative Y coordinates (percentage of screen height) correctly
    allow valid grid items and block hallucinations.
    """
    engine = TelepathicEngine()
    screen_height = 2400

    # Valid first grid item in a profile's reel tab, usually around y=700 to 1200
    valid_grid_node = {
        "semantic_string": "description: 'reel, 1 of 20', id context: 'image button'",
        "y": 800,  # well within safe zone, ~33%
        "area": 40000,
        "class_name": "android.widget.ImageView",
    }

    # Hallucinated navigation tab node pretending to be "Home" around y=1200 (middle of screen)
    hallucinated_nav_node = {
        "semantic_string": "description: 'Home', id context: 'tab'",
        "y": 1200,  # 50% height
        "area": 1000,
        "class_name": "android.view.View",
    }

    intent_grid = "first grid item"
    intent_nav = "tap home tab"

    is_valid_grid = engine._structural_sanity_check(valid_grid_node, intent_grid, screen_height)
    assert is_valid_grid is True, "Structural Guard rejected a valid reels grid item."

    # The hallucinated nav node should be rejected because navigation tabs belong at the bottom!
    # Currently it might fail if we don't have relative coordinate checks!
    is_valid_nav = engine._structural_sanity_check(hallucinated_nav_node, intent_nav, screen_height)
    assert (
        is_valid_nav is False
    ), "Structural Guard failed to reject a hallucinated navigation tab in the middle of the screen."
