按设备回执确认音量调整完成
This commit is contained in:
98
talkingq-url/tests/test_volume_command_status.py
Normal file
98
talkingq-url/tests/test_volume_command_status.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import pytest
|
||||
|
||||
from handlers.mqtt_handler import TalkingQMQTTService
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_includes_time_and_completes_on_matching_response(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
published = []
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
published.append((topic, payload))
|
||||
return True
|
||||
|
||||
async def fake_schedule_persistence(device_id, label, coro):
|
||||
del device_id, label
|
||||
coro.close()
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_schedule_persistence", fake_schedule_persistence)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: 1717560000000)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
|
||||
assert command["msg_id"] == "002"
|
||||
assert command["time"] == 1717560000000
|
||||
assert command["status"] == "sent"
|
||||
assert published == [
|
||||
(
|
||||
"device/TalkingQ_XQSN00001005/command",
|
||||
{"msg_id": "002", "params": {"level": 70}, "time": 1717560000000},
|
||||
)
|
||||
]
|
||||
|
||||
await service._handle_volume_response(
|
||||
"TalkingQ_XQSN00001005",
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {"current_level": 70},
|
||||
"time": 1717560000000,
|
||||
},
|
||||
)
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", 1717560000000)
|
||||
assert status["status"] == "completed"
|
||||
assert status["current_level"] == 70
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_response_without_time_does_not_complete_command(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
del topic, payload
|
||||
return True
|
||||
|
||||
async def fake_schedule_persistence(device_id, label, coro):
|
||||
del device_id, label
|
||||
coro.close()
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_schedule_persistence", fake_schedule_persistence)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: 1717560000000)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
await service._handle_volume_response(
|
||||
"TalkingQ_XQSN00001005",
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {"current_level": 70},
|
||||
},
|
||||
)
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", command["time"])
|
||||
assert status["status"] == "sent"
|
||||
assert status["current_level"] is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_status_times_out(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
now = 1717560000000
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
del topic, payload
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: now)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
now += 10001
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", command["time"])
|
||||
assert status["status"] == "timeout"
|
||||
assert status["error"] == "device response timeout"
|
||||
Reference in New Issue
Block a user