Add Pico communication service
This commit is contained in:
@@ -150,3 +150,81 @@ def test_uart_client_handles_invalid_json():
|
||||
|
||||
assert messages[0]["type"] == "error"
|
||||
assert messages[0]["message"] == "invalid_json"
|
||||
|
||||
|
||||
def test_communication_service_requests_status():
|
||||
from comms.uart_client import UartClient
|
||||
from comms.communication_service import CommunicationService
|
||||
|
||||
fake = FakeUart()
|
||||
state = AppState()
|
||||
service = CommunicationService(UartClient(fake), state)
|
||||
|
||||
service.request_status()
|
||||
|
||||
assert fake.writes == [b'{"type":"status_request"}\n']
|
||||
|
||||
|
||||
def test_communication_service_sends_relay_command():
|
||||
from comms.uart_client import UartClient
|
||||
from comms.communication_service import CommunicationService
|
||||
|
||||
fake = FakeUart()
|
||||
state = AppState()
|
||||
service = CommunicationService(UartClient(fake), state)
|
||||
|
||||
service.set_relay("fridge", False)
|
||||
|
||||
assert fake.writes == [b'{"type":"set_relay","relay":"fridge","enabled":false}\n']
|
||||
|
||||
|
||||
def test_communication_service_updates_state_from_status():
|
||||
from comms.uart_client import UartClient
|
||||
from comms.communication_service import CommunicationService
|
||||
|
||||
fake = FakeUart()
|
||||
fake.read_chunks = [
|
||||
b'{"type":"status_response","timestamp":99,"battery":{"soc":75},"network":{"uart_connected":true}}\n'
|
||||
]
|
||||
|
||||
state = AppState()
|
||||
service = CommunicationService(UartClient(fake), state)
|
||||
|
||||
messages = service.poll()
|
||||
|
||||
assert messages[0]["type"] == "status_response"
|
||||
assert state.last_status_timestamp == 99
|
||||
assert state.battery["soc"] == 75
|
||||
assert state.network["uart_connected"] is True
|
||||
|
||||
|
||||
def test_communication_service_updates_state_from_relay_response():
|
||||
from comms.uart_client import UartClient
|
||||
from comms.communication_service import CommunicationService
|
||||
|
||||
fake = FakeUart()
|
||||
fake.read_chunks = [
|
||||
b'{"type":"relay_response","relay":"starlink","enabled":true,"ok":true}\n'
|
||||
]
|
||||
|
||||
state = AppState()
|
||||
service = CommunicationService(UartClient(fake), state)
|
||||
|
||||
service.poll()
|
||||
|
||||
assert state.relays["starlink"] is True
|
||||
|
||||
|
||||
def test_communication_service_records_errors():
|
||||
from comms.uart_client import UartClient
|
||||
from comms.communication_service import CommunicationService
|
||||
|
||||
fake = FakeUart()
|
||||
fake.read_chunks = [b'{"type":"error","message":"invalid_json"}\n']
|
||||
|
||||
state = AppState()
|
||||
service = CommunicationService(UartClient(fake), state)
|
||||
|
||||
service.poll()
|
||||
|
||||
assert state.last_error["message"] == "invalid_json"
|
||||
|
||||
Reference in New Issue
Block a user