11from __future__ import annotations
22
3+ from typing import TYPE_CHECKING
4+ from unittest .mock import patch
5+
36import pytest
47
58from deebot_client .command import CommandResult
6- from deebot_client .commands .xml import GetLifeSpan
9+ from deebot_client .commands .xml import GetLifeSpan , ResetLifeSpan
710from deebot_client .events import LifeSpan , LifeSpanEvent
811from deebot_client .message import HandlingState
912from tests .commands import assert_command
1013
11- from . import get_request_xml
14+ from . import (
15+ assert_execute_command ,
16+ get_failure_body ,
17+ get_request_xml ,
18+ get_success_body ,
19+ )
20+
21+ if TYPE_CHECKING :
22+ from deebot_client .event_bus import EventBus
1223
1324
1425@pytest .mark .parametrize (
1526 ("component_type" , "lifespan_type" , "left" , "total" , "expected_event" ),
1627 [
1728 ("Brush" , LifeSpan .BRUSH , 50 , 100 , LifeSpanEvent (LifeSpan .BRUSH , 50 , 50 )),
29+ (
30+ "Brush" ,
31+ LifeSpan .BRUSH .xml_value ,
32+ 50 ,
33+ 100 ,
34+ LifeSpanEvent (LifeSpan .BRUSH , 50 , 50 ),
35+ ),
1836 (
1937 "DustCaseHeap" ,
2038 LifeSpan .DUST_CASE_HEAP ,
3351)
3452async def test_get_life_span (
3553 component_type : str ,
36- lifespan_type : LifeSpan ,
54+ lifespan_type : LifeSpan | str ,
3755 left : int ,
3856 total : int ,
3957 expected_event : LifeSpanEvent ,
4058) -> None :
41- json = get_request_xml (
59+ xml = get_request_xml (
4260 f"<ctl ret='ok' type='{ component_type } ' left='{ left } ' total='{ total } '/>"
4361 )
44- await assert_command (GetLifeSpan (lifespan_type ), json , expected_event )
62+ await assert_command (GetLifeSpan (lifespan_type ), xml , expected_event )
4563
4664
4765@pytest .mark .parametrize (
@@ -57,3 +75,42 @@ async def test_get_life_span_error(xml: str) -> None:
5775 None ,
5876 command_result = CommandResult (HandlingState .ANALYSE_LOGGED ),
5977 )
78+
79+
80+ @pytest .mark .parametrize (
81+ ("command" , "args" ),
82+ [
83+ (ResetLifeSpan (LifeSpan .FILTER ), {"type" : LifeSpan .FILTER .xml_value }),
84+ (ResetLifeSpan (LifeSpan .FILTER .xml_value ), {"type" : LifeSpan .FILTER .xml_value }),
85+ (
86+ ResetLifeSpan .create_from_mqtt (b'<ctl type="Brush" />' ),
87+ {"type" : LifeSpan .BRUSH .xml_value },
88+ ),
89+ ],
90+ )
91+ async def test_ResetLifeSpan (command : ResetLifeSpan , args : dict [str , str ]) -> None :
92+ await assert_execute_command (command , args )
93+
94+
95+ def test_ResetLifeSpan_invokes_refresh (event_bus : EventBus ) -> None :
96+ command = ResetLifeSpan (LifeSpan .FILTER )
97+ success_response = get_success_body ()
98+
99+ with patch .object (
100+ event_bus , "request_refresh" , return_value = None
101+ ) as mock_request_refresh :
102+ command .handle_mqtt_p2p (event_bus , success_response )
103+
104+ mock_request_refresh .assert_called_with (LifeSpanEvent )
105+
106+
107+ def test_ResetLifeSpan_not_invokes_refresh (event_bus : EventBus ) -> None :
108+ command = ResetLifeSpan (LifeSpan .FILTER )
109+ failure_response = get_failure_body ()
110+
111+ with patch .object (
112+ event_bus , "request_refresh" , return_value = None
113+ ) as mock_request_refresh :
114+ command .handle_mqtt_p2p (event_bus , failure_response )
115+
116+ mock_request_refresh .assert_not_called ()
0 commit comments