import numpy as np

from src.gameplay.core.middlewares.gameWindow import setGameWindowCreaturesMiddleware


def test_empty_battle_list_clears_game_window_creatures(mocker):
    category = mocker.patch(
        'src.gameplay.core.middlewares.gameWindow.getBeingAttackedCreatureCategory',
    )
    context = {
        'ng_battleList': {
            'creatures': [],
            'beingAttackedCreatureCategory': 'monster',
        },
        'gameWindow': {
            'creatures': [{}],
            'monsters': [{}],
            'players': [{}],
        },
    }

    assert setGameWindowCreaturesMiddleware(context) is context
    assert context['ng_battleList']['beingAttackedCreatureCategory'] is None
    assert context['gameWindow']['creatures'] == []
    assert context['gameWindow']['monsters'] == []
    assert context['gameWindow']['players'] == []
    category.assert_not_called()


def test_numpy_battle_list_is_supported(mocker):
    creatures = np.array([('Wasp', False)], dtype=[('name', 'U16'), ('isBeingAttacked', '?')])
    mocker.patch(
        'src.gameplay.core.middlewares.gameWindow.getBeingAttackedCreatureCategory',
        return_value='monster',
    )
    get_creatures = mocker.patch(
        'src.gameplay.core.middlewares.gameWindow.getCreatures',
        return_value=[],
    )
    context = {
        'ng_battleList': {
            'creatures': creatures,
            'beingAttackedCreatureCategory': None,
        },
        'ng_radar': {'coordinate': (32357, 32229, 7)},
        'ng_comingFromDirection': None,
        'gameWindow': {
            'coordinate': (303, 6),
            'image': object(),
            'walkedPixelsInSqm': 0,
            'creatures': [],
            'monsters': [],
            'players': [],
        },
    }

    assert setGameWindowCreaturesMiddleware(context) is context
    get_creatures.assert_called_once()
