Merge remote-tracking branch 'origin/dev' into demo
This commit is contained in:
@@ -7,19 +7,21 @@ import zmq
|
||||
from control_backend.agents.ri_command_agent import RICommandAgent
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def zmq_context(mocker):
|
||||
mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance")
|
||||
mock_context.return_value = MagicMock()
|
||||
return mock_context
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_bind(monkeypatch):
|
||||
async def test_setup_bind(zmq_context, mocker):
|
||||
"""Test setup with bind=True"""
|
||||
fake_socket = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_command_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
|
||||
agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=True)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_command_agent.settings",
|
||||
MagicMock(zmq_settings=MagicMock(internal_comm_address="tcp://internal:1234")),
|
||||
)
|
||||
settings = mocker.patch("control_backend.agents.ri_command_agent.settings")
|
||||
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
|
||||
|
||||
await agent.setup()
|
||||
|
||||
@@ -34,18 +36,13 @@ async def test_setup_bind(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_connect(monkeypatch):
|
||||
async def test_setup_connect(zmq_context, mocker):
|
||||
"""Test setup with bind=False"""
|
||||
fake_socket = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_command_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
|
||||
agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_command_agent.settings",
|
||||
MagicMock(zmq_settings=MagicMock(internal_comm_address="tcp://internal:1234")),
|
||||
)
|
||||
settings = mocker.patch("control_backend.agents.ri_command_agent.settings")
|
||||
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
|
||||
|
||||
await agent.setup()
|
||||
|
||||
|
||||
@@ -84,21 +84,23 @@ def fake_json_invalid_id_negototiate():
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def zmq_context(mocker):
|
||||
mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance")
|
||||
mock_context.return_value = MagicMock()
|
||||
return mock_context
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||
async def test_setup_creates_socket_and_negotiate_1(zmq_context):
|
||||
"""
|
||||
Test the setup of the communication agent
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_1()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
@@ -128,20 +130,15 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||
async def test_setup_creates_socket_and_negotiate_2(zmq_context):
|
||||
"""
|
||||
Test the setup of the communication agent
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_2()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
@@ -171,20 +168,15 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||
async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog):
|
||||
"""
|
||||
Test the functionality of setup with incorrect negotiation message
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_wrong_negototiate_1()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
|
||||
# We are sending wrong negotiation info to the communication agent,
|
||||
@@ -215,20 +207,15 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||
async def test_setup_creates_socket_and_negotiate_4(zmq_context):
|
||||
"""
|
||||
Test the setup of the communication agent with different bind value
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_3()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
@@ -258,20 +245,15 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||
async def test_setup_creates_socket_and_negotiate_5(zmq_context):
|
||||
"""
|
||||
Test the setup of the communication agent
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_4()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
@@ -301,20 +283,15 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||
async def test_setup_creates_socket_and_negotiate_6(zmq_context):
|
||||
"""
|
||||
Test the setup of the communication agent
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_correct_negototiate_5()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
@@ -344,20 +321,15 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
|
||||
async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog):
|
||||
"""
|
||||
Test the functionality of setup with incorrect id
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = fake_json_invalid_id_negototiate()
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Mock RICommandAgent agent startup
|
||||
|
||||
# We are sending wrong negotiation info to the communication agent,
|
||||
@@ -385,20 +357,15 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
|
||||
async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog):
|
||||
"""
|
||||
Test the functionality of setup with incorrect negotiation message
|
||||
"""
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
|
||||
|
||||
# Mock context.socket to return our fake socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
) as MockCommandAgent:
|
||||
@@ -480,8 +447,8 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_behaviour_timeout(caplog):
|
||||
fake_socket = AsyncMock()
|
||||
async def test_listen_behaviour_timeout(zmq_context, caplog):
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
# recv_json will never resolve, simulate timeout
|
||||
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
|
||||
@@ -529,16 +496,12 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||
fake_socket = MagicMock()
|
||||
async def test_setup_unexpected_exception(zmq_context, caplog):
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
# Simulate unexpected exception during recv_json()
|
||||
fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!"))
|
||||
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
agent = RICommunicationAgent(
|
||||
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||
)
|
||||
@@ -551,9 +514,9 @@ async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||
async def test_setup_unpacking_exception(zmq_context, caplog):
|
||||
# --- Arrange ---
|
||||
fake_socket = MagicMock()
|
||||
fake_socket = zmq_context.return_value.socket.return_value
|
||||
fake_socket.send_json = AsyncMock()
|
||||
|
||||
# Make recv_json return malformed negotiation data to trigger unpacking exception
|
||||
@@ -563,11 +526,6 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||
} # missing 'port' and 'bind'
|
||||
fake_socket.recv_json = AsyncMock(return_value=malformed_data)
|
||||
|
||||
# Patch context.socket
|
||||
monkeypatch.setattr(
|
||||
"control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket
|
||||
)
|
||||
|
||||
# Patch RICommandAgent so it won't actually start
|
||||
with patch(
|
||||
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
|
||||
|
||||
@@ -10,7 +10,9 @@ from control_backend.agents.vad_agent import VADAgent
|
||||
|
||||
@pytest.fixture
|
||||
def zmq_context(mocker):
|
||||
return mocker.patch("control_backend.agents.vad_agent.zmq_context")
|
||||
mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance")
|
||||
mock_context.return_value = MagicMock()
|
||||
return mock_context
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -54,13 +56,18 @@ def test_in_socket_creation(zmq_context, do_bind: bool):
|
||||
|
||||
assert vad_agent.audio_in_socket is not None
|
||||
|
||||
zmq_context.socket.assert_called_once_with(zmq.SUB)
|
||||
zmq_context.socket.return_value.setsockopt_string.assert_called_once_with(zmq.SUBSCRIBE, "")
|
||||
zmq_context.return_value.socket.assert_called_once_with(zmq.SUB)
|
||||
zmq_context.return_value.socket.return_value.setsockopt_string.assert_called_once_with(
|
||||
zmq.SUBSCRIBE,
|
||||
"",
|
||||
)
|
||||
|
||||
if do_bind:
|
||||
zmq_context.socket.return_value.bind.assert_called_once_with("tcp://*:12345")
|
||||
zmq_context.return_value.socket.return_value.bind.assert_called_once_with("tcp://*:12345")
|
||||
else:
|
||||
zmq_context.socket.return_value.connect.assert_called_once_with("tcp://localhost:12345")
|
||||
zmq_context.return_value.socket.return_value.connect.assert_called_once_with(
|
||||
"tcp://localhost:12345"
|
||||
)
|
||||
|
||||
|
||||
def test_out_socket_creation(zmq_context):
|
||||
@@ -73,8 +80,8 @@ def test_out_socket_creation(zmq_context):
|
||||
|
||||
assert vad_agent.audio_out_socket is not None
|
||||
|
||||
zmq_context.socket.assert_called_once_with(zmq.PUB)
|
||||
zmq_context.socket.return_value.bind_to_random_port.assert_called_once()
|
||||
zmq_context.return_value.socket.assert_called_once_with(zmq.PUB)
|
||||
zmq_context.return_value.socket.return_value.bind_to_random_port.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -83,7 +90,9 @@ async def test_out_socket_creation_failure(zmq_context):
|
||||
Test setup failure when the audio output socket cannot be created.
|
||||
"""
|
||||
with patch.object(Agent, "stop", new_callable=AsyncMock) as mock_super_stop:
|
||||
zmq_context.socket.return_value.bind_to_random_port.side_effect = zmq.ZMQBindError
|
||||
zmq_context.return_value.socket.return_value.bind_to_random_port.side_effect = (
|
||||
zmq.ZMQBindError
|
||||
)
|
||||
vad_agent = VADAgent("tcp://localhost:12345", False)
|
||||
|
||||
await vad_agent.setup()
|
||||
@@ -98,11 +107,14 @@ async def test_stop(zmq_context, transcription_agent):
|
||||
Test that when the VAD agent is stopped, the sockets are closed correctly.
|
||||
"""
|
||||
vad_agent = VADAgent("tcp://localhost:12345", False)
|
||||
zmq_context.socket.return_value.bind_to_random_port.return_value = random.randint(1000, 10000)
|
||||
zmq_context.return_value.socket.return_value.bind_to_random_port.return_value = random.randint(
|
||||
1000,
|
||||
10000,
|
||||
)
|
||||
|
||||
await vad_agent.setup()
|
||||
await vad_agent.stop()
|
||||
|
||||
assert zmq_context.socket.return_value.close.call_count == 2
|
||||
assert zmq_context.return_value.socket.return_value.close.call_count == 2
|
||||
assert vad_agent.audio_in_socket is None
|
||||
assert vad_agent.audio_out_socket is None
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
@@ -16,7 +16,6 @@ def app():
|
||||
"""
|
||||
app = FastAPI()
|
||||
app.include_router(command.router)
|
||||
app.state.internal_comm_socket = MagicMock() # mock ZMQ socket
|
||||
return app
|
||||
|
||||
|
||||
@@ -26,32 +25,30 @@ def client(app):
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_receive_command_endpoint(client, app):
|
||||
def test_receive_command_success(client):
|
||||
"""
|
||||
Test that a POST to /command sends the right multipart message
|
||||
and returns a 202 with the expected JSON body.
|
||||
Test for successful reception of a command. Ensures the status code is 202 and the response body
|
||||
is correct. It also verifies that the ZeroMQ socket's send_multipart method is called with the
|
||||
expected data.
|
||||
"""
|
||||
mock_socket = app.state.internal_comm_socket
|
||||
# Arrange
|
||||
mock_pub_socket = AsyncMock()
|
||||
client.app.state.endpoints_pub_socket = mock_pub_socket
|
||||
|
||||
# Prepare test payload that matches SpeechCommand
|
||||
payload = {"endpoint": "actuate/speech", "data": "yooo"}
|
||||
command_data = {"endpoint": "actuate/speech", "data": "This is a test"}
|
||||
speech_command = SpeechCommand(**command_data)
|
||||
|
||||
# Send POST request
|
||||
response = client.post("/command", json=payload)
|
||||
# Act
|
||||
response = client.post("/command", json=command_data)
|
||||
|
||||
# Check response
|
||||
# Assert
|
||||
assert response.status_code == 202
|
||||
assert response.json() == {"status": "Command received"}
|
||||
|
||||
# Verify that the socket was called with the correct data
|
||||
assert mock_socket.send_multipart.called, "Socket should be used to send data"
|
||||
|
||||
args, kwargs = mock_socket.send_multipart.call_args
|
||||
sent_data = args[0]
|
||||
|
||||
assert sent_data[0] == b"command"
|
||||
# Check JSON encoding roughly matches
|
||||
assert isinstance(SpeechCommand.model_validate_json(sent_data[1].decode()), SpeechCommand)
|
||||
# Verify that the ZMQ socket was used correctly
|
||||
mock_pub_socket.send_multipart.assert_awaited_once_with(
|
||||
[b"command", speech_command.model_dump_json().encode()]
|
||||
)
|
||||
|
||||
|
||||
def test_receive_command_invalid_payload(client):
|
||||
|
||||
Reference in New Issue
Block a user