microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/IntegrationTests/CreateConversationTests.cs
375lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Apps.Api.Clients; |
| 5 | using Microsoft.Teams.Core; |
| 6 | using Microsoft.Teams.Core.Schema; |
| 7 | using Xunit.Abstractions; |
| 8 | |
| 9 | namespace IntegrationTests; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Integration tests for creating conversations with different ConversationParameters. |
| 13 | /// Tests personal chats, group chats, and channel thread creation via both |
| 14 | /// core <see cref="ConversationClient"/> and the <see cref="ApiClient"/> facade. |
| 15 | /// </summary> |
| 16 | public class CreateConversationTests : IClassFixture<IntegrationTestFixture> |
| 17 | { |
| 18 | private readonly IntegrationTestFixture _f; |
| 19 | private readonly ITestOutputHelper _output; |
| 20 | private readonly ApiClient _api; |
| 21 | |
| 22 | public CreateConversationTests(IntegrationTestFixture fixture, ITestOutputHelper output) |
| 23 | { |
| 24 | _f = fixture; |
| 25 | _f.OutputHelper = output; |
| 26 | _output = output; |
| 27 | _api = _f.ScopedApiClient; |
| 28 | } |
| 29 | |
| 30 | /// <summary> |
| 31 | /// Gets MRI-format member IDs by fetching the conversation members list. |
| 32 | /// The API requires MRI IDs (e.g., "29:1abc..."), not pairwise bot framework IDs. |
| 33 | /// </summary> |
| 34 | private Task<(string first, string? second)> GetMemberMrisAsync() |
| 35 | { |
| 36 | string first = _f.MemberMri1!; |
| 37 | string? second = _f.MemberMri2; |
| 38 | |
| 39 | _output.WriteLine($"Using member MRIs: first={first}, second={second ?? "(none)"}"); |
| 40 | return Task.FromResult((first, second)); |
| 41 | } |
| 42 | |
| 43 | #region Personal Chat (1:1) — Core ConversationClient |
| 44 | |
| 45 | [Fact(Timeout = 5000)] |
| 46 | [Trait("Category", "Conversations")] |
| 47 | public async Task Core_CreatePersonalChat() |
| 48 | { |
| 49 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 50 | |
| 51 | ConversationParameters parameters = new() |
| 52 | { |
| 53 | IsGroup = false, |
| 54 | Members = [new() { Id = memberMri }], |
| 55 | TenantId = _f.TenantId |
| 56 | }; |
| 57 | |
| 58 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 59 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 60 | |
| 61 | Assert.NotNull(response); |
| 62 | Assert.NotNull(response.Id); |
| 63 | _output.WriteLine($"Created 1:1 conversation: {response.Id}"); |
| 64 | } |
| 65 | |
| 66 | [Fact(Timeout = 5000)] |
| 67 | [Trait("Category", "Conversations")] |
| 68 | public async Task Core_CreatePersonalChat_AndSendMessage() |
| 69 | { |
| 70 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 71 | |
| 72 | ConversationParameters parameters = new() |
| 73 | { |
| 74 | IsGroup = false, |
| 75 | Members = [new() { Id = memberMri }], |
| 76 | TenantId = _f.TenantId |
| 77 | }; |
| 78 | |
| 79 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 80 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 81 | |
| 82 | Assert.NotNull(response?.Id); |
| 83 | |
| 84 | CoreActivity activity = CoreActivity.CreateBuilder() |
| 85 | .WithType(ActivityType.Message) |
| 86 | .WithFrom(IntegrationTestFixture.GetConversationAccountWithAgenticProperties()) |
| 87 | .WithServiceUrl(_f.ServiceUrl) |
| 88 | .WithConversation(new(response.Id)) |
| 89 | .WithProperty("text", $"[Core] 1:1 message at `{DateTime.UtcNow:s}`") |
| 90 | .Build(); |
| 91 | |
| 92 | SendActivityResponse? sent = await _f.ConversationClient.SendActivityAsync(activity); |
| 93 | Assert.NotNull(sent?.Id); |
| 94 | _output.WriteLine($"Created 1:1 conversation {response.Id} and sent activity {sent.Id}"); |
| 95 | } |
| 96 | |
| 97 | [Fact(Timeout = 5000)] |
| 98 | [Trait("Category", "Conversations")] |
| 99 | public async Task Core_CreatePersonalChat_WithInitialActivity() |
| 100 | { |
| 101 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 102 | |
| 103 | ConversationParameters parameters = new() |
| 104 | { |
| 105 | IsGroup = false, |
| 106 | Members = [new() { Id = memberMri }], |
| 107 | TenantId = _f.TenantId, |
| 108 | Activity = CoreActivity.CreateBuilder() |
| 109 | .WithType(ActivityType.Message) |
| 110 | .WithProperty("text", $"[Core] Initial message at `{DateTime.UtcNow:s}`") |
| 111 | .Build() |
| 112 | }; |
| 113 | |
| 114 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 115 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 116 | |
| 117 | Assert.NotNull(response); |
| 118 | Assert.NotNull(response.Id); |
| 119 | _output.WriteLine($"Created 1:1 conversation with initial activity: {response.Id}, activityId: {response.ActivityId}"); |
| 120 | } |
| 121 | |
| 122 | #endregion |
| 123 | |
| 124 | #region Group Chat — Core ConversationClient |
| 125 | |
| 126 | [Fact] |
| 127 | [Trait("Category", "Conversations")] |
| 128 | public async Task Core_CreateGroupChat() |
| 129 | { |
| 130 | (string first, string? second) = await GetMemberMrisAsync(); |
| 131 | if (second is null) |
| 132 | { |
| 133 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | ConversationParameters parameters = new() |
| 138 | { |
| 139 | //IsGroup = true, |
| 140 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 141 | Members = |
| 142 | [ |
| 143 | //new() { Id = first }, |
| 144 | new() { Id = second } |
| 145 | ], |
| 146 | TenantId = _f.TenantId, |
| 147 | //TopicName = $"Integration Test Group - {DateTime.UtcNow:s}", |
| 148 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 149 | }; |
| 150 | |
| 151 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 152 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 153 | |
| 154 | Assert.NotNull(response); |
| 155 | Assert.NotNull(response.Id); |
| 156 | _output.WriteLine($"Created group conversation: {response.Id}"); |
| 157 | } |
| 158 | |
| 159 | [Fact] |
| 160 | [Trait("Category", "Conversations")] |
| 161 | public async Task Core_CreateGroupChat_AndSendMessage() |
| 162 | { |
| 163 | (string first, string? second) = await GetMemberMrisAsync(); |
| 164 | if (second is null) |
| 165 | { |
| 166 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | ConversationParameters parameters = new() |
| 171 | { |
| 172 | IsGroup = false, |
| 173 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 174 | Members = |
| 175 | [ |
| 176 | //new() { Id = first }, |
| 177 | new() { Id = second } |
| 178 | ], |
| 179 | TenantId = _f.TenantId, |
| 180 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 181 | }; |
| 182 | |
| 183 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 184 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 185 | |
| 186 | Assert.NotNull(response?.Id); |
| 187 | |
| 188 | CoreActivity activity = CoreActivity.CreateBuilder() |
| 189 | .WithType(ActivityType.Message) |
| 190 | .WithFrom(IntegrationTestFixture.GetConversationAccountWithAgenticProperties()) |
| 191 | .WithServiceUrl(_f.ServiceUrl) |
| 192 | .WithConversation(new(response.Id)) |
| 193 | .WithProperty("text", $"[Core] Group message at `{DateTime.UtcNow:s}`") |
| 194 | .Build(); |
| 195 | |
| 196 | SendActivityResponse? sent = await _f.ConversationClient.SendActivityAsync(activity); |
| 197 | Assert.NotNull(sent?.Id); |
| 198 | _output.WriteLine($"Created group {response.Id} and sent activity {sent.Id}"); |
| 199 | } |
| 200 | |
| 201 | #endregion |
| 202 | |
| 203 | #region Channel Thread — Core ConversationClient |
| 204 | |
| 205 | [Fact(Timeout = 5000)] |
| 206 | [Trait("Category", "Conversations")] |
| 207 | public async Task Core_CreateChannelThread() |
| 208 | { |
| 209 | ConversationParameters parameters = new() |
| 210 | { |
| 211 | IsGroup = true, |
| 212 | ChannelData = new { channel = new { id = _f.ChannelId } }, |
| 213 | Activity = CoreActivity.CreateBuilder() |
| 214 | .WithType(ActivityType.Message) |
| 215 | .WithProperty("text", $"[Core] New channel thread at `{DateTime.UtcNow:s}`") |
| 216 | .Build(), |
| 217 | TenantId = _f.TenantId |
| 218 | }; |
| 219 | |
| 220 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 221 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 222 | |
| 223 | Assert.NotNull(response); |
| 224 | Assert.NotNull(response.Id); |
| 225 | _output.WriteLine($"Created channel thread: {response.Id}, activityId: {response.ActivityId}"); |
| 226 | } |
| 227 | |
| 228 | #endregion |
| 229 | |
| 230 | #region Personal Chat — ApiClient |
| 231 | |
| 232 | [Fact(Timeout = 5000)] |
| 233 | [Trait("Category", "Conversations")] |
| 234 | public async Task ApiClient_CreatePersonalChat() |
| 235 | { |
| 236 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 237 | |
| 238 | ConversationParameters parameters = new() |
| 239 | { |
| 240 | IsGroup = false, |
| 241 | Members = [new() { Id = memberMri }], |
| 242 | TenantId = _f.TenantId |
| 243 | }; |
| 244 | |
| 245 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters, _f.AgenticIdentity); |
| 246 | |
| 247 | Assert.NotNull(response); |
| 248 | Assert.NotNull(response.Id); |
| 249 | _output.WriteLine($"[ApiClient] Created 1:1 conversation: {response.Id}"); |
| 250 | } |
| 251 | |
| 252 | [Fact(Timeout = 5000)] |
| 253 | [Trait("Category", "Conversations")] |
| 254 | public async Task ApiClient_CreatePersonalChat_AndSendViaActivities() |
| 255 | { |
| 256 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 257 | |
| 258 | ConversationParameters parameters = new() |
| 259 | { |
| 260 | IsGroup = false, |
| 261 | Members = [new() { Id = memberMri }], |
| 262 | TenantId = _f.TenantId |
| 263 | }; |
| 264 | |
| 265 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters, _f.AgenticIdentity); |
| 266 | Assert.NotNull(response?.Id); |
| 267 | |
| 268 | CoreActivity activity = CoreActivity.CreateBuilder() |
| 269 | .WithType(ActivityType.Message) |
| 270 | .WithFrom(IntegrationTestFixture.GetConversationAccountWithAgenticProperties()) |
| 271 | .WithProperty("text", $"[ApiClient] 1:1 via Activities.Create at `{DateTime.UtcNow:s}`") |
| 272 | .Build(); |
| 273 | |
| 274 | SendActivityResponse? sent = await _api.Conversations.Activities.CreateAsync(response.Id, activity); |
| 275 | Assert.NotNull(sent?.Id); |
| 276 | _output.WriteLine($"[ApiClient] Created 1:1 {response.Id}, sent activity {sent.Id}"); |
| 277 | } |
| 278 | |
| 279 | #endregion |
| 280 | |
| 281 | #region Group Chat — ApiClient |
| 282 | |
| 283 | [Fact] |
| 284 | [Trait("Category", "Conversations")] |
| 285 | public async Task ApiClient_CreateGroupChat() |
| 286 | { |
| 287 | (string first, string? second) = await GetMemberMrisAsync(); |
| 288 | if (second is null) |
| 289 | { |
| 290 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // Service rejects multiple members when creating via Bot + Members pattern. |
| 295 | // Using a single non-bot member creates a 1:1 "group-style" conversation. |
| 296 | ConversationParameters parameters = new() |
| 297 | { |
| 298 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 299 | Members = |
| 300 | [ |
| 301 | new() { Id = second } |
| 302 | ], |
| 303 | TenantId = _f.TenantId, |
| 304 | TopicName = $"[ApiClient] Group - {DateTime.UtcNow:s}", |
| 305 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 306 | }; |
| 307 | |
| 308 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters, _f.AgenticIdentity); |
| 309 | |
| 310 | Assert.NotNull(response); |
| 311 | Assert.NotNull(response.Id); |
| 312 | _output.WriteLine($"[ApiClient] Created group conversation: {response.Id}"); |
| 313 | } |
| 314 | |
| 315 | #endregion |
| 316 | |
| 317 | #region Channel Thread — ApiClient |
| 318 | |
| 319 | [Fact(Timeout = 5000)] |
| 320 | [Trait("Category", "Conversations")] |
| 321 | public async Task ApiClient_CreateChannelThread() |
| 322 | { |
| 323 | ConversationParameters parameters = new() |
| 324 | { |
| 325 | IsGroup = true, |
| 326 | ChannelData = new { channel = new { id = _f.ChannelId } }, |
| 327 | Activity = CoreActivity.CreateBuilder() |
| 328 | .WithType(ActivityType.Message) |
| 329 | .WithProperty("text", $"[ApiClient] New channel thread at `{DateTime.UtcNow:s}`") |
| 330 | .Build(), |
| 331 | TenantId = _f.TenantId |
| 332 | }; |
| 333 | |
| 334 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters, _f.AgenticIdentity); |
| 335 | |
| 336 | Assert.NotNull(response); |
| 337 | Assert.NotNull(response.Id); |
| 338 | _output.WriteLine($"[ApiClient] Created channel thread: {response.Id}, activityId: {response.ActivityId}"); |
| 339 | } |
| 340 | |
| 341 | [Fact(Timeout = 5000)] |
| 342 | [Trait("Category", "Conversations")] |
| 343 | public async Task ApiClient_CreateChannelThread_AndReply() |
| 344 | { |
| 345 | ConversationParameters parameters = new() |
| 346 | { |
| 347 | IsGroup = true, |
| 348 | ChannelData = new { channel = new { id = _f.ChannelId } }, |
| 349 | Activity = CoreActivity.CreateBuilder() |
| 350 | .WithType(ActivityType.Message) |
| 351 | .WithProperty("text", $"[ApiClient] Thread root at `{DateTime.UtcNow:s}`") |
| 352 | .Build(), |
| 353 | TenantId = _f.TenantId |
| 354 | }; |
| 355 | |
| 356 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters, _f.AgenticIdentity); |
| 357 | Assert.NotNull(response?.Id); |
| 358 | Assert.NotNull(response.ActivityId); |
| 359 | |
| 360 | // Reply to the thread |
| 361 | CoreActivity reply = CoreActivity.CreateBuilder() |
| 362 | .WithType(ActivityType.Message) |
| 363 | .WithFrom(IntegrationTestFixture.GetConversationAccountWithAgenticProperties()) |
| 364 | .WithProperty("text", $"[ApiClient] Thread reply at `{DateTime.UtcNow:s}`") |
| 365 | .Build(); |
| 366 | |
| 367 | SendActivityResponse? replyResponse = await _api.Conversations.Activities.ReplyAsync( |
| 368 | response.Id, response.ActivityId, reply); |
| 369 | |
| 370 | Assert.NotNull(replyResponse); |
| 371 | _output.WriteLine($"[ApiClient] Created thread {response.Id}, root activity {response.ActivityId}, reply {replyResponse?.Id}"); |
| 372 | } |
| 373 | |
| 374 | #endregion |
| 375 | } |
| 376 | |