208 lines
9.0 KiB
C#
208 lines
9.0 KiB
C#
using NTP.Application.Grains.Registry;
|
|
|
|
namespace NTP.Tests;
|
|
|
|
public class RegistryRulesTests
|
|
{
|
|
private const long Badiya = 327;
|
|
|
|
[Theory]
|
|
[InlineData("OPEN", true, true)]
|
|
[InlineData("REGREQST", true, true)]
|
|
[InlineData("INCOMP", true, true)]
|
|
[InlineData("ACCPT", false, true)]
|
|
[InlineData("RVKD", false, false)]
|
|
[InlineData("accpt", false, true)]
|
|
public void Status_transitions_follow_legacy(string status, bool canAccept, bool canRevoke)
|
|
{
|
|
Assert.Equal(canAccept, RegistryRules.CanAccept(status));
|
|
Assert.Equal(canRevoke, RegistryRules.CanRevoke(status));
|
|
Assert.Equal(canRevoke, RegistryRules.CanEdit(status));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(327, "REGREQST", "REGREQST")] // Badiya keeps the awaiting-registration state
|
|
[InlineData(327, "OPEN", null)] // ... any other state is not touched
|
|
[InlineData(327, "ACCPT", null)]
|
|
[InlineData(100, "REGREQST", "OPEN")] // other companies go back to OPEN after an edit
|
|
[InlineData(100, "ACCPT", "OPEN")]
|
|
public void Truck_edit_status(long scac, string current, string? expected)
|
|
=> Assert.Equal(expected, RegistryRules.StatusAfterTruckEdit(scac, current, Badiya));
|
|
|
|
[Theory]
|
|
[InlineData("OPEN", "جديد")]
|
|
[InlineData("ACCPT", "مرخص")]
|
|
[InlineData("RVKD", "ملغي")]
|
|
[InlineData("REGREQST", "بانتظار التسجيل")]
|
|
[InlineData("INCOMP", "غير مكتمل")]
|
|
[InlineData("OTHER", "OTHER")]
|
|
public void Status_captions(string code, string caption) => Assert.Equal(caption, RegistryRules.StatusCaption(code));
|
|
|
|
[Theory]
|
|
[InlineData("abc", false)]
|
|
[InlineData("اسم المالك 12", false)]
|
|
[InlineData("a.b", true)]
|
|
[InlineData("a_b", true)]
|
|
[InlineData("a/b", true)]
|
|
[InlineData("a@b", true)]
|
|
[InlineData("a b", false)]
|
|
[InlineData(null, false)]
|
|
public void Special_characters(string? text, bool special) => Assert.Equal(special, RegistryRules.HasSpecialChars(text));
|
|
|
|
[Theory]
|
|
[InlineData("1960", 2026, true)]
|
|
[InlineData("2027", 2026, true)] // next year is allowed
|
|
[InlineData("2028", 2026, false)]
|
|
[InlineData("1959", 2026, false)]
|
|
[InlineData("", 2026, false)]
|
|
[InlineData("20a0", 2026, false)]
|
|
[InlineData(null, 2026, false)]
|
|
public void Manufacture_year_range(string? year, int now, bool ok) => Assert.Equal(ok, RegistryRules.IsYearWithinRange(year, now));
|
|
|
|
[Theory]
|
|
[InlineData("123456789012", 12, true)]
|
|
[InlineData("1234567890123", 12, false)]
|
|
[InlineData("12a4", 12, false)]
|
|
[InlineData("", 12, false)]
|
|
public void Digits(string s, int max, bool ok) => Assert.Equal(ok, RegistryRules.IsDigits(s, max));
|
|
|
|
[Theory]
|
|
[InlineData(2, "S")]
|
|
[InlineData(6, "S")]
|
|
[InlineData(7, "T")]
|
|
[InlineData(null, null)]
|
|
public void Truck_kind_of_axials(int? axials, string? kind) => Assert.Equal(kind, RegistryRules.TruckKindOfAxials(axials));
|
|
|
|
[Theory]
|
|
[InlineData(11, "TR")]
|
|
[InlineData(12, "HTR")]
|
|
[InlineData(null, null)]
|
|
public void Trailer_kind_of_axials(int? axials, string? kind) => Assert.Equal(kind, RegistryRules.TrailerKindOfAxials(axials));
|
|
|
|
[Theory]
|
|
[InlineData(0, 0, false)]
|
|
[InlineData(1, 0, false)]
|
|
[InlineData(2, 0, true)]
|
|
[InlineData(1, 1, true)]
|
|
public void Attachments_need_two_files(int newFiles, int stored, bool ok) => Assert.Equal(ok, RegistryRules.HasEnoughAttachments(newFiles, stored));
|
|
|
|
[Fact]
|
|
public void Blacklist_expiry_uses_shift_end_of_the_day_after_the_shift_start()
|
|
{
|
|
// 14:00 on 10 Jan, shift 6..14: (14:00 - 30h) = 08:00 on 9 Jan -> date 9 Jan, + 3 days = 12 Jan, + shift end hour
|
|
var expiry = RegistryRules.BlacklistExpiry(new DateTime(2025, 1, 10, 14, 0, 0), 3, 6, 14);
|
|
Assert.Equal(new DateTime(2025, 1, 12, 14, 0, 0), expiry);
|
|
}
|
|
|
|
[Fact]
|
|
public void Blacklist_remarks_carry_the_automatic_release_date()
|
|
{
|
|
var text = RegistryRules.BlacklistRemarks("ملاحظة", new DateTime(2025, 1, 12, 14, 0, 0));
|
|
Assert.Equal("ملاحظة، سيتم فك الانتظار بشكل تلقائي بتاريخ 01/12/2025 14:00", text);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("6,14", 6, 14)]
|
|
[InlineData(" 8 , 16 ", 8, 16)]
|
|
[InlineData("", 0, 24)]
|
|
[InlineData("x,y", 0, 24)]
|
|
[InlineData(null, 0, 24)]
|
|
public void Shift_hours_setting(string? setting, int start, int end) => Assert.Equal((start, end), RegistryRules.ParseShiftHours(setting));
|
|
|
|
[Theory]
|
|
[InlineData("PHONEDECLINE", true)]
|
|
[InlineData("WAITINGLIST", true)]
|
|
[InlineData("X", false)]
|
|
[InlineData(null, false)]
|
|
public void Blacklist_categories(string? c, bool ok) => Assert.Equal(ok, RegistryRules.IsBlacklistCategory(c));
|
|
|
|
[Theory]
|
|
[InlineData(1, true)]
|
|
[InlineData(14, true)]
|
|
[InlineData(30, true)]
|
|
[InlineData(6, false)]
|
|
public void Blacklist_days(int days, bool ok) => Assert.Equal(ok, RegistryRules.BlacklistDays.Contains(days));
|
|
|
|
[Fact]
|
|
public void Ownership_skipped_when_no_codes_configured_and_missing_truck_reported()
|
|
{
|
|
Assert.Equal(RegistryMessages.TruckNotFound, RegistryRules.ValidateOwnership(null, 100, Badiya, ["A"], ["B"]));
|
|
Assert.Equal(string.Empty, RegistryRules.ValidateOwnership("X", 100, Badiya, [], []));
|
|
Assert.Equal(string.Empty, RegistryRules.ValidateOwnership("A", Badiya, Badiya, ["A"], ["B"]));
|
|
Assert.Equal(RegistryMessages.OwnershipNotAllowed, RegistryRules.ValidateOwnership("A", 100, Badiya, ["A"], ["B"]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Mot_truck_validation()
|
|
{
|
|
Assert.Equal(RegistryRules.MotNotRegistered, RegistryRules.MotTruckValidation([]));
|
|
Assert.Equal(string.Empty, RegistryRules.MotTruckValidation(["trucktype.general.cargo"]));
|
|
Assert.Equal(string.Empty, RegistryRules.MotTruckValidation(["trucktype.all"]));
|
|
Assert.Equal(RegistryRules.MotWrongType, RegistryRules.MotTruckValidation(["trucktype.fuel"]));
|
|
Assert.Equal(RegistryRules.MotNotRegistered, RegistryRules.MotTrailerValidation(0));
|
|
Assert.Equal(string.Empty, RegistryRules.MotTrailerValidation(2));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(5, 4, true)]
|
|
[InlineData(5, 5, true)]
|
|
[InlineData(5, 6, true)]
|
|
public void Fleet_decision_is_total(int fleet, int turns, bool _) =>
|
|
Assert.True(Enum.IsDefined(RegistryRules.FleetDecision(fleet, turns, false)));
|
|
|
|
[Fact]
|
|
public void Truck_validation_requires_full_set_for_badiya_only()
|
|
{
|
|
var empty = new SaveTruckRequest { Tn = "123456", Tcscac = 100 };
|
|
Assert.Empty(RegistryValidation.ValidateTruck(empty, 100, false, Badiya, 2026));
|
|
var failures = RegistryValidation.ValidateTruck(empty, Badiya, false, Badiya, 2026);
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTruckRequest.ChasesNumber));
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTruckRequest.OwnerNnpn));
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTruckRequest.HeadType));
|
|
}
|
|
|
|
[Fact]
|
|
public void Truck_edit_for_other_companies_needs_axials_and_valid_year()
|
|
{
|
|
var failures = RegistryValidation.ValidateTruck(new SaveTruckRequest(), 100, true, Badiya, 2026);
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTruckRequest.Axials));
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTruckRequest.ManifactureYear));
|
|
Assert.Empty(RegistryValidation.ValidateTruck(new SaveTruckRequest { Axials = 3, ManifactureYear = "2020" }, 100, true, Badiya, 2026));
|
|
}
|
|
|
|
[Fact]
|
|
public void Driver_validation()
|
|
{
|
|
Assert.NotEmpty(RegistryValidation.ValidateDriver(new SaveDriverRequest()));
|
|
var ok = new SaveDriverRequest
|
|
{
|
|
Nnpn = "9801012345", Name = "Test Driver", Dln = "123456789", Dled = new DateTime(2030, 1, 1), Nat = "JO", Cat = "3",
|
|
Mobile1 = "0791234567"
|
|
};
|
|
Assert.Empty(RegistryValidation.ValidateDriver(ok));
|
|
ok.Cat = "7";
|
|
Assert.Contains(RegistryValidation.ValidateDriver(ok), f => f.PropertyName == nameof(SaveDriverRequest.Cat));
|
|
ok.Cat = "3";
|
|
ok.Name = "a.b";
|
|
Assert.Contains(RegistryValidation.ValidateDriver(ok), f => f.ErrorMessage == RegistryMessages.SpecialChars);
|
|
}
|
|
|
|
[Fact]
|
|
public void Blacklist_validation()
|
|
{
|
|
var ok = new AddBlacklistRequest { EntityNum = "123456", Category = "WAITINGLIST", ExpiryDays = 5 };
|
|
Assert.Empty(RegistryValidation.ValidateBlacklist(ok));
|
|
ok.ExpiryDays = 6;
|
|
Assert.NotEmpty(RegistryValidation.ValidateBlacklist(ok));
|
|
Assert.NotEmpty(RegistryValidation.ValidateCancelBlacklist(new CancelBlacklistRequest()));
|
|
}
|
|
|
|
[Fact]
|
|
public void Trailer_edit_requires_full_set_for_every_company()
|
|
{
|
|
var failures = RegistryValidation.ValidateTrailer(new SaveTrailerRequest(), 100, true, Badiya);
|
|
Assert.Contains(failures, f => f.PropertyName == nameof(SaveTrailerRequest.OwnerName));
|
|
Assert.Empty(RegistryValidation.ValidateTrailer(new SaveTrailerRequest { Tn = "1234" }, 100, false, Badiya));
|
|
}
|
|
}
|