Merge remote-tracking branch 'origin/dev' into fix/bdi-correct-belief-management
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -199,7 +199,7 @@ cython_debug/
|
|||||||
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||||
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
||||||
# you could uncomment the following to ignore the entire vscode folder
|
# you could uncomment the following to ignore the entire vscode folder
|
||||||
# .vscode/
|
.vscode/
|
||||||
|
|
||||||
# Ruff stuff:
|
# Ruff stuff:
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
|||||||
@@ -22,5 +22,6 @@ test:
|
|||||||
tags:
|
tags:
|
||||||
- test
|
- test
|
||||||
script:
|
script:
|
||||||
- uv run --only-group test pytest
|
# - uv run --group integration-test pytest test/integration
|
||||||
|
- uv run --only-group test pytest test/unit
|
||||||
|
|
||||||
|
|||||||
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"python.testing.pytestArgs": [
|
||||||
|
"test"
|
||||||
|
],
|
||||||
|
"python.testing.unittestEnabled": false,
|
||||||
|
"python.testing.pytestEnabled": true
|
||||||
|
}
|
||||||
10
README.md
10
README.md
@@ -24,10 +24,16 @@ uv run fastapi dev src/control_backend/main.py
|
|||||||
```
|
```
|
||||||
|
|
||||||
## 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:
|
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:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv run --only-group test pytest
|
uv run --only-group test pytest test/unit
|
||||||
|
```
|
||||||
|
|
||||||
|
Or for integration tests:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run --group integration-test pytest test/integration
|
||||||
```
|
```
|
||||||
|
|
||||||
## GitHooks
|
## GitHooks
|
||||||
|
|||||||
@@ -7,10 +7,15 @@ requires-python = ">=3.13"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"fastapi[all]>=0.115.6",
|
"fastapi[all]>=0.115.6",
|
||||||
"mlx-whisper>=0.4.3 ; sys_platform == 'darwin'",
|
"mlx-whisper>=0.4.3 ; sys_platform == 'darwin'",
|
||||||
|
"numpy>=2.3.3",
|
||||||
"openai-whisper>=20250625",
|
"openai-whisper>=20250625",
|
||||||
"pyaudio>=0.2.14",
|
"pyaudio>=0.2.14",
|
||||||
"pydantic>=2.12.0",
|
"pydantic>=2.12.0",
|
||||||
"pydantic-settings>=2.11.0",
|
"pydantic-settings>=2.11.0",
|
||||||
|
"pytest>=8.4.2",
|
||||||
|
"pytest-asyncio>=1.2.0",
|
||||||
|
"pytest-cov>=7.0.0",
|
||||||
|
"pytest-mock>=3.15.1",
|
||||||
"pyzmq>=27.1.0",
|
"pyzmq>=27.1.0",
|
||||||
"silero-vad>=6.0.0",
|
"silero-vad>=6.0.0",
|
||||||
"spade>=4.1.0",
|
"spade>=4.1.0",
|
||||||
@@ -25,7 +30,11 @@ dev = [
|
|||||||
"ruff>=0.14.2",
|
"ruff>=0.14.2",
|
||||||
"ruff-format>=0.3.0",
|
"ruff-format>=0.3.0",
|
||||||
]
|
]
|
||||||
|
integration-test = [
|
||||||
|
"soundfile>=0.13.1",
|
||||||
|
]
|
||||||
test = [
|
test = [
|
||||||
|
"numpy>=2.3.3",
|
||||||
"pytest>=8.4.2",
|
"pytest>=8.4.2",
|
||||||
"pytest-asyncio>=1.2.0",
|
"pytest-asyncio>=1.2.0",
|
||||||
"pytest-cov>=7.0.0",
|
"pytest-cov>=7.0.0",
|
||||||
|
|||||||
74
src/control_backend/agents/ri_command_agent.py
Normal file
74
src/control_backend/agents/ri_command_agent.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from spade.agent import Agent
|
||||||
|
from spade.behaviour import CyclicBehaviour
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from control_backend.core.config import settings
|
||||||
|
from control_backend.core.zmq_context import context
|
||||||
|
from control_backend.schemas.ri_message import SpeechCommand
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RICommandAgent(Agent):
|
||||||
|
subsocket: zmq.Socket
|
||||||
|
pubsocket: zmq.Socket
|
||||||
|
address = ""
|
||||||
|
bind = False
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
jid: str,
|
||||||
|
password: str,
|
||||||
|
port: int = 5222,
|
||||||
|
verify_security: bool = False,
|
||||||
|
address="tcp://localhost:0000",
|
||||||
|
bind=False,
|
||||||
|
):
|
||||||
|
super().__init__(jid, password, port, verify_security)
|
||||||
|
self.address = address
|
||||||
|
self.bind = bind
|
||||||
|
|
||||||
|
class SendCommandsBehaviour(CyclicBehaviour):
|
||||||
|
async def run(self):
|
||||||
|
"""
|
||||||
|
Run the command publishing loop indefinetely.
|
||||||
|
"""
|
||||||
|
assert self.agent is not None
|
||||||
|
# Get a message internally (with topic command)
|
||||||
|
topic, body = await self.agent.subsocket.recv_multipart()
|
||||||
|
|
||||||
|
# Try to get body
|
||||||
|
try:
|
||||||
|
body = json.loads(body)
|
||||||
|
message = SpeechCommand.model_validate(body)
|
||||||
|
|
||||||
|
# Send to the robot.
|
||||||
|
await self.agent.pubsocket.send_json(message.model_dump())
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Error processing message: %s", e)
|
||||||
|
|
||||||
|
async def setup(self):
|
||||||
|
"""
|
||||||
|
Setup the command agent
|
||||||
|
"""
|
||||||
|
logger.info("Setting up %s", self.jid)
|
||||||
|
|
||||||
|
# To the robot
|
||||||
|
self.pubsocket = context.socket(zmq.PUB)
|
||||||
|
if self.bind:
|
||||||
|
self.pubsocket.bind(self.address)
|
||||||
|
else:
|
||||||
|
self.pubsocket.connect(self.address)
|
||||||
|
|
||||||
|
# Receive internal topics regarding commands
|
||||||
|
self.subsocket = context.socket(zmq.SUB)
|
||||||
|
self.subsocket.connect(settings.zmq_settings.internal_comm_address)
|
||||||
|
self.subsocket.setsockopt(zmq.SUBSCRIBE, b"command")
|
||||||
|
|
||||||
|
# Add behaviour to our agent
|
||||||
|
commands_behaviour = self.SendCommandsBehaviour()
|
||||||
|
self.add_behaviour(commands_behaviour)
|
||||||
|
|
||||||
|
logger.info("Finished setting up %s", self.jid)
|
||||||
165
src/control_backend/agents/ri_communication_agent.py
Normal file
165
src/control_backend/agents/ri_communication_agent.py
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from spade.agent import Agent
|
||||||
|
from spade.behaviour import CyclicBehaviour
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from control_backend.core.config import settings
|
||||||
|
from control_backend.core.zmq_context import context
|
||||||
|
from control_backend.schemas.message import Message
|
||||||
|
from control_backend.agents.ri_command_agent import RICommandAgent
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RICommunicationAgent(Agent):
|
||||||
|
req_socket: zmq.Socket
|
||||||
|
_address = ""
|
||||||
|
_bind = True
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
jid: str,
|
||||||
|
password: str,
|
||||||
|
port: int = 5222,
|
||||||
|
verify_security: bool = False,
|
||||||
|
address="tcp://localhost:0000",
|
||||||
|
bind=False,
|
||||||
|
):
|
||||||
|
super().__init__(jid, password, port, verify_security)
|
||||||
|
self._address = address
|
||||||
|
self._bind = bind
|
||||||
|
|
||||||
|
class ListenBehaviour(CyclicBehaviour):
|
||||||
|
async def run(self):
|
||||||
|
"""
|
||||||
|
Run the listening (ping) loop indefinetely.
|
||||||
|
"""
|
||||||
|
assert self.agent is not None
|
||||||
|
|
||||||
|
# We need to listen and sent pings.
|
||||||
|
message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}}
|
||||||
|
await self.agent.req_socket.send_json(message)
|
||||||
|
|
||||||
|
# Wait up to three seconds for a reply:)
|
||||||
|
try:
|
||||||
|
message = await asyncio.wait_for(self.agent.req_socket.recv_json(), timeout=3.0)
|
||||||
|
|
||||||
|
# We didnt get a reply :(
|
||||||
|
except asyncio.TimeoutError as e:
|
||||||
|
logger.info("No ping retrieved in 3 seconds, killing myself.")
|
||||||
|
self.kill()
|
||||||
|
|
||||||
|
logger.debug('Received message "%s"', message)
|
||||||
|
if "endpoint" not in message:
|
||||||
|
logger.error("No received endpoint in message, excepted ping endpoint.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# See what endpoint we received
|
||||||
|
match message["endpoint"]:
|
||||||
|
case "ping":
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
case _:
|
||||||
|
logger.info(
|
||||||
|
"Received message with topic different than ping, while ping expected."
|
||||||
|
)
|
||||||
|
|
||||||
|
async def setup(self, max_retries: int = 5):
|
||||||
|
"""
|
||||||
|
Try to setup the communication agent, we have 5 retries in case we dont have a response yet.
|
||||||
|
"""
|
||||||
|
logger.info("Setting up %s", self.jid)
|
||||||
|
retries = 0
|
||||||
|
|
||||||
|
# Let's try a certain amount of times before failing connection
|
||||||
|
while retries < max_retries:
|
||||||
|
# Bind request socket
|
||||||
|
self.req_socket = context.socket(zmq.REQ)
|
||||||
|
if self._bind:
|
||||||
|
self.req_socket.bind(self._address)
|
||||||
|
else:
|
||||||
|
self.req_socket.connect(self._address)
|
||||||
|
|
||||||
|
# Send our message and receive one back:)
|
||||||
|
message = {"endpoint": "negotiate/ports", "data": None}
|
||||||
|
await self.req_socket.send_json(message)
|
||||||
|
|
||||||
|
try:
|
||||||
|
received_message = await asyncio.wait_for(self.req_socket.recv_json(), timeout=20.0)
|
||||||
|
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logger.warning(
|
||||||
|
"No connection established in 20 seconds (attempt %d/%d)",
|
||||||
|
retries + 1,
|
||||||
|
max_retries,
|
||||||
|
)
|
||||||
|
retries += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Unexpected error during negotiation: %s", e)
|
||||||
|
retries += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Validate endpoint
|
||||||
|
endpoint = received_message.get("endpoint")
|
||||||
|
if endpoint != "negotiate/ports":
|
||||||
|
# TODO: Should this send a message back?
|
||||||
|
logger.error(
|
||||||
|
"Invalid endpoint '%s' received (attempt %d/%d)",
|
||||||
|
endpoint,
|
||||||
|
retries + 1,
|
||||||
|
max_retries,
|
||||||
|
)
|
||||||
|
retries += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# At this point, we have a valid response
|
||||||
|
try:
|
||||||
|
for port_data in received_message["data"]:
|
||||||
|
id = port_data["id"]
|
||||||
|
port = port_data["port"]
|
||||||
|
bind = port_data["bind"]
|
||||||
|
|
||||||
|
if not bind:
|
||||||
|
addr = f"tcp://localhost:{port}"
|
||||||
|
else:
|
||||||
|
addr = f"tcp://*:{port}"
|
||||||
|
|
||||||
|
match id:
|
||||||
|
case "main":
|
||||||
|
if addr != self._address:
|
||||||
|
if not bind:
|
||||||
|
self.req_socket.connect(addr)
|
||||||
|
else:
|
||||||
|
self.req_socket.bind(addr)
|
||||||
|
case "actuation":
|
||||||
|
ri_commands_agent = RICommandAgent(
|
||||||
|
settings.agent_settings.ri_command_agent_name
|
||||||
|
+ "@"
|
||||||
|
+ settings.agent_settings.host,
|
||||||
|
settings.agent_settings.ri_command_agent_name,
|
||||||
|
address=addr,
|
||||||
|
bind=bind,
|
||||||
|
)
|
||||||
|
await ri_commands_agent.start()
|
||||||
|
case _:
|
||||||
|
logger.warning("Unhandled negotiation id: %s", id)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Error unpacking negotiation data: %s", e)
|
||||||
|
retries += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# setup succeeded
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.error("Failed to set up RICommunicationAgent after %d retries", max_retries)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Set up ping behaviour
|
||||||
|
listen_behaviour = self.ListenBehaviour()
|
||||||
|
self.add_behaviour(listen_behaviour)
|
||||||
|
logger.info("Finished setting up %s", self.jid)
|
||||||
156
src/control_backend/agents/vad_agent.py
Normal file
156
src/control_backend/agents/vad_agent.py
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import zmq
|
||||||
|
import zmq.asyncio as azmq
|
||||||
|
from spade.agent import Agent
|
||||||
|
from spade.behaviour import CyclicBehaviour
|
||||||
|
|
||||||
|
from control_backend.core.config import settings
|
||||||
|
from control_backend.core.zmq_context import context as zmq_context
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SocketPoller[T]:
|
||||||
|
"""
|
||||||
|
Convenience class for polling a socket for data with a timeout, persisting a zmq.Poller for
|
||||||
|
multiple usages.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, socket: azmq.Socket, timeout_ms: int = 100):
|
||||||
|
"""
|
||||||
|
:param socket: The socket to poll and get data from.
|
||||||
|
:param timeout_ms: A timeout in milliseconds to wait for data.
|
||||||
|
"""
|
||||||
|
self.socket = socket
|
||||||
|
self.poller = zmq.Poller()
|
||||||
|
self.poller.register(self.socket, zmq.POLLIN)
|
||||||
|
self.timeout_ms = timeout_ms
|
||||||
|
|
||||||
|
async def poll(self, timeout_ms: int | None = None) -> T | None:
|
||||||
|
"""
|
||||||
|
Get data from the socket, or None if the timeout is reached.
|
||||||
|
|
||||||
|
:param timeout_ms: If given, the timeout. Otherwise, `self.timeout_ms` is used.
|
||||||
|
:return: Data from the socket or None.
|
||||||
|
"""
|
||||||
|
timeout_ms = timeout_ms or self.timeout_ms
|
||||||
|
socks = dict(self.poller.poll(timeout_ms))
|
||||||
|
if socks.get(self.socket) == zmq.POLLIN:
|
||||||
|
return await self.socket.recv()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class Streaming(CyclicBehaviour):
|
||||||
|
def __init__(self, audio_in_socket: azmq.Socket, audio_out_socket: azmq.Socket):
|
||||||
|
super().__init__()
|
||||||
|
self.audio_in_poller = SocketPoller[bytes](audio_in_socket)
|
||||||
|
self.model, _ = torch.hub.load(
|
||||||
|
repo_or_dir="snakers4/silero-vad", model="silero_vad", force_reload=False
|
||||||
|
)
|
||||||
|
self.audio_out_socket = audio_out_socket
|
||||||
|
|
||||||
|
self.audio_buffer = np.array([], dtype=np.float32)
|
||||||
|
self.i_since_speech = 100 # Used to allow small pauses in speech
|
||||||
|
|
||||||
|
async def run(self) -> None:
|
||||||
|
data = await self.audio_in_poller.poll()
|
||||||
|
if data is None:
|
||||||
|
if len(self.audio_buffer) > 0:
|
||||||
|
logger.debug("No audio data received. Discarding buffer until new data arrives.")
|
||||||
|
self.audio_buffer = np.array([], dtype=np.float32)
|
||||||
|
self.i_since_speech = 100
|
||||||
|
return
|
||||||
|
|
||||||
|
# copy otherwise Torch will be sad that it's immutable
|
||||||
|
chunk = np.frombuffer(data, dtype=np.float32).copy()
|
||||||
|
prob = self.model(torch.from_numpy(chunk), 16000).item()
|
||||||
|
|
||||||
|
if prob > 0.5:
|
||||||
|
if self.i_since_speech > 3:
|
||||||
|
logger.debug("Speech started.")
|
||||||
|
self.audio_buffer = np.append(self.audio_buffer, chunk)
|
||||||
|
self.i_since_speech = 0
|
||||||
|
return
|
||||||
|
self.i_since_speech += 1
|
||||||
|
|
||||||
|
# prob < 0.5, so speech maybe ended. Wait a bit more before to be more certain
|
||||||
|
if self.i_since_speech <= 3:
|
||||||
|
self.audio_buffer = np.append(self.audio_buffer, chunk)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Speech probably ended. Make sure we have a usable amount of data.
|
||||||
|
if len(self.audio_buffer) >= 3 * len(chunk):
|
||||||
|
logger.debug("Speech ended.")
|
||||||
|
await self.audio_out_socket.send(self.audio_buffer[: -2 * len(chunk)].tobytes())
|
||||||
|
|
||||||
|
# At this point, we know that the speech has ended.
|
||||||
|
# Prepend the last chunk that had no speech, for a more fluent boundary
|
||||||
|
self.audio_buffer = chunk
|
||||||
|
|
||||||
|
|
||||||
|
class VADAgent(Agent):
|
||||||
|
"""
|
||||||
|
An agent which listens to an audio stream, does Voice Activity Detection (VAD), and sends
|
||||||
|
fragments with detected speech to other agents over ZeroMQ.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, audio_in_address: str, audio_in_bind: bool):
|
||||||
|
jid = settings.agent_settings.vad_agent_name + "@" + settings.agent_settings.host
|
||||||
|
super().__init__(jid, settings.agent_settings.vad_agent_name)
|
||||||
|
|
||||||
|
self.audio_in_address = audio_in_address
|
||||||
|
self.audio_in_bind = audio_in_bind
|
||||||
|
|
||||||
|
self.audio_in_socket: azmq.Socket | None = None
|
||||||
|
self.audio_out_socket: azmq.Socket | None = None
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
"""
|
||||||
|
Stop listening to audio, stop publishing audio, close sockets.
|
||||||
|
"""
|
||||||
|
if self.audio_in_socket is not None:
|
||||||
|
self.audio_in_socket.close()
|
||||||
|
self.audio_in_socket = None
|
||||||
|
if self.audio_out_socket is not None:
|
||||||
|
self.audio_out_socket.close()
|
||||||
|
self.audio_out_socket = None
|
||||||
|
return await super().stop()
|
||||||
|
|
||||||
|
def _connect_audio_in_socket(self):
|
||||||
|
self.audio_in_socket = zmq_context.socket(zmq.SUB)
|
||||||
|
self.audio_in_socket.setsockopt_string(zmq.SUBSCRIBE, "")
|
||||||
|
if self.audio_in_bind:
|
||||||
|
self.audio_in_socket.bind(self.audio_in_address)
|
||||||
|
else:
|
||||||
|
self.audio_in_socket.connect(self.audio_in_address)
|
||||||
|
self.audio_in_poller = SocketPoller[bytes](self.audio_in_socket)
|
||||||
|
|
||||||
|
def _connect_audio_out_socket(self) -> int | None:
|
||||||
|
"""Returns the port bound, or None if binding failed."""
|
||||||
|
try:
|
||||||
|
self.audio_out_socket = zmq_context.socket(zmq.PUB)
|
||||||
|
return self.audio_out_socket.bind_to_random_port("tcp://*", max_tries=100)
|
||||||
|
except zmq.ZMQBindError:
|
||||||
|
logger.error("Failed to bind an audio output socket after 100 tries.")
|
||||||
|
self.audio_out_socket = None
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def setup(self):
|
||||||
|
logger.info("Setting up %s", self.jid)
|
||||||
|
|
||||||
|
self._connect_audio_in_socket()
|
||||||
|
|
||||||
|
audio_out_port = self._connect_audio_out_socket()
|
||||||
|
if audio_out_port is None:
|
||||||
|
await self.stop()
|
||||||
|
return
|
||||||
|
|
||||||
|
streaming = Streaming(self.audio_in_socket, self.audio_out_socket)
|
||||||
|
self.add_behaviour(streaming)
|
||||||
|
|
||||||
|
# ... start agents dependent on the output audio fragments here
|
||||||
|
|
||||||
|
logger.info("Finished setting up %s", self.jid)
|
||||||
22
src/control_backend/api/v1/endpoints/command.py
Normal file
22
src/control_backend/api/v1/endpoints/command.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from fastapi import APIRouter, Request
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from zmq import Socket
|
||||||
|
|
||||||
|
from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/command", status_code=202)
|
||||||
|
async def receive_command(command: SpeechCommand, request: Request):
|
||||||
|
# Validate and retrieve data.
|
||||||
|
SpeechCommand.model_validate(command)
|
||||||
|
topic = b"command"
|
||||||
|
pub_socket: Socket = request.app.state.internal_comm_socket
|
||||||
|
pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
||||||
|
|
||||||
|
|
||||||
|
return {"status": "Command received"}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
from fastapi.routing import APIRouter
|
from fastapi.routing import APIRouter
|
||||||
|
|
||||||
from control_backend.api.v1.endpoints import message, sse
|
from control_backend.api.v1.endpoints import message, sse, command
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|
||||||
api_router.include_router(message.router, tags=["Messages"])
|
api_router.include_router(message.router, tags=["Messages"])
|
||||||
|
|
||||||
api_router.include_router(sse.router, tags=["SSE"])
|
api_router.include_router(sse.router, tags=["SSE"])
|
||||||
|
|
||||||
|
api_router.include_router(command.router, tags=["Commands"])
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ class AgentSettings(BaseModel):
|
|||||||
host: str = "localhost"
|
host: str = "localhost"
|
||||||
bdi_core_agent_name: str = "bdi_core"
|
bdi_core_agent_name: str = "bdi_core"
|
||||||
belief_collector_agent_name: str = "belief_collector"
|
belief_collector_agent_name: str = "belief_collector"
|
||||||
test_agent_name: str = "test_agent"
|
vad_agent_name: str = "vad_agent"
|
||||||
|
|
||||||
|
ri_communication_agent_name: str = "ri_communication_agent"
|
||||||
|
ri_command_agent_name: str = "ri_command_agent"
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ from fastapi import FastAPI
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
# Internal imports
|
# Internal imports
|
||||||
|
from control_backend.agents.ri_communication_agent import RICommunicationAgent
|
||||||
from control_backend.agents.bdi.bdi_core import BDICoreAgent
|
from control_backend.agents.bdi.bdi_core import BDICoreAgent
|
||||||
|
from control_backend.agents.vad_agent import VADAgent
|
||||||
from control_backend.api.v1.router import api_router
|
from control_backend.api.v1.router import api_router
|
||||||
from control_backend.core.config import settings
|
from control_backend.core.config import settings
|
||||||
from control_backend.core.zmq_context import context
|
from control_backend.core.zmq_context import context
|
||||||
@@ -30,6 +32,14 @@ async def lifespan(app: FastAPI):
|
|||||||
logger.info("Internal publishing socket bound to %s", internal_comm_socket)
|
logger.info("Internal publishing socket bound to %s", internal_comm_socket)
|
||||||
|
|
||||||
# Initiate agents
|
# Initiate agents
|
||||||
|
ri_communication_agent = RICommunicationAgent(
|
||||||
|
settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host,
|
||||||
|
settings.agent_settings.ri_communication_agent_name,
|
||||||
|
address="tcp://*:5555",
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
await ri_communication_agent.start()
|
||||||
|
|
||||||
bdi_core = BDICoreAgent(
|
bdi_core = BDICoreAgent(
|
||||||
settings.agent_settings.bdi_core_agent_name + "@" + settings.agent_settings.host,
|
settings.agent_settings.bdi_core_agent_name + "@" + settings.agent_settings.host,
|
||||||
settings.agent_settings.bdi_core_agent_name,
|
settings.agent_settings.bdi_core_agent_name,
|
||||||
@@ -37,6 +47,9 @@ async def lifespan(app: FastAPI):
|
|||||||
)
|
)
|
||||||
await bdi_core.start()
|
await bdi_core.start()
|
||||||
|
|
||||||
|
_temp_vad_agent = VADAgent("tcp://localhost:5558", False)
|
||||||
|
await _temp_vad_agent.start()
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
logger.info("%s shutting down.", app.title)
|
logger.info("%s shutting down.", app.title)
|
||||||
|
|||||||
20
src/control_backend/schemas/ri_message.py
Normal file
20
src/control_backend/schemas/ri_message.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Literal
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field, ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
class RIEndpoint(str, Enum):
|
||||||
|
SPEECH = "actuate/speech"
|
||||||
|
PING = "ping"
|
||||||
|
NEGOTIATE_PORTS = "negotiate/ports"
|
||||||
|
|
||||||
|
|
||||||
|
class RIMessage(BaseModel):
|
||||||
|
endpoint: RIEndpoint
|
||||||
|
data: Any
|
||||||
|
|
||||||
|
|
||||||
|
class SpeechCommand(RIMessage):
|
||||||
|
endpoint: RIEndpoint = RIEndpoint(RIEndpoint.SPEECH)
|
||||||
|
data: str
|
||||||
102
test/integration/agents/test_ri_commands_agent.py
Normal file
102
test/integration/agents/test_ri_commands_agent.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
import asyncio
|
||||||
|
import zmq
|
||||||
|
import json
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
from control_backend.agents.ri_command_agent import RICommandAgent
|
||||||
|
from control_backend.schemas.ri_message import SpeechCommand
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_bind(monkeypatch):
|
||||||
|
"""Test setup with bind=True"""
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"control_backend.agents.ri_command_agent.context.socket", lambda _: fake_socket
|
||||||
|
)
|
||||||
|
|
||||||
|
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")),
|
||||||
|
)
|
||||||
|
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# Ensure PUB socket bound
|
||||||
|
fake_socket.bind.assert_any_call("tcp://localhost:5555")
|
||||||
|
# Ensure SUB socket connected to internal address and subscribed
|
||||||
|
fake_socket.connect.assert_any_call("tcp://internal:1234")
|
||||||
|
fake_socket.setsockopt.assert_any_call(zmq.SUBSCRIBE, b"command")
|
||||||
|
|
||||||
|
# Ensure behaviour attached
|
||||||
|
assert any(isinstance(b, agent.SendCommandsBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_connect(monkeypatch):
|
||||||
|
"""Test setup with bind=False"""
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"control_backend.agents.ri_command_agent.context.socket", lambda _: fake_socket
|
||||||
|
)
|
||||||
|
|
||||||
|
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")),
|
||||||
|
)
|
||||||
|
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# Ensure PUB socket connected
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_commands_behaviour_valid_message():
|
||||||
|
"""Test behaviour with valid JSON message"""
|
||||||
|
fake_socket = AsyncMock()
|
||||||
|
message_dict = {"message": "hello"}
|
||||||
|
fake_socket.recv_multipart = AsyncMock(
|
||||||
|
return_value=(b"command", json.dumps(message_dict).encode("utf-8"))
|
||||||
|
)
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
|
||||||
|
agent = RICommandAgent("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:
|
||||||
|
mock_message = MagicMock()
|
||||||
|
MockSpeechCommand.model_validate.return_value = mock_message
|
||||||
|
|
||||||
|
await behaviour.run()
|
||||||
|
|
||||||
|
fake_socket.recv_multipart.assert_awaited()
|
||||||
|
fake_socket.send_json.assert_awaited_with(mock_message.model_dump())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_commands_behaviour_invalid_message(caplog):
|
||||||
|
"""Test behaviour with invalid JSON message triggers error logging"""
|
||||||
|
fake_socket = AsyncMock()
|
||||||
|
fake_socket.recv_multipart = AsyncMock(return_value=(b"command", b"{invalid_json}"))
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
|
||||||
|
agent = RICommandAgent("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()
|
||||||
|
|
||||||
|
fake_socket.recv_multipart.assert_awaited()
|
||||||
|
fake_socket.send_json.assert_not_awaited()
|
||||||
|
assert "Error processing message" in caplog.text
|
||||||
591
test/integration/agents/test_ri_communication_agent.py
Normal file
591
test/integration/agents/test_ri_communication_agent.py
Normal file
@@ -0,0 +1,591 @@
|
|||||||
|
import asyncio
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch, ANY
|
||||||
|
from control_backend.agents.ri_communication_agent import RICommunicationAgent
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_correct_negototiate_1():
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 5555, "bind": False},
|
||||||
|
{"id": "actuation", "port": 5556, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_correct_negototiate_2():
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 5555, "bind": False},
|
||||||
|
{"id": "actuation", "port": 5557, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_correct_negototiate_3():
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 5555, "bind": True},
|
||||||
|
{"id": "actuation", "port": 5557, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_correct_negototiate_4():
|
||||||
|
# Different port, do bind
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 4555, "bind": True},
|
||||||
|
{"id": "actuation", "port": 5557, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_correct_negototiate_5():
|
||||||
|
# Different port, dont bind.
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 4555, "bind": False},
|
||||||
|
{"id": "actuation", "port": 5557, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_wrong_negototiate_1():
|
||||||
|
return AsyncMock(return_value={"endpoint": "ping", "data": ""})
|
||||||
|
|
||||||
|
|
||||||
|
def fake_json_invalid_id_negototiate():
|
||||||
|
return AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "banana", "port": 4555, "bind": False},
|
||||||
|
{"id": "tomato", "port": 5557, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_1(monkeypatch):
|
||||||
|
"""
|
||||||
|
Test the setup of the communication agent
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
) 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
|
||||||
|
)
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
fake_agent_instance.start.assert_awaited()
|
||||||
|
MockCommandAgent.assert_called_once_with(
|
||||||
|
ANY, # Server Name
|
||||||
|
ANY, # Server Password
|
||||||
|
address="tcp://*:5556", # derived from the 'port' value in negotiation
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
# Ensure the agent attached a ListenBehaviour
|
||||||
|
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_2(monkeypatch):
|
||||||
|
"""
|
||||||
|
Test the setup of the communication agent
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
) 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
|
||||||
|
)
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
fake_agent_instance.start.assert_awaited()
|
||||||
|
MockCommandAgent.assert_called_once_with(
|
||||||
|
ANY, # Server Name
|
||||||
|
ANY, # Server Password
|
||||||
|
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
# Ensure the agent attached a ListenBehaviour
|
||||||
|
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog):
|
||||||
|
"""
|
||||||
|
Test the functionality of setup with incorrect negotiation message
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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, 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:
|
||||||
|
fake_agent_instance = MockCommandAgent.return_value
|
||||||
|
fake_agent_instance.start = AsyncMock()
|
||||||
|
|
||||||
|
# --- Act ---
|
||||||
|
with caplog.at_level("ERROR"):
|
||||||
|
agent = RICommunicationAgent(
|
||||||
|
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||||
|
)
|
||||||
|
await agent.setup(max_retries=1)
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Ensure the agent did not attach a ListenBehaviour
|
||||||
|
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_4(monkeypatch):
|
||||||
|
"""
|
||||||
|
Test the setup of the communication agent with different bind value
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
) 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
|
||||||
|
)
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.bind.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
fake_agent_instance.start.assert_awaited()
|
||||||
|
MockCommandAgent.assert_called_once_with(
|
||||||
|
ANY, # Server Name
|
||||||
|
ANY, # Server Password
|
||||||
|
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
# Ensure the agent attached a ListenBehaviour
|
||||||
|
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_5(monkeypatch):
|
||||||
|
"""
|
||||||
|
Test the setup of the communication agent
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
) 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
|
||||||
|
)
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
fake_agent_instance.start.assert_awaited()
|
||||||
|
MockCommandAgent.assert_called_once_with(
|
||||||
|
ANY, # Server Name
|
||||||
|
ANY, # Server Password
|
||||||
|
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
# Ensure the agent attached a ListenBehaviour
|
||||||
|
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_6(monkeypatch):
|
||||||
|
"""
|
||||||
|
Test the setup of the communication agent
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
) 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
|
||||||
|
)
|
||||||
|
await agent.setup()
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None})
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
fake_agent_instance.start.assert_awaited()
|
||||||
|
MockCommandAgent.assert_called_once_with(
|
||||||
|
ANY, # Server Name
|
||||||
|
ANY, # Server Password
|
||||||
|
address="tcp://*:5557", # derived from the 'port' value in negotiation
|
||||||
|
bind=True,
|
||||||
|
)
|
||||||
|
# Ensure the agent attached a ListenBehaviour
|
||||||
|
assert any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog):
|
||||||
|
"""
|
||||||
|
Test the functionality of setup with incorrect id
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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, 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:
|
||||||
|
fake_agent_instance = MockCommandAgent.return_value
|
||||||
|
fake_agent_instance.start = AsyncMock()
|
||||||
|
|
||||||
|
# --- Act ---
|
||||||
|
with caplog.at_level("WARNING"):
|
||||||
|
agent = RICommunicationAgent(
|
||||||
|
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||||
|
)
|
||||||
|
await agent.setup(max_retries=1)
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
|
||||||
|
# Since it failed, there should not be any command agent.
|
||||||
|
fake_agent_instance.start.assert_not_awaited()
|
||||||
|
assert "Unhandled negotiation id:" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog):
|
||||||
|
"""
|
||||||
|
Test the functionality of setup with incorrect negotiation message
|
||||||
|
"""
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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:
|
||||||
|
fake_agent_instance = MockCommandAgent.return_value
|
||||||
|
fake_agent_instance.start = AsyncMock()
|
||||||
|
|
||||||
|
# --- Act ---
|
||||||
|
with caplog.at_level("WARNING"):
|
||||||
|
agent = RICommunicationAgent(
|
||||||
|
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||||
|
)
|
||||||
|
await agent.setup(max_retries=1)
|
||||||
|
|
||||||
|
# --- Assert ---
|
||||||
|
fake_socket.connect.assert_any_call("tcp://localhost:5555")
|
||||||
|
|
||||||
|
# Since it failed, there should not be any command agent.
|
||||||
|
fake_agent_instance.start.assert_not_awaited()
|
||||||
|
assert "No connection established in 20 seconds" in caplog.text
|
||||||
|
|
||||||
|
# Ensure the agent did not attach a ListenBehaviour
|
||||||
|
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
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": {}})
|
||||||
|
|
||||||
|
# TODO: Integration test between actual server and password needed for spade agents
|
||||||
|
agent = RICommunicationAgent("test@server", "password")
|
||||||
|
agent.req_socket = fake_socket
|
||||||
|
|
||||||
|
behaviour = agent.ListenBehaviour()
|
||||||
|
agent.add_behaviour(behaviour)
|
||||||
|
|
||||||
|
# Run once (CyclicBehaviour normally loops)
|
||||||
|
with caplog.at_level("DEBUG"):
|
||||||
|
await behaviour.run()
|
||||||
|
|
||||||
|
fake_socket.send_json.assert_awaited()
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
assert "Received message" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_listen_behaviour_ping_wrong_endpoint(caplog):
|
||||||
|
"""
|
||||||
|
Test if our listen behaviour can work with wrong messages (wrong endpoint)
|
||||||
|
"""
|
||||||
|
fake_socket = AsyncMock()
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
|
||||||
|
# This is a message for the wrong endpoint >:(
|
||||||
|
fake_socket.recv_json = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [
|
||||||
|
{"id": "main", "port": 5555, "bind": False},
|
||||||
|
{"id": "actuation", "port": 5556, "bind": True},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
agent = RICommunicationAgent("test@server", "password")
|
||||||
|
agent.req_socket = fake_socket
|
||||||
|
|
||||||
|
behaviour = agent.ListenBehaviour()
|
||||||
|
agent.add_behaviour(behaviour)
|
||||||
|
|
||||||
|
# Run once (CyclicBehaviour normally loops)
|
||||||
|
with caplog.at_level("INFO"):
|
||||||
|
await behaviour.run()
|
||||||
|
|
||||||
|
assert "Received message with topic different than ping, while ping expected." in caplog.text
|
||||||
|
fake_socket.send_json.assert_awaited()
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_listen_behaviour_timeout(caplog):
|
||||||
|
fake_socket = AsyncMock()
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
# recv_json will never resolve, simulate timeout
|
||||||
|
fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError)
|
||||||
|
|
||||||
|
agent = RICommunicationAgent("test@server", "password")
|
||||||
|
agent.req_socket = fake_socket
|
||||||
|
|
||||||
|
behaviour = agent.ListenBehaviour()
|
||||||
|
agent.add_behaviour(behaviour)
|
||||||
|
|
||||||
|
with caplog.at_level("INFO"):
|
||||||
|
await behaviour.run()
|
||||||
|
|
||||||
|
assert "No ping retrieved in 3 seconds" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_listen_behaviour_ping_no_endpoint(caplog):
|
||||||
|
"""
|
||||||
|
Test if our listen behaviour can work with wrong messages (wrong endpoint)
|
||||||
|
"""
|
||||||
|
fake_socket = AsyncMock()
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
|
||||||
|
# This is a message without endpoint >:(
|
||||||
|
fake_socket.recv_json = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"data": "I dont have an endpoint >:)",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
agent = RICommunicationAgent("test@server", "password")
|
||||||
|
agent.req_socket = fake_socket
|
||||||
|
|
||||||
|
behaviour = agent.ListenBehaviour()
|
||||||
|
agent.add_behaviour(behaviour)
|
||||||
|
|
||||||
|
# Run once (CyclicBehaviour normally loops)
|
||||||
|
with caplog.at_level("ERROR"):
|
||||||
|
await behaviour.run()
|
||||||
|
|
||||||
|
assert "No received endpoint in message, excepted ping endpoint." in caplog.text
|
||||||
|
fake_socket.send_json.assert_awaited()
|
||||||
|
fake_socket.recv_json.assert_awaited()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_unexpected_exception(monkeypatch, caplog):
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
with caplog.at_level("ERROR"):
|
||||||
|
await agent.setup(max_retries=1)
|
||||||
|
|
||||||
|
# Ensure that the error was logged
|
||||||
|
assert "Unexpected error during negotiation: boom!" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_setup_unpacking_exception(monkeypatch, caplog):
|
||||||
|
# --- Arrange ---
|
||||||
|
fake_socket = MagicMock()
|
||||||
|
fake_socket.send_json = AsyncMock()
|
||||||
|
|
||||||
|
# Make recv_json return malformed negotiation data to trigger unpacking exception
|
||||||
|
malformed_data = {
|
||||||
|
"endpoint": "negotiate/ports",
|
||||||
|
"data": [{"id": "main"}],
|
||||||
|
} # 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
|
||||||
|
) as MockCommandAgent:
|
||||||
|
fake_agent_instance = MockCommandAgent.return_value
|
||||||
|
fake_agent_instance.start = AsyncMock()
|
||||||
|
|
||||||
|
agent = RICommunicationAgent(
|
||||||
|
"test@server", "password", address="tcp://localhost:5555", bind=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# --- Act & Assert ---
|
||||||
|
with caplog.at_level("ERROR"):
|
||||||
|
await agent.setup(max_retries=1)
|
||||||
|
|
||||||
|
# Ensure the unpacking exception was logged
|
||||||
|
assert "Error unpacking negotiation data" in caplog.text
|
||||||
|
|
||||||
|
# Ensure no command agent was started
|
||||||
|
fake_agent_instance.start.assert_not_awaited()
|
||||||
|
|
||||||
|
# Ensure no behaviour was attached
|
||||||
|
assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours)
|
||||||
Binary file not shown.
99
test/integration/agents/vad_agent/test_vad_agent.py
Normal file
99
test/integration/agents/vad_agent/test_vad_agent.py
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import zmq
|
||||||
|
from spade.agent import Agent
|
||||||
|
|
||||||
|
from control_backend.agents.vad_agent import VADAgent
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def zmq_context(mocker):
|
||||||
|
return mocker.patch("control_backend.agents.vad_agent.zmq_context")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def streaming(mocker):
|
||||||
|
return mocker.patch("control_backend.agents.vad_agent.Streaming")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_normal_setup(streaming):
|
||||||
|
"""
|
||||||
|
Test that during normal setup, the VAD agent creates a Streaming behavior and creates audio
|
||||||
|
sockets.
|
||||||
|
"""
|
||||||
|
vad_agent = VADAgent("tcp://localhost:12345", False)
|
||||||
|
vad_agent.add_behaviour = MagicMock()
|
||||||
|
|
||||||
|
await vad_agent.setup()
|
||||||
|
|
||||||
|
streaming.assert_called_once()
|
||||||
|
vad_agent.add_behaviour.assert_called_once_with(streaming.return_value)
|
||||||
|
assert vad_agent.audio_in_socket is not None
|
||||||
|
assert vad_agent.audio_out_socket is not None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("do_bind", [True, False])
|
||||||
|
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)
|
||||||
|
|
||||||
|
vad_agent._connect_audio_in_socket()
|
||||||
|
|
||||||
|
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, "")
|
||||||
|
|
||||||
|
if do_bind:
|
||||||
|
zmq_context.socket.return_value.bind.assert_called_once_with("tcp://*:12345")
|
||||||
|
else:
|
||||||
|
zmq_context.socket.return_value.connect.assert_called_once_with("tcp://localhost:12345")
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
vad_agent._connect_audio_out_socket()
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
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
|
||||||
|
vad_agent = VADAgent("tcp://localhost:12345", False)
|
||||||
|
|
||||||
|
await vad_agent.setup()
|
||||||
|
|
||||||
|
assert vad_agent.audio_out_socket is None
|
||||||
|
mock_super_stop.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_stop(zmq_context):
|
||||||
|
"""
|
||||||
|
Test that when the VAD agent is stopped, the sockets are closed correctly.
|
||||||
|
"""
|
||||||
|
vad_agent = VADAgent("tcp://localhost:12345", False)
|
||||||
|
|
||||||
|
await vad_agent.setup()
|
||||||
|
await vad_agent.stop()
|
||||||
|
|
||||||
|
assert zmq_context.socket.return_value.close.call_count == 2
|
||||||
|
assert vad_agent.audio_in_socket is None
|
||||||
|
assert vad_agent.audio_out_socket is None
|
||||||
57
test/integration/agents/vad_agent/test_vad_with_audio.py
Normal file
57
test/integration/agents/vad_agent/test_vad_with_audio.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import os
|
||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import soundfile as sf
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from control_backend.agents.vad_agent import Streaming
|
||||||
|
|
||||||
|
|
||||||
|
def get_audio_chunks() -> list[bytes]:
|
||||||
|
curr_file = os.path.realpath(__file__)
|
||||||
|
curr_dir = os.path.dirname(curr_file)
|
||||||
|
file = f"{curr_dir}/speech_with_pauses_16k_1c_float32.wav"
|
||||||
|
|
||||||
|
chunk_size = 512
|
||||||
|
|
||||||
|
chunks = []
|
||||||
|
|
||||||
|
with sf.SoundFile(file, "r") as f:
|
||||||
|
assert f.samplerate == 16000
|
||||||
|
assert f.channels == 1
|
||||||
|
assert f.subtype == "FLOAT"
|
||||||
|
|
||||||
|
while True:
|
||||||
|
data = f.read(chunk_size, dtype="float32")
|
||||||
|
if len(data) != chunk_size:
|
||||||
|
break
|
||||||
|
|
||||||
|
chunks.append(data.tobytes())
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_real_audio(mocker):
|
||||||
|
"""
|
||||||
|
Test the VAD agent with only input and output mocked. Using the real model, using real audio as
|
||||||
|
input. Ensure that it outputs some fragments with audio.
|
||||||
|
"""
|
||||||
|
audio_chunks = get_audio_chunks()
|
||||||
|
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.return_value.poll.return_value = [(audio_in_socket, zmq.POLLIN)]
|
||||||
|
|
||||||
|
audio_out_socket = AsyncMock()
|
||||||
|
|
||||||
|
vad_streamer = Streaming(audio_in_socket, audio_out_socket)
|
||||||
|
for _ in audio_chunks:
|
||||||
|
await vad_streamer.run()
|
||||||
|
|
||||||
|
audio_out_socket.send.assert_called()
|
||||||
|
for args in audio_out_socket.send.call_args_list:
|
||||||
|
assert isinstance(args[0][0], bytes)
|
||||||
|
assert len(args[0][0]) >= 512 * 4 * 3 # Should be at least 3 chunks of audio
|
||||||
63
test/integration/api/endpoints/test_command_endpoint.py
Normal file
63
test/integration/api/endpoints/test_command_endpoint.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from control_backend.api.v1.endpoints import command
|
||||||
|
from control_backend.schemas.ri_message import SpeechCommand
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def app():
|
||||||
|
"""
|
||||||
|
Creates a FastAPI test app and attaches the router under test.
|
||||||
|
Also sets up a mock internal_comm_socket.
|
||||||
|
"""
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(command.router)
|
||||||
|
app.state.internal_comm_socket = MagicMock() # mock ZMQ socket
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client(app):
|
||||||
|
"""Create a test client for the app."""
|
||||||
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_receive_command_endpoint(client, app):
|
||||||
|
"""
|
||||||
|
Test that a POST to /command sends the right multipart message
|
||||||
|
and returns a 202 with the expected JSON body.
|
||||||
|
"""
|
||||||
|
mock_socket = app.state.internal_comm_socket
|
||||||
|
|
||||||
|
# Prepare test payload that matches SpeechCommand
|
||||||
|
payload = {"endpoint": "actuate/speech", "data": "yooo"}
|
||||||
|
|
||||||
|
# Send POST request
|
||||||
|
response = client.post("/command", json=payload)
|
||||||
|
|
||||||
|
# Check response
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def test_receive_command_invalid_payload(client):
|
||||||
|
"""
|
||||||
|
Test invalid data handling (schema validation).
|
||||||
|
"""
|
||||||
|
# Missing required field(s)
|
||||||
|
bad_payload = {"invalid": "data"}
|
||||||
|
response = client.post("/command", json=bad_payload)
|
||||||
|
assert response.status_code == 422 # validation error
|
||||||
36
test/integration/schemas/test_ri_message.py
Normal file
36
test/integration/schemas/test_ri_message.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import pytest
|
||||||
|
from control_backend.schemas.ri_message import RIMessage, RIEndpoint, SpeechCommand
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
def valid_command_1():
|
||||||
|
return SpeechCommand(data="Hallo?")
|
||||||
|
|
||||||
|
|
||||||
|
def invalid_command_1():
|
||||||
|
return RIMessage(endpoint=RIEndpoint.PING, data="Hello again.")
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_speech_command_1():
|
||||||
|
command = valid_command_1()
|
||||||
|
try:
|
||||||
|
RIMessage.model_validate(command)
|
||||||
|
SpeechCommand.model_validate(command)
|
||||||
|
assert True
|
||||||
|
except ValidationError:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_speech_command_1():
|
||||||
|
command = invalid_command_1()
|
||||||
|
passed_ri_message_validation = False
|
||||||
|
try:
|
||||||
|
# Should succeed, still.
|
||||||
|
RIMessage.model_validate(command)
|
||||||
|
passed_ri_message_validation = True
|
||||||
|
|
||||||
|
# Should fail.
|
||||||
|
SpeechCommand.model_validate(command)
|
||||||
|
assert False
|
||||||
|
except ValidationError:
|
||||||
|
assert passed_ri_message_validation
|
||||||
46
test/unit/agents/test_vad_socket_poller.py
Normal file
46
test/unit/agents/test_vad_socket_poller.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from control_backend.agents.vad_agent import SocketPoller
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def socket():
|
||||||
|
return AsyncMock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_socket_poller_with_data(socket, mocker):
|
||||||
|
socket_data = b"test"
|
||||||
|
socket.recv.return_value = socket_data
|
||||||
|
|
||||||
|
mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller")
|
||||||
|
mock_poller.return_value.poll.return_value = [(socket, zmq.POLLIN)]
|
||||||
|
|
||||||
|
poller = SocketPoller(socket)
|
||||||
|
# Calling `poll` twice to be able to check that the poller is reused
|
||||||
|
await poller.poll()
|
||||||
|
data = await poller.poll()
|
||||||
|
|
||||||
|
assert data == socket_data
|
||||||
|
|
||||||
|
# Ensure that the poller was reused
|
||||||
|
mock_poller.assert_called_once_with()
|
||||||
|
mock_poller.return_value.register.assert_called_once_with(socket, zmq.POLLIN)
|
||||||
|
|
||||||
|
assert socket.recv.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_socket_poller_no_data(socket, mocker):
|
||||||
|
mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller")
|
||||||
|
mock_poller.return_value.poll.return_value = []
|
||||||
|
|
||||||
|
poller = SocketPoller(socket)
|
||||||
|
data = await poller.poll()
|
||||||
|
|
||||||
|
assert data is None
|
||||||
|
|
||||||
|
socket.recv.assert_not_called()
|
||||||
95
test/unit/agents/test_vad_streaming.py
Normal file
95
test/unit/agents/test_vad_streaming.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from control_backend.agents.vad_agent import Streaming
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def audio_in_socket():
|
||||||
|
return AsyncMock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def audio_out_socket():
|
||||||
|
return AsyncMock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def streaming(audio_in_socket, audio_out_socket):
|
||||||
|
import torch
|
||||||
|
|
||||||
|
torch.hub.load.return_value = (..., ...) # Mock
|
||||||
|
return Streaming(audio_in_socket, audio_out_socket)
|
||||||
|
|
||||||
|
|
||||||
|
async def simulate_streaming_with_probabilities(streaming, probabilities: list[float]):
|
||||||
|
"""
|
||||||
|
Simulates a streaming scenario with given VAD model probabilities for testing purposes.
|
||||||
|
|
||||||
|
:param streaming: The streaming component to be tested.
|
||||||
|
:param probabilities: A list of probabilities representing the outputs of the VAD model.
|
||||||
|
"""
|
||||||
|
model_item = MagicMock()
|
||||||
|
model_item.item.side_effect = probabilities
|
||||||
|
streaming.model = MagicMock()
|
||||||
|
streaming.model.return_value = model_item
|
||||||
|
|
||||||
|
audio_in_poller = AsyncMock()
|
||||||
|
audio_in_poller.poll.return_value = np.empty(shape=512, dtype=np.float32)
|
||||||
|
streaming.audio_in_poller = audio_in_poller
|
||||||
|
|
||||||
|
for _ in probabilities:
|
||||||
|
await streaming.run()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_voice_activity_detected(audio_in_socket, audio_out_socket, streaming):
|
||||||
|
"""
|
||||||
|
Test a scenario where there is voice activity detected between silences.
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
speech_chunk_count = 5
|
||||||
|
probabilities = [0.0] * 5 + [1.0] * speech_chunk_count + [0.0] * 5
|
||||||
|
await simulate_streaming_with_probabilities(streaming, probabilities)
|
||||||
|
|
||||||
|
audio_out_socket.send.assert_called_once()
|
||||||
|
data = audio_out_socket.send.call_args[0][0]
|
||||||
|
assert isinstance(data, bytes)
|
||||||
|
# each sample has 512 frames of 4 bytes, expecting 7 chunks (5 with speech, 2 as padding)
|
||||||
|
assert len(data) == 512 * 4 * (speech_chunk_count + 2)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_voice_activity_short_pause(audio_in_socket, audio_out_socket, streaming):
|
||||||
|
"""
|
||||||
|
Test a scenario where there is a short pause between speech, checking whether it ignores the
|
||||||
|
short pause.
|
||||||
|
"""
|
||||||
|
speech_chunk_count = 5
|
||||||
|
probabilities = (
|
||||||
|
[0.0] * 5 + [1.0] * speech_chunk_count + [0.0] + [1.0] * speech_chunk_count + [0.0] * 5
|
||||||
|
)
|
||||||
|
await simulate_streaming_with_probabilities(streaming, probabilities)
|
||||||
|
|
||||||
|
audio_out_socket.send.assert_called_once()
|
||||||
|
data = audio_out_socket.send.call_args[0][0]
|
||||||
|
assert isinstance(data, bytes)
|
||||||
|
# Expecting 13 chunks (2*5 with speech, 1 pause between, 2 as padding)
|
||||||
|
assert len(data) == 512 * 4 * (speech_chunk_count * 2 + 1 + 2)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_no_data(audio_in_socket, audio_out_socket, streaming):
|
||||||
|
"""
|
||||||
|
Test a scenario where there is no data received. This should not cause errors.
|
||||||
|
"""
|
||||||
|
audio_in_poller = AsyncMock()
|
||||||
|
audio_in_poller.poll.return_value = None
|
||||||
|
streaming.audio_in_poller = audio_in_poller
|
||||||
|
|
||||||
|
await streaming.run()
|
||||||
|
|
||||||
|
audio_out_socket.send.assert_not_called()
|
||||||
|
assert len(streaming.audio_buffer) == 0
|
||||||
@@ -33,3 +33,13 @@ def pytest_configure(config):
|
|||||||
mock_config_module.settings = MagicMock()
|
mock_config_module.settings = MagicMock()
|
||||||
|
|
||||||
sys.modules["control_backend.core.config"] = mock_config_module
|
sys.modules["control_backend.core.config"] = mock_config_module
|
||||||
|
|
||||||
|
# --- Mock torch and zmq for VAD ---
|
||||||
|
mock_torch = MagicMock()
|
||||||
|
mock_zmq = MagicMock()
|
||||||
|
mock_zmq.asyncio = mock_zmq
|
||||||
|
|
||||||
|
# In individual tests, these can be imported and the return values changed
|
||||||
|
sys.modules["torch"] = mock_torch
|
||||||
|
sys.modules["zmq"] = mock_zmq
|
||||||
|
sys.modules["zmq.asyncio"] = mock_zmq.asyncio
|
||||||
35
uv.lock
generated
35
uv.lock
generated
@@ -1332,10 +1332,15 @@ source = { virtual = "." }
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "fastapi", extra = ["all"] },
|
{ name = "fastapi", extra = ["all"] },
|
||||||
{ name = "mlx-whisper", marker = "sys_platform == 'darwin'" },
|
{ name = "mlx-whisper", marker = "sys_platform == 'darwin'" },
|
||||||
|
{ name = "numpy" },
|
||||||
{ name = "openai-whisper" },
|
{ name = "openai-whisper" },
|
||||||
{ name = "pyaudio" },
|
{ name = "pyaudio" },
|
||||||
{ name = "pydantic" },
|
{ name = "pydantic" },
|
||||||
{ name = "pydantic-settings" },
|
{ name = "pydantic-settings" },
|
||||||
|
{ name = "pytest" },
|
||||||
|
{ name = "pytest-asyncio" },
|
||||||
|
{ name = "pytest-cov" },
|
||||||
|
{ name = "pytest-mock" },
|
||||||
{ name = "pyzmq" },
|
{ name = "pyzmq" },
|
||||||
{ name = "silero-vad" },
|
{ name = "silero-vad" },
|
||||||
{ name = "spade" },
|
{ name = "spade" },
|
||||||
@@ -1350,7 +1355,11 @@ dev = [
|
|||||||
{ name = "ruff" },
|
{ name = "ruff" },
|
||||||
{ name = "ruff-format" },
|
{ name = "ruff-format" },
|
||||||
]
|
]
|
||||||
|
integration-test = [
|
||||||
|
{ name = "soundfile" },
|
||||||
|
]
|
||||||
test = [
|
test = [
|
||||||
|
{ name = "numpy" },
|
||||||
{ name = "pytest" },
|
{ name = "pytest" },
|
||||||
{ name = "pytest-asyncio" },
|
{ name = "pytest-asyncio" },
|
||||||
{ name = "pytest-cov" },
|
{ name = "pytest-cov" },
|
||||||
@@ -1361,10 +1370,15 @@ test = [
|
|||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "fastapi", extras = ["all"], specifier = ">=0.115.6" },
|
{ name = "fastapi", extras = ["all"], specifier = ">=0.115.6" },
|
||||||
{ name = "mlx-whisper", marker = "sys_platform == 'darwin'", specifier = ">=0.4.3" },
|
{ name = "mlx-whisper", marker = "sys_platform == 'darwin'", specifier = ">=0.4.3" },
|
||||||
|
{ name = "numpy", specifier = ">=2.3.3" },
|
||||||
{ name = "openai-whisper", specifier = ">=20250625" },
|
{ name = "openai-whisper", specifier = ">=20250625" },
|
||||||
{ name = "pyaudio", specifier = ">=0.2.14" },
|
{ name = "pyaudio", specifier = ">=0.2.14" },
|
||||||
{ name = "pydantic", specifier = ">=2.12.0" },
|
{ name = "pydantic", specifier = ">=2.12.0" },
|
||||||
{ name = "pydantic-settings", specifier = ">=2.11.0" },
|
{ name = "pydantic-settings", specifier = ">=2.11.0" },
|
||||||
|
{ name = "pytest", specifier = ">=8.4.2" },
|
||||||
|
{ name = "pytest-asyncio", specifier = ">=1.2.0" },
|
||||||
|
{ name = "pytest-cov", specifier = ">=7.0.0" },
|
||||||
|
{ name = "pytest-mock", specifier = ">=3.15.1" },
|
||||||
{ name = "pyzmq", specifier = ">=27.1.0" },
|
{ name = "pyzmq", specifier = ">=27.1.0" },
|
||||||
{ name = "silero-vad", specifier = ">=6.0.0" },
|
{ name = "silero-vad", specifier = ">=6.0.0" },
|
||||||
{ name = "spade", specifier = ">=4.1.0" },
|
{ name = "spade", specifier = ">=4.1.0" },
|
||||||
@@ -1379,7 +1393,9 @@ dev = [
|
|||||||
{ name = "ruff", specifier = ">=0.14.2" },
|
{ name = "ruff", specifier = ">=0.14.2" },
|
||||||
{ name = "ruff-format", specifier = ">=0.3.0" },
|
{ name = "ruff-format", specifier = ">=0.3.0" },
|
||||||
]
|
]
|
||||||
|
integration-test = [{ name = "soundfile", specifier = ">=0.13.1" }]
|
||||||
test = [
|
test = [
|
||||||
|
{ name = "numpy", specifier = ">=2.3.3" },
|
||||||
{ name = "pytest", specifier = ">=8.4.2" },
|
{ name = "pytest", specifier = ">=8.4.2" },
|
||||||
{ name = "pytest-asyncio", specifier = ">=1.2.0" },
|
{ name = "pytest-asyncio", specifier = ">=1.2.0" },
|
||||||
{ name = "pytest-cov", specifier = ">=7.0.0" },
|
{ name = "pytest-cov", specifier = ">=7.0.0" },
|
||||||
@@ -2208,6 +2224,25 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" },
|
{ url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "soundfile"
|
||||||
|
version = "0.13.1"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "cffi" },
|
||||||
|
{ name = "numpy" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/e1/41/9b873a8c055582859b239be17902a85339bec6a30ad162f98c9b0288a2cc/soundfile-0.13.1.tar.gz", hash = "sha256:b2c68dab1e30297317080a5b43df57e302584c49e2942defdde0acccc53f0e5b", size = 46156, upload-time = "2025-01-25T09:17:04.831Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/64/28/e2a36573ccbcf3d57c00626a21fe51989380636e821b341d36ccca0c1c3a/soundfile-0.13.1-py2.py3-none-any.whl", hash = "sha256:a23c717560da2cf4c7b5ae1142514e0fd82d6bbd9dfc93a50423447142f2c445", size = 25751, upload-time = "2025-01-25T09:16:44.235Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/ea/ab/73e97a5b3cc46bba7ff8650a1504348fa1863a6f9d57d7001c6b67c5f20e/soundfile-0.13.1-py2.py3-none-macosx_10_9_x86_64.whl", hash = "sha256:82dc664d19831933fe59adad199bf3945ad06d84bc111a5b4c0d3089a5b9ec33", size = 1142250, upload-time = "2025-01-25T09:16:47.583Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/a0/e5/58fd1a8d7b26fc113af244f966ee3aecf03cb9293cb935daaddc1e455e18/soundfile-0.13.1-py2.py3-none-macosx_11_0_arm64.whl", hash = "sha256:743f12c12c4054921e15736c6be09ac26b3b3d603aef6fd69f9dde68748f2593", size = 1101406, upload-time = "2025-01-25T09:16:49.662Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/58/ae/c0e4a53d77cf6e9a04179535766b3321b0b9ced5f70522e4caf9329f0046/soundfile-0.13.1-py2.py3-none-manylinux_2_28_aarch64.whl", hash = "sha256:9c9e855f5a4d06ce4213f31918653ab7de0c5a8d8107cd2427e44b42df547deb", size = 1235729, upload-time = "2025-01-25T09:16:53.018Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/57/5e/70bdd9579b35003a489fc850b5047beeda26328053ebadc1fb60f320f7db/soundfile-0.13.1-py2.py3-none-manylinux_2_28_x86_64.whl", hash = "sha256:03267c4e493315294834a0870f31dbb3b28a95561b80b134f0bd3cf2d5f0e618", size = 1313646, upload-time = "2025-01-25T09:16:54.872Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/fe/df/8c11dc4dfceda14e3003bb81a0d0edcaaf0796dd7b4f826ea3e532146bba/soundfile-0.13.1-py2.py3-none-win32.whl", hash = "sha256:c734564fab7c5ddf8e9be5bf70bab68042cd17e9c214c06e365e20d64f9a69d5", size = 899881, upload-time = "2025-01-25T09:16:56.663Z" },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/14/e9/6b761de83277f2f02ded7e7ea6f07828ec78e4b229b80e4ca55dd205b9dc/soundfile-0.13.1-py2.py3-none-win_amd64.whl", hash = "sha256:1e70a05a0626524a69e9f0f4dd2ec174b4e9567f4d8b6c11d38b5c289be36ee9", size = 1019162, upload-time = "2025-01-25T09:16:59.573Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spade"
|
name = "spade"
|
||||||
version = "4.1.0"
|
version = "4.1.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user