|
| 1 | +"""Tests for the CleanSummary class.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from roborock.containers import CleanSummary |
| 8 | +from roborock.devices.traits.clean_summary import CleanSummaryTrait |
| 9 | +from roborock.devices.v1_rpc_channel import V1RpcChannel |
| 10 | +from roborock.exceptions import RoborockException |
| 11 | +from roborock.roborock_typing import RoborockCommand |
| 12 | + |
| 13 | +CLEAN_SUMMARY_DATA = [ |
| 14 | + 1442559, |
| 15 | + 24258125000, |
| 16 | + 296, |
| 17 | + [ |
| 18 | + 1756848207, |
| 19 | + 1754930385, |
| 20 | + 1753203976, |
| 21 | + 1752183435, |
| 22 | + 1747427370, |
| 23 | + 1746204046, |
| 24 | + 1745601543, |
| 25 | + 1744387080, |
| 26 | + 1743528522, |
| 27 | + 1742489154, |
| 28 | + 1741022299, |
| 29 | + 1740433682, |
| 30 | + 1739902516, |
| 31 | + 1738875106, |
| 32 | + 1738864366, |
| 33 | + 1738620067, |
| 34 | + 1736873889, |
| 35 | + 1736197544, |
| 36 | + 1736121269, |
| 37 | + 1734458038, |
| 38 | + ], |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def mock_rpc_channel() -> AsyncMock: |
| 44 | + """Create a mock RPC channel.""" |
| 45 | + mock_channel = AsyncMock(spec=V1RpcChannel) |
| 46 | + # Ensure send_command is an AsyncMock that returns awaitable coroutines |
| 47 | + mock_channel.send_command = AsyncMock() |
| 48 | + return mock_channel |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def clean_summary_trait(mock_rpc_channel: AsyncMock) -> CleanSummaryTrait: |
| 53 | + """Create a CleanSummaryTrait instance with mocked dependencies.""" |
| 54 | + return CleanSummaryTrait(mock_rpc_channel) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def sample_clean_summary() -> CleanSummary: |
| 59 | + """Create a sample CleanSummary for testing.""" |
| 60 | + return CleanSummary( |
| 61 | + clean_area=100, |
| 62 | + clean_time=3600, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_trait_name(clean_summary_trait: CleanSummaryTrait) -> None: |
| 67 | + """Test that the trait has the correct name.""" |
| 68 | + assert clean_summary_trait.name == "clean_summary" |
| 69 | + |
| 70 | + |
| 71 | +async def test_get_clean_summary_success( |
| 72 | + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, sample_clean_summary: CleanSummary |
| 73 | +) -> None: |
| 74 | + """Test successfully getting clean summary.""" |
| 75 | + # Setup mock to return the sample clean summary |
| 76 | + mock_rpc_channel.send_command.return_value = CLEAN_SUMMARY_DATA |
| 77 | + |
| 78 | + # Call the method |
| 79 | + result = await clean_summary_trait.get_clean_summary() |
| 80 | + |
| 81 | + # Verify the result |
| 82 | + assert result.clean_area == 24258125000 |
| 83 | + assert result.clean_time == 1442559 |
| 84 | + assert result.square_meter_clean_area == 24258.1 |
| 85 | + assert result.clean_count == 296 |
| 86 | + assert result.records |
| 87 | + assert len(result.records) == 20 |
| 88 | + assert result.records[0] == 1756848207 |
| 89 | + |
| 90 | + # Verify the RPC call was made correctly |
| 91 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CLEAN_SUMMARY) |
| 92 | + |
| 93 | + |
| 94 | +async def test_get_clean_summary_clean_time_only( |
| 95 | + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, sample_clean_summary: CleanSummary |
| 96 | +) -> None: |
| 97 | + """Test successfully getting clean summary where the response only has the clean time.""" |
| 98 | + |
| 99 | + mock_rpc_channel.send_command.return_value = [1442559] |
| 100 | + |
| 101 | + # Call the method |
| 102 | + result = await clean_summary_trait.get_clean_summary() |
| 103 | + |
| 104 | + # Verify the result |
| 105 | + assert result.clean_area is None |
| 106 | + assert result.clean_time == 1442559 |
| 107 | + assert result.square_meter_clean_area is None |
| 108 | + assert result.clean_count is None |
| 109 | + assert not result.records |
| 110 | + |
| 111 | + # Verify the RPC call was made correctly |
| 112 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CLEAN_SUMMARY) |
| 113 | + |
| 114 | + |
| 115 | +async def test_get_clean_summary_propagates_exception( |
| 116 | + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock |
| 117 | +) -> None: |
| 118 | + """Test that exceptions from RPC channel are propagated in get_clean_summary.""" |
| 119 | + |
| 120 | + # Setup mock to raise an exception |
| 121 | + mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") |
| 122 | + |
| 123 | + # Verify the exception is propagated |
| 124 | + with pytest.raises(RoborockException, match="Communication error"): |
| 125 | + await clean_summary_trait.get_clean_summary() |
0 commit comments