|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 9 | + "github.com/mendixlabs/mxcli/mdl/backend/mock" |
| 10 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 11 | +) |
| 12 | + |
| 13 | +// TestIfWithRuleCall_EmitsRuleSplitCondition is the regression test for the |
| 14 | +// Rule vs Expression subtype preservation bug. Prior to the fix, an IF whose |
| 15 | +// condition was a call into a rule (e.g. ControlCenterCommons.IsNotEmptyString) |
| 16 | +// was serialized as ExpressionSplitCondition, causing Mendix Studio Pro to |
| 17 | +// raise CE0117 "Error(s) in expression" and demoting the decision's subtype |
| 18 | +// from Rule to Expression on every describe → exec roundtrip. |
| 19 | +func TestIfWithRuleCall_EmitsRuleSplitCondition(t *testing.T) { |
| 20 | + mb := &mock.MockBackend{ |
| 21 | + IsRuleFunc: func(qualifiedName string) (bool, error) { |
| 22 | + return qualifiedName == "Module.IsEligible", nil |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + // if Module.IsEligible(Customer = $Customer) then return true else return false |
| 27 | + ifStmt := &ast.IfStmt{ |
| 28 | + Condition: &ast.FunctionCallExpr{ |
| 29 | + Name: "Module.IsEligible", |
| 30 | + Arguments: []ast.Expression{ |
| 31 | + &ast.BinaryExpr{ |
| 32 | + Left: &ast.IdentifierExpr{Name: "Customer"}, |
| 33 | + Operator: "=", |
| 34 | + Right: &ast.VariableExpr{Name: "Customer"}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ThenBody: []ast.MicroflowStatement{ |
| 39 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 40 | + }, |
| 41 | + ElseBody: []ast.MicroflowStatement{ |
| 42 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + fb := &flowBuilder{ |
| 47 | + posX: 100, |
| 48 | + posY: 100, |
| 49 | + spacing: HorizontalSpacing, |
| 50 | + backend: mb, |
| 51 | + varTypes: map[string]string{"Customer": "Module.Customer"}, |
| 52 | + declaredVars: map[string]string{"Customer": "Module.Customer"}, |
| 53 | + } |
| 54 | + fb.buildFlowGraph([]ast.MicroflowStatement{ifStmt}, nil) |
| 55 | + |
| 56 | + var split *microflows.ExclusiveSplit |
| 57 | + for _, obj := range fb.objects { |
| 58 | + if sp, ok := obj.(*microflows.ExclusiveSplit); ok { |
| 59 | + split = sp |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + if split == nil { |
| 64 | + t.Fatal("expected an ExclusiveSplit, found none") |
| 65 | + } |
| 66 | + |
| 67 | + rule, ok := split.SplitCondition.(*microflows.RuleSplitCondition) |
| 68 | + if !ok { |
| 69 | + t.Fatalf("split condition: got %T, want *microflows.RuleSplitCondition", split.SplitCondition) |
| 70 | + } |
| 71 | + if rule.RuleQualifiedName != "Module.IsEligible" { |
| 72 | + t.Errorf("rule name: got %q, want %q", rule.RuleQualifiedName, "Module.IsEligible") |
| 73 | + } |
| 74 | + if len(rule.ParameterMappings) != 1 { |
| 75 | + t.Fatalf("parameter mappings: got %d, want 1", len(rule.ParameterMappings)) |
| 76 | + } |
| 77 | + pm := rule.ParameterMappings[0] |
| 78 | + if pm.ParameterName != "Module.IsEligible.Customer" { |
| 79 | + t.Errorf("parameter name: got %q, want %q", pm.ParameterName, "Module.IsEligible.Customer") |
| 80 | + } |
| 81 | + if pm.Argument != "$Customer" { |
| 82 | + t.Errorf("argument: got %q, want %q", pm.Argument, "$Customer") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestIfWithNonRuleCall_EmitsExpressionSplitCondition confirms that a plain |
| 87 | +// expression-level function call (built-in or sub-microflow, not a rule) still |
| 88 | +// produces an ExpressionSplitCondition — the fix must not over-trigger. |
| 89 | +func TestIfWithNonRuleCall_EmitsExpressionSplitCondition(t *testing.T) { |
| 90 | + mb := &mock.MockBackend{ |
| 91 | + IsRuleFunc: func(qualifiedName string) (bool, error) { |
| 92 | + return false, nil |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + ifStmt := &ast.IfStmt{ |
| 97 | + Condition: &ast.FunctionCallExpr{ |
| 98 | + Name: "empty", |
| 99 | + Arguments: []ast.Expression{&ast.VariableExpr{Name: "S"}}, |
| 100 | + }, |
| 101 | + ThenBody: []ast.MicroflowStatement{ |
| 102 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + fb := &flowBuilder{ |
| 107 | + posX: 100, |
| 108 | + posY: 100, |
| 109 | + spacing: HorizontalSpacing, |
| 110 | + backend: mb, |
| 111 | + varTypes: map[string]string{"S": "String"}, |
| 112 | + declaredVars: map[string]string{"S": "String"}, |
| 113 | + } |
| 114 | + fb.buildFlowGraph([]ast.MicroflowStatement{ifStmt}, nil) |
| 115 | + |
| 116 | + var split *microflows.ExclusiveSplit |
| 117 | + for _, obj := range fb.objects { |
| 118 | + if sp, ok := obj.(*microflows.ExclusiveSplit); ok { |
| 119 | + split = sp |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + if split == nil { |
| 124 | + t.Fatal("expected an ExclusiveSplit, found none") |
| 125 | + } |
| 126 | + if _, ok := split.SplitCondition.(*microflows.ExpressionSplitCondition); !ok { |
| 127 | + t.Fatalf("split condition: got %T, want *microflows.ExpressionSplitCondition", split.SplitCondition) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// TestIfWithoutBackend_FallsBackToExpression confirms that when the flow |
| 132 | +// builder has no backend (e.g. disconnected check mode), it can't tell whether |
| 133 | +// a qualified call is a rule — it must default to ExpressionSplitCondition so |
| 134 | +// that syntax-only checks don't crash. |
| 135 | +func TestIfWithoutBackend_FallsBackToExpression(t *testing.T) { |
| 136 | + ifStmt := &ast.IfStmt{ |
| 137 | + Condition: &ast.FunctionCallExpr{ |
| 138 | + Name: "Module.IsEligible", |
| 139 | + Arguments: []ast.Expression{}, |
| 140 | + }, |
| 141 | + ThenBody: []ast.MicroflowStatement{ |
| 142 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + fb := &flowBuilder{ |
| 147 | + posX: 100, |
| 148 | + posY: 100, |
| 149 | + spacing: HorizontalSpacing, |
| 150 | + varTypes: map[string]string{}, |
| 151 | + declaredVars: map[string]string{}, |
| 152 | + } |
| 153 | + fb.buildFlowGraph([]ast.MicroflowStatement{ifStmt}, nil) |
| 154 | + |
| 155 | + for _, obj := range fb.objects { |
| 156 | + if sp, ok := obj.(*microflows.ExclusiveSplit); ok { |
| 157 | + if _, ok := sp.SplitCondition.(*microflows.ExpressionSplitCondition); !ok { |
| 158 | + t.Fatalf("without backend, split condition: got %T, want *microflows.ExpressionSplitCondition", sp.SplitCondition) |
| 159 | + } |
| 160 | + return |
| 161 | + } |
| 162 | + } |
| 163 | + t.Fatal("expected an ExclusiveSplit, found none") |
| 164 | +} |
0 commit comments