|
| 1 | +#if STREAM_TESTS_ENABLED |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using NUnit.Framework; |
| 8 | +using StreamChat.Core; |
| 9 | +using StreamChat.Core.LowLevelClient.Models; |
| 10 | +using StreamChat.Core.LowLevelClient.Requests; |
| 11 | +using StreamChat.Core.Requests; |
| 12 | +using StreamChat.Core.StatefulModels; |
| 13 | +using UnityEngine.TestTools; |
| 14 | + |
| 15 | +namespace StreamChat.Tests.StatefulClient |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Tests operations for Polls API |
| 19 | + /// </summary> |
| 20 | + internal class PollsTests : BaseStateIntegrationTests |
| 21 | + { |
| 22 | + [UnityTest] |
| 23 | + public IEnumerator When_creating_poll_with_options_expect_poll_created() |
| 24 | + => ConnectAndExecute(When_creating_poll_with_options_expect_poll_created_Async); |
| 25 | + |
| 26 | + private async Task When_creating_poll_with_options_expect_poll_created_Async() |
| 27 | + { |
| 28 | + var pollId = "poll-" + Guid.NewGuid(); |
| 29 | + |
| 30 | + var createPollRequest = new CreatePollRequest |
| 31 | + { |
| 32 | + Id = pollId, |
| 33 | + Name = "What is your favorite programming language?", |
| 34 | + Description = "Let us know which language you prefer", |
| 35 | + EnforceUniqueVote = true, |
| 36 | + AllowAnswers = false, |
| 37 | + AllowUserSuggestedOptions = false, |
| 38 | + MaxVotesAllowed = 1, |
| 39 | + VotingVisibility = VotingVisibility.Public, |
| 40 | + Options = new List<PollOptionInput> |
| 41 | + { |
| 42 | + new PollOptionInput { Text = "C#" }, |
| 43 | + new PollOptionInput { Text = "JavaScript" }, |
| 44 | + new PollOptionInput { Text = "Python" }, |
| 45 | + new PollOptionInput { Text = "Go" } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + var response = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 50 | + |
| 51 | + Assert.NotNull(response); |
| 52 | + Assert.NotNull(response.Poll); |
| 53 | + Assert.AreEqual(pollId, response.Poll.Id); |
| 54 | + Assert.AreEqual("What is your favorite programming language?", response.Poll.Name); |
| 55 | + Assert.AreEqual("Let us know which language you prefer", response.Poll.Description); |
| 56 | + Assert.AreEqual(true, response.Poll.EnforceUniqueVote); |
| 57 | + Assert.AreEqual(false, response.Poll.AllowAnswers); |
| 58 | + Assert.AreEqual(false, response.Poll.AllowUserSuggestedOptions); |
| 59 | + Assert.AreEqual(1, response.Poll.MaxVotesAllowed); |
| 60 | + Assert.AreEqual(VotingVisibility.Public, response.Poll.VotingVisibility); |
| 61 | + |
| 62 | + Assert.NotNull(response.Poll.Options); |
| 63 | + Assert.AreEqual(4, response.Poll.Options.Count); |
| 64 | + |
| 65 | + var optionTexts = response.Poll.Options.Select(o => o.Text).ToList(); |
| 66 | + Assert.Contains("C#", optionTexts); |
| 67 | + Assert.Contains("JavaScript", optionTexts); |
| 68 | + Assert.Contains("Python", optionTexts); |
| 69 | + Assert.Contains("Go", optionTexts); |
| 70 | + } |
| 71 | + |
| 72 | + [UnityTest] |
| 73 | + public IEnumerator When_fetching_poll_expect_poll_returned() |
| 74 | + => ConnectAndExecute(When_fetching_poll_expect_poll_returned_Async); |
| 75 | + |
| 76 | + private async Task When_fetching_poll_expect_poll_returned_Async() |
| 77 | + { |
| 78 | + var pollId = "poll-" + Guid.NewGuid(); |
| 79 | + var pollName = "Best IDE for Unity development?"; |
| 80 | + |
| 81 | + // First, create a poll |
| 82 | + var createPollRequest = new CreatePollRequest |
| 83 | + { |
| 84 | + Id = pollId, |
| 85 | + Name = pollName, |
| 86 | + Description = "Help us understand your preferences", |
| 87 | + Options = new List<PollOptionInput> |
| 88 | + { |
| 89 | + new PollOptionInput { Text = "Visual Studio" }, |
| 90 | + new PollOptionInput { Text = "Rider" }, |
| 91 | + new PollOptionInput { Text = "VS Code" } |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + var createResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 96 | + Assert.NotNull(createResponse); |
| 97 | + Assert.NotNull(createResponse.Poll); |
| 98 | + |
| 99 | + // Now fetch the poll |
| 100 | + var fetchResponse = await Client.InternalLowLevelClient.PollsApi.GetPollAsync(pollId); |
| 101 | + |
| 102 | + Assert.NotNull(fetchResponse); |
| 103 | + Assert.NotNull(fetchResponse.Poll); |
| 104 | + Assert.AreEqual(pollId, fetchResponse.Poll.Id); |
| 105 | + Assert.AreEqual(pollName, fetchResponse.Poll.Name); |
| 106 | + Assert.AreEqual(3, fetchResponse.Poll.Options.Count); |
| 107 | + |
| 108 | + var optionTexts = fetchResponse.Poll.Options.Select(o => o.Text).ToList(); |
| 109 | + Assert.Contains("Visual Studio", optionTexts); |
| 110 | + Assert.Contains("Rider", optionTexts); |
| 111 | + Assert.Contains("VS Code", optionTexts); |
| 112 | + } |
| 113 | + |
| 114 | + [UnityTest] |
| 115 | + public IEnumerator When_sending_message_with_poll_expect_poll_in_message() |
| 116 | + => ConnectAndExecute(When_sending_message_with_poll_expect_poll_in_message_Async); |
| 117 | + |
| 118 | + private async Task When_sending_message_with_poll_expect_poll_in_message_Async() |
| 119 | + { |
| 120 | + var channel = await CreateUniqueTempChannelAsync(); |
| 121 | + var pollId = "poll-" + Guid.NewGuid(); |
| 122 | + |
| 123 | + // Create a poll |
| 124 | + var createPollRequest = new CreatePollRequest |
| 125 | + { |
| 126 | + Id = pollId, |
| 127 | + Name = "Which feature should we prioritize?", |
| 128 | + Description = "Vote for the most important feature", |
| 129 | + Options = new List<PollOptionInput> |
| 130 | + { |
| 131 | + new PollOptionInput { Text = "Performance improvements" }, |
| 132 | + new PollOptionInput { Text = "New UI components" }, |
| 133 | + new PollOptionInput { Text = "Better documentation" }, |
| 134 | + new PollOptionInput { Text = "More examples" } |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + var pollResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 139 | + Assert.NotNull(pollResponse); |
| 140 | + Assert.NotNull(pollResponse.Poll); |
| 141 | + |
| 142 | + // Send a message with the poll |
| 143 | + var messageRequest = new StreamSendMessageRequest |
| 144 | + { |
| 145 | + Text = "Please vote on our poll!", |
| 146 | + PollId = pollId |
| 147 | + }; |
| 148 | + |
| 149 | + var message = await channel.SendNewMessageAsync(messageRequest); |
| 150 | + |
| 151 | + Assert.NotNull(message); |
| 152 | + Assert.AreEqual("Please vote on our poll!", message.Text); |
| 153 | + Assert.AreEqual(pollId, message.PollId); |
| 154 | + |
| 155 | + // Verify the poll can be fetched using the poll ID from the message |
| 156 | + var fetchedPoll = await Client.InternalLowLevelClient.PollsApi.GetPollAsync(message.PollId); |
| 157 | + Assert.NotNull(fetchedPoll); |
| 158 | + Assert.NotNull(fetchedPoll.Poll); |
| 159 | + Assert.AreEqual(pollId, fetchedPoll.Poll.Id); |
| 160 | + Assert.AreEqual("Which feature should we prioritize?", fetchedPoll.Poll.Name); |
| 161 | + } |
| 162 | + |
| 163 | + [UnityTest] |
| 164 | + public IEnumerator When_creating_poll_with_custom_data_expect_custom_data_preserved() |
| 165 | + => ConnectAndExecute(When_creating_poll_with_custom_data_expect_custom_data_preserved_Async); |
| 166 | + |
| 167 | + private async Task When_creating_poll_with_custom_data_expect_custom_data_preserved_Async() |
| 168 | + { |
| 169 | + var pollId = "poll-" + Guid.NewGuid(); |
| 170 | + |
| 171 | + var createPollRequest = new CreatePollRequest |
| 172 | + { |
| 173 | + Id = pollId, |
| 174 | + Name = "Test Poll", |
| 175 | + Options = new List<PollOptionInput> |
| 176 | + { |
| 177 | + new PollOptionInput { Text = "Option 1" }, |
| 178 | + new PollOptionInput { Text = "Option 2" } |
| 179 | + }, |
| 180 | + Custom = new Dictionary<string, object> |
| 181 | + { |
| 182 | + { "category", "testing" }, |
| 183 | + { "priority", 5 }, |
| 184 | + { "tags", new List<string> { "unit-test", "polls" } } |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + var response = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 189 | + |
| 190 | + Assert.NotNull(response); |
| 191 | + Assert.NotNull(response.Poll); |
| 192 | + Assert.NotNull(response.Poll.Custom); |
| 193 | + Assert.IsTrue(response.Poll.Custom.ContainsKey("category")); |
| 194 | + Assert.AreEqual("testing", response.Poll.Custom["category"]); |
| 195 | + Assert.IsTrue(response.Poll.Custom.ContainsKey("priority")); |
| 196 | + } |
| 197 | + |
| 198 | + [UnityTest] |
| 199 | + public IEnumerator When_updating_poll_expect_poll_updated() |
| 200 | + => ConnectAndExecute(When_updating_poll_expect_poll_updated_Async); |
| 201 | + |
| 202 | + private async Task When_updating_poll_expect_poll_updated_Async() |
| 203 | + { |
| 204 | + var pollId = "poll-" + Guid.NewGuid(); |
| 205 | + |
| 206 | + // Create a poll |
| 207 | + var createPollRequest = new CreatePollRequest |
| 208 | + { |
| 209 | + Id = pollId, |
| 210 | + Name = "Original Name", |
| 211 | + Description = "Original Description", |
| 212 | + Options = new List<PollOptionInput> |
| 213 | + { |
| 214 | + new PollOptionInput { Text = "Option 1" } |
| 215 | + } |
| 216 | + }; |
| 217 | + |
| 218 | + var createResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 219 | + Assert.NotNull(createResponse); |
| 220 | + |
| 221 | + // Update the poll |
| 222 | + var updatePollRequest = new UpdatePollRequest |
| 223 | + { |
| 224 | + Name = "Updated Name", |
| 225 | + Description = "Updated Description" |
| 226 | + }; |
| 227 | + |
| 228 | + var updateResponse = await Client.InternalLowLevelClient.PollsApi.UpdatePollAsync(pollId, updatePollRequest); |
| 229 | + |
| 230 | + Assert.NotNull(updateResponse); |
| 231 | + Assert.NotNull(updateResponse.Poll); |
| 232 | + Assert.AreEqual(pollId, updateResponse.Poll.Id); |
| 233 | + Assert.AreEqual("Updated Name", updateResponse.Poll.Name); |
| 234 | + Assert.AreEqual("Updated Description", updateResponse.Poll.Description); |
| 235 | + } |
| 236 | + |
| 237 | + [UnityTest] |
| 238 | + public IEnumerator When_closing_poll_expect_poll_closed() |
| 239 | + => ConnectAndExecute(When_closing_poll_expect_poll_closed_Async); |
| 240 | + |
| 241 | + private async Task When_closing_poll_expect_poll_closed_Async() |
| 242 | + { |
| 243 | + var pollId = "poll-" + Guid.NewGuid(); |
| 244 | + |
| 245 | + // Create a poll |
| 246 | + var createPollRequest = new CreatePollRequest |
| 247 | + { |
| 248 | + Id = pollId, |
| 249 | + Name = "Test Poll", |
| 250 | + IsClosed = false, |
| 251 | + Options = new List<PollOptionInput> |
| 252 | + { |
| 253 | + new PollOptionInput { Text = "Option 1" } |
| 254 | + } |
| 255 | + }; |
| 256 | + |
| 257 | + var createResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 258 | + Assert.NotNull(createResponse); |
| 259 | + Assert.AreEqual(false, createResponse.Poll.IsClosed); |
| 260 | + |
| 261 | + // Close the poll using partial update |
| 262 | + var updatePartialRequest = new UpdatePollPartialRequest |
| 263 | + { |
| 264 | + Set = new Dictionary<string, object> |
| 265 | + { |
| 266 | + { "is_closed", true } |
| 267 | + } |
| 268 | + }; |
| 269 | + |
| 270 | + var updateResponse = await Client.InternalLowLevelClient.PollsApi.UpdatePollPartialAsync(pollId, updatePartialRequest); |
| 271 | + |
| 272 | + Assert.NotNull(updateResponse); |
| 273 | + Assert.NotNull(updateResponse.Poll); |
| 274 | + Assert.AreEqual(true, updateResponse.Poll.IsClosed); |
| 275 | + } |
| 276 | + |
| 277 | + [UnityTest] |
| 278 | + public IEnumerator When_adding_poll_option_expect_option_added() |
| 279 | + => ConnectAndExecute(When_adding_poll_option_expect_option_added_Async); |
| 280 | + |
| 281 | + private async Task When_adding_poll_option_expect_option_added_Async() |
| 282 | + { |
| 283 | + var pollId = "poll-" + Guid.NewGuid(); |
| 284 | + |
| 285 | + // Create a poll with initial options |
| 286 | + var createPollRequest = new CreatePollRequest |
| 287 | + { |
| 288 | + Id = pollId, |
| 289 | + Name = "Test Poll", |
| 290 | + AllowUserSuggestedOptions = true, |
| 291 | + Options = new List<PollOptionInput> |
| 292 | + { |
| 293 | + new PollOptionInput { Text = "Option 1" }, |
| 294 | + new PollOptionInput { Text = "Option 2" } |
| 295 | + } |
| 296 | + }; |
| 297 | + |
| 298 | + var createResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollAsync(createPollRequest); |
| 299 | + Assert.NotNull(createResponse); |
| 300 | + Assert.AreEqual(2, createResponse.Poll.Options.Count); |
| 301 | + |
| 302 | + // Add a new option |
| 303 | + var createOptionRequest = new CreatePollOptionRequest |
| 304 | + { |
| 305 | + Text = "Option 3" |
| 306 | + }; |
| 307 | + |
| 308 | + var optionResponse = await Client.InternalLowLevelClient.PollsApi.CreatePollOptionAsync(pollId, createOptionRequest); |
| 309 | + |
| 310 | + Assert.NotNull(optionResponse); |
| 311 | + Assert.NotNull(optionResponse.PollOption); |
| 312 | + Assert.AreEqual("Option 3", optionResponse.PollOption.Text); |
| 313 | + |
| 314 | + // Verify the poll now has 3 options |
| 315 | + var fetchResponse = await Client.InternalLowLevelClient.PollsApi.GetPollAsync(pollId); |
| 316 | + Assert.AreEqual(3, fetchResponse.Poll.Options.Count); |
| 317 | + } |
| 318 | + } |
| 319 | +} |
| 320 | +#endif |
| 321 | + |
0 commit comments