|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Regression tests for anchor preservation inside IF branches — the original |
| 4 | +// @anchor implementation only handled the top-level flow between statements |
| 5 | +// at the microflow body level. Anchors on statements inside THEN/ELSE bodies |
| 6 | +// (including the flow between a branch's first statement and its successors, |
| 7 | +// and the flow leaving the last branch statement to the merge) were silently |
| 8 | +// dropped, so real-world microflows like the attempt #35 repro case lost |
| 9 | +// every vertical anchor on roundtrip. |
| 10 | +package executor |
| 11 | + |
| 12 | +import ( |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 16 | +) |
| 17 | + |
| 18 | +// buildWithAnchors is a test helper that builds the flow graph for a simple |
| 19 | +// microflow body and returns the collection for inspection. |
| 20 | +func buildWithAnchors(body []ast.MicroflowStatement) (oc *struct { |
| 21 | + Flows []anchorFlow |
| 22 | + Objects int |
| 23 | +}) { |
| 24 | + fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing} |
| 25 | + col := fb.buildFlowGraph(body, nil) |
| 26 | + oc = &struct { |
| 27 | + Flows []anchorFlow |
| 28 | + Objects int |
| 29 | + }{ |
| 30 | + Objects: len(col.Objects), |
| 31 | + } |
| 32 | + for _, f := range col.Flows { |
| 33 | + oc.Flows = append(oc.Flows, anchorFlow{ |
| 34 | + OriginIdx: f.OriginConnectionIndex, |
| 35 | + DestIdx: f.DestinationConnectionIndex, |
| 36 | + }) |
| 37 | + } |
| 38 | + return oc |
| 39 | +} |
| 40 | + |
| 41 | +type anchorFlow struct { |
| 42 | + OriginIdx int |
| 43 | + DestIdx int |
| 44 | +} |
| 45 | + |
| 46 | +// hasFlow returns true when at least one flow has the given anchor pair. |
| 47 | +func hasFlow(flows []anchorFlow, origin, dest int) bool { |
| 48 | + for _, f := range flows { |
| 49 | + if f.OriginIdx == origin && f.DestIdx == dest { |
| 50 | + return true |
| 51 | + } |
| 52 | + } |
| 53 | + return false |
| 54 | +} |
| 55 | + |
| 56 | +// countFlows counts how many flows have the given anchor pair. |
| 57 | +func countFlows(flows []anchorFlow, origin, dest int) int { |
| 58 | + n := 0 |
| 59 | + for _, f := range flows { |
| 60 | + if f.OriginIdx == origin && f.DestIdx == dest { |
| 61 | + n++ |
| 62 | + } |
| 63 | + } |
| 64 | + return n |
| 65 | +} |
| 66 | + |
| 67 | +func TestBuilder_AnchorInsideElseBranch(t *testing.T) { |
| 68 | + // Reproduces the pattern from attempt #35: |
| 69 | + // if cond then { set ... } |
| 70 | + // else { |
| 71 | + // @anchor(from: bottom, to: top) |
| 72 | + // log ... |
| 73 | + // @anchor(to: top) |
| 74 | + // return empty |
| 75 | + // } |
| 76 | + body := []ast.MicroflowStatement{ |
| 77 | + &ast.IfStmt{ |
| 78 | + Condition: &ast.LiteralExpr{Kind: ast.LiteralBoolean, Value: true}, |
| 79 | + ThenBody: []ast.MicroflowStatement{ |
| 80 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "a"}}, |
| 81 | + }, |
| 82 | + ElseBody: []ast.MicroflowStatement{ |
| 83 | + &ast.LogStmt{ |
| 84 | + Level: ast.LogInfo, |
| 85 | + Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "b"}, |
| 86 | + Annotations: &ast.ActivityAnnotations{ |
| 87 | + Anchor: &ast.FlowAnchors{From: ast.AnchorSideBottom, To: ast.AnchorSideTop}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + &ast.ReturnStmt{ |
| 91 | + Value: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "done"}, |
| 92 | + Annotations: &ast.ActivityAnnotations{ |
| 93 | + Anchor: &ast.FlowAnchors{To: ast.AnchorSideTop, From: ast.AnchorSideUnset}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + oc := buildWithAnchors(body) |
| 101 | + |
| 102 | + // Two distinct Bottom→Top flows must exist: |
| 103 | + // 1. split → log (from the user's @anchor on the log statement) |
| 104 | + // 2. log → return (propagating the log's From=Bottom and the return's To=Top) |
| 105 | + // A single hasFlow check would pass with just one match, so count explicitly |
| 106 | + // to pin the regression — see ako review note on TestBuilder_AnchorInsideElseBranch. |
| 107 | + if got := countFlows(oc.Flows, AnchorBottom, AnchorTop); got != 2 { |
| 108 | + t.Errorf("expected 2 Bottom→Top flows (split→log and log→return), got %d: %+v", got, oc.Flows) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestBuilder_AnchorToTopOnReturnPreservedInsideElse(t *testing.T) { |
| 113 | + // Minimal case: single-statement ELSE whose only statement is a RETURN |
| 114 | + // carrying @anchor(to: top). The flow from the split to that return's |
| 115 | + // EndEvent must land on DestinationConnectionIndex = AnchorTop. |
| 116 | + body := []ast.MicroflowStatement{ |
| 117 | + &ast.IfStmt{ |
| 118 | + Condition: &ast.LiteralExpr{Kind: ast.LiteralBoolean, Value: true}, |
| 119 | + ThenBody: []ast.MicroflowStatement{ |
| 120 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "a"}}, |
| 121 | + }, |
| 122 | + ElseBody: []ast.MicroflowStatement{ |
| 123 | + &ast.ReturnStmt{ |
| 124 | + Value: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "no"}, |
| 125 | + Annotations: &ast.ActivityAnnotations{ |
| 126 | + Anchor: &ast.FlowAnchors{To: ast.AnchorSideTop, From: ast.AnchorSideUnset}, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + oc := buildWithAnchors(body) |
| 134 | + |
| 135 | + // Default downward flow from split has OriginConnectionIndex=Bottom; with |
| 136 | + // @anchor(to: top) on the return, DestinationConnectionIndex must be Top. |
| 137 | + if !hasFlow(oc.Flows, AnchorBottom, AnchorTop) { |
| 138 | + t.Errorf("expected split→return flow with Bottom→Top, got %+v", oc.Flows) |
| 139 | + } |
| 140 | +} |
0 commit comments