admin/src/Oms.Tests/ChineseAssignScriptEvaluatorTests.cs
zzh 935f017f21 feat: 预报前/落库前中文赋值脚本求值与执行
渠道 PreScript 改预报报文,销售产品 PreStorageScript 在创单落库前改写入参;字段白名单对齐下单页并补 AI skill。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-24 15:10:14 +08:00

172 lines
4.9 KiB
C#

using FluentAssertions;
using Oms.Application.Dtos.Orders;
using Oms.Application.Services.Orders.Issues;
using Oms.Domain.CustomersAggregate.Entities;
using Oms.Domain.OrdersAggregate.Entities;
using Xunit;
namespace Oms.Tests;
public class ChineseAssignScriptEvaluatorTests
{
[Fact]
public void Apply_ConcatAddress2IntoAddress1_AndClearAddress2()
{
var recipient = new OrderRecipient
{
StreetAddress1 = "Line1",
StreetAddress2 = "Line2"
};
var context = OrderRuleEvaluationContext.FromOrder(
new Order { ClientNo = "C1" },
customer: null,
recipient,
sender: new OrderSender(),
declarations: null,
weights: null,
fees: null);
ChineseAssignScriptEvaluator.Apply(
"收件人地址1 = 收件人地址1 + ' ' + 收件人地址2\n收件人地址2 = ''",
context);
recipient.StreetAddress1.Should().Be("Line1 Line2");
recipient.StreetAddress2.Should().Be("");
}
[Fact]
public void Apply_EmptyScript_IsNoOp()
{
var recipient = new OrderRecipient { StreetAddress1 = "A" };
var context = OrderRuleEvaluationContext.FromOrder(
new Order(),
null,
recipient,
new OrderSender(),
null,
null,
null);
ChineseAssignScriptEvaluator.Apply(" ", context);
recipient.StreetAddress1.Should().Be("A");
}
[Fact]
public void Apply_ReadonlyOrderFieldOnLeft_Throws()
{
var context = OrderRuleEvaluationContext.FromOrder(
new Order { ClientNo = "C1" },
null,
new OrderRecipient(),
new OrderSender(),
null,
null,
null);
var act = () => ChineseAssignScriptEvaluator.Apply("客户单号 = 'X'", context);
act.Should().Throw<Fake.Application.BusinessException>()
.WithMessage("*不可赋值*");
}
[Fact]
public void Apply_PreCommitCreateInput_WritesReceiver()
{
var input = new OrderParcelCreateInput
{
ProductCode = "P1",
Receiver = new OrderParcelReceiverInput
{
CountryCode = "US",
StreetAddress1 = "A",
StreetAddress2 = "B"
}
};
var context = OrderRuleEvaluationContext.FromPreCommit(
input,
new Customer { CustomerName = "c" },
"US",
"美国");
ChineseAssignScriptEvaluator.Apply(
"收件人地址1 = 收件人地址1 + ' ' + 收件人地址2\n收件人地址2 = ''",
context);
input.Receiver!.StreetAddress1.Should().Be("A B");
input.Receiver.StreetAddress2.Should().Be("");
}
[Fact]
public void Apply_PreCommitCreateInput_WritesFirstDeclarationItem()
{
var input = new OrderParcelCreateInput
{
ProductCode = "P1",
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
Items =
[
new OrderParcelItemInput { CnName = "壳", EnName = "Case", Sku = "S1" }
]
};
var context = OrderRuleEvaluationContext.FromPreCommit(
input,
new Customer { CustomerName = "c" },
"US",
null);
ChineseAssignScriptEvaluator.Apply(
"英文品名 = 中文品名 + '-' + SKU",
context);
input.Items[0].EnName.Should().Be("壳-S1");
}
[Fact]
public void Apply_PreCommitCreateInput_WritesWeightFields()
{
var input = new OrderParcelCreateInput
{
ProductCode = "P1",
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
Length = 10,
Width = 8,
Height = 6,
ActualWeight = 1.2m
};
var context = OrderRuleEvaluationContext.FromPreCommit(
input,
new Customer { CustomerName = "c" },
"US",
null);
ChineseAssignScriptEvaluator.Apply(
"实重 = 实重 + 0.1\n长度 = 长度 + 1",
context);
input.ActualWeight.Should().Be(1.3m);
input.Length.Should().Be(11m);
}
[Fact]
public void Apply_ChargeableWeight_NotWritable()
{
var input = new OrderParcelCreateInput
{
ProductCode = "P1",
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
ActualWeight = 1.2m
};
var context = OrderRuleEvaluationContext.FromPreCommit(
input,
new Customer { CustomerName = "c" },
"US",
null);
var act = () => ChineseAssignScriptEvaluator.Apply("计费重 = 实重", context);
act.Should().Throw<Fake.Application.BusinessException>()
.WithMessage("*不可赋值*");
}
}