Skip to content

Commit 4cacf41

Browse files
add tests
1 parent 0df2ad5 commit 4cacf41

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""Tests for Agent Framework utils."""
5+
6+
import unittest
7+
8+
from microsoft_agents_a365.observability.extensions.agentframework.utils import (
9+
extract_content_as_string_list,
10+
extract_input_content,
11+
extract_output_content,
12+
)
13+
14+
15+
class TestAgentFrameworkUtils(unittest.TestCase):
16+
"""Test suite for Agent Framework utility functions."""
17+
18+
def test_extract_content_filters_text_by_role(self):
19+
"""Test text extraction with role filtering, ignoring tool calls."""
20+
msgs = '[{"role": "user", "parts": [{"type": "text", "content": "Hi"}]}, {"role": "assistant", "parts": [{"type": "tool_call"}, {"type": "text", "content": "Hello"}]}]'
21+
self.assertEqual(extract_content_as_string_list(msgs), '["Hi", "Hello"]')
22+
self.assertEqual(extract_content_as_string_list(msgs, role_filter="user"), '["Hi"]')
23+
self.assertEqual(extract_input_content(msgs), '["Hi"]')
24+
self.assertEqual(extract_output_content(msgs), '["Hello"]')
25+
26+
def test_handles_invalid_and_edge_cases(self):
27+
"""Test invalid JSON and edge cases return appropriate values."""
28+
self.assertEqual(extract_content_as_string_list("invalid"), "invalid")
29+
self.assertEqual(extract_content_as_string_list('{"not": "list"}'), '{"not": "list"}')
30+
self.assertEqual(extract_content_as_string_list("[]"), "[]")
31+
self.assertEqual(extract_content_as_string_list('[{"role": "user"}]'), "[]")
32+
33+
34+
if __name__ == "__main__":
35+
unittest.main()

0 commit comments

Comments
 (0)