from src.gameplay.core.tasks.bbot import BBotActionTask, BBotResetBackpacksTask


def test_condition_jumps_to_imported_label(mocker):
    mocker.patch('src.gameplay.core.tasks.bbot.getCapacity', return_value=40)
    waypoints = [
        {
            'label': '',
            'options': {
                'action': 'condition',
                'checks': [{
                    'source': 'capacity', 'value': None,
                    'operator': '<', 'target': 50,
                }],
                'trueLabel': 'Refill',
                'falseLabel': 'Hunt',
            },
        },
        {'label': 'Hunt', 'options': {}},
        {'label': 'Refill', 'options': {}},
    ]
    context = {
        'ng_screenshot': object(),
        'ng_cave': {'waypoints': {'items': waypoints, 'currentIndex': 0, 'state': {}}},
    }
    task = BBotActionTask(waypoints[0])

    task.do(context)
    task.onComplete(context)

    assert context['ng_cave']['waypoints']['currentIndex'] == 2
    assert context['ng_cave']['waypoints']['state'] is None


def test_reset_backpacks_reopens_main_and_loot():
    task = BBotResetBackpacksTask()
    context = {
        'ng_backpacks': {'main': 'Camouflage Backpack', 'loot': 'Crown Backpack'},
    }

    task.onBeforeStart(context)

    assert [child.name for child in task.tasks] == [
        'closeContainer', 'closeContainer', 'openBackpack', 'expandBackpack',
        'scrollToItem', 'openBackpack', 'setNextWaypoint',
    ]
