refactor: rename all agents and improve structure pt1

ref: N25B-257
This commit is contained in:
Björn Otgaar
2025-11-12 11:04:49 +01:00
parent 781a05328f
commit 0e45383027
37 changed files with 199 additions and 201 deletions

View File

@@ -5,43 +5,47 @@ import pytest
import zmq
from spade.agent import Agent
from control_backend.agents.vad_agent import VADAgent
from control_backend.agents.per_agents.per_vad_agent import PerVADAgent
@pytest.fixture
def zmq_context(mocker):
mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance")
mock_context = mocker.patch(
"control_backend.agents.per_agents.per_vad_agent.azmq.Context.instance"
)
mock_context.return_value = MagicMock()
return mock_context
@pytest.fixture
def streaming(mocker):
return mocker.patch("control_backend.agents.vad_agent.Streaming")
return mocker.patch("control_backend.agents.per_agents.per_vad_agent.Streaming")
@pytest.fixture
def transcription_agent(mocker):
return mocker.patch("control_backend.agents.vad_agent.TranscriptionAgent", autospec=True)
def per_transcription_agent(mocker):
return mocker.patch(
"control_backend.agents.per_agents.per_vad_agent.PerTranscriptionAgent", autospec=True
)
@pytest.mark.asyncio
async def test_normal_setup(streaming, transcription_agent):
async def test_normal_setup(streaming, per_transcription_agent):
"""
Test that during normal setup, the VAD agent creates a Streaming behavior and creates audio
sockets, and starts the TranscriptionAgent without loading real models.
sockets, and starts the PerTranscriptionAgent without loading real models.
"""
vad_agent = VADAgent("tcp://localhost:12345", False)
vad_agent.add_behaviour = MagicMock()
per_vad_agent = PerVADAgent("tcp://localhost:12345", False)
per_vad_agent.add_behaviour = MagicMock()
await vad_agent.setup()
await per_vad_agent.setup()
streaming.assert_called_once()
vad_agent.add_behaviour.assert_called_once_with(streaming.return_value)
transcription_agent.assert_called_once()
transcription_agent.return_value.start.assert_called_once()
assert vad_agent.audio_in_socket is not None
assert vad_agent.audio_out_socket is not None
per_vad_agent.add_behaviour.assert_called_once_with(streaming.return_value)
per_transcription_agent.assert_called_once()
per_transcription_agent.return_value.start.assert_called_once()
assert per_vad_agent.audio_in_socket is not None
assert per_vad_agent.audio_out_socket is not None
@pytest.mark.parametrize("do_bind", [True, False])
@@ -50,11 +54,11 @@ def test_in_socket_creation(zmq_context, do_bind: bool):
Test that the VAD agent creates an audio input socket, differentiating between binding and
connecting.
"""
vad_agent = VADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind)
per_vad_agent = PerVADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind)
vad_agent._connect_audio_in_socket()
per_vad_agent._connect_audio_in_socket()
assert vad_agent.audio_in_socket is not None
assert per_vad_agent.audio_in_socket is not None
zmq_context.return_value.socket.assert_called_once_with(zmq.SUB)
zmq_context.return_value.socket.return_value.setsockopt_string.assert_called_once_with(
@@ -74,11 +78,11 @@ def test_out_socket_creation(zmq_context):
"""
Test that the VAD agent creates an audio output socket correctly.
"""
vad_agent = VADAgent("tcp://localhost:12345", False)
per_vad_agent = PerVADAgent("tcp://localhost:12345", False)
vad_agent._connect_audio_out_socket()
per_vad_agent._connect_audio_out_socket()
assert vad_agent.audio_out_socket is not None
assert per_vad_agent.audio_out_socket is not None
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()
@@ -93,28 +97,28 @@ async def test_out_socket_creation_failure(zmq_context):
zmq_context.return_value.socket.return_value.bind_to_random_port.side_effect = (
zmq.ZMQBindError
)
vad_agent = VADAgent("tcp://localhost:12345", False)
per_vad_agent = PerVADAgent("tcp://localhost:12345", False)
await vad_agent.setup()
await per_vad_agent.setup()
assert vad_agent.audio_out_socket is None
assert per_vad_agent.audio_out_socket is None
mock_super_stop.assert_called_once()
@pytest.mark.asyncio
async def test_stop(zmq_context, transcription_agent):
async def test_stop(zmq_context, per_transcription_agent):
"""
Test that when the VAD agent is stopped, the sockets are closed correctly.
"""
vad_agent = VADAgent("tcp://localhost:12345", False)
per_vad_agent = PerVADAgent("tcp://localhost:12345", False)
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()
await per_vad_agent.setup()
await per_vad_agent.stop()
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
assert per_vad_agent.audio_in_socket is None
assert per_vad_agent.audio_out_socket is None

View File

@@ -5,7 +5,7 @@ import pytest
import soundfile as sf
import zmq
from control_backend.agents.vad_agent import Streaming
from control_backend.agents.per_agents.per_vad_agent import Streaming
def get_audio_chunks() -> list[bytes]:
@@ -42,7 +42,9 @@ async def test_real_audio(mocker):
audio_in_socket = AsyncMock()
audio_in_socket.recv.side_effect = audio_chunks
mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller")
mock_poller: MagicMock = mocker.patch(
"control_backend.agents.per_agents.per_vad_agent.zmq.Poller"
)
mock_poller.return_value.poll.return_value = [(audio_in_socket, zmq.POLLIN)]
audio_out_socket = AsyncMock()

View File

@@ -4,12 +4,14 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import zmq
from control_backend.agents.ri_command_agent import RICommandAgent
from control_backend.agents.act_agents.act_speech_agent import ActSpeechAgent
@pytest.fixture
def zmq_context(mocker):
mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance")
mock_context = mocker.patch(
"control_backend.agents.act_agents.act_speech_agent.zmq.Context.instance"
)
mock_context.return_value = MagicMock()
return mock_context
@@ -19,8 +21,8 @@ async def test_setup_bind(zmq_context, mocker):
"""Test setup with bind=True"""
fake_socket = zmq_context.return_value.socket.return_value
agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=True)
settings = mocker.patch("control_backend.agents.ri_command_agent.settings")
agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=True)
settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings")
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
await agent.setup()
@@ -40,8 +42,8 @@ async def test_setup_connect(zmq_context, mocker):
"""Test setup with bind=False"""
fake_socket = zmq_context.return_value.socket.return_value
agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
settings = mocker.patch("control_backend.agents.ri_command_agent.settings")
agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings")
settings.zmq_settings.internal_sub_address = "tcp://internal:1234"
await agent.setup()
@@ -60,14 +62,16 @@ async def test_send_commands_behaviour_valid_message():
)
fake_socket.send_json = AsyncMock()
agent = RICommandAgent("test@server", "password")
agent = ActSpeechAgent("test@server", "password")
agent.subsocket = fake_socket
agent.pubsocket = fake_socket
behaviour = agent.SendCommandsBehaviour()
behaviour.agent = agent
with patch("control_backend.agents.ri_command_agent.SpeechCommand") as MockSpeechCommand:
with patch(
"control_backend.agents.act_agents.act_speech_agent.SpeechCommand"
) as MockSpeechCommand:
mock_message = MagicMock()
MockSpeechCommand.model_validate.return_value = mock_message
@@ -84,16 +88,14 @@ async def test_send_commands_behaviour_invalid_message(caplog):
fake_socket.recv_multipart = AsyncMock(return_value=(b"command", b"{invalid_json}"))
fake_socket.send_json = AsyncMock()
agent = RICommandAgent("test@server", "password")
agent = ActSpeechAgent("test@server", "password")
agent.subsocket = fake_socket
agent.pubsocket = fake_socket
behaviour = agent.SendCommandsBehaviour()
behaviour.agent = agent
with caplog.at_level("ERROR"):
await behaviour.run()
await behaviour.run()
fake_socket.recv_multipart.assert_awaited()
fake_socket.send_json.assert_not_awaited()
assert "Error processing message" in caplog.text

View File

@@ -3,7 +3,11 @@ from unittest.mock import ANY, AsyncMock, MagicMock, patch
import pytest
from control_backend.agents.ri_communication_agent import RICommunicationAgent
from control_backend.agents.com_agents.com_ri_agent import ComRIAgent
def act_agent_path():
return "control_backend.agents.com_agents.com_ri_agent.ActSpeechAgent"
def fake_json_correct_negototiate_1():
@@ -86,7 +90,9 @@ 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 = mocker.patch(
"control_backend.agents.com_agents.com_ri_agent.zmq.Context.instance"
)
mock_context.return_value = MagicMock()
return mock_context
@@ -101,17 +107,13 @@ async def test_setup_creates_socket_and_negotiate_1(zmq_context):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_1()
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Mock ActSpeechAgent agent startup
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
await agent.setup()
# --- Assert ---
@@ -139,17 +141,13 @@ async def test_setup_creates_socket_and_negotiate_2(zmq_context):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_2()
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Mock ActSpeechAgent agent startup
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
await agent.setup()
# --- Assert ---
@@ -177,19 +175,17 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_wrong_negototiate_1()
# Mock RICommandAgent agent startup
# Mock ActSpeechAgent agent startup
# We are sending wrong negotiation info to the communication agent,
# so we should retry and expect a better response, within a limited time.
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
with caplog.at_level("ERROR"):
agent = RICommunicationAgent(
agent = ComRIAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
await agent.setup(max_retries=1)
@@ -200,7 +196,7 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog):
# Since it failed, there should not be any command agent.
fake_agent_instance.start.assert_not_awaited()
assert "Failed to set up RICommunicationAgent" in caplog.text
assert "Failed to set up ComRIAgent" in caplog.text
# Ensure the agent did not attach a ListenBehaviour
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
@@ -216,17 +212,13 @@ async def test_setup_creates_socket_and_negotiate_4(zmq_context):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_3()
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Mock ActSpeechAgent agent startup
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=True
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=True)
await agent.setup()
# --- Assert ---
@@ -254,17 +246,13 @@ async def test_setup_creates_socket_and_negotiate_5(zmq_context):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_4()
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Mock ActSpeechAgent agent startup
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
await agent.setup()
# --- Assert ---
@@ -292,17 +280,13 @@ async def test_setup_creates_socket_and_negotiate_6(zmq_context):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_5()
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Mock ActSpeechAgent agent startup
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
await agent.setup()
# --- Assert ---
@@ -330,19 +314,17 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_invalid_id_negototiate()
# Mock RICommandAgent agent startup
# Mock ActSpeechAgent agent startup
# We are sending wrong negotiation info to the communication agent,
# so we should retry and expect a better response, within a limited time.
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
agent = ComRIAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
await agent.setup(max_retries=1)
@@ -366,15 +348,13 @@ async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog):
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
agent = ComRIAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
await agent.setup(max_retries=1)
@@ -397,7 +377,7 @@ async def test_listen_behaviour_ping_correct(caplog):
fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}})
# TODO: Integration test between actual server and password needed for spade agents
agent = RICommunicationAgent("test@server", "password")
agent = ComRIAgent("test@server", "password")
agent.req_socket = fake_socket
behaviour = agent.ListenBehaviour()
@@ -431,7 +411,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog):
}
)
agent = RICommunicationAgent("test@server", "password")
agent = ComRIAgent("test@server", "password")
agent.req_socket = fake_socket
behaviour = agent.ListenBehaviour()
@@ -453,7 +433,7 @@ async def test_listen_behaviour_timeout(zmq_context, caplog):
# recv_json will never resolve, simulate timeout
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
agent = RICommunicationAgent("test@server", "password")
agent = ComRIAgent("test@server", "password")
agent.req_socket = fake_socket
behaviour = agent.ListenBehaviour()
@@ -480,7 +460,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
}
)
agent = RICommunicationAgent("test@server", "password")
agent = ComRIAgent("test@server", "password")
agent.req_socket = fake_socket
behaviour = agent.ListenBehaviour()
@@ -502,9 +482,7 @@ async def test_setup_unexpected_exception(zmq_context, caplog):
# Simulate unexpected exception during recv_json()
fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!"))
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
with caplog.at_level("ERROR"):
await agent.setup(max_retries=1)
@@ -526,16 +504,12 @@ async def test_setup_unpacking_exception(zmq_context, caplog):
} # missing 'port' and 'bind'
fake_socket.recv_json = AsyncMock(return_value=malformed_data)
# Patch RICommandAgent so it won't actually start
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
# Patch ActSpeechAgent so it won't actually start
with patch(act_agent_path(), autospec=True) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
agent = RICommunicationAgent(
"test@server", "password", address="tcp://localhost:5555", bind=False
)
agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False)
# --- Act & Assert ---
with caplog.at_level("ERROR"):