chore: added missing tests
This commit is contained in:
@@ -8,7 +8,17 @@ import pytest
|
||||
|
||||
from control_backend.agents.bdi.bdi_program_manager import BDIProgramManager
|
||||
from control_backend.core.agent_system import InternalMessage
|
||||
from control_backend.schemas.program import BasicNorm, Goal, Phase, Plan, Program
|
||||
from control_backend.schemas.program import (
|
||||
BasicNorm,
|
||||
ConditionalNorm,
|
||||
Goal,
|
||||
InferredBelief,
|
||||
KeywordBelief,
|
||||
Phase,
|
||||
Plan,
|
||||
Program,
|
||||
Trigger,
|
||||
)
|
||||
|
||||
# Fix Windows Proactor loop for zmq
|
||||
if sys.platform.startswith("win"):
|
||||
@@ -295,3 +305,98 @@ async def test_setup(mock_settings):
|
||||
|
||||
# 3. Adds behavior
|
||||
manager.add_behavior.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_program_to_user_interrupt(mock_settings):
|
||||
"""Test directly sending the program to the user interrupt agent."""
|
||||
mock_settings.agent_settings.user_interrupt_name = "user_interrupt_agent"
|
||||
|
||||
manager = BDIProgramManager(name="program_manager_test")
|
||||
manager.send = AsyncMock()
|
||||
|
||||
program = Program.model_validate_json(make_valid_program_json())
|
||||
|
||||
await manager._send_program_to_user_interrupt(program)
|
||||
|
||||
assert manager.send.await_count == 1
|
||||
msg = manager.send.await_args[0][0]
|
||||
assert msg.to == "user_interrupt_agent"
|
||||
assert msg.thread == "new_program"
|
||||
assert "Basic Phase" in msg.body
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_complex_program_extraction():
|
||||
manager = BDIProgramManager(name="program_manager_test")
|
||||
|
||||
# 1. Create Complex Components
|
||||
|
||||
# Inferred Belief (A & B)
|
||||
belief_left = KeywordBelief(id=uuid.uuid4(), name="b1", keyword="hot")
|
||||
belief_right = KeywordBelief(id=uuid.uuid4(), name="b2", keyword="sunny")
|
||||
inferred_belief = InferredBelief(
|
||||
id=uuid.uuid4(), name="b_inf", operator="AND", left=belief_left, right=belief_right
|
||||
)
|
||||
|
||||
# Conditional Norm
|
||||
cond_norm = ConditionalNorm(
|
||||
id=uuid.uuid4(), name="norm_cond", norm="wear_hat", condition=inferred_belief
|
||||
)
|
||||
|
||||
# Trigger with Inferred Belief condition
|
||||
dummy_plan = Plan(id=uuid.uuid4(), name="dummy_plan", steps=[])
|
||||
trigger = Trigger(id=uuid.uuid4(), name="trigger_1", condition=inferred_belief, plan=dummy_plan)
|
||||
|
||||
# Nested Goal
|
||||
sub_goal = Goal(
|
||||
id=uuid.uuid4(),
|
||||
name="sub_goal",
|
||||
description="desc",
|
||||
plan=Plan(id=uuid.uuid4(), name="empty", steps=[]),
|
||||
can_fail=True,
|
||||
)
|
||||
|
||||
parent_goal = Goal(
|
||||
id=uuid.uuid4(),
|
||||
name="parent_goal",
|
||||
description="desc",
|
||||
# The plan contains the sub_goal as a step
|
||||
plan=Plan(id=uuid.uuid4(), name="parent_plan", steps=[sub_goal]),
|
||||
can_fail=False,
|
||||
)
|
||||
|
||||
# 2. Assemble Program
|
||||
phase = Phase(
|
||||
id=uuid.uuid4(),
|
||||
name="Complex Phase",
|
||||
norms=[cond_norm],
|
||||
goals=[parent_goal],
|
||||
triggers=[trigger],
|
||||
)
|
||||
program = Program(phases=[phase])
|
||||
|
||||
# 3. Initialize Internal State (Triggers _populate_goal_mapping -> Nested Goal logic)
|
||||
manager._initialize_internal_state(program)
|
||||
|
||||
# Assertion for Line 53-54 (Mapping population)
|
||||
# Both parent and sub-goal should be mapped
|
||||
assert str(parent_goal.id) in manager._goal_mapping
|
||||
assert str(sub_goal.id) in manager._goal_mapping
|
||||
|
||||
# 4. Test Belief Extraction (Triggers lines 132-133, 142-146)
|
||||
beliefs = manager._extract_current_beliefs()
|
||||
|
||||
# Should extract recursive beliefs from cond_norm and trigger
|
||||
# Inferred belief splits into Left + Right. Since we use it twice, we get duplicates
|
||||
# checking existence is enough.
|
||||
belief_names = [b.name for b in beliefs]
|
||||
assert "b1" in belief_names
|
||||
assert "b2" in belief_names
|
||||
|
||||
# 5. Test Goal Extraction (Triggers lines 173, 185)
|
||||
goals = manager._extract_current_goals()
|
||||
|
||||
goal_names = [g.name for g in goals]
|
||||
assert "parent_goal" in goal_names
|
||||
assert "sub_goal" in goal_names
|
||||
|
||||
Reference in New Issue
Block a user