-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_execute_tool_scope.py
More file actions
137 lines (114 loc) · 4.74 KB
/
test_execute_tool_scope.py
File metadata and controls
137 lines (114 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import os
import sys
import unittest
from pathlib import Path
import pytest
from microsoft_agents_a365.observability.core import (
AgentDetails,
ExecuteToolScope,
ExecutionType,
Request,
SourceMetadata,
TenantDetails,
ToolCallDetails,
configure,
get_tracer_provider,
)
from microsoft_agents_a365.observability.core.config import _telemetry_manager
from microsoft_agents_a365.observability.core.constants import (
GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY,
GEN_AI_EXECUTION_SOURCE_NAME_KEY,
)
from microsoft_agents_a365.observability.core.opentelemetry_scope import OpenTelemetryScope
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
class TestExecuteToolScope(unittest.TestCase):
"""Unit tests for ExecuteToolScope and its methods."""
@classmethod
def setUpClass(cls):
"""Set up test environment once for all tests."""
# Configure Microsoft Agent 365 for testing
os.environ["ENABLE_A365_OBSERVABILITY"] = "true"
configure(
service_name="test-execute-tool-service",
service_namespace="test-namespace",
)
# Create test data
cls.tenant_details = TenantDetails(tenant_id="12345678-1234-5678-1234-567812345678")
cls.agent_details = AgentDetails(
agent_id="test-agent-123",
agent_name="Test Agent",
agent_description="A test agent for tool execution testing",
)
cls.tool_details = ToolCallDetails(
tool_name="weather_tool",
arguments='{"location": "Seattle", "units": "metric"}',
tool_call_id="call-123",
description="Get current weather information for a location",
)
def setUp(self):
super().setUp()
# Reset TelemetryManager state to ensure fresh configuration
_telemetry_manager._tracer_provider = None
_telemetry_manager._span_processors = {}
OpenTelemetryScope._tracer = None
# Reconfigure to get a fresh TracerProvider
configure(
service_name="test-execute-tool-service",
service_namespace="test-namespace",
)
# Set up tracer to capture spans
self.span_exporter = InMemorySpanExporter()
tracer_provider = get_tracer_provider()
tracer_provider.add_span_processor(SimpleSpanProcessor(self.span_exporter))
def tearDown(self):
super().tearDown()
self.span_exporter.clear()
def test_record_response_method_exists(self):
"""Test that record_response method exists on ExecuteToolScope."""
scope = ExecuteToolScope.start(self.tool_details, self.agent_details, self.tenant_details)
if scope is not None:
# Test that the method exists
self.assertTrue(hasattr(scope, "record_response"))
self.assertTrue(callable(scope.record_response))
scope.dispose()
def test_request_metadata_set_on_span(self):
"""Test that request source metadata is set on span attributes."""
request = Request(
content="Execute tool with request metadata",
execution_type=ExecutionType.AGENT_TO_AGENT,
session_id="session-xyz",
source_metadata=SourceMetadata(name="Channel 1", description="Link to channel"),
)
scope = ExecuteToolScope.start(
self.tool_details, self.agent_details, self.tenant_details, request
)
if scope is not None:
scope.dispose()
finished_spans = self.span_exporter.get_finished_spans()
self.assertTrue(finished_spans, "Expected at least one span to be created")
span = finished_spans[-1]
span_attributes = getattr(span, "attributes", {}) or {}
self.assertIn(
GEN_AI_EXECUTION_SOURCE_NAME_KEY,
span_attributes,
"Expected source name to be set on span",
)
self.assertEqual(
span_attributes[GEN_AI_EXECUTION_SOURCE_NAME_KEY],
request.source_metadata.name,
)
self.assertIn(
GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY,
span_attributes,
"Expected source description to be set on span",
)
self.assertEqual(
span_attributes[GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY],
request.source_metadata.description,
)
if __name__ == "__main__":
# Run pytest only on the current file
sys.exit(pytest.main([str(Path(__file__))] + sys.argv[1:]))