Files
pepperplus-cb/test/unit/agents/actuation/test_robot_gesture_agent.py

252 lines
6.9 KiB
Python

import json
from unittest.mock import AsyncMock, MagicMock
import pytest
import zmq
from control_backend.agents.actuation.robot_gesture_agent import RobotGestureAgent
from control_backend.core.agent_system import InternalMessage
from control_backend.schemas.ri_message import RIEndpoint
@pytest.fixture
def zmq_context(mocker):
mock_context = mocker.patch(
"control_backend.agents.actuation.robot_gesture_agent.azmq.Context.instance"
)
mock_context.return_value = MagicMock()
return mock_context
@pytest.mark.asyncio
async def test_setup_bind(zmq_context, mocker):
fake_socket = zmq_context.return_value.socket.return_value
agent = RobotGestureAgent("robot_gesture", address="tcp://localhost:5556", bind=True)
settings = mocker.patch("control_backend.agents.actuation.robot_gesture_agent.settings")
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
settings.zmq_settings.internal_gesture_rep_adress = "tcp://internal:5557"
agent.add_behavior = MagicMock()
await agent.setup()
fake_socket.bind.assert_any_call("tcp://localhost:5556")
fake_socket.connect.assert_any_call("tcp://internal:1234")
fake_socket.setsockopt.assert_any_call(zmq.SUBSCRIBE, b"command")
fake_socket.setsockopt.assert_any_call(zmq.SUBSCRIBE, b"send_gestures")
assert agent.add_behavior.call_count == 2
@pytest.mark.asyncio
async def test_setup_connect(zmq_context, mocker):
fake_socket = zmq_context.return_value.socket.return_value
agent = RobotGestureAgent("robot_gesture", address="tcp://localhost:5556", bind=False)
settings = mocker.patch("control_backend.agents.actuation.robot_gesture_agent.settings")
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
settings.zmq_settings.internal_gesture_rep_adress = "tcp://internal:5557"
agent.add_behavior = MagicMock()
await agent.setup()
fake_socket.connect.assert_any_call("tcp://localhost:5556")
fake_socket.connect.assert_any_call("tcp://internal:1234")
fake_socket.bind.assert_called()
assert agent.add_behavior.call_count == 2
@pytest.mark.asyncio
async def test_handle_message_valid_gesture_tag():
pubsocket = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_tags=["hello"],
)
agent.pubsocket = pubsocket
payload = {"endpoint": RIEndpoint.GESTURE_TAG, "data": "hello"}
msg = InternalMessage(to="robot", sender="tester", body=json.dumps(payload))
await agent.handle_message(msg)
pubsocket.send_json.assert_awaited_once()
@pytest.mark.asyncio
async def test_handle_message_invalid_gesture_tag():
pubsocket = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_tags=["hello"],
)
agent.pubsocket = pubsocket
payload = {"endpoint": RIEndpoint.GESTURE_TAG, "data": "nope"}
msg = InternalMessage(to="robot", sender="tester", body=json.dumps(payload))
await agent.handle_message(msg)
pubsocket.send_json.assert_not_awaited()
@pytest.mark.asyncio
async def test_handle_message_invalid_payload_logged():
pubsocket = AsyncMock()
agent = RobotGestureAgent("robot_gesture", address="")
agent.pubsocket = pubsocket
agent.logger = MagicMock()
msg = InternalMessage(to="robot", sender="tester", body="not json")
await agent.handle_message(msg)
pubsocket.send_json.assert_not_awaited()
agent.logger.exception.assert_called_once()
@pytest.mark.asyncio
async def test_zmq_command_loop_valid_gesture():
fake_socket = AsyncMock()
async def recv_once():
agent._running = False
return b"command", json.dumps(
{"endpoint": RIEndpoint.GESTURE_TAG, "data": "hello"}
).encode()
fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_tags=["hello"],
)
agent.subsocket = fake_socket
agent.pubsocket = fake_socket
agent._running = True
await agent._zmq_command_loop()
fake_socket.send_json.assert_awaited_once()
@pytest.mark.asyncio
async def test_zmq_command_loop_invalid_tag():
fake_socket = AsyncMock()
async def recv_once():
agent._running = False
return b"command", json.dumps(
{"endpoint": RIEndpoint.GESTURE_TAG, "data": "invalid"}
).encode()
fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_tags=["hello"],
)
agent.subsocket = fake_socket
agent.pubsocket = fake_socket
agent._running = True
await agent._zmq_command_loop()
fake_socket.send_json.assert_not_awaited()
@pytest.mark.asyncio
async def test_zmq_command_loop_ignores_send_gestures_topic():
fake_socket = AsyncMock()
async def recv_once():
agent._running = False
return b"send_gestures", b"{}"
fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", address="")
agent.subsocket = fake_socket
agent.pubsocket = fake_socket
agent._running = True
await agent._zmq_command_loop()
fake_socket.send_json.assert_not_awaited()
@pytest.mark.asyncio
async def test_fetch_gestures_tags():
fake_repsocket = AsyncMock()
async def recv_once():
agent._running = False
return {"type": "tags"}
fake_repsocket.recv_json = recv_once
fake_repsocket.send_json = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_tags=["hello", "yes", "no"],
)
agent.repsocket = fake_repsocket
agent._running = True
await agent._fetch_gestures_loop()
fake_repsocket.send_json.assert_awaited_once_with({"tags": ["hello", "yes", "no"]})
@pytest.mark.asyncio
async def test_fetch_gestures_basic():
fake_repsocket = AsyncMock()
async def recv_once():
agent._running = False
return {"type": "basic"}
fake_repsocket.recv_json = recv_once
fake_repsocket.send_json = AsyncMock()
agent = RobotGestureAgent(
"robot_gesture",
address="",
gesture_basic=["wave", "point"],
)
agent.repsocket = fake_repsocket
agent._running = True
await agent._fetch_gestures_loop()
fake_repsocket.send_json.assert_awaited_once_with({"basic_gestures": ["wave", "point"]})
@pytest.mark.asyncio
async def test_fetch_gestures_unknown_type():
fake_repsocket = AsyncMock()
async def recv_once():
agent._running = False
return {"type": "unknown"}
fake_repsocket.recv_json = recv_once
fake_repsocket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", address="")
agent.repsocket = fake_repsocket
agent._running = True
await agent._fetch_gestures_loop()
fake_repsocket.send_json.assert_awaited_once_with({})