|
| 1 | +"""Tests for FilterAction guardrail failure behavior.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | +from uipath.platform.guardrails import GuardrailScope |
| 10 | +from uipath.runtime.errors import UiPathErrorCode |
| 11 | + |
| 12 | +from uipath_langchain.agent.exceptions import AgentTerminationException |
| 13 | +from uipath_langchain.agent.guardrails.actions.filter_action import FilterAction |
| 14 | +from uipath_langchain.agent.guardrails.types import ExecutionStage |
| 15 | +from uipath_langchain.agent.react.types import AgentGuardrailsGraphState |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from _pytest.capture import CaptureFixture # noqa: F401 |
| 19 | + from _pytest.fixtures import FixtureRequest # noqa: F401 |
| 20 | + from _pytest.logging import LogCaptureFixture # noqa: F401 |
| 21 | + from _pytest.monkeypatch import MonkeyPatch # noqa: F401 |
| 22 | + from pytest_mock.plugin import MockerFixture # noqa: F401 |
| 23 | + |
| 24 | + |
| 25 | +class TestFilterAction: |
| 26 | + @pytest.mark.asyncio |
| 27 | + @pytest.mark.parametrize( |
| 28 | + ("scope", "stage", "expected_node_name"), |
| 29 | + [ |
| 30 | + ( |
| 31 | + GuardrailScope.LLM, |
| 32 | + ExecutionStage.PRE_EXECUTION, |
| 33 | + "llm_pre_execution_my_guardrail_v1_filter", |
| 34 | + ), |
| 35 | + ( |
| 36 | + GuardrailScope.LLM, |
| 37 | + ExecutionStage.POST_EXECUTION, |
| 38 | + "llm_post_execution_my_guardrail_v1_filter", |
| 39 | + ), |
| 40 | + ( |
| 41 | + GuardrailScope.AGENT, |
| 42 | + ExecutionStage.PRE_EXECUTION, |
| 43 | + "agent_pre_execution_my_guardrail_v1_filter", |
| 44 | + ), |
| 45 | + ( |
| 46 | + GuardrailScope.AGENT, |
| 47 | + ExecutionStage.POST_EXECUTION, |
| 48 | + "agent_post_execution_my_guardrail_v1_filter", |
| 49 | + ), |
| 50 | + ], |
| 51 | + ) |
| 52 | + async def test_node_name_and_exception_for_unsupported_scopes( |
| 53 | + self, scope: GuardrailScope, stage: ExecutionStage, expected_node_name: str |
| 54 | + ) -> None: |
| 55 | + """AGENT/LLM scopes raise AgentTerminationException and node name is sanitized.""" |
| 56 | + action = FilterAction() |
| 57 | + guardrail = MagicMock() |
| 58 | + guardrail.name = "My Guardrail v1" |
| 59 | + |
| 60 | + node_name, node = action.action_node( |
| 61 | + guardrail=guardrail, |
| 62 | + scope=scope, |
| 63 | + execution_stage=stage, |
| 64 | + guarded_component_name="guarded_node_name", |
| 65 | + ) |
| 66 | + |
| 67 | + assert node_name == expected_node_name |
| 68 | + |
| 69 | + with pytest.raises(AgentTerminationException) as excinfo: |
| 70 | + await node(AgentGuardrailsGraphState(messages=[])) |
| 71 | + |
| 72 | + # Validate rich error info |
| 73 | + assert ( |
| 74 | + excinfo.value.error_info.code |
| 75 | + == f"Python.{UiPathErrorCode.EXECUTION_ERROR.value}" |
| 76 | + ) |
| 77 | + assert excinfo.value.error_info.title == "Guardrail filter action not supported" |
| 78 | + assert ( |
| 79 | + excinfo.value.error_info.detail |
| 80 | + == f"FilterAction is not supported for scope [{scope.name}] at this time." |
| 81 | + ) |
| 82 | + |
| 83 | + @pytest.mark.asyncio |
| 84 | + @pytest.mark.parametrize( |
| 85 | + "stage", |
| 86 | + [ExecutionStage.PRE_EXECUTION, ExecutionStage.POST_EXECUTION], |
| 87 | + ) |
| 88 | + async def test_tool_scope_returns_empty_dict(self, stage: ExecutionStage) -> None: |
| 89 | + """TOOL scope currently performs no-op and returns empty dict.""" |
| 90 | + action = FilterAction() |
| 91 | + guardrail = MagicMock() |
| 92 | + guardrail.name = "My Guardrail v1" |
| 93 | + |
| 94 | + _, node = action.action_node( |
| 95 | + guardrail=guardrail, |
| 96 | + scope=GuardrailScope.TOOL, |
| 97 | + execution_stage=stage, |
| 98 | + guarded_component_name="test_tool", |
| 99 | + ) |
| 100 | + |
| 101 | + result = await node(AgentGuardrailsGraphState(messages=[])) |
| 102 | + assert result == {} |
0 commit comments