microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Apps.Tests/Activities/PromptPreviewTests.cs
196lines · modecode
| 1 | using Microsoft.Teams.Api; |
| 2 | using Microsoft.Teams.Api.Activities; |
| 3 | using Microsoft.Teams.Api.Auth; |
| 4 | using Microsoft.Teams.Api.Entities; |
| 5 | using Microsoft.Teams.Apps.Activities; |
| 6 | using Microsoft.Teams.Apps.Testing.Plugins; |
| 7 | |
| 8 | namespace Microsoft.Teams.Apps.Tests.Activities; |
| 9 | |
| 10 | #pragma warning disable ExperimentalTeamsTargeted |
| 11 | public class PromptPreviewTests |
| 12 | { |
| 13 | private readonly App _app = new(); |
| 14 | private readonly IToken _token = Globals.Token; |
| 15 | |
| 16 | public PromptPreviewTests() |
| 17 | { |
| 18 | _app.AddPlugin(new TestPlugin()); |
| 19 | } |
| 20 | |
| 21 | [Fact] |
| 22 | public async Task Send_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() |
| 23 | { |
| 24 | IActivity? sentActivity = null; |
| 25 | |
| 26 | _app.OnMessage(async (context, cancellationToken) => |
| 27 | { |
| 28 | sentActivity = await context.Send("Here is the result!", cancellationToken); |
| 29 | }); |
| 30 | |
| 31 | // Simulate an incoming targeted message (bot's Recipient.IsTargeted = true) |
| 32 | var incomingActivity = new MessageActivity("summarize") |
| 33 | .WithId("1772129782775") |
| 34 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 35 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); |
| 36 | |
| 37 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 38 | |
| 39 | Assert.NotNull(sentActivity); |
| 40 | Assert.NotNull(sentActivity!.Entities); |
| 41 | |
| 42 | var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); |
| 43 | Assert.NotNull(targetedEntity); |
| 44 | Assert.Equal("1772129782775", targetedEntity!.MessageId); |
| 45 | } |
| 46 | |
| 47 | [Fact] |
| 48 | public async Task Reply_AutoPopulates_TargetedMessageInfoEntity_WhenIncomingIsTargeted() |
| 49 | { |
| 50 | IActivity? sentActivity = null; |
| 51 | |
| 52 | _app.OnMessage(async (context, cancellationToken) => |
| 53 | { |
| 54 | sentActivity = await context.Reply("Here is the result!", cancellationToken); |
| 55 | }); |
| 56 | |
| 57 | var incomingActivity = new MessageActivity("summarize") |
| 58 | .WithId("1772129782775") |
| 59 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 60 | .WithConversation(new Api.Conversation() { Id = "conv1" }) |
| 61 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); |
| 62 | |
| 63 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 64 | |
| 65 | Assert.NotNull(sentActivity); |
| 66 | Assert.NotNull(sentActivity!.Entities); |
| 67 | |
| 68 | // Reply calls Send, which auto-populates the entity |
| 69 | var targetedEntity = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); |
| 70 | Assert.NotNull(targetedEntity); |
| 71 | Assert.Equal("1772129782775", targetedEntity!.MessageId); |
| 72 | |
| 73 | // quotedReply entities should be stripped by AddTargetedMessageInfo |
| 74 | Assert.DoesNotContain(sentActivity.Entities!, e => e.Type == "quotedReply"); |
| 75 | } |
| 76 | |
| 77 | [Fact] |
| 78 | public async Task Send_DoesNotAdd_TargetedMessageInfoEntity_WhenNotTargeted() |
| 79 | { |
| 80 | IActivity? sentActivity = null; |
| 81 | |
| 82 | _app.OnMessage(async (context, cancellationToken) => |
| 83 | { |
| 84 | sentActivity = await context.Send("Hello!", cancellationToken); |
| 85 | }); |
| 86 | |
| 87 | // Normal (non-targeted) incoming message |
| 88 | var incomingActivity = new MessageActivity("hello") |
| 89 | .WithId("123456") |
| 90 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); |
| 91 | |
| 92 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 93 | |
| 94 | Assert.NotNull(sentActivity); |
| 95 | var targetedEntity = sentActivity!.Entities?.OfType<TargetedMessageInfoEntity>().SingleOrDefault(); |
| 96 | Assert.Null(targetedEntity); |
| 97 | } |
| 98 | |
| 99 | [Fact] |
| 100 | public async Task Send_DoesNotDuplicate_TargetedMessageInfoEntity_WhenAlreadyPresent() |
| 101 | { |
| 102 | IActivity? sentActivity = null; |
| 103 | |
| 104 | _app.OnMessage(async (context, cancellationToken) => |
| 105 | { |
| 106 | // Developer manually adds the entity (proactive-like scenario) |
| 107 | var activity = new MessageActivity("Result") |
| 108 | .AddEntity(new TargetedMessageInfoEntity { MessageId = "9999" }); |
| 109 | |
| 110 | sentActivity = await context.Send(activity, cancellationToken); |
| 111 | }); |
| 112 | |
| 113 | // Incoming activity is targeted |
| 114 | var incomingActivity = new MessageActivity("summarize") |
| 115 | .WithId("1772129782775") |
| 116 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 117 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }, true); |
| 118 | |
| 119 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 120 | |
| 121 | Assert.NotNull(sentActivity); |
| 122 | Assert.NotNull(sentActivity!.Entities); |
| 123 | |
| 124 | var targetedEntities = sentActivity.Entities!.OfType<TargetedMessageInfoEntity>().ToList(); |
| 125 | Assert.Single(targetedEntities); |
| 126 | // The developer-provided entity should be preserved, not overwritten |
| 127 | Assert.Equal("9999", targetedEntities[0].MessageId); |
| 128 | } |
| 129 | |
| 130 | [Fact] |
| 131 | public async Task Send_Throws_WhenTargetedMessage_InPersonalChat() |
| 132 | { |
| 133 | _app.OnMessage(async (context, cancellationToken) => |
| 134 | { |
| 135 | await context.Send( |
| 136 | new MessageActivity("Secret!") |
| 137 | .WithRecipient(new Account() { Id = "user1", Name = "User" }, true), |
| 138 | cancellationToken); |
| 139 | }); |
| 140 | |
| 141 | var incomingActivity = new MessageActivity("hello") |
| 142 | .WithId("123456") |
| 143 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 144 | .WithConversation(new Api.Conversation() { Id = "conv1", Type = ConversationType.Personal }) |
| 145 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); |
| 146 | |
| 147 | var ex = await Assert.ThrowsAsync<InvalidOperationException>( |
| 148 | () => _app.Process<TestPlugin>(_token, incomingActivity)); |
| 149 | Assert.Contains("personal", ex.Message, StringComparison.OrdinalIgnoreCase); |
| 150 | } |
| 151 | |
| 152 | [Fact] |
| 153 | public async Task Send_Succeeds_WhenNonTargetedMessage_InPersonalChat() |
| 154 | { |
| 155 | IActivity? sentActivity = null; |
| 156 | |
| 157 | _app.OnMessage(async (context, cancellationToken) => |
| 158 | { |
| 159 | sentActivity = await context.Send("Hello!", cancellationToken); |
| 160 | }); |
| 161 | |
| 162 | var incomingActivity = new MessageActivity("hello") |
| 163 | .WithId("123456") |
| 164 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 165 | .WithConversation(new Api.Conversation() { Id = "conv1", Type = ConversationType.Personal }) |
| 166 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); |
| 167 | |
| 168 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 169 | |
| 170 | Assert.NotNull(sentActivity); |
| 171 | } |
| 172 | |
| 173 | [Fact] |
| 174 | public async Task Send_Succeeds_WhenTargetedMessage_InGroupChat() |
| 175 | { |
| 176 | IActivity? sentActivity = null; |
| 177 | |
| 178 | _app.OnMessage(async (context, cancellationToken) => |
| 179 | { |
| 180 | sentActivity = await context.Send( |
| 181 | new MessageActivity("Only you can see this!") |
| 182 | .WithRecipient(new Account() { Id = "user1", Name = "User" }, true), |
| 183 | cancellationToken); |
| 184 | }); |
| 185 | |
| 186 | var incomingActivity = new MessageActivity("hello") |
| 187 | .WithId("123456") |
| 188 | .WithFrom(new Account() { Id = "user1", Name = "User" }) |
| 189 | .WithConversation(new Api.Conversation() { Id = "conv1", Type = ConversationType.GroupChat }) |
| 190 | .WithRecipient(new Account() { Id = "bot1", Name = "Bot" }); |
| 191 | |
| 192 | await _app.Process<TestPlugin>(_token, incomingActivity); |
| 193 | |
| 194 | Assert.NotNull(sentActivity); |
| 195 | } |
| 196 | } |
| 197 | |