1+ """
2+ Tests for tool alias consistency across managed agent backends.
3+
4+ Ensures that the shared TOOL_ALIAS_MAP is properly imported and used
5+ consistently between managed_agents.py and managed_local.py to prevent
6+ contract drift and silent behavioral differences.
7+ """
8+
9+ import pytest
10+ from unittest import TestCase
11+
12+
13+ class TestToolAliasConsistency (TestCase ):
14+ """Test suite for tool alias mapping consistency."""
15+
16+ def test_tool_alias_single_source_of_truth (self ):
17+ """Assert module-level identity - both modules use the same dict object."""
18+ # Import the shared mapping
19+ from praisonai .praisonai .integrations ._tool_aliases import TOOL_ALIAS_MAP as shared_map
20+
21+ # Import the mapping as used in managed_agents.py
22+ from praisonai .praisonai .integrations .managed_agents import TOOL_ALIAS_MAP as agents_map
23+
24+ # Import the mapping as used in managed_local.py
25+ from praisonai .praisonai .integrations .managed_local import TOOL_ALIAS_MAP as local_map
26+
27+ # Assert all three are the same object (module-level identity)
28+ assert shared_map is agents_map , "managed_agents.py should import the shared TOOL_ALIAS_MAP"
29+ assert shared_map is local_map , "managed_local.py should import the shared TOOL_ALIAS_MAP"
30+ assert agents_map is local_map , "Both modules should use the same dict object"
31+
32+ def test_known_aliases_stable (self ):
33+ """Lock in the final mapping so future changes need deliberate test updates."""
34+ from praisonai .praisonai .integrations ._tool_aliases import TOOL_ALIAS_MAP
35+
36+ # Expected stable mapping based on consolidation decisions
37+ expected_mapping = {
38+ "bash" : "execute_command" ,
39+ "read" : "read_file" ,
40+ "write" : "write_file" ,
41+ "edit" : "apply_diff" ,
42+ "glob" : "list_files" ,
43+ "grep" : "search_file" ,
44+ "web_fetch" : "web_fetch" ,
45+ "search" : "search_web" ,
46+ "web_search" : "search_web" ,
47+ }
48+
49+ # Assert the mapping matches exactly
50+ assert TOOL_ALIAS_MAP == expected_mapping , (
51+ f"TOOL_ALIAS_MAP has changed from expected stable mapping.\n "
52+ f"Expected: { expected_mapping } \n "
53+ f"Actual: { TOOL_ALIAS_MAP } \n "
54+ f"If this change is intentional, update this test."
55+ )
56+
57+ def test_resolved_conflicts (self ):
58+ """Verify that previously conflicting mappings are resolved consistently."""
59+ from praisonai .praisonai .integrations ._tool_aliases import TOOL_ALIAS_MAP
60+
61+ # Test conflict resolutions based on issue analysis:
62+
63+ # grep: chose 'search_file' over 'execute_command'
64+ # (matches PraisonAI grep_tool.py built-in)
65+ assert TOOL_ALIAS_MAP ["grep" ] == "search_file"
66+
67+ # web_fetch: chose 'web_fetch' over 'web_crawl'
68+ # (keeping original name as no web_crawl tool found)
69+ assert TOOL_ALIAS_MAP ["web_fetch" ] == "web_fetch"
70+
71+ # edit: chose 'apply_diff' over 'write_file'
72+ # (matches PraisonAI code/tools/apply_diff.py)
73+ assert TOOL_ALIAS_MAP ["edit" ] == "apply_diff"
74+
75+ def test_backward_compatibility (self ):
76+ """Verify that functions using the alias map maintain their signatures."""
77+ from praisonai .praisonai .integrations .managed_agents import map_managed_tools
78+
79+ # Test function signature and behavior is preserved
80+ test_tools = ["bash" , "grep" , "web_fetch" , "unknown_tool" ]
81+ result = map_managed_tools (test_tools )
82+
83+ expected = ["execute_command" , "search_file" , "web_fetch" , "unknown_tool" ]
84+ assert result == expected , f"map_managed_tools should map known tools and pass through unknown ones"
85+
86+ def test_no_duplicate_definitions (self ):
87+ """Ensure that TOOL_MAPPING and local TOOL_ALIAS_MAP definitions are removed."""
88+ import inspect
89+
90+ # Check managed_agents.py doesn't have TOOL_MAPPING anymore
91+ from praisonai .praisonai import integrations
92+ managed_agents_module = integrations .managed_agents
93+
94+ # TOOL_MAPPING should not exist as a module-level variable
95+ assert not hasattr (managed_agents_module , 'TOOL_MAPPING' ), (
96+ "TOOL_MAPPING should be removed from managed_agents.py"
97+ )
98+
99+ # Check that managed_local.py source doesn't contain local definition
100+ import praisonai .praisonai .integrations .managed_local as managed_local_module
101+ source = inspect .getsource (managed_local_module )
102+
103+ # Should not have a local TOOL_ALIAS_MAP definition
104+ assert 'TOOL_ALIAS_MAP = {' not in source , (
105+ "Local TOOL_ALIAS_MAP definition should be removed from managed_local.py"
106+ )
107+
108+ # Should import from _tool_aliases
109+ assert 'from ._tool_aliases import TOOL_ALIAS_MAP' in source , (
110+ "managed_local.py should import TOOL_ALIAS_MAP from _tool_aliases"
111+ )
112+
113+
114+ if __name__ == "__main__" :
115+ pytest .main ([__file__ ])
0 commit comments