using System.Globalization; using System.Text.Json.Nodes; using XFEExtension.NetCore.XUnit.Assertions; using XFEExtension.NetCore.XUnit.Attributes; using XFEWorkflow.Abstractions; using XFEWorkflow.Core.Runtime; namespace XFEWorkflow.Test; [TestFixture] public sealed class BasicDataTests { [TestCase("math.add", """{"left":0.1,"right":0.2}""", "0.3")] [TestCase("math.subtract", """{"left":5,"right":8}""", "-3")] [TestCase("math.multiply", """{"left":1.2,"right":3}""", "3.6")] [TestCase("math.divide", """{"left":1,"right":4}""", "0.25")] [TestCase("math.modulo", """{"left":-7,"right":3}""", "-1")] [TestCase("math.min", """{"left":3,"right":2}""", "2")] [TestCase("math.max", """{"left":3,"right":2}""", "3")] [TestCase("math.power", """{"left":2,"right":10}""", "1024")] [TestCase("math.abs", """{"value":-3.5}""", "3.5")] [TestCase("math.negate", """{"value":-3.5}""", "3.5")] [TestCase("math.floor", """{"value":-3.5}""", "-4")] [TestCase("math.ceiling", """{"value":-3.5}""", "-3")] [TestCase("math.truncate", """{"value":-3.5}""", "-3")] [TestCase("math.sign", """{"value":-3.5}""", "-1")] [TestCase("math.round", """{"value":2.345,"digits":2}""", "2.35")] [TestCase("math.round", """{"value":2.345,"digits":2,"mode":"toEven"}""", "2.34")] [TestCase("math.clamp", """{"value":200,"min":1,"max":100}""", "100")] [TestCase("math.function", """{"value":9,"function":"sqrt"}""", "3")] [TestCase("math.format", """{"value":12.3,"format":"F2"}""", """"12.30"""")] [TestCase("math.bitwise", """{"left":5,"right":3,"operator":"and"}""", "1")] [TestCase("math.bitwise", """{"left":5,"right":3,"operator":"leftShift"}""", "40")] [TestCase("compare.eq", """{"left":"1","right":1}""", "false")] [TestCase("compare.ne", """{"left":null,"right":0}""", "true")] [TestCase("compare.gt", """{"left":11,"right":2}""", "true")] [TestCase("compare.gte", """{"left":11,"right":11}""", "true")] [TestCase("compare.lt", """{"left":"a","right":"B","ignoreCase":true}""", "true")] [TestCase("compare.lte", """{"left":-1,"right":-1}""", "true")] [TestCase("boolean.and", """{"left":true,"right":false}""", "false")] [TestCase("boolean.or", """{"left":true,"right":false}""", "true")] [TestCase("boolean.xor", """{"left":true,"right":true}""", "false")] [TestCase("boolean.not", """{"left":true}""", "false")] [TestCase("boolean.all", """{"items":[]}""", "true")] [TestCase("boolean.any", """{"items":[]}""", "false")] [TestCase("value.type", """{"value":[]}""", """"array"""")] [TestCase("value.is", """{"value":"1","type":"number"}""", "false")] [TestCase("value.isNull", """{"value":""}""", "false")] [TestCase("value.isNull", """{"value":null}""", "true")] [TestCase("value.coalesce", """{"value":0,"fallback":123}""", "0")] [TestCase("value.coalesce", """{"value":null,"fallback":"原文"}""", """"原文"""")] [TestCase("value.select", """{"condition":false,"whenTrue":1,"whenFalse":"原文"}""", """"原文"""")] [TestCase("value.text", """{"value":" {0} $var ¥var 🌚\\u4e2d "}""", """" {0} $var ¥var 🌚\\u4e2d """")] [TestCase("value.number", """{"value":1.23}""", "1.23")] [TestCase("value.boolean", """{"value":true}""", "true")] [TestCase("value.object", """{"value":{"$var":"不解析"}}""", """{"$var":"不解析"}""")] [TestCase("value.array", """{"value":[1,"一",null]}""", """[1,"一",null]""")] [TestCase("value.null", "{}", "null")] [TestCase("array.pack", """{"first":"一","second":null,"third":3,"count":3}""", """["一",null,3]""")] [TestCase("array.append", """{"items":[1],"value":[2,3]}""", """[1,[2,3]]""")] [TestCase("array.prepend", """{"items":[1],"value":"零"}""", """["零",1]""")] [TestCase("array.concat", """{"left":[1],"right":[2,1]}""", """[1,2,1]""")] [TestCase("array.set", """{"items":[1,2],"index":1,"value":null}""", """[1,null]""")] [TestCase("array.insert", """{"items":[1],"index":1,"value":2}""", """[1,2]""")] [TestCase("array.remove", """{"items":[1,2,3,4],"index":1,"count":2}""", """[1,4]""")] [TestCase("array.slice", """{"items":[1,2,3,4],"start":2}""", """[3,4]""")] [TestCase("array.length", """{"items":[null,null]}""", "2")] [TestCase("array.contains", """{"items":[1,2],"value":"1"}""", "false")] [TestCase("array.reverse", """{"items":[1,null,"三"]}""", """["三",null,1]""")] [TestCase("array.distinct", """{"items":[1,1.0,"1",null,null,{"a":1,"b":2},{"b":2,"a":1}]}""", """[1,"1",null,{"a":1,"b":2}]""")] [TestCase("array.sort", """{"items":[10,2,null,1]}""", """[null,1,2,10]""")] [TestCase("array.sort", """{"items":[{"id":"a","n":2},{"id":"b","n":2},{"id":"c","n":1}],"path":"n","descending":true}""", """[{"id":"a","n":2},{"id":"b","n":2},{"id":"c","n":1}]""")] [TestCase("array.filter", """{"items":[{"id":"一"},{"id":"二"},{"id":"三"}],"path":"id","operator":"in","right":["一","三"]}""", """[{"id":"一"},{"id":"三"}]""")] [TestCase("array.filter", """{"items":[1,2,3,4],"operator":"gt","right":2}""", """[3,4]""")] [TestCase("array.filter", """{"items":["10","2","3"],"operator":"lt","right":"2"}""", """["10"]""")] [TestCase("array.mapField", """{"items":[{"id":"一"},{"id":null},{}],"path":"id","skipMissing":true}""", """["一",null]""")] [TestCase("array.range", """{"start":5,"count":3,"step":-2}""", """[5,3,1]""")] [TestCase("array.flatten", """{"items":[1,[2,[3]],null]}""", """[1,2,[3],null]""")] [TestCase("array.chunk", """{"items":[1,2,3,4,5],"size":2}""", """[[1,2],[3,4],[5]]""")] [TestCase("array.zip", """{"left":[1,2],"right":["一"]}""", """[[1,"一"]]""")] [TestCase("array.groupBy", """{"items":[{"k":1,"v":"a"},{"k":2},{"k":1,"v":"b"}],"path":"k"}""", """[{"key":1,"items":[{"k":1,"v":"a"},{"k":1,"v":"b"}]},{"key":2,"items":[{"k":2}]}]""")] [TestCase("array.union", """{"left":[1,2,1],"right":[2,3]}""", """[1,2,3]""")] [TestCase("array.intersect", """{"left":[1,2,1,3],"right":[2,3,4]}""", """[2,3]""")] [TestCase("array.except", """{"left":[1,2,1,3],"right":[2,4]}""", """[1,3]""")] [TestCase("object.set", """{"value":{"a":1},"key":"b.c","field":"中文🌚"}""", """{"a":1,"b.c":"中文🌚"}""")] [TestCase("object.has", """{"value":{"a":null},"path":"a"}""", "true")] [TestCase("object.keys", """{"value":{"a":1,"b":2}}""", """["a","b"]""")] [TestCase("object.values", """{"value":{"a":1,"b":null}}""", """[1,null]""")] [TestCase("object.entries", """{"value":{"a":1,"b":null}}""", """[{"key":"a","value":1},{"key":"b","value":null}]""")] [TestCase("object.count", """{"value":{"a":null}}""", "1")] [TestCase("object.merge", """{"left":{"a":1,"nested":{"a":1}},"right":{"a":2,"nested":{"b":2}}}""", """{"a":2,"nested":{"b":2}}""")] [TestCase("object.merge", """{"left":{"a":1},"right":{"a":2,"b":3},"overwrite":false}""", """{"a":1,"b":3}""")] [TestCase("object.pick", """{"value":{"a":1,"b":null},"keys":["b","missing"]}""", """{"b":null}""")] [TestCase("object.fromPairs", """{"keys":["a","表情"],"values":[1,"🌚"]}""", """{"a":1,"表情":"🌚"}""")] [TestCase("object.fromEntries", """{"entries":[{"key":"a","value":1},{"key":"b","value":null}]}""", """{"a":1,"b":null}""")] [TestCase("value.clone", """{"value":{"$node":"原文字段"}}""", """{"$node":"原文字段"}""")] [TestCase("json.parse", """{"value":"[1,null,\"中文🌚\"]"}""", """[1,null,"中文🌚"]""")] public void ArithmeticLogicAndCollectionsKeepExactTypesAndProduceNewValues(string id, string json, string expected) { var input = JsonNode.Parse(json)!.AsObject(); var original = input.ToJsonString(); var output = WorkflowBasicNodes.Execute("core." + id, input); var expectedJson = output["value"]?.GetValueKind() == System.Text.Json.JsonValueKind.String ? "\"" + expected + "\"" : expected; Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expectedJson), output["value"]), id + ":" + output.Display()); Assert.Equal(original, input.ToJsonString(), "基础节点不允许修改输入"); } [Test] public void LookupFoundFlagsDistinguishMissingFromPresentNullAndStatisticsDefineEmptyArrays() { var found = WorkflowBasicNodes.Execute("core.array.get", new() { ["items"] = new JsonArray((JsonNode?)null), ["index"] = 0 }); Assert.True(found.Boolean("found")); Assert.True(found["value"] is null); var missing = WorkflowBasicNodes.Execute("core.array.get", new() { ["items"] = new JsonArray(1), ["index"] = 2 }); Assert.False(missing.Boolean("found")); Assert.True(missing["value"] is null); var index = WorkflowBasicNodes.Execute("core.array.indexOf", new() { ["items"] = new JsonArray(1, "1"), ["value"] = "1" }); Assert.Equal(1, index.Integer("index")); Assert.True(index.Boolean("found")); var match = WorkflowBasicNodes.Execute("core.array.find", new() { ["items"] = new JsonArray(1, 2, 3), ["operator"] = "gt", ["right"] = 2 }); Assert.Equal(2, match.Integer("index")); Assert.True(match.Boolean("found")); var absent = WorkflowBasicNodes.Execute("core.array.find", new() { ["items"] = new JsonArray(1, 2), ["operator"] = "eq", ["right"] = 3 }); Assert.Equal(-1, absent.Integer("index")); Assert.False(absent.Boolean("found")); var removed = WorkflowBasicNodes.Execute("core.object.remove", new() { ["value"] = new JsonObject { ["a"] = null }, ["key"] = "a" }); Assert.True(removed.Boolean("removed")); Assert.Equal(0, removed["value"]!.AsObject().Count); var stats = WorkflowBasicNodes.Execute("core.math.aggregate", new() { ["items"] = new JsonArray(0.1m, 0.2m, 0.3m) }); Assert.Equal(0.6m, stats["sum"]!.GetValue()); Assert.Equal(0.2m, stats["average"]!.GetValue()); var empty = WorkflowBasicNodes.Execute("core.math.aggregate", []); Assert.Equal(0m, empty["sum"]!.GetValue()); Assert.True(empty["average"] is null && empty["min"] is null && empty["max"] is null); } [Test] public void InvalidTypesBoundsAndArithmeticFailuresAreNeverSilentlyCoerced() { Assert.Throws(() => WorkflowBasicNodes.Execute("core.math.add", new() { ["left"] = "1" })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.math.divide", new() { ["right"] = 0 })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.math.multiply", new() { ["left"] = decimal.MaxValue, ["right"] = 2 })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.math.function", new() { ["value"] = -1, ["function"] = "sqrt" })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.compare.gt", new() { ["left"] = 1, ["right"] = "2" })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.boolean.all", new() { ["items"] = new JsonArray(true, 1) })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.array.set", new() { ["items"] = new JsonArray(1), ["index"] = 1 })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.array.range", new() { ["count"] = 1.2m })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.array.sort", new() { ["items"] = new JsonArray(1, "2") })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.array.filter", new() { ["operator"] = "in", ["right"] = "x", ["items"] = new JsonArray(1) })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.array.filter", new() { ["operator"] = "gt", ["right"] = 1, ["items"] = new JsonArray("2") })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.object.fromPairs", new() { ["keys"] = new JsonArray("a", "a"), ["values"] = new JsonArray(1, 2) })); Assert.Throws(() => WorkflowBasicNodes.Execute("core.object.fromPairs", new() { ["keys"] = new JsonArray(1), ["values"] = new JsonArray(1) })); } [Test] public void NumbersDatesAndCaseConversionDoNotDependOnTheUiCulture() { var previous = CultureInfo.CurrentCulture; try { CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("tr-TR"); Assert.Equal("12.30", WorkflowBasicNodes.Execute("core.math.format", new() { ["value"] = 12.3m, ["format"] = "F2" }).String("value")); Assert.Equal("I", WorkflowBasicNodes.Execute("core.text.case", new() { ["value"] = "i" }).String("value")); Assert.Equal("2026-10-13", WorkflowBasicNodes.Execute("core.date.format", new() { ["value"] = "2026-10-13T03:04:05+08:00", ["format"] = "yyyy-MM-dd" }).String("value")); } finally { CultureInfo.CurrentCulture = previous; } } [Test] public void RandomNumbersStayInTheirDeclaredRangeAndLargeIdsRemainExact() { for (var i = 0; i < 100; i++) { var number = WorkflowBasicNodes.Execute("core.math.random", new() { ["min"] = 9_007_199_254_740_992m, ["max"] = 9_007_199_254_740_995m })["value"]!.GetValue(); Assert.True(number >= 9_007_199_254_740_992m && number < 9_007_199_254_740_995m && number == decimal.Truncate(number)); } var ids = JsonNode.Parse("""[9007199254740992,9007199254740993,9007199254740992]""")!.AsArray(); Assert.Equal(2, WorkflowBasicNodes.Execute("core.array.distinct", new() { ["items"] = ids })["value"]!.AsArray().Count); Assert.False(WorkflowBasicNodes.Execute("core.compare.eq", new() { ["left"] = "9007199254740992", ["right"] = "9007199254740993" }).Boolean("value")); } [Test] public void CatalogPortsAreUniqueDescribedAndAvailableToCodeAndGraphs() { var catalog = CoreCapabilities.Catalog(); Assert.Equal(CoreCapabilities.All.Count + WorkflowTriggerNodes.BuiltIns.Count, catalog.Count); foreach (var descriptor in WorkflowBasicNodes.Descriptors.Concat(WorkflowControlNodes.Descriptors)) { Assert.True(descriptor.Description.Length > 0, descriptor.Id); Assert.Equal("core", descriptor.PluginId); foreach (var ports in new[] { descriptor.Inputs, descriptor.Outputs }) { Assert.Equal(ports.Count, ports.Select(p => p.Name).Distinct().Count(), descriptor.Id); Assert.True(ports.All(p => p.Name.Length > 0 && p.Label.Length > 0 && p.Description.Length > 0), descriptor.Id); foreach (var port in ports) if (port.DefaultValue is { } value) Assert.True(GraphValidator.Matches(value, port.Type), descriptor.Id + "." + port.Name); } } var output = Path.Combine(TestSupport.Root, ".artifacts", "basic-nodes-catalog.json"); Directory.CreateDirectory(Path.GetDirectoryName(output)!); File.WriteAllText(output, WorkflowJson.SerializeDisplay(CoreCapabilities.All)); } }