|
| 1 | +import os |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from swarms.structs.agent import Agent |
4 | 6 | from swarms.structs.multi_agent_router import MultiAgentRouter |
5 | 7 |
|
6 | 8 |
|
| 9 | +def _minimal_agents(): |
| 10 | + """Lightweight agents for tests that don't need a real LLM call.""" |
| 11 | + return [ |
| 12 | + Agent( |
| 13 | + agent_name="Agent1", |
| 14 | + agent_description="Test Agent1", |
| 15 | + system_prompt="You are Agent1", |
| 16 | + model_name="openai/gpt-4o", |
| 17 | + max_loops=1, |
| 18 | + verbose=False, |
| 19 | + print_on=False, |
| 20 | + ), |
| 21 | + Agent( |
| 22 | + agent_name="Agent2", |
| 23 | + agent_description="Test Agent2", |
| 24 | + system_prompt="You are Agent2", |
| 25 | + model_name="openai/gpt-4o", |
| 26 | + max_loops=1, |
| 27 | + verbose=False, |
| 28 | + print_on=False, |
| 29 | + ), |
| 30 | + ] |
| 31 | + |
| 32 | + |
7 | 33 | # Test fixtures |
8 | 34 | def real_agents(): |
9 | 35 | """Create real agents for testing""" |
@@ -349,5 +375,59 @@ def test_concurrent_large_batch_processing(): |
349 | 375 | assert all(isinstance(result, (list, dict)) for result in results) |
350 | 376 |
|
351 | 377 |
|
| 378 | +# ============================================================================ |
| 379 | +# BOSS SYSTEM PROMPT / DISCOVERY TESTS |
| 380 | +# ============================================================================ |
| 381 | + |
| 382 | + |
| 383 | +def test_boss_system_prompt_contains_agent_names(): |
| 384 | + """The generated boss prompt must mention each registered agent by name.""" |
| 385 | + router = MultiAgentRouter(agents=_minimal_agents()) |
| 386 | + prompt = router._create_boss_system_prompt() |
| 387 | + |
| 388 | + assert "Agent1" in prompt |
| 389 | + assert "Agent2" in prompt |
| 390 | + assert "You are a boss agent" in prompt |
| 391 | + |
| 392 | + |
| 393 | +def test_agents_dict_membership(): |
| 394 | + """router.agents is a name-keyed dict; lookups should be membership tests.""" |
| 395 | + router = MultiAgentRouter(agents=_minimal_agents()) |
| 396 | + assert "Agent1" in router.agents |
| 397 | + assert "NonexistentAgent" not in router.agents |
| 398 | + |
| 399 | + |
| 400 | +# ============================================================================ |
| 401 | +# CONSTRUCTION-TIME VALIDATION |
| 402 | +# ============================================================================ |
| 403 | + |
| 404 | + |
| 405 | +def test_missing_api_key_raises(): |
| 406 | + """MultiAgentRouter with no agents and no OPENAI_API_KEY must raise ValueError.""" |
| 407 | + saved = os.environ.pop("OPENAI_API_KEY", None) |
| 408 | + try: |
| 409 | + with pytest.raises(ValueError, match="OpenAI API key"): |
| 410 | + MultiAgentRouter(agents=[]) |
| 411 | + finally: |
| 412 | + if saved is not None: |
| 413 | + os.environ["OPENAI_API_KEY"] = saved |
| 414 | + |
| 415 | + |
| 416 | +def test_route_task_with_no_agents_raises(): |
| 417 | + """Routing with an empty agent list must raise.""" |
| 418 | + if not os.getenv("OPENAI_API_KEY"): |
| 419 | + pytest.skip("OPENAI_API_KEY not set") |
| 420 | + router = MultiAgentRouter(agents=[]) |
| 421 | + with pytest.raises(Exception): |
| 422 | + router.route_task("Test task") |
| 423 | + |
| 424 | + |
| 425 | +def test_route_task_with_empty_task_raises(): |
| 426 | + """An empty task string must raise ValueError.""" |
| 427 | + router = MultiAgentRouter(agents=_minimal_agents()) |
| 428 | + with pytest.raises(ValueError): |
| 429 | + router.route_task("") |
| 430 | + |
| 431 | + |
352 | 432 | if __name__ == "__main__": |
353 | 433 | pytest.main([__file__]) |
0 commit comments