|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Tests for safety telemetry integration.""" |
| 7 | + |
| 8 | +from unittest.mock import MagicMock |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from trpc_agent_sdk.tools.safety._telemetry import set_safety_span_attrs |
| 12 | +from trpc_agent_sdk.tools.safety._types import Decision |
| 13 | +from trpc_agent_sdk.tools.safety._types import RiskLevel |
| 14 | +from trpc_agent_sdk.tools.safety._types import RiskType |
| 15 | +from trpc_agent_sdk.tools.safety._types import RuleFinding |
| 16 | +from trpc_agent_sdk.tools.safety._types import ScanReport |
| 17 | + |
| 18 | + |
| 19 | +class TestSetSafetySpanAttrs: |
| 20 | + def test_span_attributes_set_on_active_span(self): |
| 21 | + mock_span = MagicMock() |
| 22 | + mock_span.is_recording.return_value = True |
| 23 | + |
| 24 | + report = ScanReport( |
| 25 | + decision=Decision.DENY, |
| 26 | + risk_level=RiskLevel.CRITICAL, |
| 27 | + findings=[ |
| 28 | + RuleFinding( |
| 29 | + rule_id="DANGEROUS_DELETE_001", |
| 30 | + risk_type=RiskType.DANGEROUS_FILE_OP, |
| 31 | + risk_level=RiskLevel.CRITICAL, |
| 32 | + evidence="rm -rf /", |
| 33 | + message="dangerous delete", |
| 34 | + recommendation="don't", |
| 35 | + ), |
| 36 | + ], |
| 37 | + scan_duration_ms=15.0, |
| 38 | + ) |
| 39 | + |
| 40 | + with patch("trpc_agent_sdk.tools.safety._telemetry.trace.get_current_span", return_value=mock_span): |
| 41 | + set_safety_span_attrs(report) |
| 42 | + |
| 43 | + mock_span.set_attribute.assert_any_call("tool.safety.decision", "deny") |
| 44 | + mock_span.set_attribute.assert_any_call("tool.safety.risk_level", "critical") |
| 45 | + mock_span.set_attribute.assert_any_call("tool.safety.scan_duration_ms", 15.0) |
| 46 | + |
| 47 | + def test_no_crash_when_no_span(self): |
| 48 | + mock_span = MagicMock() |
| 49 | + mock_span.is_recording.return_value = False |
| 50 | + |
| 51 | + report = ScanReport(decision=Decision.ALLOW) |
| 52 | + |
| 53 | + with patch("trpc_agent_sdk.tools.safety._telemetry.trace.get_current_span", return_value=mock_span): |
| 54 | + set_safety_span_attrs(report) |
| 55 | + |
| 56 | + mock_span.set_attribute.assert_not_called() |
0 commit comments