fix: merge refactor/zmq-internal-socket-behaviour into feat/cb2ui-robot-connections. (And fixed all ruff/ test issues to commit)

ref: None
This commit is contained in:
Björn Otgaar
2025-10-31 14:16:11 +01:00
38 changed files with 1761 additions and 167 deletions

View File

@@ -93,12 +93,14 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
fake_socket = MagicMock()
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = fake_json_correct_negototiate_1()
fake_pub_socket = AsyncMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -107,13 +109,11 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -143,10 +143,14 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -155,13 +159,11 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -191,10 +193,14 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -206,13 +212,11 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
with caplog.at_level("ERROR"):
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -239,10 +243,14 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -251,12 +259,10 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=True,
)
@@ -286,10 +292,14 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -298,12 +308,10 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -333,10 +341,14 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -345,12 +357,10 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -380,10 +390,14 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Mock RICommandAgent agent startup
@@ -395,14 +409,12 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -426,10 +438,14 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
fake_socket = MagicMock()
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
with patch(
@@ -437,14 +453,12 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
# --- Act ---
with caplog.at_level("WARNING"):
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -466,11 +480,11 @@ async def test_listen_behaviour_ping_correct(caplog):
fake_socket = AsyncMock()
fake_socket.send_json = AsyncMock()
fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}})
fake_pub_socket = AsyncMock()
fake_socket.send_multipart = AsyncMock()
# TODO: Integration test between actual server and password needed for spade agents
agent = RICommunicationAgent("test@server", "password", fake_pub_socket)
agent.req_socket = fake_socket
agent = RICommunicationAgent("test@server", "password")
agent._req_socket = fake_socket
behaviour = agent.ListenBehaviour()
agent.add_behaviour(behaviour)
@@ -505,7 +519,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog):
fake_pub_socket = AsyncMock()
agent = RICommunicationAgent("test@server", "password", fake_pub_socket)
agent.req_socket = fake_socket
agent._req_socket = fake_socket
behaviour = agent.ListenBehaviour()
agent.add_behaviour(behaviour)
@@ -525,10 +539,10 @@ async def test_listen_behaviour_timeout(caplog):
fake_socket.send_json = AsyncMock()
# recv_json will never resolve, simulate timeout
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
fake_pub_socket = AsyncMock()
fake_socket.send_multipart = AsyncMock()
agent = RICommunicationAgent("test@server", "password", fake_pub_socket)
agent.req_socket = fake_socket
agent = RICommunicationAgent("test@server", "password")
agent._req_socket = fake_socket
behaviour = agent.ListenBehaviour()
agent.add_behaviour(behaviour)
@@ -546,6 +560,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
"""
fake_socket = AsyncMock()
fake_socket.send_json = AsyncMock()
fake_socket.send_multipart = AsyncMock()
# This is a message without endpoint >:(
fake_socket.recv_json = AsyncMock(
@@ -553,10 +568,9 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
"data": "I dont have an endpoint >:)",
}
)
fake_pub_socket = AsyncMock()
agent = RICommunicationAgent("test@server", "password", fake_pub_socket)
agent.req_socket = fake_socket
agent = RICommunicationAgent("test@server", "password")
agent._req_socket = fake_socket
behaviour = agent.ListenBehaviour()
agent.add_behaviour(behaviour)
@@ -574,18 +588,20 @@ async def test_listen_behaviour_ping_no_endpoint(caplog):
async def test_setup_unexpected_exception(monkeypatch, caplog):
fake_socket = MagicMock()
fake_socket.send_json = AsyncMock()
fake_pub_socket = 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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)
@@ -602,6 +618,7 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
# --- Arrange ---
fake_socket = MagicMock()
fake_socket.send_json = AsyncMock()
fake_socket.send_multipart = AsyncMock()
# Make recv_json return malformed negotiation data to trigger unpacking exception
malformed_data = {
@@ -611,8 +628,11 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
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.socket", lambda _: fake_socket
"control_backend.agents.ri_communication_agent.Context",
MagicMock(instance=MagicMock(return_value=fake_context)),
)
# Patch RICommandAgent so it won't actually start
@@ -621,12 +641,10 @@ async def test_setup_unpacking_exception(monkeypatch, caplog):
) as MockCommandAgent:
fake_agent_instance = MockCommandAgent.return_value
fake_agent_instance.start = AsyncMock()
fake_pub_socket = AsyncMock()
agent = RICommunicationAgent(
"test@server",
"password",
pub_socket=fake_pub_socket,
address="tcp://localhost:5555",
bind=False,
)