NTP_V2/tests/NTP.Tests/Refinery/TripCalculatorTests.cs

231 lines
10 KiB
C#

using NTP.Application.Refinery.Trips;
namespace NTP.Tests.Refinery;
public class TripCalculatorTests
{
// REFINERY_VARIABLES group 30 (August 2023), values as stored in the database
private static readonly TripVariables August2023 = TripVariables.FromPairs(
[
new("COMMISSION", "0.95"), new("DIESEL_LITER_PRICE", "0.715"), new("DRIVER_TIP", "20"), new("FIRST_ADVANCE_PAYMENT", "100"),
new("PERMIT_DISPATCH", "2.5"), new("TOLERANCE_PERCENTAGE", "0.005"), new("TON_PRICE", "700"), new("TON_WAGE", "17.936"),
new("WAYBILL_FEE", "0.75")
]);
[Fact]
public void Penalised_trip_matches_the_stored_legacy_figures()
{
// REFINERY_TRIPS id 77314 (COMPLETE): loaded 34560 kg, discharged 34290 kg, no water
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 0, null), August2023, null);
Assert.Equal(172m, a.ToleranceQuantity);
Assert.Equal(98m, a.PenaltyQuantity);
Assert.Equal(68.6m, a.PenaltyAmount);
Assert.Equal(34.29m, a.CalculatedQuantityTon);
Assert.Equal(615.025m, a.QuantityWage);
Assert.Equal(271.975m, a.TripNetWage);
Assert.Equal(271.975m, a.NetWage);
Assert.Equal(0m, a.RemainingBalance);
}
[Fact]
public void Loss_inside_the_tolerance_is_not_penalised()
{
// id 77683: 39820 / 39770, tolerance floor(199.1) = 199 >= 50
var a = TripCalculator.Calculate(new TripWeights(39820, 39770, 0, null), August2023, null);
Assert.Equal(0m, a.PenaltyQuantity);
Assert.Equal(0m, a.PenaltyAmount);
Assert.Equal(713.315m, a.QuantityWage);
Assert.Equal(438.865m, a.TripNetWage);
}
[Fact]
public void Remaining_balance_of_the_truck_is_added_to_the_net_wage_only()
{
var a = TripCalculator.Calculate(new TripWeights(39820, 39770, 0, null), August2023, -12.5m);
Assert.Equal(438.865m, a.TripNetWage);
Assert.Equal(426.365m, a.NetWage);
Assert.Equal(-12.5m, a.RemainingBalance);
}
[Fact]
public void Accompanying_water_is_paid_as_if_delivered()
{
// water 300 kg >= loss 270 kg: the whole loaded weight is paid and nothing is fined
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 300, null), August2023, null);
Assert.Equal(34.56m, a.CalculatedQuantityTon);
Assert.Equal(0m, a.PenaltyAmount);
}
[Fact]
public void Water_flag_1_ignores_the_water_and_the_tolerance()
{
// legacy factory 816-819: WATER_FLAG = 1 and water > 0 -> water 0 and tolerance 0, so the full loss is fined
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 300, 1), August2023, null);
Assert.Equal(270m, a.PenaltyQuantity);
Assert.Equal(189m, a.PenaltyAmount);
Assert.Equal(34.29m, a.CalculatedQuantityTon);
}
[Fact]
public void Ton_price_correction_changes_only_the_penalty_and_skips_the_balance()
{
var withCorrection = August2023 with { TonPriceCorrection = 800m };
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 0, null), withCorrection, 99m, useTonPriceCorrection: true);
Assert.Equal(78.4m, a.PenaltyAmount); // 98 kg * 800 / 1000
Assert.Equal(a.TripNetWage, a.NetWage);
Assert.Equal(0m, a.RemainingBalance);
Assert.Equal(271.975m - 9.8m, a.TripNetWage);
}
[Fact]
public void Second_advance_is_the_diesel_value_minus_the_first()
{
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 0, null), August2023, null);
Assert.Equal(350m * 0.715m - 100m, a.SecondAdvancePayment);
}
[Fact]
public void Missing_variables_give_a_zero_wage_that_the_payment_rules_refuse()
{
var empty = TripVariables.FromPairs([]);
var a = TripCalculator.Calculate(new TripWeights(34560, 34290, 0, null), empty, null);
Assert.Equal(0m, a.QuantityWage);
// zero net wage = "price not declared"
Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsurePayable(0, 500));
}
[Fact]
public void Variables_take_the_first_value_of_a_key_and_ignore_unparsable_values()
{
var v = TripVariables.FromPairs([new("TON_WAGE", "17.936"), new("TON_WAGE", "99"), new("DRIVER_TIP", "abc")]);
Assert.Equal(17.936m, v.TonWage);
Assert.Equal(0m, v.DriverTip);
}
}
public class TripPaymentRulesTests
{
[Fact]
public void Zero_negative_and_too_big_amounts_are_refused_with_the_legacy_texts()
{
Assert.Equal("السعر غير معرف لتاريخ التحميل", Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsurePayable(0, 500)).Message);
Assert.Equal("الدفع مسموح للمبالغ اكبر من صفر", Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsurePayable(-1, 500)).Message);
Assert.Equal("الدفع مسموح فقط للمبالغ اقل من 500 دينار", Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsurePayable(500.001m, 500)).Message);
TripPaymentRules.EnsurePayable(500m, 500);
}
[Fact]
public void Orange_payment_only_checks_for_zero()
{
TripPaymentRules.EnsurePayable(-5m, 500, checkSign: false);
TripPaymentRules.EnsurePayable(5000m, 500, checkSign: false);
Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsurePayable(0, 500, checkSign: false));
}
[Theory]
[InlineData(false, 1, "795891836", "1", "x", "CANNOT_COMPLETE_ACTION_NO_PAYMENT_METHOD")]
[InlineData(true, 0, "795891836", "1", "x", "THERE_IS_EWALLET_PLEASE_RELOAD_FORM")]
[InlineData(true, 1, "", "1", "x", "WALLET_NUMBER_IS_EMPTY")]
[InlineData(true, 1, "795891836", "", "x", "OWNER_NNPN_IS_EMPTY")]
[InlineData(true, 1, "795891836", "1", "", "REFINERY_OWNER_NNPN_WALLET_IS_EMPTY")]
public void Wallet_checks_follow_the_legacy_order(bool exists, int epayment, string wallet, string nnpn, string name, string code)
{
var ex = Assert.Throws<NTP.Application.Common.BusinessException>(() => TripPaymentRules.EnsureWalletReady(exists, epayment, wallet, nnpn, name));
Assert.Equal(code, ex.Code);
}
[Fact]
public void Ready_wallet_passes() => TripPaymentRules.EnsureWalletReady(true, 1, "795891836", "9871012345", "اسم");
}
public class TripLogBuilderTests
{
private static (bool, object?) Old(string key) => key switch
{
"STATUS_CODE" => (true, "DISCHARGED"),
"REMARKS" => (true, ""),
"CURRENT_BALANCE" => (true, 100m),
_ => (false, null)
};
[Fact]
public void Status_and_remarks_are_stored_as_arabic_captions()
{
var log = TripLogBuilder.Build("EXCHANGE", Old, new Dictionary<string, object?>
{
["STATUS_CODE"] = "EXCHANGED", ["REMARKS"] = "SUCCESSFULL_EXCHANGE_OPERATION"
});
Assert.NotNull(log);
Assert.Contains("\"STATUS_CODE\":\"تم التفريغ\"", log!.Value.OldJson);
Assert.Contains("\"STATUS_CODE\":\"تم الصرف\"", log.Value.NewJson);
Assert.Contains("\"REMARKS\":\"تمت عملية الصرف بنجاح\"", log.Value.NewJson);
}
[Fact]
public void Unchanged_values_and_unknown_columns_are_left_out()
{
Assert.Null(TripLogBuilder.Build("FIRST_ADVANCED_PAYMENT", Old, new Dictionary<string, object?> { ["CURRENT_BALANCE"] = 100.0m }));
Assert.Null(TripLogBuilder.Build("X", Old, new Dictionary<string, object?> { ["NOPE"] = "1" }));
var changed = TripLogBuilder.Build("FIRST_ADVANCED_PAYMENT", Old, new Dictionary<string, object?> { ["CURRENT_BALANCE"] = 350.5m });
Assert.Equal("{\"CURRENT_BALANCE\":\"100\"}", changed!.Value.OldJson);
Assert.Equal("{\"CURRENT_BALANCE\":\"350.5\"}", changed.Value.NewJson);
}
[Fact]
public void Print_stores_its_arguments_with_empty_old_data()
{
var log = TripLogBuilder.Build("PRINT", _ => (false, null), new Dictionary<string, object?> { ["NOTES"] = "" });
Assert.Equal("{}", log!.Value.OldJson);
Assert.Equal("{\"NOTES\":\"\"}", log.Value.NewJson);
}
}
public class TripActionRulesTests
{
[Fact]
public void Diesel_buttons_follow_the_diesel_charge_step()
{
Assert.Contains("firstAdvance", TripActionRules.Available("RESD", 0, null, false, false));
var step1 = TripActionRules.Available("LOADED", 1, null, false, false);
Assert.Contains("secondAdvance", step1);
Assert.Contains("firstAdvanceReverse", step1);
Assert.DoesNotContain("secondAdvance", TripActionRules.Available("RESD", 1, null, false, false));
Assert.Contains("secondAdvanceReverse", TripActionRules.Available("DISCHARGED", 2, 1, false, false));
Assert.DoesNotContain("firstAdvance", TripActionRules.Available("RVKD", 0, null, false, false));
}
[Fact]
public void Discharged_trip_offers_exchange_or_audit_for_a_wallet_and_receipt_otherwise()
{
var wallet = TripActionRules.Available("DISCHARGED", 2, 1, auditMode: false, hasTransferLog: false);
Assert.Contains("exchange", wallet);
Assert.DoesNotContain("exchangeAudit", wallet);
Assert.DoesNotContain("receipt", wallet);
Assert.Contains("print", wallet);
Assert.Contains("exchangeAudit", TripActionRules.Available("DISCHARGED", 2, 1, auditMode: true, hasTransferLog: false));
var bank = TripActionRules.Available("DISCHARGED", 2, 0, false, false);
Assert.Contains("receipt", bank);
Assert.DoesNotContain("exchange", bank);
}
[Fact]
public void Payment_follows_receipt_and_confirm_needs_a_transfer_log()
{
Assert.Contains("payment", TripActionRules.Available("READY_TO_PAY", 2, 0, false, false));
var pending = TripActionRules.Available("PENDING_EXCHANGE", 2, 1, true, hasTransferLog: true);
Assert.Contains("exchange", pending);
Assert.Contains("activateWalletExchange", pending);
Assert.Contains("confirmExchange", pending);
Assert.DoesNotContain("confirmExchange", TripActionRules.Available("PENDING_EXCHANGE", 2, 1, true, hasTransferLog: false));
}
}