microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/TaskModules/TaskSerializationTests.cs
182lines · modecode
| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | namespace Microsoft.Teams.Api.Tests; |
| 5 | |
| 6 | public class TaskSerializationTests |
| 7 | { |
| 8 | private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions |
| 9 | { |
| 10 | PropertyNameCaseInsensitive = true, |
| 11 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 12 | }; |
| 13 | |
| 14 | |
| 15 | [Fact] |
| 16 | public void Should_Serialize_Response_With_ContinueTask() |
| 17 | { |
| 18 | // arrange |
| 19 | var taskInfo = new Microsoft.Teams.Api.TaskModules.TaskInfo |
| 20 | { |
| 21 | Title = "Test Dialog" |
| 22 | }; |
| 23 | var continueTask = new Microsoft.Teams.Api.TaskModules.ContinueTask(taskInfo); |
| 24 | var response = new Microsoft.Teams.Api.TaskModules.Response(continueTask); |
| 25 | |
| 26 | // act |
| 27 | var json = JsonSerializer.Serialize(response, _jsonOptions); |
| 28 | |
| 29 | // assert |
| 30 | using var doc = JsonDocument.Parse(json); |
| 31 | var root = doc.RootElement; |
| 32 | |
| 33 | Assert.True(root.TryGetProperty("task", out var taskElement)); |
| 34 | Assert.Equal("continue", taskElement.GetProperty("type").GetString()); |
| 35 | Assert.True(taskElement.TryGetProperty("value", out var valueElement)); |
| 36 | Assert.Equal("Test Dialog", valueElement.GetProperty("title").GetString()); |
| 37 | } |
| 38 | |
| 39 | [Fact] |
| 40 | public void Should_Serialize_Response_With_MessageTask() |
| 41 | { |
| 42 | // arrange |
| 43 | var messageTask = new Microsoft.Teams.Api.TaskModules.MessageTask("Operation completed"); |
| 44 | var response = new Microsoft.Teams.Api.TaskModules.Response(messageTask); |
| 45 | |
| 46 | // act |
| 47 | var json = JsonSerializer.Serialize(response, _jsonOptions); |
| 48 | |
| 49 | // assert |
| 50 | using var doc = JsonDocument.Parse(json); |
| 51 | var root = doc.RootElement; |
| 52 | |
| 53 | Assert.True(root.TryGetProperty("task", out var taskElement)); |
| 54 | Assert.Equal("message", taskElement.GetProperty("type").GetString()); |
| 55 | Assert.Equal("Operation completed", taskElement.GetProperty("value").GetString()); |
| 56 | } |
| 57 | |
| 58 | [Fact] |
| 59 | public void Should_Deserialize_Response_With_ContinueTask() |
| 60 | { |
| 61 | // arrange |
| 62 | string json = """ |
| 63 | { |
| 64 | "task": { |
| 65 | "type": "continue", |
| 66 | "value": { |
| 67 | "title": "Test Dialog", |
| 68 | "url": "https://example.com" |
| 69 | } |
| 70 | }, |
| 71 | "cacheInfo": null |
| 72 | } |
| 73 | """; |
| 74 | |
| 75 | // act |
| 76 | var response = JsonSerializer.Deserialize<Microsoft.Teams.Api.TaskModules.Response>(json, _jsonOptions); |
| 77 | |
| 78 | // assert |
| 79 | Assert.NotNull(response); |
| 80 | Assert.NotNull(response.Task); |
| 81 | Assert.IsType<Microsoft.Teams.Api.TaskModules.ContinueTask>(response.Task); |
| 82 | |
| 83 | var continueTask = (Microsoft.Teams.Api.TaskModules.ContinueTask)response.Task; |
| 84 | Assert.NotNull(continueTask.Value); |
| 85 | Assert.Equal("Test Dialog", continueTask.Value.Title); |
| 86 | Assert.Equal("https://example.com", continueTask.Value.Url); |
| 87 | } |
| 88 | |
| 89 | [Fact] |
| 90 | public void Should_Handle_TaskInfo_With_AdaptiveCard() |
| 91 | { |
| 92 | // arrange |
| 93 | var card = new Microsoft.Teams.Cards.AdaptiveCard |
| 94 | { |
| 95 | Body = new List<Microsoft.Teams.Cards.CardElement> |
| 96 | { |
| 97 | new Microsoft.Teams.Cards.TextBlock("Test card") |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | var taskInfo = new Microsoft.Teams.Api.TaskModules.TaskInfo |
| 102 | { |
| 103 | Title = "Card Dialog", |
| 104 | Card = new Microsoft.Teams.Api.Attachment |
| 105 | { |
| 106 | ContentType = new Microsoft.Teams.Api.ContentType("application/vnd.microsoft.card.adaptive"), |
| 107 | Content = card |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | var continueTask = new Microsoft.Teams.Api.TaskModules.ContinueTask(taskInfo); |
| 112 | |
| 113 | // act |
| 114 | var json = JsonSerializer.Serialize(continueTask, _jsonOptions); |
| 115 | |
| 116 | // assert |
| 117 | using var doc = JsonDocument.Parse(json); |
| 118 | var root = doc.RootElement; |
| 119 | |
| 120 | Assert.Equal("continue", root.GetProperty("type").GetString()); |
| 121 | Assert.True(root.TryGetProperty("value", out var valueElement)); |
| 122 | Assert.Equal("Card Dialog", valueElement.GetProperty("title").GetString()); |
| 123 | Assert.True(valueElement.TryGetProperty("card", out var cardElement)); |
| 124 | Assert.Equal("application/vnd.microsoft.card.adaptive", cardElement.GetProperty("contentType").GetString()); |
| 125 | } |
| 126 | |
| 127 | [Fact] |
| 128 | public void Should_Throw_JsonException_For_Unknown_Task_Type() |
| 129 | { |
| 130 | // arrange |
| 131 | string json = """ |
| 132 | { |
| 133 | "type": "unknown_type", |
| 134 | "value": "some value" |
| 135 | } |
| 136 | """; |
| 137 | |
| 138 | // act & assert |
| 139 | Assert.Throws<JsonException>(() => |
| 140 | JsonSerializer.Deserialize<Microsoft.Teams.Api.TaskModules.Task>(json, _jsonOptions)); |
| 141 | } |
| 142 | |
| 143 | [Fact] |
| 144 | public void Should_Throw_JsonException_When_Type_Property_Missing() |
| 145 | { |
| 146 | // arrange |
| 147 | string json = """ |
| 148 | { |
| 149 | "value": "some value" |
| 150 | } |
| 151 | """; |
| 152 | |
| 153 | // act & assert |
| 154 | Assert.Throws<JsonException>(() => |
| 155 | JsonSerializer.Deserialize<Microsoft.Teams.Api.TaskModules.Task>(json, _jsonOptions)); |
| 156 | } |
| 157 | |
| 158 | [Fact] |
| 159 | public void TaskFetchAction_Should_Merge_Properties() |
| 160 | { |
| 161 | // arrange - TaskFetchAction should merge properties into root data (like TypeScript version) |
| 162 | var action = new Microsoft.Teams.Cards.TaskFetchAction(Microsoft.Teams.Cards.TaskFetchAction.FromObject(new { opendialogtype = "simple_form", customProperty = "value" })); |
| 163 | |
| 164 | // act |
| 165 | var json = JsonSerializer.Serialize(action, _jsonOptions); |
| 166 | |
| 167 | // assert |
| 168 | using var doc = JsonDocument.Parse(json); |
| 169 | var root = doc.RootElement; |
| 170 | |
| 171 | Assert.True(root.TryGetProperty("data", out var dataElement)); |
| 172 | Assert.True(dataElement.TryGetProperty("msteams", out var msTeamsElement)); |
| 173 | Assert.Equal("task/fetch", msTeamsElement.GetProperty("type").GetString()); |
| 174 | |
| 175 | // TaskFetchAction is special - it merges custom properties into the root SubmitActionData |
| 176 | // This matches the TypeScript implementation behavior |
| 177 | Assert.True(dataElement.TryGetProperty("opendialogtype", out var dialogTypeElement)); |
| 178 | Assert.Equal("simple_form", dialogTypeElement.GetString()); |
| 179 | Assert.True(dataElement.TryGetProperty("customProperty", out var customElement)); |
| 180 | Assert.Equal("value", customElement.GetString()); |
| 181 | } |
| 182 | } |