swf-runtime 1.0.0-alpha9

Runtime engine for Serverless Workflow DSL — execute, validate, and orchestrate workflows
Documentation
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
use super::*;

#[tokio::test]
async fn test_runner_fork_simple() {
    let output = run_workflow_from_yaml(&testdata("fork_simple.yaml"), json!({}))
        .await
        .unwrap();
    // Fork with 2 branches returns array of results, then joinResult flattens
    // Go SDK expects: {"colors": ["red", "blue"]}
    let colors = output["colors"].as_array().unwrap();
    assert!(colors.contains(&json!("red")));
    assert!(colors.contains(&json!("blue")));
}

// === Conditional Logic with input.from ===

#[tokio::test]
async fn test_runner_fork_wait() {
    let output = run_workflow_from_yaml(&testdata("fork_wait.yaml"), json!({}))
        .await
        .unwrap();
    // Non-compete fork with 2 branches returns object with branch names as keys
    assert!(output.is_object());
    assert_eq!(output.as_object().unwrap().len(), 2);
}

// === Emit event ===

#[tokio::test]
async fn test_runner_fork_compete() {
    let output = run_workflow_from_yaml(&testdata("fork_compete.yaml"), json!({}))
        .await
        .unwrap();
    // Compete mode: first branch to complete wins
    assert!(output["patientId"].is_string());
    assert!(output["room"].is_number());
}

// === Fork: Compete with timing ===

#[tokio::test]
async fn test_runner_fork_compete_timed() {
    let output = run_workflow_from_yaml(&testdata("fork_compete_timed.yaml"), json!({}))
        .await
        .unwrap();
    // Fast branch (no wait) should win over slow branch (100ms wait)
    assert_eq!(output["winner"], json!("fast"));
}

// === Export as context ===

#[tokio::test]
async fn test_runner_fork_no_compete() {
    let output = run_workflow_from_yaml(&testdata("fork_no_compete.yaml"), json!({}))
        .await
        .unwrap();
    // Non-compete with multiple branches returns object with branch names as keys
    assert!(output.is_object());
    assert_eq!(output.as_object().unwrap().len(), 2);
}

// === Emit: Simple (no data) ===

#[tokio::test]
async fn test_runner_fork_no_compete_with_names() {
    let output = run_workflow_from_yaml(&testdata("fork_no_compete.yaml"), json!({}))
        .await
        .unwrap();
    // Non-compete returns object with branch names as keys
    assert!(output.is_object());
    assert!(output.get("callNurse").is_some());
    assert!(output.get("callDoctor").is_some());
}

// === HTTP Call: POST with body expression and output.as extracting field ===

#[tokio::test]
async fn test_runner_fork_compete_returns_first() {
    let output = run_workflow_from_yaml(&testdata("fork_compete.yaml"), json!({}))
        .await
        .unwrap();
    // Compete mode returns the first completed branch result
    assert!(output["patientId"].is_string());
    assert!(output["room"].is_number());
}

// === Shell: arguments (static) ===

#[tokio::test]
async fn test_runner_fork_compete_output_as() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-output-as
  version: '0.1.0'
do:
  - race:
      fork:
        compete: true
        branches:
          - branch1:
              do:
                - setResult:
                    set:
                      winner: branch1
          - branch2:
              do:
                - setResult:
                    set:
                      winner: branch2
      output:
        as: .winner
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Compete mode: first branch wins (sequential handle join)
    assert!(output == json!("branch1") || output == json!("branch2"));
}

// === For loop with export accumulating context across iterations ===

#[tokio::test]
async fn test_runner_fork_single_branch() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-single
  version: '0.1.0'
do:
  - singleFork:
      fork:
        compete: false
        branches:
          - onlyBranch:
              do:
                - setResult:
                    set:
                      value: 42
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Single branch non-compete fork returns object with branch name as key
    assert_eq!(output["onlyBranch"]["value"], json!(42));
}

// === Set with null and boolean values ===

#[tokio::test]
async fn test_runner_fork_compete_with_output_as() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-output-as
  version: '0.1.0'
do:
  - race:
      fork:
        compete: true
        branches:
          - fastBranch:
              do:
                - setResult:
                    set:
                      winner: fast
          - slowBranch:
              do:
                - waitABit:
                    wait:
                      milliseconds: 5
                - setResult:
                    set:
                      winner: slow
      output:
        as: .winner
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    assert_eq!(output, json!("fast"));
}

// === For loop with output.as extracting array ===

#[tokio::test]
async fn test_runner_fork_compete_winner_output() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-winner
  version: '0.1.0'
