"""Testing Device operations."""

import json
import unittest.mock as mock

from click.testing import CliRunner
import homeassistant_cli.cli as cli


def test_device_list(default_devices) -> None:
    """Test Device List."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):

        runner = CliRunner()
        result = runner.invoke(
            cli.cli,
            ["--output=json", "device", "list"],
            catch_exceptions=False,
        )
        assert result.exit_code == 0

        data = json.loads(result.output)
        assert len(data) == 23


def test_device_list_filter(default_devices) -> None:
    """Test Device List."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):

        runner = CliRunner()
        result = runner.invoke(
            cli.cli,
            ["--output=json", "device", "list", "table"],
            catch_exceptions=False,
        )
        assert result.exit_code == 0

        data = json.loads(result.output)
        assert len(data) == 2
        assert data[0]['name'] == "Kitchen table left"
        assert data[1]['name'] == "Kitchen table right"


def test_device_assign(default_areas, default_devices) -> None:
    """Test basic device assign."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):
        with mock.patch(
            'homeassistant_cli.remote.get_areas', return_value=default_areas
        ):
            with mock.patch(
                'homeassistant_cli.remote.assign_area',
                return_value={'success': True},
            ):

                runner = CliRunner()
                result = runner.invoke(
                    cli.cli,
                    ["device", "assign", "Kitchen", "Kitchen table left"],
                    catch_exceptions=False,
                )
                print(result.output)
                assert result.exit_code == 0
                expected = (
                    "Successfully assigned 'Kitchen'"
                    " to 'Kitchen table left'\n"
                )
                assert result.output == expected

[((12, 9, 14, 5), 'unittest.mock.patch', 'mock.patch', (), '', True, 'import unittest.mock as mock\n'), ((16, 17, 16, 28), 'click.testing.CliRunner', 'CliRunner', ({}, {}), '()', False, 'from click.testing import CliRunner\n'), ((24, 15, 24, 40), 'json.loads', 'json.loads', ({(24, 26, 24, 39): 'result.output'}, {}), '(result.output)', False, 'import json\n'), ((30, 9, 32, 5), 'unittest.mock.patch', 'mock.patch', (), '', True, 'import unittest.mock as mock\n'), ((34, 17, 34, 28), 'click.testing.CliRunner', 'CliRunner', ({}, {}), '()', False, 'from click.testing import CliRunner\n'), ((42, 15, 42, 40), 'json.loads', 'json.loads', ({(42, 26, 42, 39): 'result.output'}, {}), '(result.output)', False, 'import json\n'), ((50, 9, 52, 5), 'unittest.mock.patch', 'mock.patch', (), '', True, 'import unittest.mock as mock\n'), ((53, 13, 55, 9), 'unittest.mock.patch', 'mock.patch', (), '', True, 'import unittest.mock as mock\n'), ((56, 17, 59, 13), 'unittest.mock.patch', 'mock.patch', (), '', True, 'import unittest.mock as mock\n'), ((61, 25, 61, 36), 'click.testing.CliRunner', 'CliRunner', ({}, {}), '()', False, 'from click.testing import CliRunner\n')]