using InterfaceForward.Application.Contracts.Dtos.ScriptAssistant; using InterfaceForward.Application.Contracts.ForwardCore; using InterfaceForward.Application.ForwardCore; using InterfaceForward.Application.Services.ScriptAssistant; using InterfaceForward.Domain.Shared; using InterfaceForward.Domain.Shared.Dtos; using InterfaceForward.Domain.Shared.Enum; using Newtonsoft.Json.Linq; namespace InterfaceForward.Application.Tests; public class ScriptAssistantServiceTests { static ScriptAssistantServiceTests() { NatashaManagement.Preheating(false, false); } [Fact] public void Generate_ShouldCreateTemplateFromFields() { var service = new ScriptAssistantService(); var output = service.Generate(new GenerateForwardScriptInput { Requirement = "把 appKey 放到 header", AccountFields = ["appKey"], Headers = ["appKey"] }); Assert.Contains("GetAccountFieldValueOrNull(\"appKey\")", output.Script); Assert.Contains("context.AddHeader(\"appKey\"", output.Script); } [Fact] public void Validate_ShouldCompileGeneratedScript() { var service = new ScriptAssistantService(); var output = service.Validate(new ValidateForwardScriptInput { Script = "var appKey = context.GetAccountFieldValueOrNull(\"appKey\") ?? string.Empty;\ncontext.AddHeader(\"appKey\", appKey);" }); Assert.True(output.Success, output.Error); } [Fact] public async Task Preview_ShouldRunScriptAndReturnChangedFields() { var service = new ScriptAssistantService(); var output = await service.Preview(new PreviewForwardScriptInput { Script = "var appKey = context.GetAccountFieldValueOrNull(\"appKey\") ?? string.Empty;\ncontext.AddHeader(\"appKey\", appKey);", OriginalInput = new JObject { ["orderNo"] = "A001" }, MappedInput = new JObject { ["targetOrderNo"] = "A001" }, AccountFields = new Dictionary { ["appKey"] = "test-key" } }); Assert.True(output.Success, output.Error); var field = Assert.Single(output.FixedFields); Assert.Equal("appKey", field.FieldName); Assert.Equal("test-key", field.FieldValue); } [Fact] public void Validate_ShouldReturnWarningForDangerousToken() { var service = new ScriptAssistantService(); var output = service.Validate(new ValidateForwardScriptInput { Script = "System.IO.File.ReadAllText(\"a.txt\");" }); Assert.Contains(output.Warnings, x => x.Contains("System.IO")); } } public class DefaultForwardFlowTests { [Fact] public void SerializationBody_ShouldUseMappedInputForJson() { var flow = new TestForwardFlow(); var context = new ForwardCoreContext { TargetInterface = new InterfaceDto { ContentType = ContentType.Json }, OriginalInterfaceMappedInput = new JObject { ["orderId"] = "A001" } }; var body = flow.SerializationBody(context); Assert.Equal("{\"orderId\":\"A001\"}", body); } [Fact] public async Task SetContentType_ShouldReplaceFromMappingBodyForFormUrlEncoded() { var flow = new TestForwardFlow(); var context = new ForwardCoreContext { TargetInterface = new InterfaceDto { ContentType = ContentType.FormUrlEncoded }, TargetInterfaceInput = "{\"orderId\":\"A001\"}", OriginalInterfaceMappedInput = new JObject { ["orderId"] = "A001" }, FormFieldList = [ new InterfaceFormFieldDto { Name = "notifyBizEventDTO", Value = GlobalConst.FromMappingBody } ] }; using var message = new HttpRequestMessage(); flow.SetContentTypeForTest(context, message); var content = await message.Content!.ReadAsStringAsync(); Assert.Equal("notifyBizEventDTO=%7B%22orderId%22%3A%22A001%22%7D", content); } class TestForwardFlow : DefaultForwardFlow { public void SetContentTypeForTest(ForwardCoreContext context, HttpRequestMessage message) { SetContentType(context, message); } } }