do:
  - race:
      fork:
        compete: true
        branches:
          - fastBranch:
              do:
                - setFast:
                    set:
                      winner: fast
                      time: 10
          - slowBranch:
              do:
                - waitABit:
                    wait: PT0.1S
                - setSlow:
                    set:
                      winner: slow
                      time: 100
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Fast branch should win in compete mode
    assert_eq!(output["winner"], json!("fast"));
}

// === Expression: add (sum array) ===

#[tokio::test]
async fn test_runner_fork_branch_output_as() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-branch-output-as
  version: '0.1.0'
do:
  - parallel:
      fork:
        compete: true
        branches:
          - branchA:
              do:
                - computeA:
                    set:
                      winner: fast
                      time: 10
          - branchB:
              do:
                - waitABit:
                    wait: PT0.1S
                - computeB:
                    set:
                      winner: slow
                      time: 100
      output:
        as: "${ {winner: .winner, elapsed: .time} }"
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Compete mode: fast branch wins
    assert_eq!(output["winner"], json!("fast"));
    assert_eq!(output["elapsed"], json!(10));
}

// === Set with array expression ===

#[tokio::test]
async fn test_runner_fork_try_catch_branch() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-try-catch-branch
  version: '0.1.0'
do:
  - parallel:
      fork:
        compete: false
        branches:
          - safeBranch:
              do:
                - guarded:
                    try:
                      - failStep:
                          raise:
                            error:
                              type: validation
                              title: Branch Error
                              status: 400
                    catch:
                      errors:
                        with:
                          type: validation
                      do:
                        - recover:
                            set:
                              recovered: true
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    assert_eq!(output["safeBranch"]["recovered"], json!(true));
}

// === Raise with all error types ===

#[tokio::test]
async fn test_runner_fork_single_behaves_sequential() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-single-sequential
  version: '0.1.0'
do:
  - parallel:
      fork:
        compete: false
        branches:
          - onlyBranch:
              do:
                - step1:
                    set:
                      value: 42
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    assert_eq!(output["onlyBranch"]["value"], json!(42));
}

// === Set: if condition with expression result ===

#[tokio::test]
async fn test_runner_fork_two_branches() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-two-branches
  version: '0.1.0'
do:
  - parallel:
      fork:
        compete: false
        branches:
          - branchA:
              set:
                a: 1
          - branchB:
              set:
                b: 2
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Non-compete fork with multiple branches returns object with branch names as keys
    assert!(output.is_object());
    assert_eq!(output.as_object().unwrap().len(), 2);
}

// === Try-catch: catch all errors ===

// Fork: compete where one branch errors, other wins
#[tokio::test]
async fn test_runner_fork_compete_error_branch() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-error-branch
  version: '0.1.0'
do:
  - race:
      fork:
        compete: true
        branches:
          - fail:
              raise:
                error:
                  type: runtime
                  title: Fail
                  status: 500
          - succeed:
              set:
                winner: true
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    assert_eq!(output["winner"], json!(true));
}

// Fork: compete where one branch errors, other wins (fast returns, slow fails)
#[tokio::test]
async fn test_runner_fork_compete_speed() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-speed
  version: '0.1.0'
do:
  - race:
      fork:
        compete: true
        branches:
          - fails:
              raise:
                error:
                  type: runtime
                  title: Fail
                  status: 500
          - succeeds:
              set:
                winner: fast
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    assert_eq!(output["winner"], json!("fast"));
}

#[tokio::test]
async fn test_runner_fork_compete_with_set_branches() {
    // Matches Java SDK's fork.yaml - compete mode with set task branches
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-set
  version: '0.1.0'
do:
  - callSomeone:
      fork:
        compete: true
        branches:
          - callNurse:
              set:
                patientId: John
                room: 1
          - callDoctor:
              set:
                patientId: Smith
                room: 2
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Compete mode: one branch wins, output is that branch's result
    // The patientId should be either John or Smith
    let patient_id = output["patientId"].as_str();
    assert!(patient_id == Some("John") || patient_id == Some("Smith"));
}

// === Fork compete with wait tasks (speed-based winner) ===

#[tokio::test]
async fn test_runner_fork_compete_with_timing() {
    // Fork compete where one branch is faster
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-compete-timing
  version: '0.1.0'
do:
  - raceTask:
      fork:
        compete: true
        branches:
          - fastBranch:
              do:
                - quickSet:
                    set:
                      winner: fast
          - slowBranch:
              do:
                - delay:
                    wait: PT0.05S
                - lateSet:
                    set:
                      winner: slow
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Fast branch should win
    assert_eq!(output["winner"], json!("fast"));
}

