from unittest.mock import MagicMock

from GramAddict.core.bot_flow import _wait_for_profile_loaded


def test_wait_for_profile_loaded_success():
    device = MagicMock()
    # First dump is empty, second dump has profile_header
    device.dump_hierarchy.side_effect = [
        "<hierarchy></hierarchy>",
        '<hierarchy><node resource-id="com.instagram.android:id/profile_header" /></hierarchy>',
    ]

    assert _wait_for_profile_loaded(device, timeout=2)
    assert device.dump_hierarchy.call_count == 2


def test_wait_for_profile_loaded_timeout():
    device = MagicMock()
    # Always reel
    device.dump_hierarchy.return_value = '<hierarchy><node resource-id="clips_video_container" /></hierarchy>'

    assert not _wait_for_profile_loaded(device, timeout=1)
    assert device.dump_hierarchy.call_count >= 1
