using Govor.Application.Interfaces.PushNotifications.Models; using Govor.Application.PushNotifications; using Govor.Application.PushNotifications.Providers; using Govor.Domain.Common; using Microsoft.Extensions.Logging.Abstractions; using Moq; using SmartRes; namespace Govor.Server.Tests; [TestFixture] public class PushDeliveryTests { private Mock _tokens = null!; private Mock _provider = null!; private PushNotificationService _service = null!; private Guid _user; [SetUp] public void SetUp() { _user = Guid.NewGuid(); _tokens = new Mock(MockBehavior.Strict); _provider = new Mock(MockBehavior.Strict); _service = new PushNotificationService(_provider.Object, _tokens.Object, NullLogger.Instance); } [Test] public async Task MulticastDeduplicatesAndContinuesAfterOneBatchFails() { var tokens = Enumerable.Range(0, 501).Select(i => $"token-{i}").ToList(); _tokens.Setup(t => t.GetStringsActiveTokensAsync(_user)) .ReturnsAsync(Result, Error>.Success(tokens.Concat(tokens).ToList())); _provider.Setup(p => p.SendMulticastAsync(It.Is>(t => t.Count == 500), It.IsAny())) .ThrowsAsync(new HttpRequestException("Provider unavailable")); _provider.Setup(p => p.SendMulticastAsync(It.Is>(t => t.Count == 1 && t[0] == "token-500"), It.IsAny())) .ReturnsAsync(new SendPushResult(1, 0, [])); var result = await _service.SendToUserAsync(_user, "title", "body", "chat_messages"); Assert.That(result.IsFailure, Is.True); _provider.VerifyAll(); _tokens.Verify(t => t.RemoveTokensAsync(It.IsAny>()), Times.Never); } [TestCase(1, true)] [TestCase(2, false)] public async Task OnlyUnregisteredTokensAreRemovedAndTemporaryFailuresRequireRetry(int failures, bool success) { _tokens.Setup(t => t.GetStringsActiveTokensAsync(_user)) .ReturnsAsync(Result, Error>.Success(["good", "unregistered", "temporary"])); _provider.Setup(p => p.SendMulticastAsync(It.IsAny>(), It.IsAny())) .ReturnsAsync(new SendPushResult(3 - failures, failures, ["unregistered"])); _tokens.Setup(t => t.RemoveTokensAsync(It.Is>(t => t.SequenceEqual(new[] { "unregistered" })))) .ReturnsAsync(Govor.Domain.Common.Result.Success()); var result = await _service.SendToUserAsync(_user, "title", "body", "chat_messages"); Assert.That(result.IsSuccess, Is.EqualTo(success)); _tokens.VerifyAll(); } [Test] public async Task FailedCleanupIsNotAcknowledgedForEitherSingleOrMulticastDelivery() { _tokens.Setup(t => t.GetStringsActiveTokensAsync(_user)) .ReturnsAsync(Result, Error>.Success(["unregistered"])); _tokens.Setup(t => t.GetActiveTokenBySessionAsync(_user)) .ReturnsAsync(Result.Success("unregistered")); _tokens.Setup(t => t.RemoveTokensAsync(It.IsAny>())) .ReturnsAsync(Govor.Domain.Common.Result.Failure(Error.ServerError("Test.Cleanup", "Unavailable"))); _provider.Setup(p => p.SendMulticastAsync(It.IsAny>(), It.IsAny())) .ReturnsAsync(new SendPushResult(0, 1, ["unregistered"])); _provider.Setup(p => p.SendToTokenAsync("unregistered", It.IsAny())) .ReturnsAsync(new SendPushResult(0, 1, ["unregistered"])); Assert.That((await _service.SendToUserAsync(_user, "title", "body", "chat_messages")).IsFailure, Is.True); Assert.That((await _service.SendToSessionAsync(_user, "title", "body", "chat_messages")).IsFailure, Is.True); } [Test] public async Task IncompleteProviderResponseDoesNotLoseTheNotification() { _tokens.Setup(t => t.GetStringsActiveTokensAsync(_user)) .ReturnsAsync(Result, Error>.Success(["token"])); _provider.Setup(p => p.SendMulticastAsync(It.IsAny>(), It.IsAny())) .ReturnsAsync(new SendPushResult(0, 0, [])); Assert.That((await _service.SendToUserAsync(_user, "title", "body", "chat_messages")).IsFailure, Is.True); } [Test] public async Task NoTokensIsSuccessfulWithoutContactingProvider() { _tokens.Setup(t => t.GetStringsActiveTokensAsync(_user)) .ReturnsAsync(Result, Error>.Success([])); Assert.That((await _service.SendToUserAsync(_user, "title", "body", "chat_messages")).IsSuccess, Is.True); _provider.VerifyNoOtherCalls(); } }