microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/IntegrationTests/CreateConversationTests.cs
373lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Api.Clients; |
| 5 | using Microsoft.Teams.Bot.Core; |
| 6 | using Microsoft.Teams.Bot.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 async Task<(string first, string? second)> GetMemberMrisAsync() |
| 35 | { |
| 36 | IList<ConversationAccount> members = await _f.ConversationClient.GetConversationMembersAsync( |
| 37 | _f.ConversationId, _f.ServiceUrl, _f.AgenticIdentity); |
| 38 | |
| 39 | Assert.True(members.Count >= 1, "Need at least 1 member in the test conversation"); |
| 40 | |
| 41 | string first = members[0].Id!; |
| 42 | string? second = members.Count >= 2 ? members[1].Id : null; |
| 43 | |
| 44 | _output.WriteLine($"Using member MRIs: first={first}, second={second ?? "(none)"}"); |
| 45 | return (first, second); |
| 46 | } |
| 47 | |
| 48 | #region Personal Chat (1:1) — Core ConversationClient |
| 49 | |
| 50 | [Fact] |
| 51 | public async Task Core_CreatePersonalChat() |
| 52 | { |
| 53 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 54 | |
| 55 | ConversationParameters parameters = new() |
| 56 | { |
| 57 | IsGroup = false, |
| 58 | Members = [new() { Id = memberMri }], |
| 59 | TenantId = _f.TenantId |
| 60 | }; |
| 61 | |
| 62 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 63 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 64 | |
| 65 | Assert.NotNull(response); |
| 66 | Assert.NotNull(response.Id); |
| 67 | _output.WriteLine($"Created 1:1 conversation: {response.Id}"); |
| 68 | } |
| 69 | |
| 70 | [Fact] |
| 71 | public async Task Core_CreatePersonalChat_AndSendMessage() |
| 72 | { |
| 73 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 74 | |
| 75 | ConversationParameters parameters = new() |
| 76 | { |
| 77 | IsGroup = false, |
| 78 | Members = [new() { Id = memberMri }], |
| 79 | TenantId = _f.TenantId |
| 80 | }; |
| 81 | |
| 82 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 83 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 84 | |
| 85 | Assert.NotNull(response?.Id); |
| 86 | |
| 87 | CoreActivity activity = new() |
| 88 | { |
| 89 | Type = ActivityType.Message, |
| 90 | Properties = { { "text", $"[Core] 1:1 message at `{DateTime.UtcNow:s}`" } }, |
| 91 | ServiceUrl = _f.ServiceUrl, |
| 92 | Conversation = new(response.Id) |
| 93 | }; |
| 94 | |
| 95 | SendActivityResponse? sent = await _f.ConversationClient.SendActivityAsync(activity); |
| 96 | Assert.NotNull(sent?.Id); |
| 97 | _output.WriteLine($"Created 1:1 conversation {response.Id} and sent activity {sent.Id}"); |
| 98 | } |
| 99 | |
| 100 | [Fact] |
| 101 | public async Task Core_CreatePersonalChat_WithInitialActivity() |
| 102 | { |
| 103 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 104 | |
| 105 | ConversationParameters parameters = new() |
| 106 | { |
| 107 | IsGroup = false, |
| 108 | Members = [new() { Id = memberMri }], |
| 109 | TenantId = _f.TenantId, |
| 110 | Activity = new CoreActivity |
| 111 | { |
| 112 | Type = ActivityType.Message, |
| 113 | Properties = { { "text", $"[Core] Initial message at `{DateTime.UtcNow:s}`" } } |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 118 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 119 | |
| 120 | Assert.NotNull(response); |
| 121 | Assert.NotNull(response.Id); |
| 122 | _output.WriteLine($"Created 1:1 conversation with initial activity: {response.Id}, activityId: {response.ActivityId}"); |
| 123 | } |
| 124 | |
| 125 | #endregion |
| 126 | |
| 127 | #region Group Chat — Core ConversationClient |
| 128 | |
| 129 | [Fact(Skip = "Teams Bot Framework API does not support group chat creation")] |
| 130 | public async Task Core_CreateGroupChat() |
| 131 | { |
| 132 | (string first, string? second) = await GetMemberMrisAsync(); |
| 133 | if (second is null) |
| 134 | { |
| 135 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | ConversationParameters parameters = new() |
| 140 | { |
| 141 | IsGroup = true, |
| 142 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 143 | Members = |
| 144 | [ |
| 145 | new() { Id = first }, |
| 146 | new() { Id = second } |
| 147 | ], |
| 148 | TenantId = _f.TenantId, |
| 149 | TopicName = $"Integration Test Group - {DateTime.UtcNow:s}", |
| 150 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 151 | }; |
| 152 | |
| 153 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 154 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 155 | |
| 156 | Assert.NotNull(response); |
| 157 | Assert.NotNull(response.Id); |
| 158 | _output.WriteLine($"Created group conversation: {response.Id}"); |
| 159 | } |
| 160 | |
| 161 | [Fact(Skip = "Teams Bot Framework API does not support group chat creation")] |
| 162 | public async Task Core_CreateGroupChat_AndSendMessage() |
| 163 | { |
| 164 | (string first, string? second) = await GetMemberMrisAsync(); |
| 165 | if (second is null) |
| 166 | { |
| 167 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | ConversationParameters parameters = new() |
| 172 | { |
| 173 | IsGroup = true, |
| 174 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 175 | Members = |
| 176 | [ |
| 177 | new() { Id = first }, |
| 178 | new() { Id = second } |
| 179 | ], |
| 180 | TenantId = _f.TenantId, |
| 181 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 182 | }; |
| 183 | |
| 184 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 185 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 186 | |
| 187 | Assert.NotNull(response?.Id); |
| 188 | |
| 189 | CoreActivity activity = new() |
| 190 | { |
| 191 | Type = ActivityType.Message, |
| 192 | Properties = { { "text", $"[Core] Group message at `{DateTime.UtcNow:s}`" } }, |
| 193 | ServiceUrl = _f.ServiceUrl, |
| 194 | Conversation = new(response.Id) |
| 195 | }; |
| 196 | |
| 197 | SendActivityResponse? sent = await _f.ConversationClient.SendActivityAsync(activity); |
| 198 | Assert.NotNull(sent?.Id); |
| 199 | _output.WriteLine($"Created group {response.Id} and sent activity {sent.Id}"); |
| 200 | } |
| 201 | |
| 202 | #endregion |
| 203 | |
| 204 | #region Channel Thread — Core ConversationClient |
| 205 | |
| 206 | [Fact] |
| 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 = new CoreActivity |
| 214 | { |
| 215 | Type = ActivityType.Message, |
| 216 | Properties = { { "text", $"[Core] New channel thread at `{DateTime.UtcNow:s}`" } } |
| 217 | }, |
| 218 | TenantId = _f.TenantId |
| 219 | }; |
| 220 | |
| 221 | CreateConversationResponse response = await _f.ConversationClient.CreateConversationAsync( |
| 222 | parameters, _f.ServiceUrl, _f.AgenticIdentity); |
| 223 | |
| 224 | Assert.NotNull(response); |
| 225 | Assert.NotNull(response.Id); |
| 226 | _output.WriteLine($"Created channel thread: {response.Id}, activityId: {response.ActivityId}"); |
| 227 | } |
| 228 | |
| 229 | #endregion |
| 230 | |
| 231 | #region Personal Chat — ApiClient |
| 232 | |
| 233 | [Fact] |
| 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); |
| 246 | |
| 247 | Assert.NotNull(response); |
| 248 | Assert.NotNull(response.Id); |
| 249 | _output.WriteLine($"[ApiClient] Created 1:1 conversation: {response.Id}"); |
| 250 | } |
| 251 | |
| 252 | [Fact] |
| 253 | public async Task ApiClient_CreatePersonalChat_AndSendViaActivities() |
| 254 | { |
| 255 | (string memberMri, _) = await GetMemberMrisAsync(); |
| 256 | |
| 257 | ConversationParameters parameters = new() |
| 258 | { |
| 259 | IsGroup = false, |
| 260 | Members = [new() { Id = memberMri }], |
| 261 | TenantId = _f.TenantId |
| 262 | }; |
| 263 | |
| 264 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters); |
| 265 | Assert.NotNull(response?.Id); |
| 266 | |
| 267 | CoreActivity activity = new() |
| 268 | { |
| 269 | Type = ActivityType.Message, |
| 270 | Properties = { { "text", $"[ApiClient] 1:1 via Activities.Create at `{DateTime.UtcNow:s}`" } } |
| 271 | }; |
| 272 | |
| 273 | SendActivityResponse? sent = await _api.Conversations.Activities.CreateAsync(response.Id, activity); |
| 274 | Assert.NotNull(sent?.Id); |
| 275 | _output.WriteLine($"[ApiClient] Created 1:1 {response.Id}, sent activity {sent.Id}"); |
| 276 | } |
| 277 | |
| 278 | #endregion |
| 279 | |
| 280 | #region Group Chat — ApiClient |
| 281 | |
| 282 | [Fact(Skip = "Teams Bot Framework API does not support group chat creation")] |
| 283 | public async Task ApiClient_CreateGroupChat() |
| 284 | { |
| 285 | (string first, string? second) = await GetMemberMrisAsync(); |
| 286 | if (second is null) |
| 287 | { |
| 288 | _output.WriteLine("Skipping: need at least 2 members in conversation"); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | ConversationParameters parameters = new() |
| 293 | { |
| 294 | IsGroup = true, |
| 295 | Bot = new() { Id = $"28:{_f.BotAppId}" }, |
| 296 | Members = |
| 297 | [ |
| 298 | new() { Id = first }, |
| 299 | new() { Id = second } |
| 300 | ], |
| 301 | TenantId = _f.TenantId, |
| 302 | TopicName = $"[ApiClient] Group - {DateTime.UtcNow:s}", |
| 303 | ChannelData = new { tenant = new { id = _f.TenantId } } |
| 304 | }; |
| 305 | |
| 306 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters); |
| 307 | |
| 308 | Assert.NotNull(response); |
| 309 | Assert.NotNull(response.Id); |
| 310 | _output.WriteLine($"[ApiClient] Created group conversation: {response.Id}"); |
| 311 | } |
| 312 | |
| 313 | #endregion |
| 314 | |
| 315 | #region Channel Thread — ApiClient |
| 316 | |
| 317 | [Fact] |
| 318 | public async Task ApiClient_CreateChannelThread() |
| 319 | { |
| 320 | ConversationParameters parameters = new() |
| 321 | { |
| 322 | IsGroup = true, |
| 323 | ChannelData = new { channel = new { id = _f.ChannelId } }, |
| 324 | Activity = new CoreActivity |
| 325 | { |
| 326 | Type = ActivityType.Message, |
| 327 | Properties = { { "text", $"[ApiClient] New channel thread at `{DateTime.UtcNow:s}`" } } |
| 328 | }, |
| 329 | TenantId = _f.TenantId |
| 330 | }; |
| 331 | |
| 332 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters); |
| 333 | |
| 334 | Assert.NotNull(response); |
| 335 | Assert.NotNull(response.Id); |
| 336 | _output.WriteLine($"[ApiClient] Created channel thread: {response.Id}, activityId: {response.ActivityId}"); |
| 337 | } |
| 338 | |
| 339 | [Fact] |
| 340 | public async Task ApiClient_CreateChannelThread_AndReply() |
| 341 | { |
| 342 | ConversationParameters parameters = new() |
| 343 | { |
| 344 | IsGroup = true, |
| 345 | ChannelData = new { channel = new { id = _f.ChannelId } }, |
| 346 | Activity = new CoreActivity |
| 347 | { |
| 348 | Type = ActivityType.Message, |
| 349 | Properties = { { "text", $"[ApiClient] Thread root at `{DateTime.UtcNow:s}`" } } |
| 350 | }, |
| 351 | TenantId = _f.TenantId |
| 352 | }; |
| 353 | |
| 354 | CreateConversationResponse response = await _api.Conversations.CreateAsync(parameters); |
| 355 | Assert.NotNull(response?.Id); |
| 356 | Assert.NotNull(response.ActivityId); |
| 357 | |
| 358 | // Reply to the thread |
| 359 | CoreActivity reply = new() |
| 360 | { |
| 361 | Type = ActivityType.Message, |
| 362 | Properties = { { "text", $"[ApiClient] Thread reply at `{DateTime.UtcNow:s}`" } } |
| 363 | }; |
| 364 | |
| 365 | SendActivityResponse? replyResponse = await _api.Conversations.Activities.ReplyAsync( |
| 366 | response.Id, response.ActivityId, reply); |
| 367 | |
| 368 | Assert.NotNull(replyResponse); |
| 369 | _output.WriteLine($"[ApiClient] Created thread {response.Id}, root activity {response.ActivityId}, reply {replyResponse?.Id}"); |
| 370 | } |
| 371 | |
| 372 | #endregion |
| 373 | } |
| 374 | |