File: test_network_info.py

package info (click to toggle)
python-roborock 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,480 kB
  • sloc: python: 16,602; makefile: 17; sh: 6
file content (63 lines) | stat: -rw-r--r-- 2,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Tests for the NetworkInfoTrait class."""

from unittest.mock import AsyncMock

import pytest

from roborock.data import NetworkInfo
from roborock.devices.cache import Cache, DeviceCache
from roborock.devices.device import RoborockDevice
from roborock.devices.traits.v1.network_info import NetworkInfoTrait
from roborock.roborock_typing import RoborockCommand
from tests.mock_data import NETWORK_INFO

DEVICE_UID = "abc123"


@pytest.fixture
def network_info_trait(device: RoborockDevice) -> NetworkInfoTrait:
    """Create a NetworkInfoTrait instance with mocked dependencies."""
    assert device.v1_properties
    return device.v1_properties.network_info


async def test_network_info_from_cache(
    network_info_trait: NetworkInfoTrait, roborock_cache: Cache, mock_rpc_channel: AsyncMock
) -> None:
    """Test that network info is read from the cache."""
    device_cache = DeviceCache(DEVICE_UID, roborock_cache)
    device_cache_data = await device_cache.get()
    device_cache_data.network_info = NetworkInfo.from_dict(NETWORK_INFO)
    await device_cache.set(device_cache_data)

    await network_info_trait.refresh()

    assert network_info_trait.ip == "1.1.1.1"
    assert network_info_trait.mac == "aa:bb:cc:dd:ee:ff"
    assert network_info_trait.bssid == "aa:bb:cc:dd:ee:ff"
    assert network_info_trait.rssi == -50
    mock_rpc_channel.send_command.assert_not_called()


async def test_network_info_from_device(
    network_info_trait: NetworkInfoTrait, roborock_cache: Cache, mock_rpc_channel: AsyncMock
) -> None:
    """Test that network info is fetched from the device when not in cache."""
    mock_rpc_channel.send_command.return_value = {
        **NETWORK_INFO,
        "ip": "2.2.2.2",
    }

    await network_info_trait.refresh()

    assert network_info_trait.ip == "2.2.2.2"
    assert network_info_trait.mac == "aa:bb:cc:dd:ee:ff"
    assert network_info_trait.bssid == "aa:bb:cc:dd:ee:ff"
    assert network_info_trait.rssi == -50
    mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_NETWORK_INFO)

    # Verify it's now in the cache
    device_cache = DeviceCache(DEVICE_UID, roborock_cache)
    device_cache_data = await device_cache.get()
    assert device_cache_data.network_info
    assert device_cache_data.network_info.ip == "2.2.2.2"