|
| 1 | +"""Tests for escalation_tool.py module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import Mock, patch |
| 5 | + |
| 6 | +from uipath.agent.models.agent import ( |
| 7 | + AgentEscalationRecipientType, |
| 8 | + StandardRecipient, |
| 9 | + AssetRecipient, |
| 10 | +) |
| 11 | +from uipath_langchain.agent.tools.escalation_tool import ( |
| 12 | + resolve_recipient_value, |
| 13 | + resolve_asset, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestResolveAsset: |
| 18 | + """Tests for resolve_asset function.""" |
| 19 | + |
| 20 | + @patch("uipath_langchain.agent.tools.escalation_tool.UiPath") |
| 21 | + def test_resolve_asset_success(self, mock_uipath): |
| 22 | + """Test successful asset resolution.""" |
| 23 | + # Mock the asset retrieval |
| 24 | + mock_client = Mock() |
| 25 | + mock_result = Mock() |
| 26 | + mock_result.value = "test@example.com" |
| 27 | + mock_client.assets.retrieve.return_value = mock_result |
| 28 | + mock_uipath.return_value = mock_client |
| 29 | + |
| 30 | + result = resolve_asset("email_asset", "Shared") |
| 31 | + |
| 32 | + assert result == "test@example.com" |
| 33 | + mock_client.assets.retrieve.assert_called_once_with( |
| 34 | + name="email_asset", folder_path="Shared" |
| 35 | + ) |
| 36 | + |
| 37 | + @patch("uipath_langchain.agent.tools.escalation_tool.UiPath") |
| 38 | + def test_resolve_asset_not_found(self, mock_uipath): |
| 39 | + """Test asset resolution when asset doesn't exist.""" |
| 40 | + mock_client = Mock() |
| 41 | + mock_client.assets.retrieve.side_effect = Exception("Asset not found") |
| 42 | + mock_uipath.return_value = mock_client |
| 43 | + |
| 44 | + with pytest.raises(Exception) as exc_info: |
| 45 | + resolve_asset("nonexistent_asset", "Shared") |
| 46 | + |
| 47 | + assert "Asset not found" in str(exc_info.value) |
| 48 | + |
| 49 | + @patch("uipath_langchain.agent.tools.escalation_tool.UiPath") |
| 50 | + def test_resolve_asset_raises_error_when_value_is_empty(self, mock_uipath): |
| 51 | + """Test asset resolution raises error when asset value is empty.""" |
| 52 | + mock_client = Mock() |
| 53 | + mock_result = Mock() |
| 54 | + mock_result.value = None |
| 55 | + mock_client.assets.retrieve.return_value = mock_result |
| 56 | + mock_uipath.return_value = mock_client |
| 57 | + |
| 58 | + with pytest.raises(ValueError) as exc_info: |
| 59 | + resolve_asset("empty_asset", "Shared") |
| 60 | + |
| 61 | + assert "has no value" in str(exc_info.value) |
| 62 | + |
| 63 | + |
| 64 | +class TestResolveRecipientValue: |
| 65 | + """Tests for resolve_recipient_value function.""" |
| 66 | + |
| 67 | + def test_resolve_recipient_value_returns_email_for_user_email_type(self): |
| 68 | + """Test resolving StandardRecipient with USER_EMAIL.""" |
| 69 | + recipient = StandardRecipient( |
| 70 | + type=AgentEscalationRecipientType.USER_EMAIL, value="user@example.com" |
| 71 | + ) |
| 72 | + |
| 73 | + result = resolve_recipient_value(recipient) |
| 74 | + |
| 75 | + assert result == "user@example.com" |
| 76 | + |
| 77 | + @patch("uipath_langchain.agent.tools.escalation_tool.resolve_asset") |
| 78 | + def test_resolve_recipient_value_calls_resolve_asset_for_asset_recipient(self, mock_resolve_asset): |
| 79 | + """Test resolving AssetRecipient calls resolve_asset.""" |
| 80 | + mock_resolve_asset.return_value = "asset@example.com" |
| 81 | + |
| 82 | + recipient = AssetRecipient( |
| 83 | + type=AgentEscalationRecipientType.ASSET_USER_EMAIL, |
| 84 | + asset_name="email_asset", |
| 85 | + folder_path="Shared", |
| 86 | + ) |
| 87 | + |
| 88 | + result = resolve_recipient_value(recipient) |
| 89 | + |
| 90 | + assert result == "asset@example.com" |
| 91 | + mock_resolve_asset.assert_called_once_with("email_asset", "Shared") |
| 92 | + |
| 93 | + @patch("uipath_langchain.agent.tools.escalation_tool.resolve_asset") |
| 94 | + def test_resolve_recipient_value_propagates_error_when_asset_resolution_fails(self, mock_resolve_asset): |
| 95 | + """Test AssetRecipient when asset resolution fails.""" |
| 96 | + mock_resolve_asset.side_effect = ValueError("Asset not found") |
| 97 | + |
| 98 | + recipient = AssetRecipient( |
| 99 | + type=AgentEscalationRecipientType.ASSET_USER_EMAIL, |
| 100 | + asset_name="nonexistent", |
| 101 | + folder_path="Shared", |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises(ValueError) as exc_info: |
| 105 | + resolve_recipient_value(recipient) |
| 106 | + |
| 107 | + assert "Asset not found" in str(exc_info.value) |
0 commit comments