import numpy as np

from src.repositories.battleList import core


def test_current_battle_list_layout_falls_back_to_shifted_name_row(mocker):
    legacy = np.zeros((1, 115), dtype=np.uint8)
    current = np.zeros((1, 115), dtype=np.uint8)
    expected = np.zeros(115, dtype=np.uint8)
    expected[4:8] = 192
    current[0] = expected
    current[0, 8] = 192
    mocker.patch.object(core, 'getCreaturesNamesImages', return_value=legacy)
    mocker.patch.object(core, 'getCurrentCreaturesNamesImages', return_value=current)
    mocker.patch.object(core, 'creaturesNamesImagesHashes', {})
    mocker.patch.object(core, 'currentCreaturesNamesImagesHashes', {})
    mocker.patch.object(core, 'currentCreaturesNames', ['Cave Rat'])
    mocker.patch.object(core, 'currentCreaturesNamesImages', np.asarray([expected]))

    assert list(core.getCreaturesNames(object(), 1)) == ['Cave Rat']


def test_current_battle_list_layout_handles_brightness_and_22px_rows(mocker):
    empty = np.zeros((3, 115), dtype=np.uint8)
    expected = np.zeros(115, dtype=np.uint8)
    expected[4:8] = 192
    content = np.zeros((67, 156), dtype=np.uint8)
    for y in (8, 30, 53):
        content[y, 27:31] = 170
    mocker.patch.object(core, 'getCreaturesNamesImages', return_value=empty)
    mocker.patch.object(core, 'getCurrentCreaturesNamesImages', return_value=empty)
    mocker.patch.object(core, 'creaturesNamesImagesHashes', {})
    mocker.patch.object(core, 'currentCreaturesNamesImagesHashes', {})
    mocker.patch.object(core, 'currentCreaturesNames', ['Cave Rat'])
    mocker.patch.object(core, 'currentCreaturesNamesImages', np.asarray([expected]))

    assert list(core.getCreaturesNames(content, 3)) == [
        'Cave Rat', 'Cave Rat', 'Cave Rat',
    ]
