from src.gameplay.core.tasks.walkToTargetCreature import WalkToTargetCreatureTask


def test_keeps_chasing_when_target_moves():
    task = WalkToTargetCreatureTask()
    task.targetCreatureCoordinateSinceLastRestart = [2, 0, 7]
    context = {
        'ng_cave': {'targetCreature': {'coordinate': [3, 0, 7]}},
    }

    assert task.shouldRestart(context) is True


def test_attacks_corridor_blocker_before_fleeing_target(mocker):
    target = {
        'coordinate': [3, 0, 7],
        'gameWindowCoordinate': (700, 352),
        'windowCoordinate': (700, 352),
    }
    blocker = {
        'coordinate': [1, 0, 7],
        'gameWindowCoordinate': (480, 352),
        'windowCoordinate': (480, 352),
    }
    mocker.patch(
        'src.gameplay.core.tasks.walkToTargetCreature.generateFloorWalkpoints',
        return_value=[[1, 0, 7], [2, 0, 7], [3, 0, 7]],
    )
    attack = mocker.patch(
        'src.gameplay.core.tasks.walkToTargetCreature.mouse.attackClick',
    )
    context = {
        'ng_cave': {
            'targetCreature': target,
            'holesOrStairs': [],
        },
        'ng_radar': {'coordinate': [0, 0, 7]},
        'gameWindow': {
            'monsters': [target, blocker],
            'image': type('Image', (), {'shape': (704, 960)})(),
        },
    }

    task = WalkToTargetCreatureTask()
    task.calculatePathToTargetCreature(context)

    attack.assert_called_once_with(blocker['windowCoordinate'])
    assert task.targetCreatureCoordinateSinceLastRestart == target['coordinate']
