|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +import json |
4 | 5 | from unittest.mock import MagicMock |
5 | 6 |
|
6 | 7 | import pytest |
|
9 | 10 | ActivityEventNames, |
10 | 11 | ActivityTypes, |
11 | 12 | ChannelAccount, |
| 13 | + ChannelId, |
12 | 14 | ConversationAccount, |
13 | 15 | ) |
14 | 16 | from microsoft_agents.hosting.core import TurnContext |
15 | 17 | from microsoft_agents_a365.observability.core.constants import ( |
| 18 | + CHANNEL_LINK_KEY, |
16 | 19 | TENANT_ID_KEY, |
17 | 20 | USER_ID_KEY, |
18 | 21 | ) |
@@ -53,6 +56,31 @@ def _make_turn_context( |
53 | 56 | return TurnContext(adapter, activity) |
54 | 57 |
|
55 | 58 |
|
| 59 | +def _make_channel_data_turn_context( |
| 60 | + channel_id: ChannelId | str = "msteams", |
| 61 | + channel_data: object | None = None, |
| 62 | +) -> TurnContext: |
| 63 | + """Create a TurnContext with channel_data for testing.""" |
| 64 | + activity = Activity( |
| 65 | + type="message", |
| 66 | + text="Hello", |
| 67 | + from_property=ChannelAccount( |
| 68 | + aad_object_id="caller-id", |
| 69 | + name="Caller", |
| 70 | + ), |
| 71 | + recipient=ChannelAccount( |
| 72 | + tenant_id="tenant-123", |
| 73 | + name="Agent", |
| 74 | + ), |
| 75 | + conversation=ConversationAccount(id="conv-id"), |
| 76 | + service_url="https://example.com", |
| 77 | + channel_id=channel_id, |
| 78 | + channel_data=channel_data, |
| 79 | + ) |
| 80 | + adapter = MagicMock() |
| 81 | + return TurnContext(adapter, activity) |
| 82 | + |
| 83 | + |
56 | 84 | @pytest.mark.asyncio |
57 | 85 | async def test_baggage_middleware_propagates_baggage(): |
58 | 86 | """BaggageMiddleware should set baggage context for the downstream logic.""" |
@@ -95,3 +123,89 @@ async def logic(): |
95 | 123 | assert logic_called is True |
96 | 124 | # Baggage should NOT be set because the middleware skipped it |
97 | 125 | assert captured_caller_id is None |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.asyncio |
| 129 | +async def test_baggage_middleware_extracts_product_context_from_channel_data(): |
| 130 | + """BaggageMiddleware should extract productContext from channel_data when sub_channel is not set.""" |
| 131 | + |
| 132 | + middleware = BaggageMiddleware() |
| 133 | + ctx = _make_channel_data_turn_context( |
| 134 | + channel_id=ChannelId(channel="msteams"), # No sub_channel |
| 135 | + channel_data={"productContext": "COPILOT"}, |
| 136 | + ) |
| 137 | + |
| 138 | + captured_channel_link = None |
| 139 | + |
| 140 | + async def logic(): |
| 141 | + nonlocal captured_channel_link |
| 142 | + captured_channel_link = baggage.get_baggage(CHANNEL_LINK_KEY) |
| 143 | + |
| 144 | + await middleware.on_turn(ctx, logic) |
| 145 | + |
| 146 | + assert captured_channel_link == "COPILOT" |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.asyncio |
| 150 | +async def test_baggage_middleware_sub_channel_takes_precedence_over_product_context(): |
| 151 | + """BaggageMiddleware should use sub_channel when both sub_channel and productContext are present.""" |
| 152 | + |
| 153 | + middleware = BaggageMiddleware() |
| 154 | + ctx = _make_channel_data_turn_context( |
| 155 | + channel_id=ChannelId(channel="msteams", sub_channel="teams-subchannel"), |
| 156 | + channel_data={"productContext": "COPILOT"}, # Should be ignored |
| 157 | + ) |
| 158 | + |
| 159 | + captured_channel_link = None |
| 160 | + |
| 161 | + async def logic(): |
| 162 | + nonlocal captured_channel_link |
| 163 | + captured_channel_link = baggage.get_baggage(CHANNEL_LINK_KEY) |
| 164 | + |
| 165 | + await middleware.on_turn(ctx, logic) |
| 166 | + |
| 167 | + # sub_channel should take precedence, productContext should be ignored |
| 168 | + assert captured_channel_link == "teams-subchannel" |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.asyncio |
| 172 | +async def test_baggage_middleware_extracts_product_context_from_json_string_channel_data(): |
| 173 | + """BaggageMiddleware should extract productContext from channel_data when it's a JSON string.""" |
| 174 | + |
| 175 | + middleware = BaggageMiddleware() |
| 176 | + ctx = _make_channel_data_turn_context( |
| 177 | + channel_id=ChannelId(channel="msteams"), # No sub_channel |
| 178 | + channel_data=json.dumps({"productContext": "COPILOT"}), # JSON string |
| 179 | + ) |
| 180 | + |
| 181 | + captured_channel_link = None |
| 182 | + |
| 183 | + async def logic(): |
| 184 | + nonlocal captured_channel_link |
| 185 | + captured_channel_link = baggage.get_baggage(CHANNEL_LINK_KEY) |
| 186 | + |
| 187 | + await middleware.on_turn(ctx, logic) |
| 188 | + |
| 189 | + assert captured_channel_link == "COPILOT" |
| 190 | + |
| 191 | + |
| 192 | +@pytest.mark.asyncio |
| 193 | +async def test_baggage_middleware_handles_invalid_json_channel_data_gracefully(): |
| 194 | + """BaggageMiddleware should handle invalid JSON in channel_data gracefully without setting baggage.""" |
| 195 | + |
| 196 | + middleware = BaggageMiddleware() |
| 197 | + ctx = _make_channel_data_turn_context( |
| 198 | + channel_id=ChannelId(channel="msteams"), # No sub_channel |
| 199 | + channel_data="not valid json", # Non-JSON string |
| 200 | + ) |
| 201 | + |
| 202 | + captured_channel_link = None |
| 203 | + |
| 204 | + async def logic(): |
| 205 | + nonlocal captured_channel_link |
| 206 | + captured_channel_link = baggage.get_baggage(CHANNEL_LINK_KEY) |
| 207 | + |
| 208 | + await middleware.on_turn(ctx, logic) |
| 209 | + |
| 210 | + # Should not set ChannelLink, should fail gracefully |
| 211 | + assert captured_channel_link is None |
0 commit comments