Compare commits

..

6 Commits

Author SHA1 Message Date
Pim Hutting
08812371fd chore: applied feedback and merged dev into branch
ref: N25B-355
2026-01-02 16:08:13 +01:00
Pim Hutting
6cf25cc587 Merge remote-tracking branch 'origin/dev' into feat/program-reset-llm 2026-01-02 16:06:14 +01:00
Pim Hutting
fb276133d9 chore: removed comment that was no longer needed
ref: N25B-355
2025-12-15 18:43:23 +01:00
Pim Hutting
35548f6864 chore: fixed test that wasn't passing
this was not my test
I stole the fix from Björn's cb2ri-gestures

ref: N25B-355
2025-12-15 18:40:24 +01:00
Pim Hutting
06503d568f Merge branch 'dev' of git.science.uu.nl:ics/sp/2025/n25b/pepperplus-cb into feat/program-reset-llm 2025-12-15 18:02:24 +01:00
Pim Hutting
cd0ca77af9 feat: made program reset LLM
also added test_bdi_program_manager back cause
it was somehow missing in my files

ref: N25B-355
2025-12-15 17:57:38 +01:00
11 changed files with 42 additions and 111 deletions

View File

@@ -1,20 +0,0 @@
# Example .env file. To use, make a copy, call it ".env" (i.e. removing the ".example" suffix), then you edit values.
# The hostname of the Robot Interface. Change if the Control Backend and Robot Interface are running on different computers.
RI_HOST="localhost"
# URL for the local LLM API. Must be an API that implements the OpenAI Chat Completions API, but most do.
LLM_SETTINGS__LOCAL_LLM_URL="http://localhost:1234/v1/chat/completions"
# Name of the local LLM model to use.
LLM_SETTINGS__LOCAL_LLM_MODEL="gpt-oss"
# Number of non-speech chunks to wait before speech ended. A chunk is approximately 31 ms. Increasing this number allows longer pauses in speech, but also increases response time.
BEHAVIOUR_SETTINGS__VAD_NON_SPEECH_PATIENCE_CHUNKS=3
# Timeout in milliseconds for socket polling. Increase this number if network latency/jitter is high, often the case when using Wi-Fi. Perhaps 500 ms. A symptom of this issue is transcriptions getting cut off.
BEHAVIOUR_SETTINGS__SOCKET_POLLER_TIMEOUT_MS=100
# For an exhaustive list of options, see the control_backend.core.config module in the docs.

View File

@@ -1,7 +1,5 @@
version: 1 version: 1
# Maak nieuwe (obvervation action)
# tussen 20-30
custom_levels: custom_levels:
OBSERVATION: 25 OBSERVATION: 25
ACTION: 26 ACTION: 26
@@ -21,8 +19,6 @@ formatters:
format: "{name} {levelname} {levelno} {message} {created} {relativeCreated}" format: "{name} {levelname} {levelno} {message} {created} {relativeCreated}"
style: "{" style: "{"
# Maak class = logging.fileHandler
#
handlers: handlers:
console: console:
class: logging.StreamHandler class: logging.StreamHandler
@@ -39,8 +35,6 @@ root:
level: WARN level: WARN
handlers: [console] handlers: [console]
# Maak research logger, laagste level (21)
# Handler: UI Handler
loggers: loggers:
control_backend: control_backend:
level: LLM level: LLM

View File

