1+ #!/usr/bin/env python3
2+ """
3+ Comprehensive test to verify display_generating fix works for both OpenAI and custom LLM paths.
4+ Tests the complete fix for issue #981.
5+ """
6+
7+ import sys
8+ import os
9+
10+ # Add the source path to Python path
11+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), 'src' , 'praisonai-agents' ))
12+
13+ def test_display_logic ():
14+ """Test the core logic that determines when display_generating should be called"""
15+ print ("=== Testing Display Logic ===" )
16+
17+ # Test cases covering all scenarios
18+ test_cases = [
19+ {"stream" : False , "verbose" : False , "expected" : False , "description" : "No display (stream=False, verbose=False)" },
20+ {"stream" : False , "verbose" : True , "expected" : True , "description" : "Display in verbose mode (stream=False, verbose=True) - MAIN FIX" },
21+ {"stream" : True , "verbose" : False , "expected" : True , "description" : "Display in stream mode (stream=True, verbose=False)" },
22+ {"stream" : True , "verbose" : True , "expected" : True , "description" : "Display in both modes (stream=True, verbose=True)" },
23+ ]
24+
25+ print (f"{ 'Description' :<55} { 'Stream' :<8} { 'Verbose' :<8} { 'Expected' :<8} { 'Result' :<8} { 'Status' } " )
26+ print ("-" * 95 )
27+
28+ all_passed = True
29+ for case in test_cases :
30+ # Test the actual logic used in the fix
31+ result = (case ["stream" ] or case ["verbose" ])
32+ expected = case ["expected" ]
33+ status = "✅ PASS" if result == expected else "❌ FAIL"
34+
35+ if result != expected :
36+ all_passed = False
37+
38+ print (f"{ case ['description' ]:<55} { str (case ['stream' ]):<8} { str (case ['verbose' ]):<8} { str (expected ):<8} { str (result ):<8} { status } " )
39+
40+ print ("-" * 95 )
41+ if all_passed :
42+ print ("✅ All logic tests PASSED!" )
43+ else :
44+ print ("❌ Some logic tests FAILED!" )
45+ sys .exit (1 )
46+
47+ return all_passed
48+
49+ def test_agent_paths ():
50+ """Test that both OpenAI and custom LLM paths are correctly handled"""
51+ print ("\n === Testing Agent Path Coverage ===" )
52+
53+ # Test file inspection - check that both paths have the fix
54+ agent_file = os .path .join (os .path .dirname (__file__ ), 'src' , 'praisonai-agents' , 'praisonaiagents' , 'agent' , 'agent.py' )
55+
56+ if not os .path .exists (agent_file ):
57+ print ("❌ Agent file not found" )
58+ return False
59+
60+ with open (agent_file , 'r' ) as f :
61+ content = f .read ()
62+
63+ # Check for OpenAI path fix
64+ openai_fix = "display_fn=display_generating if (stream or self.verbose) else None"
65+ has_openai_fix = openai_fix in content
66+
67+ # Check for custom LLM path fix
68+ custom_llm_fix = "if (stream or self.verbose) and self.console:"
69+ has_custom_fix = custom_llm_fix in content
70+
71+ print (f"OpenAI path fix present: { '✅ YES' if has_openai_fix else '❌ NO' } " )
72+ print (f"Custom LLM path fix present: { '✅ YES' if has_custom_fix else '❌ NO' } " )
73+
74+ if has_openai_fix and has_custom_fix :
75+ print ("✅ Both agent paths have the display fix!" )
76+ return True
77+ else :
78+ print ("❌ Missing fix in one or both paths!" )
79+ return False
80+
81+ def test_backward_compatibility ():
82+ """Test that existing functionality is preserved"""
83+ print ("\n === Testing Backward Compatibility ===" )
84+
85+ # Test cases that should maintain existing behavior
86+ scenarios = [
87+ {"name" : "Default streaming behavior" , "stream" : True , "verbose" : True , "should_display" : True },
88+ {"name" : "Non-verbose non-streaming" , "stream" : False , "verbose" : False , "should_display" : False },
89+ {"name" : "Streaming with verbose off" , "stream" : True , "verbose" : False , "should_display" : True },
90+ ]
91+
92+ all_compat = True
93+ for scenario in scenarios :
94+ result = (scenario ["stream" ] or scenario ["verbose" ])
95+ expected = scenario ["should_display" ]
96+ status = "✅ COMPATIBLE" if result == expected else "❌ INCOMPATIBLE"
97+
98+ if result != expected :
99+ all_compat = False
100+
101+ print (f"{ scenario ['name' ]:<30} : { status } " )
102+
103+ if all_compat :
104+ print ("✅ All backward compatibility tests PASSED!" )
105+ else :
106+ print ("❌ Backward compatibility issues detected!" )
107+
108+ return all_compat
109+
110+ if __name__ == "__main__" :
111+ print ("Comprehensive Display Fix Test for Issue #981" )
112+ print ("=" * 50 )
113+
114+ # Run all tests
115+ logic_ok = test_display_logic ()
116+ paths_ok = test_agent_paths ()
117+ compat_ok = test_backward_compatibility ()
118+
119+ # Final result
120+ print ("\n " + "=" * 50 )
121+ if logic_ok and paths_ok and compat_ok :
122+ print ("🎉 ALL TESTS PASSED - Fix is comprehensive and correct!" )
123+ print ("✅ Issue #981 is fully resolved for both OpenAI and custom LLM users" )
124+ sys .exit (0 )
125+ else :
126+ print ("❌ SOME TESTS FAILED - Fix needs more work" )
127+ sys .exit (1 )
0 commit comments