using Microsoft.Extensions.Logging.Abstractions; using NTP.Application.Abstractions; using NTP.Application.Grains.Permits; using NTP.Infrastructure.Grains; namespace NTP.Tests; public class NotificationRulesTests { [Fact] public void Fill_replaces_each_placeholder_once_in_order() { Assert.Equal("شاحنة 1234 - قمح - عقبة", NotificationRules.Fill("شاحنة VAR1 - VAR2 - VAR3", "1234", "قمح", "عقبة")); // ColdFusion Replace() without scope changes only the first occurrence Assert.Equal("1234 VAR1", NotificationRules.Fill("VAR1 VAR1", "1234")); } [Fact] public void Sms_number_follows_the_legacy_prefix_rule() { Assert.Equal("+962791234567", NotificationRules.SmsNumber("0791234567")); Assert.Equal("+962791234567", NotificationRules.SmsNumber("791234567")); } [Fact] public void Sms_text_adds_the_app_link() { Assert.Equal("نص، للتفاصيل الرجاء الضغط على الرابطhttp://x", NotificationRules.SmsText("نص", "http://x")); } [Fact] public void Entries_reads_dictionaries_of_mixed_types() { var payload = new Dictionary { ["NOTIFICATIONARY"] = new List> { new Dictionary { ["TN"] = "12345", ["SHIFT"] = 2L, ["ORDERDATE"] = new DateTime(2026, 1, 5) } } }; var e = Assert.Single(NotificationRules.Entries(payload)); Assert.Equal("12345", NotificationRules.Get(e, "tn")); Assert.Equal("2", NotificationRules.Get(e, "SHIFT")); Assert.Equal("05/01/2026", NotificationRules.Get(e, "ORDERDATE")); } } public class NotificationDispatcherTests { private sealed class FakeRepo : INotificationRepository { public Dictionary<(string Role, string? Mobile, string? Login, string? Part), long[]> Users = new(); public Dictionary Devices = new(); public string Phone = "0791234567"; public long? Scac = 45; public string? DispatcherLogin = "045DSP1"; public List TemplatesAsked = []; public Task GetTemplateAsync(string type, CancellationToken ct = default) { TemplatesAsked.Add(type); return Task.FromResult(new NotificationTemplateRow { Id = 7, Title = "عنوان", Message = type == NotificationRules.SmsWithoutApp ? "الشاحنة VAR1" : "VAR1 / VAR2 / VAR3 / VAR4 / VAR5" }); } public Task> FindUserIdsAsync(string roleCode, string? mobile, string? login, string? loginContains, CancellationToken ct = default) => Task.FromResult>(Users.GetValueOrDefault((roleCode, mobile, login, loginContains), [])); public Task> GetActiveDevicesAsync(long userId, CancellationToken ct = default) => Task.FromResult>(Devices.GetValueOrDefault(userId, [])); public Task GetTruckContactAsync(string tn, CancellationToken ct = default) => Task.FromResult(new TruckContactRow { Phone = Phone, Scac = Scac }); public Task GetCompanyDispatcherLoginAsync(long scac, CancellationToken ct = default) => Task.FromResult(DispatcherLogin); public Task InsertSmsLogAsync(string userLogin, string mobile, string status, string request, string? response, string provider, string serviceType, CancellationToken ct = default) => Task.CompletedTask; } private sealed class FakePush : IPushGatewayService { public List Sent = []; public Task SendAsync(PushMessage message, CancellationToken ct = default) { Sent.Add(message); return Task.FromResult(true); } } private sealed class FakeSms : ISmsGatewayService { public List<(string Mobile, string Text, string User)> Sent = []; public Task SendAsync(string mobile, string text, string userId, string serviceType, CancellationToken ct = default) { Sent.Add((mobile, text, userId)); return Task.CompletedTask; } } private sealed class Settings(bool sms) : IGrainsPermitsSettingsService { public GrainsPermitsOptions Value { get; } = new() { Notifications = new NotificationOptions { Enabled = true, PushUrl = "x", SendSmsToOwnersWithoutApp = sms, AppLink = "http://link" } }; } private sealed class NoErrors : ISystemErrorLogger { public Task LogAsync(Exception exception, string? userId, string? ipAddress, string? browser, string? requestInfo) => Task.CompletedTask; } private static (NotificationDispatcher d, FakeRepo repo, FakePush push, FakeSms sms) Make(bool smsFlag) { var repo = new FakeRepo(); var push = new FakePush(); var sms = new FakeSms(); return (new NotificationDispatcher(repo, push, sms, new Settings(smsFlag), new NoErrors(), NullLogger.Instance), repo, push, sms); } private static Dictionary OwnerPayload() => new() { ["NOTIFICATIONARY"] = new[] { new Dictionary { ["TN"] = "5555", ["TT"] = "قمح", ["LOCATION"] = "صومعة", ["ORDERDATE"] = "05/01/2026", ["SHIFT"] = "1" } } }; [Fact] public async Task Owner_distribution_pushes_to_the_devices_of_driver_and_owner_accounts() { var (d, repo, push, sms) = Make(smsFlag: true); repo.Users[("DRVR", "0791234567", null, null)] = [11]; repo.Users[("TRKOWNER", "0791234567", null, null)] = [12]; repo.Devices[11] = [new DeviceRow { FcmToken = "tok-a", Os = "ANDROID" }, new DeviceRow { FcmToken = "tok-b", Os = "IOS" }]; repo.Devices[12] = [new DeviceRow { FcmToken = "tok-c", Os = "ANDROID" }]; await d.RunAsync(NotificationRules.DistributeOwners, OwnerPayload(), CancellationToken.None); Assert.Equal(["tok-a", "tok-b", "tok-c"], push.Sent.Select(m => m.Token)); Assert.All(push.Sent, m => Assert.Equal("5555 / قمح / صومعة / 05/01/2026 / 1", m.Message)); Assert.Empty(sms.Sent); // everybody has the app } [Fact] public async Task Account_without_a_device_gets_the_sms_only_when_the_flag_is_on() { var on = Make(smsFlag: true); on.repo.Users[("DRVR", "0791234567", null, null)] = [11]; await on.d.RunAsync(NotificationRules.DistributeOwners, OwnerPayload(), CancellationToken.None); var sent = Assert.Single(on.sms.Sent); Assert.Equal("+962791234567", sent.Mobile); Assert.Equal("11", sent.User); Assert.Equal("الشاحنة 5555، للتفاصيل الرجاء الضغط على الرابطhttp://link", sent.Text); Assert.Empty(on.push.Sent); var off = Make(smsFlag: false); off.repo.Users[("DRVR", "0791234567", null, null)] = [11]; await off.d.RunAsync(NotificationRules.DistributeOwners, OwnerPayload(), CancellationToken.None); Assert.Empty(off.sms.Sent); } [Fact] public async Task No_account_for_the_phone_means_no_push_and_no_sms() { var (d, _, push, sms) = Make(smsFlag: true); await d.RunAsync(NotificationRules.DistributeOwners, OwnerPayload(), CancellationToken.None); Assert.Empty(push.Sent); Assert.Empty(sms.Sent); } [Fact] public async Task Company_distribution_goes_to_the_dispatchers_whose_login_contains_the_company_code() { var (d, repo, push, _) = Make(smsFlag: false); repo.Users[("DISP", null, null, "045DSP")] = [21]; repo.Devices[21] = [new DeviceRow { FcmToken = "tok-d", Os = "ANDROID" }]; var payload = new Dictionary { ["045"] = 3, ["TT"] = "قمح" }; await d.RunAsync(NotificationRules.DistributeCompanies, payload, CancellationToken.None); var m = Assert.Single(push.Sent); Assert.Equal("3 / قمح / VAR3 / VAR4 / VAR5", m.Message); Assert.Equal(21, m.UserId); } [Fact] public async Task Manual_distribution_notifies_the_dispatcher_of_the_truck_company() { var (d, repo, push, sms) = Make(smsFlag: true); repo.Users[("DISP", null, "045DSP1", null)] = [31]; repo.Devices[31] = [new DeviceRow { FcmToken = "tok-e", Os = "IOS" }]; await d.RunAsync(NotificationRules.DistributeOwnersCompanies, OwnerPayload(), CancellationToken.None); Assert.Equal("tok-e", Assert.Single(push.Sent).Token); Assert.Empty(sms.Sent); // dispatchers never get the SMS } [Fact] public async Task Phone_confirmation_uses_the_mobile_of_the_payload() { var (d, repo, push, _) = Make(smsFlag: true); repo.Users[("TRKOWNER", "0799999999", null, null)] = [41]; repo.Devices[41] = [new DeviceRow { FcmToken = "tok-f", Os = "ANDROID" }]; var payload = new Dictionary { ["MOBILE"] = "0799999999", ["TN"] = "7", ["PN"] = "P1", ["TT"] = "قمح", ["ORDERDATE"] = "06/01/2026" }; await d.RunAsync(NotificationRules.ConfirmPn, payload, CancellationToken.None); Assert.Equal("7 / P1 / قمح / 06/01/2026 / VAR5", Assert.Single(push.Sent).Message); } }