// === Fork non-compete with named branches (Java SDK fork-no-compete.yaml enhanced) ===

#[tokio::test]
async fn test_runner_fork_non_compete_named_branches() {
    // Java SDK's fork-no-compete.yaml pattern with named branches
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-no-compete-named
  version: '0.1.0'
do:
  - parallelTask:
      fork:
        compete: false
        branches:
          - callNurse:
              set:
                patientId: John
                room: 1
          - callDoctor:
              set:
                patientId: Smith
                room: 2
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Non-compete: both branches run, output is object with branch names as keys
    if output.is_object() {
        assert_eq!(output.as_object().unwrap().len(), 2);
        assert!(output.get("callNurse").is_some());
        assert!(output.get("callDoctor").is_some());
        assert_eq!(output["callNurse"]["patientId"], json!("John"));
        assert_eq!(output["callDoctor"]["patientId"], json!("Smith"));
    }
}

// === Set with conditional expression (ternary-like) (Go SDK TestSetTaskExecutor_ConditionalExpressions hot path) ===

#[tokio::test]
async fn test_runner_fork_simple_non_compete() {
    // Go SDK's fork_simple.yaml - non-compete fork with set tasks
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-simple
  version: '1.0.0'
do:
  - forkColors:
      fork:
        compete: false
        branches:
          - addRed:
              set:
                color: red
          - addBlue:
              set:
                color: blue
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Non-compete fork returns object with branch names as keys
    assert!(output.is_object());
    assert_eq!(output.as_object().unwrap().len(), 2);
}

// === Output.as + Export.as combined (Java SDK pattern) ===

// --- Go SDK: fork_simple.yaml ---
// Fork with set tasks + [.[] | .[]] merge
#[tokio::test]
async fn test_e2e_fork_simple() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-example
  version: '0.1.0'
do:
  - branchColors:
      fork:
        compete: false
        branches:
          - setRed:
              set:
                color1: red
          - setBlue:
              set:
                color2: blue
  - joinResult:
      set:
        colors: "${ [to_entries[].value[]] }"
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Fork returns {setRed: {color1: "red"}, setBlue: {color2: "blue"}}
    // to_entries | .[].value | .[] extracts values (order may vary due to concurrency)
    let colors = output["colors"].as_array().unwrap();
    assert_eq!(colors.len(), 2);
    assert!(colors.contains(&json!("red")));
    assert!(colors.contains(&json!("blue")));
}

// --- Java SDK: fork-no-compete.yaml ---
// Fork with wait + set in branches
#[tokio::test]
async fn test_e2e_fork_no_compete_with_wait() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-example
  version: '0.1.0'
do:
  - callSomeone:
      fork:
        compete: false
        branches:
          - callNurse:
              do:
                - waitForNurse:
                    wait:
                      milliseconds: 5
                - nurseArrived:
                    set:
                      patientId: John
                      room: 1
          - callDoctor:
              do:
                - waitForDoctor:
                    wait:
                      milliseconds: 5
                - doctorArrived:
                    set:
                      patientId: Smith
                      room: 2
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Fork returns object with branch names as keys
    assert!(output.is_object());
    assert!(output.get("callNurse").is_some());
    assert!(output.get("callDoctor").is_some());
    assert_eq!(output["callNurse"]["room"], json!(1));
    assert_eq!(output["callDoctor"]["room"], json!(2));
}

// --- Java SDK: fork-wait.yaml ---
// Fork with wait in branches + set
#[tokio::test]
async fn test_e2e_fork_wait() {
    let yaml_str = r#"
document:
  dsl: '1.0.0'
  namespace: test
  name: fork-wait
  version: '0.1.0'
do:
  - incrParallel:
      fork:
        compete: false
        branches:
          - helloBranch:
              do:
                - waitABit:
                    wait:
                      milliseconds: 5
                - set:
                    set:
                      value: 1
          - byeBranch:
              do:
                - waitABit:
                    wait:
                      milliseconds: 5
                - set:
                    set:
                      value: 2
"#;
    let output = run_workflow_yaml(&yaml_str, json!({})).await.unwrap();
    // Fork returns object with branch names as keys
    assert!(output.is_object());
    assert!(output.get("helloBranch").is_some());
    assert!(output.get("byeBranch").is_some());
    assert_eq!(output["helloBranch"]["value"], json!(1));
    assert_eq!(output["byeBranch"]["value"], json!(2));
}

// =========================================================================
// E2E Integration Tests — Multi-task workflows
// =========================================================================

// === E2E-1: Shell + Set — command result processing ===