using NTP.Application.Abstractions; using NTP.Application.Access; using NTP.Application.Common; using NTP.Application.Grains.Communication; using NTP.Application.Grains.Permits; using NTP.Application.Grains.Reports; namespace NTP.Tests; public class CommunicationServiceTests { private sealed class FakeUnit : IUnitOfWork { public Task BeginAsync(CancellationToken ct = default) => Task.CompletedTask; public Task CommitAsync(CancellationToken ct = default) => Task.CompletedTask; public Task RollbackAsync(CancellationToken ct = default) => Task.CompletedTask; public Task ExecuteInTransactionAsync(Func> work, CancellationToken ct = default) => work(ct); } private sealed class FakeUser : ICurrentUser { public bool IsAuthenticated => true; public long Id => 1; public string UserId => "GTO1"; public string Name => "t"; public string RoleCode => "GTO"; public string RoleName => "GTO"; public string Domain => "ASEZA"; public long? LocationId => null; public string LocationName => ""; public long? StkId => null; public long? CompId => null; public long? CompanyId => null; public string CompanyName => ""; public string CompanyCode => ""; public string EmpNo => ""; public string Lang => "AR"; public bool IsStaff => false; public bool IsInRole(string roleCode) => true; public bool IsInAnyRole(params string[] roleCodes) => true; } private sealed class FakePermissions(params string[] keys) : IPermissionService { public Task GetForCurrentUserAsync(CancellationToken ct = default) => throw new NotSupportedException(); public Task IsAllowedAsync(string resourceKey, CancellationToken ct = default) => Task.FromResult(keys.Contains(resourceKey)); } private sealed class FakeSettings : ICommunicationSettingsService { public CommunicationOptions Value { get; } = new() { AutoPostponeMinutes = 30 }; } private sealed class FakeNotifications : IMobileNotificationGatewayService { public readonly List<(string Template, IReadOnlyDictionary Payload)> Sent = []; public Task SendAsync(string template, IReadOnlyDictionary payload, CancellationToken ct = default) { Sent.Add((template, payload)); return Task.CompletedTask; } } private sealed class FakeExcel : IReportExcelService { public byte[] Build(ReportTable table, string printedBy, DateTime printedAt, bool arabic) => [1]; public List ReadFirstSheet(Stream stream) => []; } private sealed class FakeRepo : ICommunicationRepository { public List<(long PermitId, string Tn)> Unanswered = []; public List Blocked = []; public Task> SearchAsync(CommunicationQuery query, PageRequest page, CancellationToken ct = default) => throw new NotSupportedException(); public Task> GetCargoTypesAsync(CancellationToken ct = default) => Task.FromResult>([]); public Task> GetUnansweredAsync(CancellationToken ct = default) => Task.FromResult>(Unanswered); public Task BlockAsync(IReadOnlyList permitIds, string userId, CancellationToken ct = default) { Blocked.AddRange(permitIds); return Task.CompletedTask; } } private static (CommunicationService svc, FakeRepo repo, FakeNotifications push) Make(params string[] keys) { var repo = new FakeRepo(); var push = new FakeNotifications(); return (new CommunicationService(repo, new FakeUnit(), push, new FakeExcel(), new FakePermissions(keys), new FakeSettings(), new FakeUser()), repo, push); } [Fact] public void Answers_are_read_from_status_and_blacklist() { var (svc, _, _) = Make(); var closed = svc.Item(new CommunicationRow { StatusCode = "CLOSE" }); Assert.Equal("CLOSE", closed.Answer); Assert.Equal("تم الإجابة - موافق", closed.Notes); Assert.Null(closed.RemainingMinutes); var postponed = svc.Item(new CommunicationRow { StatusCode = "PENDING", BlackListId = 5, ExpiryDate = "20/11/2026" }); Assert.Equal("DELETED", postponed.Answer); Assert.Equal("تم الإجابة - تأجيل ، سيتم فك التأجيل بتاريخ 20/11/2026", postponed.Notes); var waiting = svc.Item(new CommunicationRow { StatusCode = "PENDING", LogDuration = 12 }); Assert.Equal("PENDING", waiting.Answer); Assert.Equal("لم يتم الرد", waiting.Notes); Assert.Equal(18, waiting.RemainingMinutes); Assert.Equal(0, svc.Item(new CommunicationRow { StatusCode = "PENDING", LogDuration = 99 }).RemainingMinutes); } [Fact] public async Task Blacklist_blocks_the_permits_once_and_tells_every_truck_owner() { var (svc, repo, push) = Make(PermissionKeys.CommunicationBlacklist); repo.Unanswered = [(1, "T1"), (1, "T1"), (2, "T2")]; var r = await svc.AddToBlacklistAsync(default); Assert.Equal(3, r.Count); Assert.Equal("تم ايقاف خاصية التثبيت والتأجيل عن ( 3 ) ارسالية", r.Message); Assert.Equal([1L, 2L], repo.Blocked); var (template, payload) = Assert.Single(push.Sent); Assert.Equal(NotificationRules.BlockedDistributedOwners, template); Assert.Equal(3, NotificationRules.Entries(payload).Count); } [Fact] public async Task Nothing_to_block_is_a_quiet_zero_and_the_key_is_required() { var (svc, repo, push) = Make(PermissionKeys.CommunicationBlacklist); Assert.Equal(0, (await svc.AddToBlacklistAsync(default)).Count); Assert.Empty(repo.Blocked); Assert.Empty(push.Sent); await Assert.ThrowsAsync(() => Make().svc.AddToBlacklistAsync(default)); await Assert.ThrowsAsync(() => Make(PermissionKeys.Communication).svc.ExportAsync(new CommunicationQuery(null, null, null, null, null, null, null, null), default)); } }