microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Bot.Core.Tests/ConversationClientTest.cs
717lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Core.Hosting; |
| 5 | using Microsoft.Teams.Bot.Core.Schema; |
| 6 | using Microsoft.Extensions.Configuration; |
| 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | using Microsoft.Teams.Bot.Core; |
| 9 | |
| 10 | namespace Microsoft.Bot.Core.Tests; |
| 11 | |
| 12 | public class ConversationClientTest |
| 13 | { |
| 14 | private readonly ServiceProvider _serviceProvider; |
| 15 | private readonly ConversationClient _conversationClient; |
| 16 | private readonly Uri _serviceUrl; |
| 17 | |
| 18 | public ConversationClientTest() |
| 19 | { |
| 20 | IConfigurationBuilder builder = new ConfigurationBuilder() |
| 21 | .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) |
| 22 | .AddEnvironmentVariables(); |
| 23 | |
| 24 | IConfiguration configuration = builder.Build(); |
| 25 | |
| 26 | ServiceCollection services = new(); |
| 27 | services.AddLogging(); |
| 28 | services.AddSingleton(configuration); |
| 29 | services.AddBotApplication<BotApplication>(); |
| 30 | _serviceProvider = services.BuildServiceProvider(); |
| 31 | _conversationClient = _serviceProvider.GetRequiredService<ConversationClient>(); |
| 32 | _serviceUrl = new Uri(Environment.GetEnvironmentVariable("TEST_SERVICEURL") ?? "https://smba.trafficmanager.net/teams/"); |
| 33 | } |
| 34 | |
| 35 | [Fact] |
| 36 | public async Task SendActivityDefault() |
| 37 | { |
| 38 | CoreActivity activity = new() |
| 39 | { |
| 40 | Type = ActivityType.Message, |
| 41 | Properties = { { "text", $"Message from Automated tests, running in SDK `{BotApplication.Version}` at `{DateTime.UtcNow:s}`" } }, |
| 42 | ServiceUrl = _serviceUrl, |
| 43 | Conversation = new() |
| 44 | { |
| 45 | Id = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set") |
| 46 | } |
| 47 | }; |
| 48 | SendActivityResponse res = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 49 | Assert.NotNull(res); |
| 50 | Assert.NotNull(res.Id); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | [Fact] |
| 55 | public async Task SendActivityToChannel() |
| 56 | { |
| 57 | CoreActivity activity = new() |
| 58 | { |
| 59 | Type = ActivityType.Message, |
| 60 | Properties = { { "text", $"Message from Automated tests, running in SDK `{BotApplication.Version}` at `{DateTime.UtcNow:s}`" } }, |
| 61 | ServiceUrl = _serviceUrl, |
| 62 | Conversation = new() |
| 63 | { |
| 64 | Id = Environment.GetEnvironmentVariable("TEST_CHANNELID") ?? throw new InvalidOperationException("TEST_CHANNELID environment variable not set") |
| 65 | } |
| 66 | }; |
| 67 | SendActivityResponse res = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 68 | Assert.NotNull(res); |
| 69 | Assert.NotNull(res.Id); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public async Task SendActivityToPersonalChat_FailsWithBad_ConversationId() |
| 74 | { |
| 75 | CoreActivity activity = new() |
| 76 | { |
| 77 | Type = ActivityType.Message, |
| 78 | Properties = { { "text", $"Message from Automated tests, running in SDK `{BotApplication.Version}` at `{DateTime.UtcNow:s}`" } }, |
| 79 | ServiceUrl = _serviceUrl, |
| 80 | Conversation = new() |
| 81 | { |
| 82 | Id = "a:1" |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | await Assert.ThrowsAsync<HttpRequestException>(() |
| 87 | => _conversationClient.SendActivityAsync(activity)); |
| 88 | } |
| 89 | |
| 90 | [Fact] |
| 91 | public async Task UpdateActivity() |
| 92 | { |
| 93 | // First send an activity to get an ID |
| 94 | CoreActivity activity = new() |
| 95 | { |
| 96 | Type = ActivityType.Message, |
| 97 | Properties = { { "text", $"Original message from Automated tests at `{DateTime.UtcNow:s}`" } }, |
| 98 | ServiceUrl = _serviceUrl, |
| 99 | Conversation = new() |
| 100 | { |
| 101 | Id = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set") |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 106 | Assert.NotNull(sendResponse); |
| 107 | Assert.NotNull(sendResponse.Id); |
| 108 | |
| 109 | // Now update the activity |
| 110 | CoreActivity updatedActivity = new() |
| 111 | { |
| 112 | Type = ActivityType.Message, |
| 113 | Properties = { { "text", $"Updated message from Automated tests at `{DateTime.UtcNow:s}`" } }, |
| 114 | ServiceUrl = _serviceUrl, |
| 115 | }; |
| 116 | |
| 117 | UpdateActivityResponse updateResponse = await _conversationClient.UpdateActivityAsync( |
| 118 | activity.Conversation.Id, |
| 119 | sendResponse.Id, |
| 120 | updatedActivity, |
| 121 | cancellationToken: CancellationToken.None); |
| 122 | |
| 123 | Assert.NotNull(updateResponse); |
| 124 | Assert.NotNull(updateResponse.Id); |
| 125 | } |
| 126 | |
| 127 | [Fact] |
| 128 | public async Task DeleteActivity() |
| 129 | { |
| 130 | // First send an activity to get an ID |
| 131 | CoreActivity activity = new() |
| 132 | { |
| 133 | Type = ActivityType.Message, |
| 134 | Properties = { { "text", $"Message to delete from Automated tests at `{DateTime.UtcNow:s}`" } }, |
| 135 | ServiceUrl = _serviceUrl, |
| 136 | Conversation = new() |
| 137 | { |
| 138 | Id = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set") |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 143 | Assert.NotNull(sendResponse); |
| 144 | Assert.NotNull(sendResponse.Id); |
| 145 | |
| 146 | // Add a delay for 5 seconds |
| 147 | await Task.Delay(TimeSpan.FromSeconds(5)); |
| 148 | |
| 149 | // Now delete the activity |
| 150 | await _conversationClient.DeleteActivityAsync( |
| 151 | activity.Conversation.Id, |
| 152 | sendResponse.Id, |
| 153 | _serviceUrl, |
| 154 | cancellationToken: CancellationToken.None); |
| 155 | |
| 156 | // If no exception was thrown, the delete was successful |
| 157 | } |
| 158 | |
| 159 | [Fact] |
| 160 | public async Task GetConversationMembers() |
| 161 | { |
| 162 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 163 | |
| 164 | IList<ConversationAccount> members = await _conversationClient.GetConversationMembersAsync( |
| 165 | conversationId, |
| 166 | _serviceUrl, |
| 167 | cancellationToken: CancellationToken.None); |
| 168 | |
| 169 | Assert.NotNull(members); |
| 170 | Assert.NotEmpty(members); |
| 171 | |
| 172 | // Log members |
| 173 | Console.WriteLine($"Found {members.Count} members in conversation {conversationId}:"); |
| 174 | foreach (ConversationAccount member in members) |
| 175 | { |
| 176 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 177 | Assert.NotNull(member); |
| 178 | Assert.NotNull(member.Id); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | [Fact] |
| 183 | public async Task GetConversationMember() |
| 184 | { |
| 185 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 186 | string userId = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"); |
| 187 | |
| 188 | ConversationAccount member = await _conversationClient.GetConversationMemberAsync<ConversationAccount>( |
| 189 | conversationId, |
| 190 | userId, |
| 191 | _serviceUrl, |
| 192 | cancellationToken: CancellationToken.None); |
| 193 | |
| 194 | Assert.NotNull(member); |
| 195 | |
| 196 | // Log member |
| 197 | Console.WriteLine($"Found member in conversation {conversationId}:"); |
| 198 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 199 | Assert.NotNull(member); |
| 200 | Assert.NotNull(member.Id); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | [Fact] |
| 205 | public async Task GetConversationMembersInChannel() |
| 206 | { |
| 207 | string channelId = Environment.GetEnvironmentVariable("TEST_CHANNELID") ?? throw new InvalidOperationException("TEST_CHANNELID environment variable not set"); |
| 208 | |
| 209 | IList<ConversationAccount> members = await _conversationClient.GetConversationMembersAsync( |
| 210 | channelId, |
| 211 | _serviceUrl, |
| 212 | cancellationToken: CancellationToken.None); |
| 213 | |
| 214 | Assert.NotNull(members); |
| 215 | Assert.NotEmpty(members); |
| 216 | |
| 217 | // Log members |
| 218 | Console.WriteLine($"Found {members.Count} members in channel {channelId}:"); |
| 219 | foreach (ConversationAccount member in members) |
| 220 | { |
| 221 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 222 | Assert.NotNull(member); |
| 223 | Assert.NotNull(member.Id); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | [Fact] |
| 228 | public async Task GetActivityMembers() |
| 229 | { |
| 230 | // First send an activity to get an activity ID |
| 231 | CoreActivity activity = new() |
| 232 | { |
| 233 | Type = ActivityType.Message, |
| 234 | Properties = { { "text", $"Message for GetActivityMembers test at `{DateTime.UtcNow:s}`" } }, |
| 235 | ServiceUrl = _serviceUrl, |
| 236 | Conversation = new() |
| 237 | { |
| 238 | Id = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set") |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 243 | Assert.NotNull(sendResponse); |
| 244 | Assert.NotNull(sendResponse.Id); |
| 245 | |
| 246 | // Now get the members of this activity |
| 247 | IList<ConversationAccount> members = await _conversationClient.GetActivityMembersAsync( |
| 248 | activity.Conversation.Id, |
| 249 | sendResponse.Id, |
| 250 | _serviceUrl, |
| 251 | cancellationToken: CancellationToken.None); |
| 252 | |
| 253 | Assert.NotNull(members); |
| 254 | Assert.NotEmpty(members); |
| 255 | |
| 256 | // Log activity members |
| 257 | Console.WriteLine($"Found {members.Count} members for activity {sendResponse.Id}:"); |
| 258 | foreach (ConversationAccount member in members) |
| 259 | { |
| 260 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 261 | Assert.NotNull(member); |
| 262 | Assert.NotNull(member.Id); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // TODO: This doesn't work |
| 267 | [Fact(Skip = "Method not allowed by API")] |
| 268 | public async Task GetConversations() |
| 269 | { |
| 270 | GetConversationsResponse response = await _conversationClient.GetConversationsAsync( |
| 271 | _serviceUrl, |
| 272 | cancellationToken: CancellationToken.None); |
| 273 | |
| 274 | Assert.NotNull(response); |
| 275 | Assert.NotNull(response.Conversations); |
| 276 | Assert.NotEmpty(response.Conversations); |
| 277 | |
| 278 | // Log conversations |
| 279 | Console.WriteLine($"Found {response.Conversations.Count} conversations:"); |
| 280 | foreach (ConversationMembers conversation in response.Conversations) |
| 281 | { |
| 282 | Console.WriteLine($" - Conversation Id: {conversation.Id}"); |
| 283 | Assert.NotNull(conversation); |
| 284 | Assert.NotNull(conversation.Id); |
| 285 | |
| 286 | if (conversation.Members != null && conversation.Members.Any()) |
| 287 | { |
| 288 | Console.WriteLine($" Members ({conversation.Members.Count}):"); |
| 289 | foreach (ConversationAccount member in conversation.Members) |
| 290 | { |
| 291 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | [Fact] |
| 298 | public async Task CreateConversation_WithMembers() |
| 299 | { |
| 300 | // Create a 1-on-1 conversation with a member |
| 301 | ConversationParameters parameters = new() |
| 302 | { |
| 303 | IsGroup = false, |
| 304 | Members = |
| 305 | [ |
| 306 | new() |
| 307 | { |
| 308 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"), |
| 309 | } |
| 310 | ], |
| 311 | // TODO: This is required for some reason. Should it be required in the api? |
| 312 | TenantId = Environment.GetEnvironmentVariable("AzureAd__TenantId") ?? throw new InvalidOperationException("AzureAd__TenantId environment variable not set") |
| 313 | }; |
| 314 | |
| 315 | CreateConversationResponse response = await _conversationClient.CreateConversationAsync( |
| 316 | parameters, |
| 317 | _serviceUrl, |
| 318 | cancellationToken: CancellationToken.None); |
| 319 | |
| 320 | Assert.NotNull(response); |
| 321 | Assert.NotNull(response.Id); |
| 322 | |
| 323 | Console.WriteLine($"Created conversation: {response.Id}"); |
| 324 | Console.WriteLine($" ActivityId: {response.ActivityId}"); |
| 325 | Console.WriteLine($" ServiceUrl: {response.ServiceUrl}"); |
| 326 | |
| 327 | // Send a message to the newly created conversation |
| 328 | CoreActivity activity = new() |
| 329 | { |
| 330 | Type = ActivityType.Message, |
| 331 | Properties = { { "text", $"Test message to new conversation at {DateTime.UtcNow:s}" } }, |
| 332 | ServiceUrl = _serviceUrl, |
| 333 | Conversation = new() |
| 334 | { |
| 335 | Id = response.Id |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 340 | Assert.NotNull(sendResponse); |
| 341 | Assert.NotNull(sendResponse.Id); |
| 342 | |
| 343 | Console.WriteLine($" Sent message with activity ID: {sendResponse.Id}"); |
| 344 | } |
| 345 | |
| 346 | // TODO: This doesn't work |
| 347 | [Fact(Skip = "Incorrect conversation creation parameters")] |
| 348 | public async Task CreateConversation_WithGroup() |
| 349 | { |
| 350 | // Create a group conversation |
| 351 | ConversationParameters parameters = new() |
| 352 | { |
| 353 | IsGroup = true, |
| 354 | Members = |
| 355 | [ |
| 356 | new() |
| 357 | { |
| 358 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"), |
| 359 | }, |
| 360 | new() |
| 361 | { |
| 362 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID_2") ?? throw new InvalidOperationException("TEST_USER_ID_2 environment variable not set"), |
| 363 | } |
| 364 | ], |
| 365 | TenantId = Environment.GetEnvironmentVariable("TENANT_ID") ?? throw new InvalidOperationException("TENANT_ID environment variable not set") |
| 366 | }; |
| 367 | |
| 368 | CreateConversationResponse response = await _conversationClient.CreateConversationAsync( |
| 369 | parameters, |
| 370 | _serviceUrl, |
| 371 | cancellationToken: CancellationToken.None); |
| 372 | |
| 373 | Assert.NotNull(response); |
| 374 | Assert.NotNull(response.Id); |
| 375 | |
| 376 | Console.WriteLine($"Created group conversation: {response.Id}"); |
| 377 | |
| 378 | // Send a message to the newly created group conversation |
| 379 | CoreActivity activity = new() |
| 380 | { |
| 381 | Type = ActivityType.Message, |
| 382 | Properties = { { "text", $"Test message to new group conversation at {DateTime.UtcNow:s}" } }, |
| 383 | ServiceUrl = _serviceUrl, |
| 384 | Conversation = new() |
| 385 | { |
| 386 | Id = response.Id |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 391 | Assert.NotNull(sendResponse); |
| 392 | Assert.NotNull(sendResponse.Id); |
| 393 | |
| 394 | Console.WriteLine($" Sent message with activity ID: {sendResponse.Id}"); |
| 395 | } |
| 396 | |
| 397 | // TODO: This doesn't work |
| 398 | [Fact(Skip = "Incorrect conversation creation parameters")] |
| 399 | public async Task CreateConversation_WithTopicName() |
| 400 | { |
| 401 | // Create a conversation with a topic name |
| 402 | ConversationParameters parameters = new() |
| 403 | { |
| 404 | IsGroup = true, |
| 405 | TopicName = $"Test Conversation - {DateTime.UtcNow:s}", |
| 406 | Members = |
| 407 | [ |
| 408 | new() |
| 409 | { |
| 410 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"), |
| 411 | } |
| 412 | ], |
| 413 | TenantId = Environment.GetEnvironmentVariable("TENANT_ID") ?? throw new InvalidOperationException("TENANT_ID environment variable not set") |
| 414 | }; |
| 415 | |
| 416 | CreateConversationResponse response = await _conversationClient.CreateConversationAsync( |
| 417 | parameters, |
| 418 | _serviceUrl, |
| 419 | cancellationToken: CancellationToken.None); |
| 420 | |
| 421 | Assert.NotNull(response); |
| 422 | Assert.NotNull(response.Id); |
| 423 | |
| 424 | Console.WriteLine($"Created conversation with topic '{parameters.TopicName}': {response.Id}"); |
| 425 | |
| 426 | // Send a message to the newly created conversation |
| 427 | CoreActivity activity = new() |
| 428 | { |
| 429 | Type = ActivityType.Message, |
| 430 | Properties = { { "text", $"Test message to conversation with topic name at {DateTime.UtcNow:s}" } }, |
| 431 | ServiceUrl = _serviceUrl, |
| 432 | Conversation = new() |
| 433 | { |
| 434 | Id = response.Id |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | SendActivityResponse sendResponse = await _conversationClient.SendActivityAsync(activity, cancellationToken: CancellationToken.None); |
| 439 | Assert.NotNull(sendResponse); |
| 440 | Assert.NotNull(sendResponse.Id); |
| 441 | |
| 442 | Console.WriteLine($" Sent message with activity ID: {sendResponse.Id}"); |
| 443 | } |
| 444 | |
| 445 | // TODO: This doesn't fail, but doesn't actually create the initial activity |
| 446 | [Fact] |
| 447 | public async Task CreateConversation_WithInitialActivity() |
| 448 | { |
| 449 | // Create a conversation with an initial message |
| 450 | ConversationParameters parameters = new() |
| 451 | { |
| 452 | IsGroup = false, |
| 453 | Members = |
| 454 | [ |
| 455 | new() |
| 456 | { |
| 457 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"), |
| 458 | } |
| 459 | ], |
| 460 | Activity = new CoreActivity |
| 461 | { |
| 462 | Type = ActivityType.Message, |
| 463 | Properties = { { "text", $"Initial message sent at {DateTime.UtcNow:s}" } }, |
| 464 | }, |
| 465 | TenantId = Environment.GetEnvironmentVariable("AzureAd__TenantId") ?? throw new InvalidOperationException("AzureAd__TenantId environment variable not set") |
| 466 | }; |
| 467 | |
| 468 | CreateConversationResponse response = await _conversationClient.CreateConversationAsync( |
| 469 | parameters, |
| 470 | _serviceUrl, |
| 471 | cancellationToken: CancellationToken.None); |
| 472 | |
| 473 | Assert.NotNull(response); |
| 474 | Assert.NotNull(response.Id); |
| 475 | // Assert.NotNull(response.ActivityId); // Should have an activity ID since we sent an initial message |
| 476 | |
| 477 | Console.WriteLine($"Created conversation with initial activity: {response.Id}"); |
| 478 | Console.WriteLine($" Initial activity ID: {response.ActivityId}"); |
| 479 | } |
| 480 | |
| 481 | [Fact] |
| 482 | public async Task CreateConversation_WithChannelData() |
| 483 | { |
| 484 | // Create a conversation with channel-specific data |
| 485 | ConversationParameters parameters = new() |
| 486 | { |
| 487 | IsGroup = false, |
| 488 | Members = |
| 489 | [ |
| 490 | new() |
| 491 | { |
| 492 | Id = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"), |
| 493 | } |
| 494 | ], |
| 495 | ChannelData = new |
| 496 | { |
| 497 | teamsChannelId = Environment.GetEnvironmentVariable("TEST_CHANNELID") |
| 498 | }, |
| 499 | TenantId = Environment.GetEnvironmentVariable("AzureAd__TenantId") ?? throw new InvalidOperationException("AzureAd__TenantId environment variable not set") |
| 500 | }; |
| 501 | |
| 502 | CreateConversationResponse response = await _conversationClient.CreateConversationAsync( |
| 503 | parameters, |
| 504 | _serviceUrl, |
| 505 | cancellationToken: CancellationToken.None); |
| 506 | |
| 507 | Assert.NotNull(response); |
| 508 | Assert.NotNull(response.Id); |
| 509 | |
| 510 | Console.WriteLine($"Created conversation with channel data: {response.Id}"); |
| 511 | } |
| 512 | |
| 513 | [Fact] |
| 514 | public async Task GetConversationPagedMembers() |
| 515 | { |
| 516 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 517 | |
| 518 | PagedMembersResult result = await _conversationClient.GetConversationPagedMembersAsync( |
| 519 | conversationId, |
| 520 | _serviceUrl, |
| 521 | cancellationToken: CancellationToken.None); |
| 522 | |
| 523 | Assert.NotNull(result); |
| 524 | Assert.NotNull(result.Members); |
| 525 | Assert.NotEmpty(result.Members); |
| 526 | |
| 527 | Console.WriteLine($"Found {result.Members.Count} members in page:"); |
| 528 | foreach (ConversationAccount member in result.Members) |
| 529 | { |
| 530 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 531 | Assert.NotNull(member); |
| 532 | Assert.NotNull(member.Id); |
| 533 | } |
| 534 | |
| 535 | if (!string.IsNullOrWhiteSpace(result.ContinuationToken)) |
| 536 | { |
| 537 | Console.WriteLine($"Continuation token: {result.ContinuationToken}"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | [Fact(Skip = "PageSize parameter not respected by API")] |
| 542 | public async Task GetConversationPagedMembers_WithPageSize() |
| 543 | { |
| 544 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 545 | |
| 546 | PagedMembersResult result = await _conversationClient.GetConversationPagedMembersAsync( |
| 547 | conversationId, |
| 548 | _serviceUrl, |
| 549 | pageSize: 1, |
| 550 | cancellationToken: CancellationToken.None); |
| 551 | |
| 552 | Assert.NotNull(result); |
| 553 | Assert.NotNull(result.Members); |
| 554 | Assert.NotEmpty(result.Members); |
| 555 | Assert.Single(result.Members); |
| 556 | |
| 557 | Console.WriteLine($"Found {result.Members.Count} members with pageSize=1:"); |
| 558 | foreach (ConversationAccount member in result.Members) |
| 559 | { |
| 560 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 561 | } |
| 562 | |
| 563 | // If there's a continuation token, get the next page |
| 564 | if (!string.IsNullOrWhiteSpace(result.ContinuationToken)) |
| 565 | { |
| 566 | Console.WriteLine($"Getting next page with continuation token..."); |
| 567 | |
| 568 | PagedMembersResult nextPage = await _conversationClient.GetConversationPagedMembersAsync( |
| 569 | conversationId, |
| 570 | _serviceUrl, |
| 571 | pageSize: 1, |
| 572 | continuationToken: result.ContinuationToken, |
| 573 | cancellationToken: CancellationToken.None); |
| 574 | |
| 575 | Assert.NotNull(nextPage); |
| 576 | Assert.NotNull(nextPage.Members); |
| 577 | |
| 578 | Console.WriteLine($"Found {nextPage.Members.Count} members in next page:"); |
| 579 | foreach (ConversationAccount member in nextPage.Members) |
| 580 | { |
| 581 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | [Fact(Skip = "Method not allowed by API")] |
| 587 | public async Task DeleteConversationMember() |
| 588 | { |
| 589 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 590 | |
| 591 | // Get members before deletion |
| 592 | IList<ConversationAccount> membersBefore = await _conversationClient.GetConversationMembersAsync( |
| 593 | conversationId, |
| 594 | _serviceUrl, |
| 595 | cancellationToken: CancellationToken.None); |
| 596 | |
| 597 | Assert.NotNull(membersBefore); |
| 598 | Assert.NotEmpty(membersBefore); |
| 599 | |
| 600 | Console.WriteLine($"Members before deletion: {membersBefore.Count}"); |
| 601 | foreach (ConversationAccount member in membersBefore) |
| 602 | { |
| 603 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 604 | } |
| 605 | |
| 606 | // Delete the test user |
| 607 | string memberToDelete = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new InvalidOperationException("TEST_USER_ID environment variable not set"); |
| 608 | |
| 609 | // Verify the member is in the conversation before attempting to delete |
| 610 | Assert.Contains(membersBefore, m => m.Id == memberToDelete); |
| 611 | |
| 612 | await _conversationClient.DeleteConversationMemberAsync( |
| 613 | conversationId, |
| 614 | memberToDelete, |
| 615 | _serviceUrl, |
| 616 | cancellationToken: CancellationToken.None); |
| 617 | |
| 618 | Console.WriteLine($"Deleted member: {memberToDelete}"); |
| 619 | |
| 620 | // Get members after deletion |
| 621 | IList<ConversationAccount> membersAfter = await _conversationClient.GetConversationMembersAsync( |
| 622 | conversationId, |
| 623 | _serviceUrl, |
| 624 | cancellationToken: CancellationToken.None); |
| 625 | |
| 626 | Assert.NotNull(membersAfter); |
| 627 | |
| 628 | Console.WriteLine($"Members after deletion: {membersAfter.Count}"); |
| 629 | foreach (ConversationAccount member in membersAfter) |
| 630 | { |
| 631 | Console.WriteLine($" - Id: {member.Id}, Name: {member.Name}"); |
| 632 | } |
| 633 | |
| 634 | // Verify the member was deleted |
| 635 | Assert.DoesNotContain(membersAfter, m => m.Id == memberToDelete); |
| 636 | } |
| 637 | |
| 638 | [Fact(Skip = "Unknown activity type error")] |
| 639 | public async Task SendConversationHistory() |
| 640 | { |
| 641 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 642 | |
| 643 | // Create a transcript with historic activities |
| 644 | Transcript transcript = new() |
| 645 | { |
| 646 | Activities = |
| 647 | [ |
| 648 | new() |
| 649 | { |
| 650 | Type = ActivityType.Message, |
| 651 | Id = Guid.NewGuid().ToString(), |
| 652 | Properties = { { "text", "Historic message 1" } }, |
| 653 | ServiceUrl = _serviceUrl, |
| 654 | Conversation = new() { Id = conversationId } |
| 655 | }, |
| 656 | new() |
| 657 | { |
| 658 | Type = ActivityType.Message, |
| 659 | Id = Guid.NewGuid().ToString(), |
| 660 | Properties = { { "text", "Historic message 2" } }, |
| 661 | ServiceUrl = _serviceUrl, |
| 662 | Conversation = new() { Id = conversationId } |
| 663 | }, |
| 664 | new() |
| 665 | { |
| 666 | Type = ActivityType.Message, |
| 667 | Id = Guid.NewGuid().ToString(), |
| 668 | Properties = { { "text", "Historic message 3" } }, |
| 669 | ServiceUrl = _serviceUrl, |
| 670 | Conversation = new() { Id = conversationId } |
| 671 | } |
| 672 | ] |
| 673 | }; |
| 674 | |
| 675 | SendConversationHistoryResponse response = await _conversationClient.SendConversationHistoryAsync( |
| 676 | conversationId, |
| 677 | transcript, |
| 678 | _serviceUrl, |
| 679 | cancellationToken: CancellationToken.None); |
| 680 | |
| 681 | Assert.NotNull(response); |
| 682 | |
| 683 | Console.WriteLine($"Sent conversation history with {transcript.Activities?.Count} activities"); |
| 684 | Console.WriteLine($"Response ID: {response.Id}"); |
| 685 | } |
| 686 | |
| 687 | [Fact(Skip = "Attachment upload endpoint not found")] |
| 688 | public async Task UploadAttachment() |
| 689 | { |
| 690 | string conversationId = Environment.GetEnvironmentVariable("TEST_CONVERSATIONID") ?? throw new InvalidOperationException("TEST_ConversationId environment variable not set"); |
| 691 | |
| 692 | // Create a simple text file as an attachment |
| 693 | string fileContent = "This is a test attachment file created at " + DateTime.UtcNow.ToString("s"); |
| 694 | byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileContent); |
| 695 | |
| 696 | AttachmentData attachmentData = new() |
| 697 | { |
| 698 | Type = "text/plain", |
| 699 | Name = "test-attachment.txt", |
| 700 | OriginalBase64 = fileBytes |
| 701 | }; |
| 702 | |
| 703 | UploadAttachmentResponse response = await _conversationClient.UploadAttachmentAsync( |
| 704 | conversationId, |
| 705 | attachmentData, |
| 706 | _serviceUrl, |
| 707 | cancellationToken: CancellationToken.None); |
| 708 | |
| 709 | Assert.NotNull(response); |
| 710 | Assert.NotNull(response.Id); |
| 711 | |
| 712 | Console.WriteLine($"Uploaded attachment: {attachmentData.Name}"); |
| 713 | Console.WriteLine($" Attachment ID: {response.Id}"); |
| 714 | Console.WriteLine($" Content-Type: {attachmentData.Type}"); |
| 715 | Console.WriteLine($" Size: {fileBytes.Length} bytes"); |
| 716 | } |
| 717 | } |
| 718 | |