|
| 1 | +from copy import deepcopy |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from astrbot.core.subagent_orchestrator import SubAgentOrchestrator |
| 7 | + |
| 8 | + |
| 9 | +def _build_cfg(agent_overrides: dict) -> dict: |
| 10 | + agent = { |
| 11 | + "name": "planner", |
| 12 | + "enabled": True, |
| 13 | + "persona_id": None, |
| 14 | + "system_prompt": "inline prompt", |
| 15 | + "public_description": "", |
| 16 | + "tools": ["tool_a", " ", "tool_b"], |
| 17 | + } |
| 18 | + agent.update(agent_overrides) |
| 19 | + return {"agents": [agent]} |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_reload_from_config_default_persona_is_resolved(): |
| 24 | + tool_mgr = MagicMock() |
| 25 | + persona_mgr = MagicMock() |
| 26 | + default_persona = { |
| 27 | + "name": "default", |
| 28 | + "prompt": "You are a helpful and friendly assistant.", |
| 29 | + "tools": None, |
| 30 | + "_begin_dialogs_processed": [], |
| 31 | + } |
| 32 | + persona_mgr.get_persona_v3_by_id.return_value = deepcopy(default_persona) |
| 33 | + orchestrator = SubAgentOrchestrator(tool_mgr=tool_mgr, persona_mgr=persona_mgr) |
| 34 | + |
| 35 | + await orchestrator.reload_from_config(_build_cfg({"persona_id": "default"})) |
| 36 | + |
| 37 | + assert len(orchestrator.handoffs) == 1 |
| 38 | + handoff = orchestrator.handoffs[0] |
| 39 | + assert handoff.agent.instructions == default_persona["prompt"] |
| 40 | + assert handoff.agent.tools is None |
| 41 | + assert handoff.agent.begin_dialogs == default_persona["_begin_dialogs_processed"] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_reload_from_config_missing_persona_falls_back_to_inline_and_warns(): |
| 46 | + tool_mgr = MagicMock() |
| 47 | + persona_mgr = MagicMock() |
| 48 | + persona_mgr.get_persona_v3_by_id.return_value = None |
| 49 | + orchestrator = SubAgentOrchestrator(tool_mgr=tool_mgr, persona_mgr=persona_mgr) |
| 50 | + |
| 51 | + with patch("astrbot.core.subagent_orchestrator.logger") as mock_logger: |
| 52 | + await orchestrator.reload_from_config(_build_cfg({"persona_id": "not_exists"})) |
| 53 | + |
| 54 | + assert len(orchestrator.handoffs) == 1 |
| 55 | + handoff = orchestrator.handoffs[0] |
| 56 | + assert handoff.agent.instructions == "inline prompt" |
| 57 | + assert handoff.agent.tools == ["tool_a", "tool_b"] |
| 58 | + assert handoff.agent.begin_dialogs is None |
| 59 | + mock_logger.warning.assert_called_once_with( |
| 60 | + "SubAgent persona %s not found, fallback to inline prompt.", |
| 61 | + "not_exists", |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_reload_from_config_uses_processed_begin_dialogs_and_deepcopy(): |
| 67 | + tool_mgr = MagicMock() |
| 68 | + persona_mgr = MagicMock() |
| 69 | + processed_dialogs = [{"role": "user", "content": "hello", "_no_save": True}] |
| 70 | + persona_mgr.get_persona_v3_by_id.return_value = { |
| 71 | + "name": "custom", |
| 72 | + "prompt": "persona prompt", |
| 73 | + "tools": ["tool_from_persona"], |
| 74 | + "_begin_dialogs_processed": processed_dialogs, |
| 75 | + } |
| 76 | + orchestrator = SubAgentOrchestrator(tool_mgr=tool_mgr, persona_mgr=persona_mgr) |
| 77 | + |
| 78 | + await orchestrator.reload_from_config(_build_cfg({"persona_id": "custom"})) |
| 79 | + processed_dialogs[0]["content"] = "mutated" |
| 80 | + |
| 81 | + handoff = orchestrator.handoffs[0] |
| 82 | + assert handoff.agent.instructions == "persona prompt" |
| 83 | + assert handoff.agent.tools == ["tool_from_persona"] |
| 84 | + assert handoff.agent.begin_dialogs[0]["content"] == "hello" |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +@pytest.mark.parametrize( |
| 89 | + ("raw_tools", "expected_tools"), |
| 90 | + [ |
| 91 | + (None, None), |
| 92 | + ([], []), |
| 93 | + ("not-a-list", []), |
| 94 | + ], |
| 95 | +) |
| 96 | +async def test_reload_from_config_tool_normalization(raw_tools, expected_tools): |
| 97 | + tool_mgr = MagicMock() |
| 98 | + persona_mgr = MagicMock() |
| 99 | + persona_mgr.get_persona_v3_by_id.return_value = { |
| 100 | + "name": "custom", |
| 101 | + "prompt": "persona prompt", |
| 102 | + "tools": raw_tools, |
| 103 | + "_begin_dialogs_processed": [], |
| 104 | + } |
| 105 | + orchestrator = SubAgentOrchestrator(tool_mgr=tool_mgr, persona_mgr=persona_mgr) |
| 106 | + |
| 107 | + await orchestrator.reload_from_config(_build_cfg({"persona_id": "custom"})) |
| 108 | + |
| 109 | + handoff = orchestrator.handoffs[0] |
| 110 | + assert handoff.agent.tools == expected_tools |
0 commit comments