|
| 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 | + # Test empty usage dict gets both defaults |
| 51 | + info = SessionInfo(usage={}) |
| 52 | + assert info.usage["input_tokens"] == 0 |
| 53 | + assert info.usage["output_tokens"] == 0 |
| 54 | + |
| 55 | + |
| 56 | +def test_session_info_complete(): |
| 57 | + """Test SessionInfo with all fields provided.""" |
| 58 | + from praisonai.integrations._session_info import SessionInfo |
| 59 | + |
| 60 | + info = SessionInfo( |
| 61 | + id="session-123", |
| 62 | + status="idle", |
| 63 | + title="Test Session", |
| 64 | + usage={"input_tokens": 150, "output_tokens": 75} |
| 65 | + ) |
| 66 | + |
| 67 | + result = info.to_dict() |
| 68 | + expected = { |
| 69 | + "id": "session-123", |
| 70 | + "status": "idle", |
| 71 | + "title": "Test Session", |
| 72 | + "usage": {"input_tokens": 150, "output_tokens": 75} |
| 73 | + } |
| 74 | + assert result == expected |
| 75 | + |
| 76 | + |
| 77 | +# Test schema consistency between backends |
| 78 | +@patch('praisonai.integrations.managed_agents.AnthropicManagedAgent._get_client') |
| 79 | +def test_anthropic_retrieve_session_schema(mock_get_client): |
| 80 | + """Test AnthropicManagedAgent.retrieve_session returns unified schema.""" |
| 81 | + from praisonai.integrations.managed_agents import AnthropicManagedAgent |
| 82 | + |
| 83 | + # Mock Anthropic client response |
| 84 | + mock_session = Mock() |
| 85 | + mock_session.id = "anthropic-session-123" |
| 86 | + mock_session.status = "idle" |
| 87 | + mock_session.title = "Anthropic Session" |
| 88 | + |
| 89 | + mock_usage = Mock() |
| 90 | + mock_usage.input_tokens = 100 |
| 91 | + mock_usage.output_tokens = 50 |
| 92 | + mock_session.usage = mock_usage |
| 93 | + |
| 94 | + mock_client = Mock() |
| 95 | + mock_client.beta.sessions.retrieve.return_value = mock_session |
| 96 | + mock_get_client.return_value = mock_client |
| 97 | + |
| 98 | + # Create agent and test retrieve_session |
| 99 | + agent = AnthropicManagedAgent() |
| 100 | + agent._session_id = "anthropic-session-123" |
| 101 | + |
| 102 | + result = agent.retrieve_session() |
| 103 | + |
| 104 | + # Verify schema structure |
| 105 | + assert isinstance(result, dict) |
| 106 | + assert "id" in result |
| 107 | + assert "status" in result |
| 108 | + assert "title" in result |
| 109 | + assert "usage" in result |
| 110 | + |
| 111 | + # Verify field values |
| 112 | + assert result["id"] == "anthropic-session-123" |
| 113 | + assert result["status"] == "idle" |
| 114 | + assert result["title"] == "Anthropic Session" |
| 115 | + assert result["usage"] == {"input_tokens": 100, "output_tokens": 50} |
| 116 | + |
| 117 | + |
| 118 | +def test_local_retrieve_session_schema(): |
| 119 | + """Test LocalManagedAgent.retrieve_session returns unified schema.""" |
| 120 | + from praisonai.integrations.managed_local import LocalManagedAgent |
| 121 | + |
| 122 | + # Create local agent with session |
| 123 | + agent = LocalManagedAgent() |
| 124 | + agent._session_id = "local-session-456" |
| 125 | + agent.total_input_tokens = 200 |
| 126 | + agent.total_output_tokens = 100 |
| 127 | + |
| 128 | + result = agent.retrieve_session() |
| 129 | + |
| 130 | + # Verify schema structure |
| 131 | + assert isinstance(result, dict) |
| 132 | + assert "id" in result |
| 133 | + assert "status" in result |
| 134 | + assert "title" in result |
| 135 | + assert "usage" in result |
| 136 | + |
| 137 | + # Verify field values |
| 138 | + assert result["id"] == "local-session-456" |
| 139 | + assert result["status"] == "idle" |
| 140 | + assert result["title"] == "" |
| 141 | + assert result["usage"] == {"input_tokens": 200, "output_tokens": 100} |
| 142 | + |
| 143 | + |
| 144 | +def test_schema_equality_between_backends(): |
| 145 | + """Test that both backends return schemas with identical structure.""" |
| 146 | + from praisonai.integrations.managed_local import LocalManagedAgent |
| 147 | + |
| 148 | + # Test LocalManagedAgent (easier to set up) |
| 149 | + local_agent = LocalManagedAgent() |
| 150 | + local_agent._session_id = "test-session" |
| 151 | + local_agent.total_input_tokens = 0 |
| 152 | + local_agent.total_output_tokens = 0 |
| 153 | + |
| 154 | + local_result = local_agent.retrieve_session() |
| 155 | + |
| 156 | + # Both should have identical keys |
| 157 | + expected_keys = {"id", "status", "title", "usage"} |
| 158 | + assert set(local_result.keys()) == expected_keys |
| 159 | + |
| 160 | + # Usage should be a dict with input_tokens and output_tokens |
| 161 | + assert isinstance(local_result["usage"], dict) |
| 162 | + assert "input_tokens" in local_result["usage"] |
| 163 | + assert "output_tokens" in local_result["usage"] |
| 164 | + |
| 165 | + # All values should be present (no None values) |
| 166 | + assert local_result["id"] is not None |
| 167 | + assert local_result["status"] is not None |
| 168 | + assert local_result["title"] is not None |
| 169 | + assert local_result["usage"] is not None |
| 170 | + |
| 171 | + |
| 172 | +def test_empty_session_anthropic(): |
| 173 | + """Test AnthropicManagedAgent.retrieve_session with no session.""" |
| 174 | + from praisonai.integrations.managed_agents import AnthropicManagedAgent |
| 175 | + |
| 176 | + agent = AnthropicManagedAgent() |
| 177 | + agent._session_id = None |
| 178 | + |
| 179 | + result = agent.retrieve_session() |
| 180 | + |
| 181 | + # Should return SessionInfo defaults |
| 182 | + expected = { |
| 183 | + "id": "", |
| 184 | + "status": "unknown", |
| 185 | + "title": "", |
| 186 | + "usage": {"input_tokens": 0, "output_tokens": 0} |
| 187 | + } |
| 188 | + assert result == expected |
| 189 | + |
| 190 | + |
| 191 | +def test_empty_session_local(): |
| 192 | + """Test LocalManagedAgent.retrieve_session with no session.""" |
| 193 | + from praisonai.integrations.managed_local import LocalManagedAgent |
| 194 | + |
| 195 | + agent = LocalManagedAgent() |
| 196 | + agent._session_id = "" |
| 197 | + agent.total_input_tokens = 0 |
| 198 | + agent.total_output_tokens = 0 |
| 199 | + |
| 200 | + result = agent.retrieve_session() |
| 201 | + |
| 202 | + # Should return unified schema with "unknown" status (matching Anthropic) |
| 203 | + assert result["id"] == "" |
| 204 | + assert result["status"] == "unknown" |
| 205 | + assert result["title"] == "" |
| 206 | + assert result["usage"] == {"input_tokens": 0, "output_tokens": 0} |
| 207 | + |
| 208 | + |
| 209 | +def test_empty_session_local_none_id(): |
| 210 | + """Test LocalManagedAgent.retrieve_session normalizes None session id to empty string.""" |
| 211 | + from praisonai.integrations.managed_local import LocalManagedAgent |
| 212 | + |
| 213 | + agent = LocalManagedAgent() |
| 214 | + agent._session_id = None |
| 215 | + agent.total_input_tokens = 0 |
| 216 | + agent.total_output_tokens = 0 |
| 217 | + |
| 218 | + result = agent.retrieve_session() |
| 219 | + |
| 220 | + assert result["id"] == "" |
| 221 | + assert result["status"] == "none" |
| 222 | + |
| 223 | + |
| 224 | +if __name__ == "__main__": |
| 225 | + pytest.main([__file__]) |
0 commit comments