feat: apply new agent naming standards
Expanding abbreviations to remove ambiguity, simplifying agent names to reduce repetition. ref: N25B-257
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from control_backend.agents.bdi.belief_collector_agent.behaviours.belief_collector_behaviour import ( # noqa: E501
|
||||
BeliefCollectorBehaviour,
|
||||
)
|
||||
|
||||
|
||||
def create_mock_message(sender_node: str, body: str) -> MagicMock:
|
||||
"""Helper function to create a configured mock message."""
|
||||
msg = MagicMock()
|
||||
msg.sender.node = sender_node # MagicMock automatically creates nested mocks
|
||||
msg.body = body
|
||||
return msg
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_agent(mocker):
|
||||
"""Fixture to create a mock Agent."""
|
||||
agent = MagicMock()
|
||||
agent.jid = "belief_collector_agent@test"
|
||||
return agent
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bel_collector_behaviouror(mock_agent, mocker):
|
||||
"""Fixture to create an instance of BelCollectorBehaviour with a mocked agent."""
|
||||
# Patch asyncio.sleep to prevent tests from actually waiting
|
||||
mocker.patch("asyncio.sleep", return_value=None)
|
||||
|
||||
collector = BeliefCollectorBehaviour()
|
||||
collector.agent = mock_agent
|
||||
# Mock the receive method, we will control its return value in each test
|
||||
collector.receive = AsyncMock()
|
||||
return collector
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_message_received(bel_collector_behaviouror, mocker):
|
||||
"""
|
||||
Test that when a message is received, _process_message is called with that message.
|
||||
"""
|
||||
# Arrange
|
||||
mock_msg = MagicMock()
|
||||
bel_collector_behaviouror.receive.return_value = mock_msg
|
||||
mocker.patch.object(bel_collector_behaviouror, "_process_message")
|
||||
|
||||
# Act
|
||||
await bel_collector_behaviouror.run()
|
||||
|
||||
# Assert
|
||||
bel_collector_behaviouror._process_message.assert_awaited_once_with(mock_msg)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_routes_to_handle_belief_text_by_type(bel_collector_behaviouror, mocker):
|
||||
msg = create_mock_message(
|
||||
"anyone",
|
||||
json.dumps({"type": "belief_extraction_text", "beliefs": {"user_said": [["hi"]]}}),
|
||||
)
|
||||
spy = mocker.patch.object(bel_collector_behaviouror, "_handle_belief_text", new=AsyncMock())
|
||||
await bel_collector_behaviouror._process_message(msg)
|
||||
spy.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_routes_to_handle_belief_text_by_sender(bel_collector_behaviouror, mocker):
|
||||
msg = create_mock_message(
|
||||
"bel_text_agent_mock", json.dumps({"beliefs": {"user_said": [["hi"]]}})
|
||||
)
|
||||
spy = mocker.patch.object(bel_collector_behaviouror, "_handle_belief_text", new=AsyncMock())
|
||||
await bel_collector_behaviouror._process_message(msg)
|
||||
spy.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_routes_to_handle_emo_text(bel_collector_behaviouror, mocker):
|
||||
msg = create_mock_message("anyone", json.dumps({"type": "emotion_extraction_text"}))
|
||||
spy = mocker.patch.object(bel_collector_behaviouror, "_handle_emo_text", new=AsyncMock())
|
||||
await bel_collector_behaviouror._process_message(msg)
|
||||
spy.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_belief_text_happy_path_sends(bel_collector_behaviouror, mocker):
|
||||
payload = {"type": "belief_extraction_text", "beliefs": {"user_said": ["hello test", "No"]}}
|
||||
bel_collector_behaviouror.send = AsyncMock()
|
||||
await bel_collector_behaviouror._handle_belief_text(payload, "bel_text_agent_mock")
|
||||
|
||||
# make sure we attempted a send
|
||||
bel_collector_behaviouror.send.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_belief_text_coerces_non_strings(bel_collector_behaviouror, mocker):
|
||||
payload = {"type": "belief_extraction_text", "beliefs": {"user_said": [["hi", 123]]}}
|
||||
bel_collector_behaviouror.send = AsyncMock()
|
||||
await bel_collector_behaviouror._handle_belief_text(payload, "origin")
|
||||
bel_collector_behaviouror.send.assert_awaited_once()
|
||||
Reference in New Issue
Block a user