fix: fixed new tests and merged dev into branch

ref: N25B-256
This commit is contained in:
Björn Otgaar
2025-11-05 16:29:56 +01:00
29 changed files with 520 additions and 298 deletions

View File

@@ -84,25 +84,24 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
@@ -135,24 +134,16 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
@@ -185,24 +176,16 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
# We are sending wrong negotiation info to the communication agent,
@@ -235,24 +218,16 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
@@ -284,24 +259,16 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
@@ -333,24 +300,16 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
@@ -382,28 +341,20 @@ 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()
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
# We are sending wrong negotiation info to the communication agent,
# so we should retry and expect a etter response, within a limited time.
# 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:
@@ -430,24 +381,16 @@ 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)
fake_socket.send_multipart = AsyncMock()
# Mock context.socket to return our fake socket
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True
) as MockCommandAgent:
@@ -534,8 +477,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)
@@ -585,20 +528,13 @@ 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!"))
fake_socket.send_multipart = AsyncMock()
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
agent = RICommunicationAgent(
"test@server",
"password",
@@ -614,9 +550,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()
fake_socket.send_multipart = AsyncMock()
@@ -627,14 +563,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
fake_context = MagicMock()
fake_context.socket.return_value = fake_socket
monkeypatch.setattr(
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Patch RICommandAgent so it won't actually start
with patch(
"control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True