|
| 1 | +package executor |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 7 | + "github.com/mendixlabs/mxcli/model" |
| 8 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 9 | +) |
| 10 | + |
| 11 | +// Empty custom error handler followed by an IF that checks the call's |
| 12 | +// output variable: the error edge must rejoin the fallback path at the |
| 13 | +// first ELSE activity, sharing a merge with the split's `false` branch. |
| 14 | +// Without this rejoin, terminatePendingErrorHandlersAtEnd synthesises a |
| 15 | +// phantom EndEvent at the microflow tail (CE0079); wiring the error edge |
| 16 | +// straight to the IF split bypasses the output-variable declaration and |
| 17 | +// triggers CE0108 "variable not in scope" downstream. |
| 18 | +func TestEmptyErrorHandlerRejoinsAtIfElseFirstActivity(t *testing.T) { |
| 19 | + fb := &flowBuilder{ |
| 20 | + spacing: HorizontalSpacing, |
| 21 | + measurer: &layoutMeasurer{}, |
| 22 | + declaredVars: map[string]string{"R": "String"}, |
| 23 | + } |
| 24 | + |
| 25 | + oc := fb.buildFlowGraph([]ast.MicroflowStatement{ |
| 26 | + &ast.CallMicroflowStmt{ |
| 27 | + MicroflowName: ast.QualifiedName{Module: "M", Name: "Helper"}, |
| 28 | + OutputVariable: "R", |
| 29 | + ErrorHandling: &ast.ErrorHandlingClause{ |
| 30 | + Type: ast.ErrorHandlingCustomWithoutRollback, |
| 31 | + Body: nil, |
| 32 | + }, |
| 33 | + }, |
| 34 | + &ast.IfStmt{ |
| 35 | + Condition: &ast.VariableExpr{Name: "R"}, |
| 36 | + ThenBody: []ast.MicroflowStatement{ |
| 37 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "ok"}}, |
| 38 | + &ast.ReturnStmt{}, |
| 39 | + }, |
| 40 | + ElseBody: []ast.MicroflowStatement{ |
| 41 | + &ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "fail"}}, |
| 42 | + &ast.ReturnStmt{}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, nil) |
| 46 | + |
| 47 | + // Count EndEvents — should be 2 (one per IF branch), no phantom at mf end. |
| 48 | + endEvents := 0 |
| 49 | + for _, obj := range oc.Objects { |
| 50 | + if _, ok := obj.(*microflows.EndEvent); ok { |
| 51 | + endEvents++ |
| 52 | + } |
| 53 | + } |
| 54 | + if endEvents != 2 { |
| 55 | + t.Errorf("got %d EndEvents, want 2", endEvents) |
| 56 | + } |
| 57 | + |
| 58 | + // Find call activity, if split, first-else activity |
| 59 | + var callID, splitID, firstElseID model.ID |
| 60 | + logCount := 0 |
| 61 | + for _, obj := range oc.Objects { |
| 62 | + switch o := obj.(type) { |
| 63 | + case *microflows.ActionActivity: |
| 64 | + if _, ok := o.Action.(*microflows.MicroflowCallAction); ok { |
| 65 | + callID = o.ID |
| 66 | + } |
| 67 | + if _, ok := o.Action.(*microflows.LogMessageAction); ok { |
| 68 | + logCount++ |
| 69 | + if logCount == 2 { |
| 70 | + // Second log in visitation order = the else body log ("fail") |
| 71 | + firstElseID = o.ID |
| 72 | + } |
| 73 | + } |
| 74 | + case *microflows.ExclusiveSplit: |
| 75 | + splitID = o.ID |
| 76 | + } |
| 77 | + } |
| 78 | + if callID == "" || splitID == "" || firstElseID == "" { |
| 79 | + t.Fatal("missing call/split/firstElse") |
| 80 | + } |
| 81 | + |
| 82 | + // Error flow must originate at call and terminate at a merge reachable from split(false). |
| 83 | + // Trace error flow from call: |
| 84 | + var errorDest model.ID |
| 85 | + for _, f := range oc.Flows { |
| 86 | + if f.OriginID == callID && f.IsErrorHandler { |
| 87 | + errorDest = f.DestinationID |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + if errorDest == "" { |
| 92 | + t.Fatal("no error-handler flow from call") |
| 93 | + } |
| 94 | + // errorDest should be a merge that also receives the split false branch and leads to firstElseID |
| 95 | + mergeReachesFirstElse := false |
| 96 | + splitFalseReachesMerge := false |
| 97 | + for _, f := range oc.Flows { |
| 98 | + if f.OriginID == errorDest && f.DestinationID == firstElseID { |
| 99 | + mergeReachesFirstElse = true |
| 100 | + } |
| 101 | + if f.OriginID == splitID && f.DestinationID == errorDest { |
| 102 | + splitFalseReachesMerge = true |
| 103 | + } |
| 104 | + } |
| 105 | + if !mergeReachesFirstElse { |
| 106 | + t.Errorf("merge %s does not flow to first-else activity %s", string(errorDest)[:6], string(firstElseID)[:6]) |
| 107 | + } |
| 108 | + if !splitFalseReachesMerge { |
| 109 | + t.Errorf("split %s (false) does not flow to merge %s", string(splitID)[:6], string(errorDest)[:6]) |
| 110 | + } |
| 111 | +} |
0 commit comments