using InterfaceForward.Application.Contracts.Dtos.ScriptAssistant; using InterfaceForward.Application.Services.ScriptAssistant; 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")); } }