microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Apps.Tests/Contexts/ContextQuotedReplyTests.cs
232lines · 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.Contexts; |
| 9 | |
| 10 | public class ContextQuotedReplyTests |
| 11 | { |
| 12 | private readonly IToken _token = Globals.Token; |
| 13 | |
| 14 | private static MessageActivity CreateInbound(string text, string? id = "msg-123") |
| 15 | { |
| 16 | return new MessageActivity(text) |
| 17 | { |
| 18 | Id = id, |
| 19 | From = new Account { Id = "user1" }, |
| 20 | Recipient = new Account { Id = "bot1" }, |
| 21 | Conversation = new Api.Conversation { Id = "conv1" } |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | [Fact] |
| 26 | public async Task Reply_Should_Stamp_QuotedReplyEntity_With_ActivityId() |
| 27 | { |
| 28 | var app = new App(); |
| 29 | app.AddPlugin(new TestPlugin()); |
| 30 | |
| 31 | MessageActivity? sent = null; |
| 32 | |
| 33 | app.OnMessage(async context => |
| 34 | { |
| 35 | sent = await context.Reply(new MessageActivity("reply text")); |
| 36 | }); |
| 37 | |
| 38 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-123")); |
| 39 | |
| 40 | Assert.NotNull(sent); |
| 41 | var quotedEntity = Assert.Single(sent!.Entities!.OfType<QuotedReplyEntity>()); |
| 42 | Assert.Equal("msg-123", quotedEntity.QuotedReply.MessageId); |
| 43 | } |
| 44 | |
| 45 | [Fact] |
| 46 | public async Task Reply_Should_Prepend_Placeholder_To_Text() |
| 47 | { |
| 48 | var app = new App(); |
| 49 | app.AddPlugin(new TestPlugin()); |
| 50 | |
| 51 | MessageActivity? sent = null; |
| 52 | |
| 53 | app.OnMessage(async context => |
| 54 | { |
| 55 | sent = await context.Reply(new MessageActivity("reply text")); |
| 56 | }); |
| 57 | |
| 58 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-123")); |
| 59 | |
| 60 | Assert.NotNull(sent); |
| 61 | Assert.StartsWith("<quoted messageId=\"msg-123\"/>", sent!.Text); |
| 62 | Assert.Contains("reply text", sent.Text); |
| 63 | } |
| 64 | |
| 65 | [Fact] |
| 66 | public async Task Reply_Should_Handle_Empty_Text() |
| 67 | { |
| 68 | var app = new App(); |
| 69 | app.AddPlugin(new TestPlugin()); |
| 70 | |
| 71 | MessageActivity? sent = null; |
| 72 | |
| 73 | app.OnMessage(async context => |
| 74 | { |
| 75 | sent = await context.Reply(new MessageActivity()); |
| 76 | }); |
| 77 | |
| 78 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-456")); |
| 79 | |
| 80 | Assert.NotNull(sent); |
| 81 | Assert.Equal("<quoted messageId=\"msg-456\"/>", sent!.Text); |
| 82 | } |
| 83 | |
| 84 | [Fact] |
| 85 | public async Task Reply_Should_Not_Stamp_Entity_When_ActivityId_Is_Null() |
| 86 | { |
| 87 | var app = new App(); |
| 88 | app.AddPlugin(new TestPlugin()); |
| 89 | |
| 90 | MessageActivity? sent = null; |
| 91 | |
| 92 | app.OnMessage(async context => |
| 93 | { |
| 94 | sent = await context.Reply(new MessageActivity("reply text")); |
| 95 | }); |
| 96 | |
| 97 | await app.Process<TestPlugin>(_token, CreateInbound("hello", null)); |
| 98 | |
| 99 | Assert.NotNull(sent); |
| 100 | var quotedEntities = (sent!.Entities ?? new List<IEntity>()).OfType<QuotedReplyEntity>().ToList(); |
| 101 | Assert.Empty(quotedEntities); |
| 102 | } |
| 103 | |
| 104 | [Fact] |
| 105 | public async Task Reply_Should_Preserve_Existing_Entities() |
| 106 | { |
| 107 | var app = new App(); |
| 108 | app.AddPlugin(new TestPlugin()); |
| 109 | |
| 110 | MessageActivity? sent = null; |
| 111 | |
| 112 | app.OnMessage(async context => |
| 113 | { |
| 114 | var activity = new MessageActivity("reply text"); |
| 115 | activity.Entities = new List<IEntity> |
| 116 | { |
| 117 | new MentionEntity { Mentioned = new Account { Id = "user2", Name = "User Two" }, Text = "<at>User Two</at>" } |
| 118 | }; |
| 119 | sent = await context.Reply(activity); |
| 120 | }); |
| 121 | |
| 122 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-789")); |
| 123 | |
| 124 | Assert.NotNull(sent); |
| 125 | Assert.Equal(2, sent!.Entities!.Count); |
| 126 | Assert.Single(sent.Entities.OfType<MentionEntity>()); |
| 127 | Assert.Single(sent.Entities.OfType<QuotedReplyEntity>()); |
| 128 | } |
| 129 | |
| 130 | [Fact] |
| 131 | public async Task Quote_Should_Stamp_Entity_With_Provided_MessageId() |
| 132 | { |
| 133 | var app = new App(); |
| 134 | app.AddPlugin(new TestPlugin()); |
| 135 | |
| 136 | MessageActivity? sent = null; |
| 137 | |
| 138 | app.OnMessage(async context => |
| 139 | { |
| 140 | sent = await context.Quote("custom-msg-id", new MessageActivity("quote reply text")); |
| 141 | }); |
| 142 | |
| 143 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-000")); |
| 144 | |
| 145 | Assert.NotNull(sent); |
| 146 | var quotedEntity = Assert.Single(sent!.Entities!.OfType<QuotedReplyEntity>()); |
| 147 | Assert.Equal("custom-msg-id", quotedEntity.QuotedReply.MessageId); |
| 148 | } |
| 149 | |
| 150 | [Fact] |
| 151 | public async Task Quote_Should_Prepend_Placeholder_To_Text() |
| 152 | { |
| 153 | var app = new App(); |
| 154 | app.AddPlugin(new TestPlugin()); |
| 155 | |
| 156 | MessageActivity? sent = null; |
| 157 | |
| 158 | app.OnMessage(async context => |
| 159 | { |
| 160 | sent = await context.Quote("custom-msg-id", new MessageActivity("quote reply text")); |
| 161 | }); |
| 162 | |
| 163 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-000")); |
| 164 | |
| 165 | Assert.NotNull(sent); |
| 166 | Assert.StartsWith("<quoted messageId=\"custom-msg-id\"/>", sent!.Text); |
| 167 | Assert.Contains("quote reply text", sent.Text); |
| 168 | } |
| 169 | |
| 170 | [Fact] |
| 171 | public async Task Quote_Should_Handle_Empty_Text() |
| 172 | { |
| 173 | var app = new App(); |
| 174 | app.AddPlugin(new TestPlugin()); |
| 175 | |
| 176 | MessageActivity? sent = null; |
| 177 | |
| 178 | app.OnMessage(async context => |
| 179 | { |
| 180 | sent = await context.Quote("custom-msg-id", new MessageActivity()); |
| 181 | }); |
| 182 | |
| 183 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-000")); |
| 184 | |
| 185 | Assert.NotNull(sent); |
| 186 | Assert.Equal("<quoted messageId=\"custom-msg-id\"/>", sent!.Text); |
| 187 | } |
| 188 | |
| 189 | [Fact] |
| 190 | public async Task Quote_String_Overload_Should_Stamp_Entity_And_Prepend_Placeholder() |
| 191 | { |
| 192 | var app = new App(); |
| 193 | app.AddPlugin(new TestPlugin()); |
| 194 | |
| 195 | MessageActivity? sent = null; |
| 196 | |
| 197 | app.OnMessage(async context => |
| 198 | { |
| 199 | sent = await context.Quote("custom-msg-id", "quote reply text"); |
| 200 | }); |
| 201 | |
| 202 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-000")); |
| 203 | |
| 204 | Assert.NotNull(sent); |
| 205 | var quotedEntity = Assert.Single(sent!.Entities!.OfType<QuotedReplyEntity>()); |
| 206 | Assert.Equal("custom-msg-id", quotedEntity.QuotedReply.MessageId); |
| 207 | Assert.StartsWith("<quoted messageId=\"custom-msg-id\"/>", sent.Text); |
| 208 | Assert.Contains("quote reply text", sent.Text); |
| 209 | } |
| 210 | |
| 211 | [Fact] |
| 212 | public async Task Reply_String_Overload_Should_Stamp_Entity_And_Prepend_Placeholder() |
| 213 | { |
| 214 | var app = new App(); |
| 215 | app.AddPlugin(new TestPlugin()); |
| 216 | |
| 217 | MessageActivity? sent = null; |
| 218 | |
| 219 | app.OnMessage(async context => |
| 220 | { |
| 221 | sent = await context.Reply("reply text"); |
| 222 | }); |
| 223 | |
| 224 | await app.Process<TestPlugin>(_token, CreateInbound("hello", "msg-123")); |
| 225 | |
| 226 | Assert.NotNull(sent); |
| 227 | var quotedEntity = Assert.Single(sent!.Entities!.OfType<QuotedReplyEntity>()); |
| 228 | Assert.Equal("msg-123", quotedEntity.QuotedReply.MessageId); |
| 229 | Assert.StartsWith("<quoted messageId=\"msg-123\"/>", sent.Text); |
| 230 | Assert.Contains("reply text", sent.Text); |
| 231 | } |
| 232 | } |