import importlib.util
from pathlib import Path
from unittest.mock import Mock

import pytest


def load_numlock_command():
    command_path = (
        Path(__file__).parents[2]
        / "src"
        / "fenrirscreenreader"
        / "commands"
        / "onKeyInput"
        / "80500-numlock.py"
    )
    spec = importlib.util.spec_from_file_location(
        "numlock_command", command_path
    )
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module.command


@pytest.mark.unit
def test_numlock_off_can_be_announced_from_release_event():
    output_manager = Mock()
    command = load_numlock_command()()
    command.initialize(
        {
            "input": {
                "old_num_lock": True,
                "new_num_lock": False,
                "curr_input": [],
                "prev_input": ["KEY_NUMLOCK"],
            },
            "runtime": {"OutputManager": output_manager},
        }
    )

    command.run()

    output_manager.present_text.assert_called_once()
    assert output_manager.present_text.call_args.args[0] == "Numlock off"


@pytest.mark.unit
def test_numlock_command_ignores_non_numlock_release():
    output_manager = Mock()
    command = load_numlock_command()()
    command.initialize(
        {
            "input": {
                "old_num_lock": True,
                "new_num_lock": False,
                "curr_input": [],
                "prev_input": ["KEY_KP1"],
            },
            "runtime": {"OutputManager": output_manager},
        }
    )

    command.run()

    output_manager.present_text.assert_not_called()