@@ -27,7 +27,6 @@ This + part might differ based on what model you choose.
copy the model name in the module loaded and replace local_llm_modelL. In settings. copy the model name in the module loaded and replace local_llm_modelL. In settings.
## Running ## Running
To run the project (development server), execute the following command (while inside the root repository): To run the project (development server), execute the following command (while inside the root repository):
@@ -35,14 +34,6 @@ To run the project (development server), execute the following command (while in
uv run fastapi dev src/control_backend/main.py uv run fastapi dev src/control_backend/main.py
``` ```
### Environment Variables
You can use environment variables to change settings. Make a copy of the [`.env.example`](.env.example) file, name it `.env` and put it in the root directory. The file itself describes how to do the configuration.
For an exhaustive list of environment options, see the `control_backend.core.config` module in the docs.
## Testing ## Testing
Testing happens automatically when opening a merge request to any branch. If you want to manually run the test suite, you can do so by running the following for unit tests: Testing happens automatically when opening a merge request to any branch. If you want to manually run the test suite, you can do so by running the following for unit tests:

View File

@@ -33,7 +33,7 @@ class RobotGestureAgent(BaseAgent):
def __init__( def __init__(
self, self,
name: str, name: str,
address: str, address=settings.zmq_settings.ri_command_address,
bind=False, bind=False,
gesture_data=None, gesture_data=None,
single_gesture_data=None, single_gesture_data=None,

View File

@@ -64,7 +64,7 @@ class BDIProgramManager(BaseAgent):
""" """
Clear the LLM Agent's conversation history. Clear the LLM Agent's conversation history.
Sends an empty history to the LLM Agent to reset its state. Sends a message to the LLM Agent instructing it to clear its history.
""" """
message = InternalMessage( message = InternalMessage(
to=settings.agent_settings.llm_name, to=settings.agent_settings.llm_name,

View File

@@ -38,7 +38,7 @@ class RICommunicationAgent(BaseAgent):
def __init__( def __init__(
self, self,
name: str, name: str,
address=settings.zmq_settings.ri_communication_address, address=settings.zmq_settings.ri_command_address,
bind=False, bind=False,
): ):
super().__init__(name) super().__init__(name)
@@ -168,7 +168,7 @@ class RICommunicationAgent(BaseAgent):
bind = port_data["bind"] bind = port_data["bind"]
if not bind: if not bind:
addr = f"tcp://{settings.ri_host}:{port}" addr = f"tcp://localhost:{port}"
else: else:
addr = f"tcp://*:{port}" addr = f"tcp://*:{port}"

View File

@@ -103,11 +103,12 @@ class VADAgent(BaseAgent):
self._connect_audio_in_socket() self._connect_audio_in_socket()
audio_out_address = self._connect_audio_out_socket() audio_out_port = self._connect_audio_out_socket()
if audio_out_address is None: if audio_out_port is None:
self.logger.error("Could not bind output socket, stopping.") self.logger.error("Could not bind output socket, stopping.")
await self.stop() await self.stop()
return return
audio_out_address = f"tcp://localhost:{audio_out_port}"
# Connect to internal communication socket # Connect to internal communication socket
self.program_sub_socket = azmq.Context.instance().socket(zmq.SUB) self.program_sub_socket = azmq.Context.instance().socket(zmq.SUB)
@@ -160,14 +161,13 @@ class VADAgent(BaseAgent):
self.audio_in_socket.connect(self.audio_in_address) self.audio_in_socket.connect(self.audio_in_address)
self.audio_in_poller = SocketPoller[bytes](self.audio_in_socket) self.audio_in_poller = SocketPoller[bytes](self.audio_in_socket)
def _connect_audio_out_socket(self) -> str | None: def _connect_audio_out_socket(self) -> int | None:
""" """
Returns the address that was bound to, or None if binding failed. Returns the port bound, or None if binding failed.
""" """
try: try:
self.audio_out_socket = azmq.Context.instance().socket(zmq.PUB) self.audio_out_socket = azmq.Context.instance().socket(zmq.PUB)
self.audio_out_socket.bind(settings.zmq_settings.vad_pub_address) return self.audio_out_socket.bind_to_random_port("tcp://localhost", max_tries=100)
return settings.zmq_settings.vad_pub_address
except zmq.ZMQBindError: except zmq.ZMQBindError:
self.logger.error("Failed to bind an audio output socket after 100 tries.") self.logger.error("Failed to bind an audio output socket after 100 tries.")
self.audio_out_socket = None self.audio_out_socket = None

View File

@@ -1,12 +1,3 @@
"""
An exhaustive overview of configurable options. All of these can be set using environment variables
by nesting with double underscores (__). Start from the ``Settings`` class.
For example, ``settings.ri_host`` becomes ``RI_HOST``, and
``settings.zmq_settings.ri_communication_address`` becomes
``ZMQ_SETTINGS__RI_COMMUNICATION_ADDRESS``.
"""
from pydantic import BaseModel from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -17,17 +8,16 @@ class ZMQSettings(BaseModel):
:ivar internal_pub_address: Address for the internal PUB socket. :ivar internal_pub_address: Address for the internal PUB socket.
:ivar internal_sub_address: Address for the internal SUB socket. :ivar internal_sub_address: Address for the internal SUB socket.
:ivar ri_communication_address: Address for the endpoint that the Robot Interface connects to. :ivar ri_command_address: Address for sending commands to the Robot Interface.
:ivar vad_pub_address: Address that the VAD agent binds to and publishes audio segments to. :ivar ri_communication_address: Address for receiving communication from the Robot Interface.
:ivar vad_agent_address: Address for the Voice Activity Detection (VAD) agent.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
internal_pub_address: str = "tcp://localhost:5560" internal_pub_address: str = "tcp://localhost:5560"
internal_sub_address: str = "tcp://localhost:5561" internal_sub_address: str = "tcp://localhost:5561"
ri_command_address: str = "tcp://localhost:0000"
ri_communication_address: str = "tcp://*:5555" ri_communication_address: str = "tcp://*:5555"
internal_gesture_rep_adress: str = "tcp://localhost:7788" internal_gesture_rep_adress: str = "tcp://localhost:7788"
vad_pub_address: str = "inproc://vad_stream"
class AgentSettings(BaseModel): class AgentSettings(BaseModel):
@@ -46,8 +36,6 @@ class AgentSettings(BaseModel):
:ivar robot_speech_name: Name of the Robot Speech Agent. :ivar robot_speech_name: Name of the Robot Speech Agent.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
# agent names # agent names
bdi_core_name: str = "bdi_core_agent" bdi_core_name: str = "bdi_core_agent"
bdi_belief_collector_name: str = "belief_collector_agent" bdi_belief_collector_name: str = "belief_collector_agent"
@@ -79,8 +67,6 @@ class BehaviourSettings(BaseModel):
:ivar transcription_token_buffer: Buffer for transcription tokens. :ivar transcription_token_buffer: Buffer for transcription tokens.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
sleep_s: float = 1.0 sleep_s: float = 1.0
comm_setup_max_retries: int = 5 comm_setup_max_retries: int = 5
socket_poller_timeout_ms: int = 100 socket_poller_timeout_ms: int = 100
@@ -105,8 +91,6 @@ class LLMSettings(BaseModel):
:ivar local_llm_model: Name of the local LLM model to use. :ivar local_llm_model: Name of the local LLM model to use.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
local_llm_url: str = "http://localhost:1234/v1/chat/completions" local_llm_url: str = "http://localhost:1234/v1/chat/completions"
local_llm_model: str = "gpt-oss" local_llm_model: str = "gpt-oss"
@@ -120,8 +104,6 @@ class VADSettings(BaseModel):
:ivar sample_rate_hz: Sample rate in Hz for the VAD model. :ivar sample_rate_hz: Sample rate in Hz for the VAD model.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
repo_or_dir: str = "snakers4/silero-vad" repo_or_dir: str = "snakers4/silero-vad"
model_name: str = "silero_vad" model_name: str = "silero_vad"
sample_rate_hz: int = 16000 sample_rate_hz: int = 16000
@@ -135,8 +117,6 @@ class SpeechModelSettings(BaseModel):
:ivar openai_model_name: Model name for OpenAI-based speech recognition. :ivar openai_model_name: Model name for OpenAI-based speech recognition.
""" """
# ATTENTION: When adding/removing settings, make sure to update the .env.example file
# model identifiers for speech recognition # model identifiers for speech recognition
mlx_model_name: str = "mlx-community/whisper-small.en-mlx" mlx_model_name: str = "mlx-community/whisper-small.en-mlx"
openai_model_name: str = "small.en" openai_model_name: str = "small.en"
@@ -148,7 +128,6 @@ class Settings(BaseSettings):
:ivar app_title: Title of the application. :ivar app_title: Title of the application.
:ivar ui_url: URL of the frontend UI. :ivar ui_url: URL of the frontend UI.
:ivar ri_host: The hostname of the Robot Interface.
:ivar zmq_settings: ZMQ configuration. :ivar zmq_settings: ZMQ configuration.
:ivar agent_settings: Agent name configuration. :ivar agent_settings: Agent name configuration.
:ivar behaviour_settings: Behavior configuration. :ivar behaviour_settings: Behavior configuration.
@@ -161,8 +140,6 @@ class Settings(BaseSettings):
ui_url: str = "http://localhost:5173" ui_url: str = "http://localhost:5173"
ri_host: str = "localhost"
zmq_settings: ZMQSettings = ZMQSettings() zmq_settings: ZMQSettings = ZMQSettings()
agent_settings: AgentSettings = AgentSettings() agent_settings: AgentSettings = AgentSettings()

View File

@@ -91,7 +91,7 @@ def test_out_socket_creation(zmq_context):
assert per_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.assert_called_once_with(zmq.PUB)
zmq_context.return_value.socket.return_value.bind.assert_called_once_with("inproc://vad_stream") zmq_context.return_value.socket.return_value.bind_to_random_port.assert_called_once()
@pytest.mark.asyncio @pytest.mark.asyncio

View File

@@ -73,7 +73,7 @@ async def test_setup_connect(zmq_context, mocker):
async def test_handle_message_sends_valid_gesture_command(): async def test_handle_message_sends_valid_gesture_command():
"""Internal message with valid gesture tag is forwarded to robot pub socket.""" """Internal message with valid gesture tag is forwarded to robot pub socket."""
pubsocket = AsyncMock() pubsocket = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.pubsocket = pubsocket agent.pubsocket = pubsocket
payload = { payload = {
@@ -91,7 +91,7 @@ async def test_handle_message_sends_valid_gesture_command():
async def test_handle_message_sends_non_gesture_command(): async def test_handle_message_sends_non_gesture_command():
"""Internal message with non-gesture endpoint is not forwarded by this agent.""" """Internal message with non-gesture endpoint is not forwarded by this agent."""
pubsocket = AsyncMock() pubsocket = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.pubsocket = pubsocket agent.pubsocket = pubsocket
payload = {"endpoint": "some_other_endpoint", "data": "invalid_tag_not_in_list"} payload = {"endpoint": "some_other_endpoint", "data": "invalid_tag_not_in_list"}
@@ -107,7 +107,7 @@ async def test_handle_message_sends_non_gesture_command():
async def test_handle_message_rejects_invalid_gesture_tag(): async def test_handle_message_rejects_invalid_gesture_tag():
"""Internal message with invalid gesture tag is not forwarded.""" """Internal message with invalid gesture tag is not forwarded."""
pubsocket = AsyncMock() pubsocket = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.pubsocket = pubsocket agent.pubsocket = pubsocket
# Use a tag that's not in gesture_data # Use a tag that's not in gesture_data
@@ -123,7 +123,7 @@ async def test_handle_message_rejects_invalid_gesture_tag():
async def test_handle_message_invalid_payload(): async def test_handle_message_invalid_payload():
"""Invalid payload is caught and does not send.""" """Invalid payload is caught and does not send."""
pubsocket = AsyncMock() pubsocket = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.pubsocket = pubsocket agent.pubsocket = pubsocket
msg = InternalMessage(to="robot", sender="tester", body=json.dumps({"bad": "data"})) msg = InternalMessage(to="robot", sender="tester", body=json.dumps({"bad": "data"}))
@@ -142,12 +142,12 @@ async def test_zmq_command_loop_valid_gesture_payload():
async def recv_once(): async def recv_once():
# stop after first iteration # stop after first iteration
agent._running = False agent._running = False
return b"command", json.dumps(command).encode("utf-8") return (b"command", json.dumps(command).encode("utf-8"))
fake_socket.recv_multipart = recv_once fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock() fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.subsocket = fake_socket agent.subsocket = fake_socket
agent.pubsocket = fake_socket agent.pubsocket = fake_socket
agent._running = True agent._running = True
@@ -165,12 +165,12 @@ async def test_zmq_command_loop_valid_non_gesture_payload():
async def recv_once(): async def recv_once():
agent._running = False agent._running = False
return b"command", json.dumps(command).encode("utf-8") return (b"command", json.dumps(command).encode("utf-8"))
fake_socket.recv_multipart = recv_once fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock() fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.subsocket = fake_socket agent.subsocket = fake_socket
agent.pubsocket = fake_socket agent.pubsocket = fake_socket
agent._running = True agent._running = True
@@ -188,12 +188,12 @@ async def test_zmq_command_loop_invalid_gesture_tag():
async def recv_once(): async def recv_once():
agent._running = False agent._running = False
return b"command", json.dumps(command).encode("utf-8") return (b"command", json.dumps(command).encode("utf-8"))
fake_socket.recv_multipart = recv_once fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock() fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.subsocket = fake_socket agent.subsocket = fake_socket
agent.pubsocket = fake_socket agent.pubsocket = fake_socket
agent._running = True agent._running = True
@@ -210,12 +210,12 @@ async def test_zmq_command_loop_invalid_json():
async def recv_once(): async def recv_once():
agent._running = False agent._running = False
return b"command", b"{not_json}" return (b"command", b"{not_json}")
fake_socket.recv_multipart = recv_once fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock() fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.subsocket = fake_socket agent.subsocket = fake_socket
agent.pubsocket = fake_socket agent.pubsocket = fake_socket
agent._running = True agent._running = True
@@ -232,12 +232,12 @@ async def test_zmq_command_loop_ignores_send_gestures_topic():
async def recv_once(): async def recv_once():
agent._running = False agent._running = False
return b"send_gestures", b"{}" return (b"send_gestures", b"{}")
fake_socket.recv_multipart = recv_once fake_socket.recv_multipart = recv_once
fake_socket.send_json = AsyncMock() fake_socket.send_json = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.subsocket = fake_socket agent.subsocket = fake_socket
agent.pubsocket = fake_socket agent.pubsocket = fake_socket
agent._running = True agent._running = True
@@ -259,9 +259,7 @@ async def test_fetch_gestures_loop_without_amount():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent( agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no", "wave", "point"])
"robot_gesture", gesture_data=["hello", "yes", "no", "wave", "point"], address=""
)
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent._running = True agent._running = True
@@ -289,9 +287,7 @@ async def test_fetch_gestures_loop_with_amount():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent( agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no", "wave", "point"])
"robot_gesture", gesture_data=["hello", "yes", "no", "wave", "point"], address=""
)
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent._running = True agent._running = True
@@ -319,7 +315,7 @@ async def test_fetch_gestures_loop_with_integer_request():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent._running = True agent._running = True
@@ -344,7 +340,7 @@ async def test_fetch_gestures_loop_with_invalid_json():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent._running = True agent._running = True
@@ -369,7 +365,7 @@ async def test_fetch_gestures_loop_with_non_integer_json():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent._running = True agent._running = True
@@ -385,7 +381,7 @@ async def test_fetch_gestures_loop_with_non_integer_json():
def test_gesture_data_attribute(): def test_gesture_data_attribute():
"""Test that gesture_data returns the expected list.""" """Test that gesture_data returns the expected list."""
gesture_data = ["hello", "yes", "no", "wave"] gesture_data = ["hello", "yes", "no", "wave"]
agent = RobotGestureAgent("robot_gesture", gesture_data=gesture_data, address="") agent = RobotGestureAgent("robot_gesture", gesture_data=gesture_data)
assert agent.gesture_data == gesture_data assert agent.gesture_data == gesture_data
assert isinstance(agent.gesture_data, list) assert isinstance(agent.gesture_data, list)
@@ -402,7 +398,7 @@ async def test_stop_closes_sockets():
pubsocket = MagicMock() pubsocket = MagicMock()
subsocket = MagicMock() subsocket = MagicMock()
repsocket = MagicMock() repsocket = MagicMock()
agent = RobotGestureAgent("robot_gesture", address="") agent = RobotGestureAgent("robot_gesture")
agent.pubsocket = pubsocket agent.pubsocket = pubsocket
agent.subsocket = subsocket agent.subsocket = subsocket
agent.repsocket = repsocket agent.repsocket = repsocket
@@ -419,7 +415,7 @@ async def test_stop_closes_sockets():
async def test_initialization_with_custom_gesture_data(): async def test_initialization_with_custom_gesture_data():
"""Agent can be initialized with custom gesture data.""" """Agent can be initialized with custom gesture data."""
custom_gestures = ["custom1", "custom2", "custom3"] custom_gestures = ["custom1", "custom2", "custom3"]
agent = RobotGestureAgent("robot_gesture", gesture_data=custom_gestures, address="") agent = RobotGestureAgent("robot_gesture", gesture_data=custom_gestures)
assert agent.gesture_data == custom_gestures assert agent.gesture_data == custom_gestures
@@ -436,7 +432,7 @@ async def test_fetch_gestures_loop_handles_exception():
fake_repsocket.recv = recv_once fake_repsocket.recv = recv_once
fake_repsocket.send = AsyncMock() fake_repsocket.send = AsyncMock()
agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"], address="") agent = RobotGestureAgent("robot_gesture", gesture_data=["hello", "yes", "no"])
agent.repsocket = fake_repsocket agent.repsocket = fake_repsocket
agent.logger = MagicMock() agent.logger = MagicMock()
agent._running = True agent._running = True

View File

@@ -7,15 +7,6 @@ import zmq
from control_backend.agents.perception.vad_agent import VADAgent from control_backend.agents.perception.vad_agent import VADAgent
# We don't want to use real ZMQ in unit tests, for example because it can give errors when sockets
# aren't closed properly.
@pytest.fixture(autouse=True)
def mock_zmq():
with patch("zmq.asyncio.Context") as mock:
mock.instance.return_value = MagicMock()
yield mock
@pytest.fixture @pytest.fixture
def audio_out_socket(): def audio_out_socket():
return AsyncMock() return AsyncMock()
@@ -149,10 +140,12 @@ async def test_vad_model_load_failure_stops_agent(vad_agent):
# Patch stop to an AsyncMock so we can check it was awaited # Patch stop to an AsyncMock so we can check it was awaited
vad_agent.stop = AsyncMock() vad_agent.stop = AsyncMock()
await vad_agent.setup() result = await vad_agent.setup()
# Assert stop was called # Assert stop was called
vad_agent.stop.assert_awaited_once() vad_agent.stop.assert_awaited_once()
# Assert setup returned None
assert result is None
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -162,7 +155,7 @@ async def test_audio_out_bind_failure_sets_none_and_logs(vad_agent, caplog):
audio_out_socket is set to None, None is returned, and an error is logged. audio_out_socket is set to None, None is returned, and an error is logged.
""" """
mock_socket = MagicMock() mock_socket = MagicMock()
mock_socket.bind.side_effect = zmq.ZMQBindError() mock_socket.bind_to_random_port.side_effect = zmq.ZMQBindError()
with patch("control_backend.agents.perception.vad_agent.azmq.Context.instance") as mock_ctx: with patch("control_backend.agents.perception.vad_agent.azmq.Context.instance") as mock_ctx:
mock_ctx.return_value.socket.return_value = mock_socket mock_ctx.return_value.socket.return_value = mock_socket