using NTP.Application.Grains.Permits; using NTP.Domain.Grains.Permits; namespace NTP.Tests; public class PermitNumberTests { [Theory] [InlineData("BULK", 15L, 327L, 7L, "30" + "07" + "000015")] [InlineData("BULK", 15L, 12L, 7L, "1" + "07" + "000015")] [InlineData("PCKD", 15L, 327L, 7L, "3" + "48" + "0000015")] [InlineData("PCKD", 15L, 12L, 7L, "1" + "48" + "000015")] [InlineData("OTHER", 15L, 12L, 7L, "")] public void Permit_number_layout_follows_generatePermitNumber(string cargo, long seq, long company, long tender, string expected) => Assert.Equal(expected, PermitNumberGenerator.Generate(cargo, seq, company, 327, tender)); } public class WeighbridgeDecisionTests { [Theory] [InlineData("ACCPT", 215L, 100L, 215L, "EN", true, "LTW", "ACCPT")] [InlineData("RESD", 215L, 100L, 215L, "EX", true, "LGW", "ACTRL")] [InlineData("ACTRL", 215L, 100L, 100L, "EN", true, "DGW", "CLRPR")] [InlineData("CLRPR", 215L, 100L, 100L, "EX", true, "DTW", "CLOSE")] [InlineData("CLOSE", 215L, 100L, 100L, "EX", false, null, null)] [InlineData("OPEN", 215L, 100L, 215L, "EN", false, null, null)] public void Decide_maps_location_and_direction_to_an_event(string status, long origin, long dest, long loc, string trx, bool allowed, string? eventType, string? current) { var d = WeighbridgeRules.Decide(status, origin, dest, loc, trx); Assert.Equal(allowed, d.Allowed); Assert.Equal(eventType, d.EventType); Assert.Equal(current, d.CurrentStatus); } [Fact] public void Location_off_the_route_is_error_2() => Assert.Equal("2", WeighbridgeRules.Decide("ACCPT", 215, 100, 999, "EN").ErrorCode); [Theory] [InlineData(12000L, 40000L, true)] [InlineData(9999L, 40000L, false)] [InlineData(12000L, 76000L, false)] [InlineData(20000L, 36000L, true)] public void Manual_weights_must_be_in_range(long first, long second, bool ok) => Assert.Equal(ok, WeighbridgeRules.ValidateManualWeights(first, second) is null); [Fact] public void Gross_must_exceed_tare() => Assert.NotNull(WeighbridgeRules.ValidateGrossTare(10000, 10000)); [Theory] [InlineData("12345", "54321", true)] [InlineData("1234", "54321", false)] public void Stored_weights_need_five_digits(string empty, string gross, bool ok) => Assert.Equal(ok, WeighbridgeRules.ValidateStoredWeights(empty, gross) is null); } public class PrintWeightTests { [Fact] public void Breakdown_splits_ton_and_kilo_and_signs_the_difference() { var b = WeightBreakdown.Compute(loadingTare: 14000, loadingGross: 40500, dischargeTare: 14000, dischargeGross: 40200); Assert.Equal(26500, b.LoadingNet); Assert.Equal(26200, b.DischargeNet); Assert.Equal((26, 500), (b.LoadingTon, b.LoadingKilo)); Assert.Equal((26, 200), (b.DischargeTon, b.DischargeKilo)); Assert.Equal("(-)", b.Sign); Assert.Equal(-300, b.KiloDifference); } } public class PhoneRejectRuleTests { [Fact] public void Blacklist_expiry_is_date_part_plus_days_plus_shift_end() { // shift 1 = [6,18]: (now - 30h).date + 2 days + 18h var expiry = PhoneRejectRules.BlacklistExpiry(new DateTime(2026, 5, 10, 9, 0, 0), 6, 18, 2); Assert.Equal(new DateTime(2026, 5, 11, 18, 0, 0), expiry); } [Theory] [InlineData("عدم الحضور بعد التسريب", true)] [InlineData("لايرد", false)] public void Absence_reasons_are_recognised(string reason, bool absence) => Assert.Equal(absence, PhoneRejectRules.IsAbsence(reason)); [Theory] [InlineData("one", 1)] [InlineData("fiv", 5)] [InlineData("xyz", 0)] public void Dialer_postponement_suffix(string key, int days) => Assert.Equal(days, PhoneRejectRules.PostponeDaysFromDialerKey(key)); } public class DestinationRuleTests { [Fact] public void Packed_trucks_lose_the_bulk_locations_and_others_keep_only_them() { long[] all = [100, 215, 220, 350, 410]; Assert.Equal(new long[] { 100, 410 }, DestinationRules.FilterCompanyLocations(all, "201401693756")); Assert.Equal(new long[] { 215, 220, 350 }, DestinationRules.FilterCompanyLocations(all, "999")); } [Theory] [InlineData(38L, "38,39")] [InlineData(39L, "38,39")] [InlineData(41L, "40,41")] public void Cargo_groups_are_interchangeable(long cargo, string expected) => Assert.Equal(expected, string.Join(',', DestinationRules.CargoGroup(cargo)!)); [Fact] public void Operation_window_is_inclusive() => Assert.True(OperationWindow.IsWithin(new DateTime(2026, 1, 2, 6, 0, 0), new DateTime(2026, 1, 2), 6, 18)); } public class DistributionCategoryTests { [Theory] [InlineData("PCKD", "PCKD", "", "PCKD")] [InlineData("BULK_AHALE", "BULK", "AHALE", "BULK")] [InlineData("BULK_GALLAB_COMP", "BULK_GALLAB", "COMP", "BULK")] public void Category_maps_to_queue_code_type_and_cargo(string request, string code, string type, string cargo) { var c = DistributionCategory.Parse(request)!; Assert.Equal((code, type, cargo), (c.DistributionCode, c.QueueType, c.PermitCargoType)); } [Fact] public void Unknown_category_is_rejected() => Assert.Null(DistributionCategory.Parse("NOPE")); [Fact] public void Queue_filter_uses_the_AHALI_spelling() => Assert.Equal(("BULK", "AHALI"), DistributionCategory.Parse("BULK_AHALE")!.QueueFilter); } public class PermitActionRuleTests { [Fact] public void Requested_permit_can_be_submitted_by_a_company_but_phone_buttons_are_staff_only() { var company = PermitActionRules.For(PermitStatus.Requested, 12, isCompany: true, isOperator: true, isConvertRole: false, isReactivator: false, 327); Assert.Contains(PermitActionRules.Submit, company); Assert.DoesNotContain(PermitActionRules.PhoneAccept, company); var staff = PermitActionRules.For(PermitStatus.Requested, 12, isCompany: false, isOperator: true, isConvertRole: false, isReactivator: false, 327); Assert.Contains(PermitActionRules.PhoneAccept, staff); } [Fact] public void A_permit_on_the_turn_can_be_edited_like_a_confirmed_one() { var planned = PermitActionRules.For(PermitStatus.Planned, 12, isCompany: false, isOperator: true, isConvertRole: false, isReactivator: false, 327); var open = PermitActionRules.For(PermitStatus.Open, 12, isCompany: false, isOperator: true, isConvertRole: false, isReactivator: false, 327); Assert.Contains(PermitActionRules.Edit, planned); Assert.Contains(PermitActionRules.Edit, open); // على الدور Assert.DoesNotContain(PermitActionRules.Accept, open); // confirming the turn is still only for PLND Assert.Contains(PermitActionRules.RevokePermit, open); // staff keep the revoke button var company = PermitActionRules.For(PermitStatus.Open, 12, isCompany: true, isOperator: true, isConvertRole: false, isReactivator: false, 327); Assert.Contains(PermitActionRules.Edit, company); // the owning company can correct its own permit Assert.DoesNotContain(PermitActionRules.RevokePermit, company); } [Fact] public void Accepted_permit_offers_convert_to_resd_only_to_su_gpo() { var a = PermitActionRules.For(PermitStatus.Accepted, 327, false, true, true, false, 327); var b = PermitActionRules.For(PermitStatus.Accepted, 327, false, true, false, false, 327); Assert.Contains(PermitActionRules.ConvertToResd, a); Assert.DoesNotContain(PermitActionRules.ConvertToResd, b); } [Fact] public void Revoked_owner_permit_can_be_reactivated_by_su_gto_only() { Assert.Contains(PermitActionRules.Reactivate, PermitActionRules.For(PermitStatus.Revoked, 327, false, true, false, true, 327)); Assert.DoesNotContain(PermitActionRules.Reactivate, PermitActionRules.For(PermitStatus.Revoked, 12, false, true, false, true, 327)); } } public class PermitSupportTests { [Theory] [InlineData("ASE000094", 94L)] [InlineData("ASE", null)] [InlineData(null, null)] public void Company_code_number_is_mid_4_6(string? code, long? expected) => Assert.Equal(expected, PermitUserContext.ParseCompanyCodeNo(code)); [Theory] [InlineData("2026-03-05", 2026, 3, 5)] [InlineData("05/03/2026", 2026, 3, 5)] [InlineData("05\\03\\2026", 2026, 3, 5)] public void Legacy_dates_parse(string text, int y, int m, int d) => Assert.Equal(new DateTime(y, m, d), PermitDates.ParseLegacy(text)); [Fact] public void Bad_date_is_null() => Assert.Null(PermitDates.ParseLegacy("nonsense")); }