using System.Text.Json.Nodes; using XFEExtension.NetCore.XUnit.Assertions; using XFEExtension.NetCore.XUnit.Attributes; using XFEWorkflow.Abstractions; using XFEWorkflow.Core.Runtime; using XFEWorkflow.Infrastructure.Compilation; namespace XFEWorkflow.Test; [TestFixture] public class ConditionTests { [Test] public async Task GeneralConditionGroupsSupportKeywordSetsAndMetadataGuards() { var predicate = new NodeDefinition { CapabilityId = "core.if", Parameters = new() { ["conditions"] = new JsonArray( new JsonObject { ["left"] = new JsonObject { ["$input"] = "hasContent" }, ["right"] = true }, new JsonObject { ["left"] = new JsonObject { ["$input"] = "content" }, ["right"] = new JsonArray("abc", "接单"), ["operator"] = "containsAny", ["normalizeText"] = true, ["ignoreCase"] = true }) } }; var flow = TestSupport.Graph(predicate); flow.Graph.Edges.Single(x => x.FromNode == predicate.Id).FromPort = "true"; var positive = flow.Graph.Nodes[^1]; positive.Parameters["result"] = "matched"; var negative = new NodeDefinition { CapabilityId = "core.end", Parameters = new() { ["result"] = "not-matched" } }; flow.Graph.Nodes.Add(negative); flow.Graph.Edges.Add(new() { FromNode = predicate.Id, FromPort = "false", ToNode = negative.Id }); using var compiler = new CodeCompiler(); var engine = new WorkflowEngine(compiler, CoreCapabilities.Catalog()); foreach (var (body, available, expected) in new[] { ("ABC 来了", true, "matched"), ("接 单", true, "matched"), ("接单", false, "not-matched"), ("其他", true, "not-matched") }) { var context = TestSupport.Context(inputs: new() { ["content"] = body, ["hasContent"] = available }); Assert.Equal(expected, (await engine.ExecuteAsync(flow, context, default)).String("result")); Assert.Equal(expected == "matched", context.NodeOutputs[predicate.Id].Boolean("matched")); Assert.Equal(expected == "matched" ? "match" : "noMatch", context.NodeOutputs[predicate.Id].String("branch")); } var comparison = (JsonObject)((JsonArray)predicate.Parameters["conditions"]!)[1]!; comparison["operator"] = "containsAll"; Assert.Equal("not-matched", (await engine.ExecuteAsync(flow, TestSupport.Context(inputs: new() { ["content"] = "abc", ["hasContent"] = true }), default)).String("result")); Assert.Equal("matched", (await engine.ExecuteAsync(flow, TestSupport.Context(inputs: new() { ["content"] = "ABC 接 单", ["hasContent"] = true }), default)).String("result")); } [Test] public void EqualityKeepsTypesAndFullSenderIdsAndMalformedConditionsFail() { Assert.False(WorkflowConditions.Evaluate(new() { ["left"] = "1", ["right"] = 1 })); Assert.False(WorkflowConditions.Evaluate(new() { ["left"] = "9007199254740992", ["right"] = "9007199254740993" })); Assert.True(WorkflowConditions.Evaluate(new() { ["left"] = new JsonArray("9007199254740993", "1001"), ["right"] = "9007199254740993", ["operator"] = "contains" })); Assert.False(WorkflowConditions.Evaluate(new() { ["left"] = "a b", ["right"] = "ab" })); Assert.True(WorkflowConditions.Evaluate(new() { ["left"] = "A b", ["right"] = "ab", ["normalizeText"] = true, ["ignoreCase"] = true })); Assert.Throws(() => WorkflowConditions.Evaluate(new() { ["conditions"] = "invalid" })); Assert.Throws(() => WorkflowConditions.Evaluate(new() { ["operator"] = "containsAny", ["right"] = "invalid" })); var nested = new JsonObject { ["left"] = true, ["right"] = true }; for (var i = 0; i < 18; i++) nested = new() { ["conditions"] = new JsonArray(nested) }; Assert.Throws(() => WorkflowConditions.Evaluate(nested)); } }