|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Tests for Agent Framework span enricher.""" |
| 5 | + |
| 6 | +import unittest |
| 7 | +from unittest.mock import Mock |
| 8 | + |
| 9 | +from microsoft_agents_a365.observability.core.constants import ( |
| 10 | + GEN_AI_INPUT_MESSAGES_KEY, |
| 11 | + GEN_AI_OUTPUT_MESSAGES_KEY, |
| 12 | + GEN_AI_TOOL_ARGS_KEY, |
| 13 | + GEN_AI_TOOL_CALL_RESULT_KEY, |
| 14 | +) |
| 15 | +from microsoft_agents_a365.observability.extensions.agentframework.span_enricher import ( |
| 16 | + AF_TOOL_CALL_ARGUMENTS_KEY, |
| 17 | + AF_TOOL_CALL_RESULT_KEY, |
| 18 | + enrich_agent_framework_span, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class TestAgentFrameworkSpanEnricher(unittest.TestCase): |
| 23 | + """Test suite for enrich_agent_framework_span function.""" |
| 24 | + |
| 25 | + def test_invoke_agent_span_enrichment(self): |
| 26 | + """Test invoke_agent span extracts user input and assistant output text only.""" |
| 27 | + span = Mock( |
| 28 | + name="invoke_agent Agent365Assistant", |
| 29 | + attributes={ |
| 30 | + GEN_AI_INPUT_MESSAGES_KEY: '[{"role": "user", "parts": [{"type": "text", "content": "Compute 15 % 4"}]}]', |
| 31 | + GEN_AI_OUTPUT_MESSAGES_KEY: '[{"role": "assistant", "parts": [{"type": "tool_call", "id": "c1"}]}, {"role": "tool", "parts": [{"type": "tool_call_response"}]}, {"role": "assistant", "parts": [{"type": "text", "content": "Result is 3."}]}]', |
| 32 | + }, |
| 33 | + ) |
| 34 | + span.name = "invoke_agent Agent365Assistant" |
| 35 | + result = enrich_agent_framework_span(span) |
| 36 | + self.assertEqual(result.attributes[GEN_AI_INPUT_MESSAGES_KEY], '["Compute 15 % 4"]') |
| 37 | + self.assertEqual(result.attributes[GEN_AI_OUTPUT_MESSAGES_KEY], '["Result is 3."]') |
| 38 | + |
| 39 | + def test_execute_tool_span_enrichment(self): |
| 40 | + """Test execute_tool span maps tool arguments and result to standard keys.""" |
| 41 | + span = Mock( |
| 42 | + name="execute_tool calculate", |
| 43 | + attributes={ |
| 44 | + AF_TOOL_CALL_ARGUMENTS_KEY: '{"expression": "2 + 2"}', |
| 45 | + AF_TOOL_CALL_RESULT_KEY: "Result is 4", |
| 46 | + }, |
| 47 | + ) |
| 48 | + span.name = "execute_tool calculate" |
| 49 | + result = enrich_agent_framework_span(span) |
| 50 | + self.assertEqual(result.attributes[GEN_AI_TOOL_ARGS_KEY], '{"expression": "2 + 2"}') |
| 51 | + self.assertEqual(result.attributes[GEN_AI_TOOL_CALL_RESULT_KEY], "Result is 4") |
| 52 | + |
| 53 | + def test_non_matching_and_edge_cases_return_original(self): |
| 54 | + """Test non-matching, None, and empty attribute spans return unchanged.""" |
| 55 | + span = Mock(name="other_op", attributes={"key": "value"}) |
| 56 | + span.name = "other_op" |
| 57 | + self.assertEqual(enrich_agent_framework_span(span), span) |
| 58 | + |
| 59 | + span.name = "invoke_agent Test" |
| 60 | + span.attributes = None |
| 61 | + self.assertEqual(enrich_agent_framework_span(span), span) |
| 62 | + |
| 63 | + span.attributes = {} |
| 64 | + self.assertEqual(enrich_agent_framework_span(span), span) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + unittest.main() |
0 commit comments