forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_microflows_builder_terminal_test.go
More file actions
926 lines (860 loc) · 31.3 KB
/
cmd_microflows_builder_terminal_test.go
File metadata and controls
926 lines (860 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
// SPDX-License-Identifier: Apache-2.0
package executor
import (
"strings"
"testing"
"github.com/mendixlabs/mxcli/mdl/ast"
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/microflows"
)
func TestLastStmtIsReturn_EmptyBody(t *testing.T) {
if lastStmtIsReturn(nil) {
t.Error("empty body must not be terminal")
}
}
func TestLastStmtIsReturn_PlainReturn(t *testing.T) {
body := []ast.MicroflowStatement{&ast.ReturnStmt{}}
if !lastStmtIsReturn(body) {
t.Error("body ending in ReturnStmt must be terminal")
}
}
func TestLastStmtIsReturn_RaiseError(t *testing.T) {
body := []ast.MicroflowStatement{&ast.RaiseErrorStmt{}}
if !lastStmtIsReturn(body) {
t.Error("body ending in RaiseErrorStmt must be terminal")
}
}
func TestLastStmtIsReturn_BreakAndContinue(t *testing.T) {
for _, stmt := range []ast.MicroflowStatement{&ast.BreakStmt{}, &ast.ContinueStmt{}} {
if !lastStmtIsReturn([]ast.MicroflowStatement{stmt}) {
t.Errorf("body ending in %T must be terminal", stmt)
}
}
}
func TestLastStmtIsReturn_IfWithoutElse_NotTerminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
}
if lastStmtIsReturn(body) {
t.Error("IF without ELSE must not be terminal (false path falls through)")
}
}
func TestLastStmtIsReturn_IfBothBranchesReturn_Terminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
},
}
if !lastStmtIsReturn(body) {
t.Error("IF/ELSE where both branches return must be terminal")
}
}
func TestLastStmtIsReturn_IfOnlyThenReturns_NotTerminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.LogStmt{}}, // non-terminal
},
}
if lastStmtIsReturn(body) {
t.Error("IF/ELSE where only THEN terminates must not be terminal")
}
}
func TestLastStmtIsReturn_NestedIfChain_Terminal(t *testing.T) {
// if { return } else if { return } else { return }
inner := &ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
}
outer := &ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
ElseBody: []ast.MicroflowStatement{inner},
}
if !lastStmtIsReturn([]ast.MicroflowStatement{outer}) {
t.Error("else-if chain where every terminal branch returns must be terminal")
}
}
func TestLastStmtIsReturn_RaiseErrorMixed_Terminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.RaiseErrorStmt{}},
},
}
if !lastStmtIsReturn(body) {
t.Error("IF/ELSE with return on one side and raise error on the other must be terminal")
}
}
func TestLastStmtIsReturn_IfBreakContinueBranches_Terminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{
ThenBody: []ast.MicroflowStatement{&ast.ContinueStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.BreakStmt{}},
},
}
if !lastStmtIsReturn(body) {
t.Error("IF/ELSE with continue on one side and break on the other must be terminal")
}
}
func TestLastStmtIsReturn_LoopNotTerminal(t *testing.T) {
// A LOOP whose body only returns is still non-terminal — BREAK can exit.
body := []ast.MicroflowStatement{
&ast.LoopStmt{Body: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
}
if lastStmtIsReturn(body) {
t.Error("LOOP must never be terminal (BREAK path)")
}
}
func TestBuildFlowGraph_LoopIfPreservesBreakAndContinue(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.LoopStmt{
LoopVariable: "Item",
ListVariable: "Items",
Body: []ast.MicroflowStatement{
&ast.IfStmt{
Condition: &ast.VariableExpr{Name: "Changed"},
ThenBody: []ast.MicroflowStatement{&ast.ContinueStmt{}},
ElseBody: []ast.MicroflowStatement{&ast.BreakStmt{}},
},
},
},
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
varTypes: map[string]string{"Items": "List of MyModule.Item"},
declaredVars: map[string]string{"Changed": "Boolean"},
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, nil)
var loop *microflows.LoopedActivity
for _, obj := range oc.Objects {
if l, ok := obj.(*microflows.LoopedActivity); ok {
loop = l
break
}
}
if loop == nil || loop.ObjectCollection == nil {
t.Fatal("expected loop with object collection")
}
var hasBreak, hasContinue bool
for _, obj := range loop.ObjectCollection.Objects {
switch obj.(type) {
case *microflows.BreakEvent:
hasBreak = true
case *microflows.ContinueEvent:
hasContinue = true
}
}
if !hasBreak || !hasContinue {
t.Fatalf("expected break and continue events in loop body, got break=%v continue=%v", hasBreak, hasContinue)
}
}
func TestBuildFlowGraph_ManualWhileTrueContinueUsesBackEdgeMerge(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.WhileStmt{
Condition: &ast.LiteralExpr{Kind: ast.LiteralBoolean, Value: true},
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "retry"}},
&ast.ContinueStmt{},
},
},
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, nil)
var merge *microflows.ExclusiveMerge
for _, obj := range oc.Objects {
switch o := obj.(type) {
case *microflows.LoopedActivity:
t.Fatal("manual while true with continue must not be rebuilt as LoopedActivity")
case *microflows.ContinueEvent:
t.Fatal("manual while true back-edge must not emit ContinueEvent outside a LoopedActivity")
case *microflows.ExclusiveMerge:
merge = o
}
}
if merge == nil {
t.Fatal("expected manual loop header ExclusiveMerge")
}
var backEdges int
for _, flow := range oc.Flows {
if flow.DestinationID == merge.ID {
backEdges++
}
}
if backEdges == 0 {
t.Fatal("expected continue branch to connect back to the manual-loop merge")
}
}
func TestBuildFlowGraph_ManualWhileTrueTerminalDoesNotAddFallthroughEnd(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.WhileStmt{
Condition: &ast.LiteralExpr{Kind: ast.LiteralBoolean, Value: true},
Body: []ast.MicroflowStatement{
&ast.IfStmt{
Condition: &ast.VariableExpr{Name: "Done"},
ThenBody: []ast.MicroflowStatement{&ast.ReturnStmt{Value: &ast.LiteralExpr{Kind: ast.LiteralBoolean, Value: true}}},
},
&ast.ContinueStmt{},
},
},
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
declaredVars: map[string]string{"Done": "Boolean"},
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, &ast.MicroflowReturnType{Type: ast.DataType{Kind: ast.TypeBoolean}})
for _, obj := range oc.Objects {
end, ok := obj.(*microflows.EndEvent)
if !ok {
continue
}
if end.ReturnValue == "" {
t.Fatal("manual while true ending in continue must not add a fallthrough EndEvent without return value")
}
}
}
func TestLastStmtIsReturn_EnumSplitAllBranchesReturn_Terminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.EnumSplitStmt{
Cases: []ast.EnumSplitCase{
{Values: []string{"Open"}, Body: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
{Values: []string{"Closed"}, Body: []ast.MicroflowStatement{&ast.RaiseErrorStmt{}}},
},
ElseBody: []ast.MicroflowStatement{&ast.ReturnStmt{}},
},
}
if !lastStmtIsReturn(body) {
t.Error("ENUM split where all cases and ELSE terminate must be terminal")
}
}
func TestLastStmtIsReturn_EnumSplitWithoutElseAllBranchesReturn_Terminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.EnumSplitStmt{
Cases: []ast.EnumSplitCase{
{Values: []string{"Open"}, Body: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
{Values: []string{"Closed"}, Body: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
},
},
}
if !lastStmtIsReturn(body) {
t.Error("ENUM split without ELSE must be terminal when every emitted case terminates")
}
}
func TestLastStmtIsReturn_EnumSplitWithoutElseNonTerminalCase_NotTerminal(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.EnumSplitStmt{
Cases: []ast.EnumSplitCase{
{Values: []string{"Open"}, Body: []ast.MicroflowStatement{&ast.ReturnStmt{}}},
{Values: []string{"Closed"}, Body: []ast.MicroflowStatement{&ast.LogStmt{}}},
},
},
}
if lastStmtIsReturn(body) {
t.Error("ENUM split without ELSE must not be terminal when any emitted case can continue")
}
}
func TestBuildFlowGraph_NonTerminalCustomHandlerRejoinsContinuation(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "RefreshExternalData"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "refresh failed"}},
},
},
},
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "ContinueWithNextBatch"},
},
}
fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}}
oc := fb.buildFlowGraph(body, nil)
var sourceID, handlerLogID, nextID model.ID
for _, obj := range oc.Objects {
activity, ok := obj.(*microflows.ActionActivity)
if !ok {
continue
}
switch action := activity.Action.(type) {
case *microflows.MicroflowCallAction:
if action.MicroflowCall != nil && action.MicroflowCall.Microflow == "SampleSync.RefreshExternalData" {
sourceID = activity.ID
}
if action.MicroflowCall != nil && action.MicroflowCall.Microflow == "SampleSync.ContinueWithNextBatch" {
nextID = activity.ID
}
case *microflows.LogMessageAction:
if action.LogLevel == "Error" {
handlerLogID = activity.ID
}
}
}
if sourceID == "" || handlerLogID == "" || nextID == "" {
t.Fatalf("expected source call, handler log, and continuation; got source=%q log=%q next=%q", sourceID, handlerLogID, nextID)
}
if !flowPathExists(oc.Flows, handlerLogID, nextID) {
t.Fatal("non-terminal custom error handler should rejoin the next safe continuation")
}
for _, flow := range oc.Flows {
if flow.IsErrorHandler && flow.OriginID == sourceID && flow.DestinationID == nextID {
t.Fatal("custom handler must execute its body before rejoining")
}
}
}
func TestBuildFlowGraph_ConsecutiveCustomHandlersEachRejoinsContinuation(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "RetryFirstBatch"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "first failed"}},
},
},
},
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "RetrySecondBatch"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "second failed"}},
},
},
},
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "RetryFinalBatch"},
},
}
fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}}
oc := fb.buildFlowGraph(body, nil)
callIDs := map[string]model.ID{}
logIDs := map[string]model.ID{}
for _, obj := range oc.Objects {
activity, ok := obj.(*microflows.ActionActivity)
if !ok {
continue
}
switch action := activity.Action.(type) {
case *microflows.MicroflowCallAction:
if action.MicroflowCall != nil {
callIDs[action.MicroflowCall.Microflow] = activity.ID
}
case *microflows.LogMessageAction:
if action.MessageTemplate != nil {
logIDs[action.MessageTemplate.Translations["en_US"]] = activity.ID
}
}
}
firstLog := logIDs["first failed"]
secondLog := logIDs["second failed"]
secondCall := callIDs["SampleSync.RetrySecondBatch"]
finalCall := callIDs["SampleSync.RetryFinalBatch"]
if firstLog == "" || secondLog == "" || secondCall == "" || finalCall == "" {
t.Fatalf("expected all handler logs and continuation calls; logs=%#v calls=%#v", logIDs, callIDs)
}
if !flowPathExists(oc.Flows, firstLog, secondCall) {
t.Fatal("first pending handler must rejoin before the second call instead of being overwritten")
}
if !flowPathExists(oc.Flows, secondLog, finalCall) {
t.Fatal("second pending handler must rejoin before the final continuation")
}
}
func TestBodyHasContinuingCustomErrorHandler_CoversActionStatements(t *testing.T) {
continuingHandler := &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "failed"}},
},
}
tests := []struct {
name string
stmt ast.MicroflowStatement
}{
{name: "retrieve", stmt: &ast.RetrieveStmt{ErrorHandling: continuingHandler}},
{name: "create object", stmt: &ast.CreateObjectStmt{ErrorHandling: continuingHandler}},
{name: "commit", stmt: &ast.MfCommitStmt{ErrorHandling: continuingHandler}},
{name: "delete", stmt: &ast.DeleteObjectStmt{ErrorHandling: continuingHandler}},
{name: "call microflow", stmt: &ast.CallMicroflowStmt{ErrorHandling: continuingHandler}},
{name: "call nanoflow", stmt: &ast.CallNanoflowStmt{ErrorHandling: continuingHandler}},
{name: "call java action", stmt: &ast.CallJavaActionStmt{ErrorHandling: continuingHandler}},
{name: "call javascript action", stmt: &ast.CallJavaScriptActionStmt{ErrorHandling: continuingHandler}},
{name: "call web service", stmt: &ast.CallWebServiceStmt{ErrorHandling: continuingHandler}},
{name: "execute database query", stmt: &ast.ExecuteDatabaseQueryStmt{ErrorHandling: continuingHandler}},
{name: "call external action", stmt: &ast.CallExternalActionStmt{ErrorHandling: continuingHandler}},
{name: "download file", stmt: &ast.DownloadFileStmt{ErrorHandling: continuingHandler}},
{name: "rest call", stmt: &ast.RestCallStmt{ErrorHandling: continuingHandler}},
{name: "send rest request", stmt: &ast.SendRestRequestStmt{ErrorHandling: continuingHandler}},
{name: "import mapping", stmt: &ast.ImportFromMappingStmt{ErrorHandling: continuingHandler}},
{name: "export mapping", stmt: &ast.ExportToMappingStmt{ErrorHandling: continuingHandler}},
{name: "transform json", stmt: &ast.TransformJsonStmt{ErrorHandling: continuingHandler}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !bodyHasContinuingCustomErrorHandler([]ast.MicroflowStatement{tt.stmt}) {
t.Fatalf("%T must be visible to continuing custom handler detection", tt.stmt)
}
})
}
}
func TestStatementReferencesVar_CoversActionStatementInputs(t *testing.T) {
ref := &ast.VariableExpr{Name: "SkippedOutput"}
tests := []struct {
name string
stmt ast.MicroflowStatement
}{
{name: "call nanoflow", stmt: &ast.CallNanoflowStmt{Arguments: []ast.CallArgument{{Name: "value", Value: ref}}}},
{name: "call javascript action", stmt: &ast.CallJavaScriptActionStmt{Arguments: []ast.CallArgument{{Name: "value", Value: ref}}}},
{name: "call web service timeout", stmt: &ast.CallWebServiceStmt{Timeout: ref}},
{name: "execute database query argument", stmt: &ast.ExecuteDatabaseQueryStmt{Arguments: []ast.CallArgument{{Name: "value", Value: ref}}}},
{name: "execute database query connection argument", stmt: &ast.ExecuteDatabaseQueryStmt{ConnectionArguments: []ast.CallArgument{{Name: "value", Value: ref}}}},
{name: "call external action", stmt: &ast.CallExternalActionStmt{Arguments: []ast.CallArgument{{Name: "value", Value: ref}}}},
{name: "rest auth", stmt: &ast.RestCallStmt{Auth: &ast.RestAuth{Username: ref}}},
{name: "send rest request parameter", stmt: &ast.SendRestRequestStmt{Parameters: []ast.SendRestParamDef{{Name: "value", Expression: "$SkippedOutput/Name"}}}},
{name: "send rest request body", stmt: &ast.SendRestRequestStmt{BodyVariable: "SkippedOutput"}},
{name: "import mapping", stmt: &ast.ImportFromMappingStmt{SourceVariable: "SkippedOutput"}},
{name: "export mapping", stmt: &ast.ExportToMappingStmt{SourceVariable: "SkippedOutput"}},
{name: "transform json", stmt: &ast.TransformJsonStmt{InputVariable: "SkippedOutput"}},
{name: "download file", stmt: &ast.DownloadFileStmt{FileDocument: "SkippedOutput"}},
{name: "validation feedback target", stmt: &ast.ValidationFeedbackStmt{AttributePath: &ast.AttributePathExpr{Variable: "SkippedOutput", Path: []string{"Name"}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !statementReferencesVar(tt.stmt, "SkippedOutput") {
t.Fatalf("%T must expose input variable references for custom handler routing", tt.stmt)
}
})
}
}
func TestBuildFlowGraph_EmptyOutputHandlerTerminatesBeforeOutputDependentTail(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallJavaActionStmt{
OutputVariable: "ProcessedCount",
ActionName: ast.QualifiedName{Module: "SampleMigration", Name: "CountProcessedItems"},
ErrorHandling: &ast.ErrorHandlingClause{Type: ast.ErrorHandlingCustomWithoutRollback},
},
&ast.LogStmt{
Level: ast.LogInfo,
Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "processed {1}"},
Template: []ast.TemplateParam{{
Index: 1,
Value: &ast.VariableExpr{Name: "ProcessedCount"},
}},
},
}
fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}}
oc := fb.buildFlowGraph(body, nil)
var javaID, logID model.ID
endIDs := map[model.ID]bool{}
for _, obj := range oc.Objects {
switch o := obj.(type) {
case *microflows.ActionActivity:
switch action := o.Action.(type) {
case *microflows.JavaActionCallAction:
if action.ResultVariableName == "ProcessedCount" {
javaID = o.ID
}
case *microflows.LogMessageAction:
if action.MessageTemplate != nil && action.MessageTemplate.Translations["en_US"] == "processed {1}" {
logID = o.ID
}
}
case *microflows.EndEvent:
endIDs[o.ID] = true
}
}
if javaID == "" || logID == "" || len(endIDs) == 0 {
t.Fatalf("expected java action, output-dependent log, and end event; got java=%q log=%q ends=%v", javaID, logID, endIDs)
}
var errorFlowTerminates bool
for _, flow := range oc.Flows {
if !flow.IsErrorHandler || flow.OriginID != javaID {
continue
}
if flowPathExists(oc.Flows, flow.DestinationID, logID) {
t.Fatal("empty output handler must not rejoin before a statement that reads the missing output")
}
for endID := range endIDs {
if flow.DestinationID == endID || flowPathExists(oc.Flows, flow.DestinationID, endID) {
errorFlowTerminates = true
}
}
}
if !errorFlowTerminates {
t.Fatal("empty output handler should terminate at an EndEvent before the output-dependent tail")
}
}
func TestBuildFlowGraph_OutputHandlerTerminatesBeforeDeclareReferencingOutput(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
OutputVariable: "CreatedRecord",
MicroflowName: ast.QualifiedName{Module: "SampleSync", Name: "CreateRecord"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "create failed"}},
},
},
},
&ast.DeclareStmt{
Variable: "SuccessMessage",
Type: ast.DataType{Kind: ast.TypeString},
InitialValue: &ast.BinaryExpr{
Left: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "Created "},
Operator: "+",
Right: &ast.AttributePathExpr{Variable: "CreatedRecord", Path: []string{"Name"}},
},
},
}
if !statementReferencesVar(body[1], "CreatedRecord") {
t.Fatal("DECLARE initial values must be visible to custom handler skip-var routing")
}
fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}}
oc := fb.buildFlowGraph(body, nil)
var callID, declareID model.ID
endIDs := map[model.ID]bool{}
for _, obj := range oc.Objects {
switch o := obj.(type) {
case *microflows.ActionActivity:
switch action := o.Action.(type) {
case *microflows.MicroflowCallAction:
if action.ResultVariableName == "CreatedRecord" {
callID = o.ID
}
case *microflows.CreateVariableAction:
if action.VariableName == "SuccessMessage" {
declareID = o.ID
}
}
case *microflows.EndEvent:
endIDs[o.ID] = true
}
}
if callID == "" || declareID == "" || len(endIDs) == 0 {
t.Fatalf("expected call, output-dependent declare, and end event; got call=%q declare=%q ends=%v", callID, declareID, endIDs)
}
var errorFlowTerminates bool
for _, flow := range oc.Flows {
if !flow.IsErrorHandler || flow.OriginID != callID {
continue
}
if flowPathExists(oc.Flows, flow.DestinationID, declareID) {
t.Fatal("custom handler must not rejoin before a DECLARE that reads the missing output")
}
for endID := range endIDs {
if flow.DestinationID == endID || flowPathExists(oc.Flows, flow.DestinationID, endID) {
errorFlowTerminates = true
}
}
}
if !errorFlowTerminates {
t.Fatal("custom handler should terminate at an EndEvent before the output-dependent declare")
}
}
func TestBuildFlowGraph_OutputHandlerInReturningBranchSkipsDerivedSuccessTail(t *testing.T) {
successMessage := &ast.VariableExpr{Name: "SuccessMessage"}
body := []ast.MicroflowStatement{
&ast.DeclareStmt{
Variable: "ErrorMessage",
Type: ast.DataType{Kind: ast.TypeString},
InitialValue: &ast.LiteralExpr{Kind: ast.LiteralString, Value: ""},
},
&ast.IfStmt{
Condition: &ast.VariableExpr{Name: "CanCreate"},
ThenBody: []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
OutputVariable: "CreatedRecord",
MicroflowName: ast.QualifiedName{Module: "SampleService", Name: "CreateRecord"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustomWithoutRollback,
Body: []ast.MicroflowStatement{
&ast.MfSetStmt{
Target: "ErrorMessage",
Value: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "create failed"},
},
},
},
},
&ast.DeclareStmt{
Variable: "SuccessMessage",
Type: ast.DataType{Kind: ast.TypeString},
InitialValue: &ast.BinaryExpr{
Left: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "Created "},
Operator: "+",
Right: &ast.AttributePathExpr{Variable: "CreatedRecord", Path: []string{"Name"}},
},
},
&ast.LogStmt{
Level: ast.LogInfo,
Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "{1}"},
Template: []ast.TemplateParam{
{Index: 1, Value: successMessage},
},
},
&ast.ShowMessageStmt{
Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "{1}"},
Type: "Information",
TemplateArgs: []ast.Expression{successMessage},
},
&ast.ClosePageStmt{},
&ast.ReturnStmt{},
},
ElseBody: []ast.MicroflowStatement{
&ast.MfSetStmt{
Target: "ErrorMessage",
Value: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "not found"},
},
},
},
&ast.LogStmt{
Level: ast.LogError,
Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "{1}"},
Template: []ast.TemplateParam{
{Index: 1, Value: &ast.VariableExpr{Name: "ErrorMessage"}},
},
},
&ast.ShowMessageStmt{
Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "{1}"},
Type: "Error",
TemplateArgs: []ast.Expression{&ast.VariableExpr{Name: "ErrorMessage"}},
},
&ast.ReturnStmt{},
}
if !statementReferencesVar(body[1].(*ast.IfStmt).ThenBody[3], "SuccessMessage") {
t.Fatal("SHOW MESSAGE arguments must be visible to custom handler skip-var routing")
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
declaredVars: map[string]string{"CanCreate": "Boolean"},
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, nil)
var callID, handlerSetID, successDeclareID, errorLogID model.ID
for _, obj := range oc.Objects {
activity, ok := obj.(*microflows.ActionActivity)
if !ok {
continue
}
switch action := activity.Action.(type) {
case *microflows.MicroflowCallAction:
if action.ResultVariableName == "CreatedRecord" {
callID = activity.ID
}
case *microflows.ChangeVariableAction:
if action.VariableName == "ErrorMessage" && strings.Contains(action.Value, "create failed") {
handlerSetID = activity.ID
}
case *microflows.CreateVariableAction:
if action.VariableName == "SuccessMessage" {
successDeclareID = activity.ID
}
case *microflows.LogMessageAction:
if action.LogLevel == "Error" {
errorLogID = activity.ID
}
}
}
if callID == "" || handlerSetID == "" || successDeclareID == "" || errorLogID == "" {
t.Fatalf("expected source call, handler set, success declare, and error log; got call=%q handler=%q declare=%q errorLog=%q", callID, handlerSetID, successDeclareID, errorLogID)
}
if !flowPathExists(oc.Flows, callID, handlerSetID) {
t.Fatal("source call must connect to the custom handler body")
}
if flowPathExists(oc.Flows, handlerSetID, successDeclareID) {
t.Fatal("custom handler must skip the output-derived success tail")
}
if !flowPathExists(oc.Flows, handlerSetID, errorLogID) {
t.Fatal("custom handler in a returning branch must rejoin at the shared safe continuation")
}
}
func TestBuildFlowGraph_EmptyNoOutputHandlerRejoinsAtNextAction(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "SampleMigration", Name: "RefreshCache"},
ErrorHandling: &ast.ErrorHandlingClause{Type: ast.ErrorHandlingCustomWithoutRollback},
},
&ast.CallJavaActionStmt{
OutputVariable: "ProcessedCount",
ActionName: ast.QualifiedName{Module: "SampleMigration", Name: "CountProcessedItems"},
},
}
fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}}
oc := fb.buildFlowGraph(body, nil)
var callID, javaID model.ID
for _, obj := range oc.Objects {
activity, ok := obj.(*microflows.ActionActivity)
if !ok {
continue
}
switch action := activity.Action.(type) {
case *microflows.MicroflowCallAction:
if action.MicroflowCall != nil && action.MicroflowCall.Microflow == "SampleMigration.RefreshCache" {
callID = activity.ID
}
case *microflows.JavaActionCallAction:
if action.ResultVariableName == "ProcessedCount" {
javaID = activity.ID
}
}
}
if callID == "" || javaID == "" {
t.Fatalf("expected no-output call and output-producing java action; got call=%q java=%q", callID, javaID)
}
for _, flow := range oc.Flows {
if flow.IsErrorHandler && flow.OriginID == callID && flowPathExists(oc.Flows, flow.DestinationID, javaID) {
return
}
}
t.Fatal("empty no-output handler should rejoin at the next action")
}
func TestBuildFlowGraph_ErrorHandlerEmptyElseKeepsFalseCaseOnRejoin(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "Synthetic", Name: "PatchRemoteState"},
ErrorHandling: &ast.ErrorHandlingClause{
Type: ast.ErrorHandlingCustom,
Body: []ast.MicroflowStatement{
&ast.IfStmt{
Condition: &ast.VariableExpr{Name: "HasResponse"},
HasElse: true,
ThenBody: []ast.MicroflowStatement{
&ast.ReturnStmt{Value: &ast.VariableExpr{Name: "ErrorResponse"}},
},
},
},
},
},
&ast.CallMicroflowStmt{
MicroflowName: ast.QualifiedName{Module: "Synthetic", Name: "ContinueAfterPatch"},
},
&ast.ReturnStmt{Value: &ast.LiteralExpr{Kind: ast.LiteralNull}},
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
declaredVars: map[string]string{"HasResponse": "Boolean", "ErrorResponse": "Synthetic.Error"},
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, &ast.MicroflowReturnType{Type: ast.DataType{Kind: ast.TypeEntity, EntityRef: &ast.QualifiedName{Module: "Synthetic", Name: "Error"}}})
var splitID, continuationID model.ID
for _, obj := range oc.Objects {
switch o := obj.(type) {
case *microflows.ExclusiveSplit:
if condition, ok := o.SplitCondition.(*microflows.ExpressionSplitCondition); ok && condition.Expression == "$HasResponse" {
splitID = o.ID
}
case *microflows.ActionActivity:
if action, ok := o.Action.(*microflows.MicroflowCallAction); ok && action.MicroflowCall != nil && action.MicroflowCall.Microflow == "Synthetic.ContinueAfterPatch" {
continuationID = o.ID
}
}
}
if splitID == "" || continuationID == "" {
t.Fatalf("expected error-handler split and continuation call, got split=%q continuation=%q", splitID, continuationID)
}
for _, flow := range oc.Flows {
if flow.OriginID != splitID || flowCaseString(flow.CaseValue) != "false" {
continue
}
if flow.DestinationID == continuationID || flowPathExists(oc.Flows, flow.DestinationID, continuationID) {
return
}
}
t.Fatal("expected deferred custom error-handler ELSE path to retain CaseValue=false when rejoining")
}
func TestBuildFlowGraph_ExplicitEmptyElseProvidesFalseContinuation(t *testing.T) {
body := []ast.MicroflowStatement{
&ast.IfStmt{
Condition: &ast.VariableExpr{Name: "HasResponse"},
HasElse: true,
ThenBody: []ast.MicroflowStatement{
&ast.ReturnStmt{Value: &ast.VariableExpr{Name: "ErrorResponse"}},
},
},
&ast.ReturnStmt{Value: &ast.LiteralExpr{Kind: ast.LiteralNull}},
}
fb := &flowBuilder{
posX: 100,
posY: 100,
spacing: HorizontalSpacing,
declaredVars: map[string]string{"HasResponse": "Boolean", "ErrorResponse": "Synthetic.Error"},
measurer: &layoutMeasurer{},
}
oc := fb.buildFlowGraph(body, &ast.MicroflowReturnType{Type: ast.DataType{Kind: ast.TypeEntity, EntityRef: &ast.QualifiedName{Module: "Synthetic", Name: "Error"}}})
var splitID, emptyEndID model.ID
for _, obj := range oc.Objects {
switch o := obj.(type) {
case *microflows.ExclusiveSplit:
splitID = o.ID
case *microflows.EndEvent:
if o.ReturnValue == "empty" {
emptyEndID = o.ID
}
}
}
if splitID == "" || emptyEndID == "" {
t.Fatalf("expected split and empty return end event, got split=%q emptyEnd=%q", splitID, emptyEndID)
}
for _, flow := range oc.Flows {
if flow.OriginID != splitID || flowCaseString(flow.CaseValue) != "false" {
continue
}
if flowPathExists(oc.Flows, flow.DestinationID, emptyEndID) {
return
}
}
t.Fatal("expected explicit empty else to produce a false-path continuation")
}
func flowCaseString(caseValue microflows.CaseValue) string {
switch c := caseValue.(type) {
case microflows.EnumerationCase:
return c.Value
case *microflows.EnumerationCase:
if c != nil {
return c.Value
}
case *microflows.ExpressionCase:
if c != nil {
return c.Expression
}
}
return ""
}
func flowPathExists(flows []*microflows.SequenceFlow, startID, targetID model.ID) bool {
if startID == "" || targetID == "" {
return false
}
seen := map[model.ID]bool{}
queue := []model.ID{startID}
for len(queue) > 0 {
id := queue[0]
queue = queue[1:]
if id == targetID {
return true
}
if seen[id] {
continue
}
seen[id] = true
for _, flow := range flows {
if flow.OriginID == id && !seen[flow.DestinationID] {
queue = append(queue, flow.DestinationID)
}
}
}
return false
}