1+ """
2+ Tests for unified SessionInfo schema across managed agent backends.
3+
4+ Ensures that AnthropicManagedAgent and LocalManagedAgent return consistent
5+ retrieve_session() schemas as specified in issue #1429.
6+ """
7+
8+ import pytest
9+ from unittest .mock import Mock , patch
10+ from typing import Dict , Any
11+
12+ # Test SessionInfo dataclass directly
13+ def test_session_info_defaults ():
14+ """Test SessionInfo provides proper defaults for all fields."""
15+ # Import from the wrapper package where SessionInfo is defined
16+ from praisonai .integrations ._session_info import SessionInfo
17+
18+ # Test default instance
19+ info = SessionInfo ()
20+ assert info .id == ""
21+ assert info .status == "unknown"
22+ assert info .title == ""
23+ assert info .usage == {"input_tokens" : 0 , "output_tokens" : 0 }
24+
25+ # Test to_dict() returns expected structure
26+ result = info .to_dict ()
27+ expected = {
28+ "id" : "" ,
29+ "status" : "unknown" ,
30+ "title" : "" ,
31+ "usage" : {"input_tokens" : 0 , "output_tokens" : 0 }
32+ }
33+ assert result == expected
34+
35+
36+ def test_session_info_partial_usage ():
37+ """Test SessionInfo handles partial usage dictionaries properly."""
38+ from praisonai .integrations ._session_info import SessionInfo
39+
40+ # Test partial usage (missing output_tokens)
41+ info = SessionInfo (usage = {"input_tokens" : 100 })
42+ assert info .usage ["input_tokens" ] == 100
43+ assert info .usage ["output_tokens" ] == 0
44+
45+ # Test partial usage (missing input_tokens)
46+ info = SessionInfo (usage = {"output_tokens" : 200 })
47+ assert info .usage ["input_tokens" ] == 0
48+ assert info .usage ["output_tokens" ] == 200
49+
50+
51+ def test_session_info_complete ():
52+ """Test SessionInfo with all fields provided."""
53+ from praisonai .integrations ._session_info import SessionInfo
54+
55+ info = SessionInfo (
56+ id = "session-123" ,
57+ status = "idle" ,
58+ title = "Test Session" ,
59+ usage = {"input_tokens" : 150 , "output_tokens" : 75 }
60+ )
61+
62+ result = info .to_dict ()
63+ expected = {
64+ "id" : "session-123" ,
65+ "status" : "idle" ,
66+ "title" : "Test Session" ,
67+ "usage" : {"input_tokens" : 150 , "output_tokens" : 75 }
68+ }
69+ assert result == expected
70+
71+
72+ # Test schema consistency between backends
73+ @patch ('praisonai.integrations.managed_agents.AnthropicManagedAgent._get_client' )
74+ def test_anthropic_retrieve_session_schema (mock_get_client ):
75+ """Test AnthropicManagedAgent.retrieve_session returns unified schema."""
76+ from praisonai .integrations .managed_agents import AnthropicManagedAgent
77+
78+ # Mock Anthropic client response
79+ mock_session = Mock ()
80+ mock_session .id = "anthropic-session-123"
81+ mock_session .status = "idle"
82+ mock_session .title = "Anthropic Session"
83+
84+ mock_usage = Mock ()
85+ mock_usage .input_tokens = 100
86+ mock_usage .output_tokens = 50
87+ mock_session .usage = mock_usage
88+
89+ mock_client = Mock ()
90+ mock_client .beta .sessions .retrieve .return_value = mock_session
91+ mock_get_client .return_value = mock_client
92+
93+ # Create agent and test retrieve_session
94+ agent = AnthropicManagedAgent ()
95+ agent ._session_id = "anthropic-session-123"
96+
97+ result = agent .retrieve_session ()
98+
99+ # Verify schema structure
100+ assert isinstance (result , dict )
101+ assert "id" in result
102+ assert "status" in result
103+ assert "title" in result
104+ assert "usage" in result
105+
106+ # Verify field values
107+ assert result ["id" ] == "anthropic-session-123"
108+ assert result ["status" ] == "idle"
109+ assert result ["title" ] == "Anthropic Session"
110+ assert result ["usage" ] == {"input_tokens" : 100 , "output_tokens" : 50 }
111+
112+
113+ def test_local_retrieve_session_schema ():
114+ """Test LocalManagedAgent.retrieve_session returns unified schema."""
115+ from praisonai .integrations .managed_local import LocalManagedAgent
116+
117+ # Create local agent with session
118+ agent = LocalManagedAgent ()
119+ agent ._session_id = "local-session-456"
120+ agent .total_input_tokens = 200
121+ agent .total_output_tokens = 100
122+
123+ result = agent .retrieve_session ()
124+
125+ # Verify schema structure
126+ assert isinstance (result , dict )
127+ assert "id" in result
128+ assert "status" in result
129+ assert "title" in result
130+ assert "usage" in result
131+
132+ # Verify field values
133+ assert result ["id" ] == "local-session-456"
134+ assert result ["status" ] == "idle"
135+ assert result ["title" ] == ""
136+ assert result ["usage" ] == {"input_tokens" : 200 , "output_tokens" : 100 }
137+
138+
139+ def test_schema_equality_between_backends ():
140+ """Test that both backends return schemas with identical structure."""
141+ from praisonai .integrations .managed_local import LocalManagedAgent
142+
143+ # Test LocalManagedAgent (easier to set up)
144+ local_agent = LocalManagedAgent ()
145+ local_agent ._session_id = "test-session"
146+ local_agent .total_input_tokens = 0
147+ local_agent .total_output_tokens = 0
148+
149+ local_result = local_agent .retrieve_session ()
150+
151+ # Both should have identical keys
152+ expected_keys = {"id" , "status" , "title" , "usage" }
153+ assert set (local_result .keys ()) == expected_keys
154+
155+ # Usage should be a dict with input_tokens and output_tokens
156+ assert isinstance (local_result ["usage" ], dict )
157+ assert "input_tokens" in local_result ["usage" ]
158+ assert "output_tokens" in local_result ["usage" ]
159+
160+ # All values should be present (no None values)
161+ assert local_result ["id" ] is not None
162+ assert local_result ["status" ] is not None
163+ assert local_result ["title" ] is not None
164+ assert local_result ["usage" ] is not None
165+
166+
167+ def test_empty_session_anthropic ():
168+ """Test AnthropicManagedAgent.retrieve_session with no session."""
169+ from praisonai .integrations .managed_agents import AnthropicManagedAgent
170+
171+ agent = AnthropicManagedAgent ()
172+ agent ._session_id = None
173+
174+ result = agent .retrieve_session ()
175+
176+ # Should return SessionInfo defaults
177+ expected = {
178+ "id" : "" ,
179+ "status" : "unknown" ,
180+ "title" : "" ,
181+ "usage" : {"input_tokens" : 0 , "output_tokens" : 0 }
182+ }
183+ assert result == expected
184+
185+
186+ def test_empty_session_local ():
187+ """Test LocalManagedAgent.retrieve_session with no session."""
188+ from praisonai .integrations .managed_local import LocalManagedAgent
189+
190+ agent = LocalManagedAgent ()
191+ agent ._session_id = ""
192+ agent .total_input_tokens = 0
193+ agent .total_output_tokens = 0
194+
195+ result = agent .retrieve_session ()
196+
197+ # Should return unified schema with "none" status
198+ assert result ["id" ] == ""
199+ assert result ["status" ] == "none"
200+ assert result ["title" ] == ""
201+ assert result ["usage" ] == {"input_tokens" : 0 , "output_tokens" : 0 }
202+
203+
204+ if __name__ == "__main__" :
205+ pytest .main ([__file__ ])
0 commit comments