ui-automata 0.1.0

Declarative Workflow Engine for UI Automation
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
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/// Integration tests for step-level flow control: `on_failure` and `on_success`.
mod common;
use common::*;

// ── on_failure: continue ──────────────────────────────────────────────────────

/// `on_failure: continue` — a failing step is logged but execution continues to
/// the next step, and the phase (and workflow) ultimately succeeds.
#[test]
fn on_failure_continue_proceeds_to_next_step() {
    // Step 1 times out; with on_failure:continue it must not abort the phase.
    // Step 2 succeeds — proving execution reached it.
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: this step will time out
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        timeout: 50ms
        on_failure: continue
      - intent: this step must still run
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"],
    );
}

/// Multiple `on_failure: continue` steps — each timeout is swallowed; the final
/// succeeding step carries the phase to completion.
#[test]
fn on_failure_continue_multiple_steps_all_continue() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: step 1 times out
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=Missing1]"
        timeout: 50ms
        on_failure: continue
      - intent: step 2 times out
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=Missing2]"
        timeout: 50ms
        on_failure: continue
      - intent: step 3 succeeds
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"],
    );
}

/// Default `on_failure: abort` behaviour — timeout aborts the phase.
#[test]
fn on_failure_abort_is_default() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: this step times out with default on_failure
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        timeout: 50ms
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_err(), "default abort must propagate error");
    assert_eq!(
        event_names(&events),
        ["started:main", "failed:main", "Failed"],
    );
}

// ── on_success: return_phase ──────────────────────────────────────────────────

/// `on_success: return_phase` — after the step succeeds, the remaining steps
/// in the phase are skipped and the phase completes normally.
/// Step 2 would time out if reached — proving it was skipped.
#[test]
fn on_success_return_phase_stops_remaining_steps() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: early return
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        on_success: return_phase
      - intent: this must NOT run
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        timeout: 50ms
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"],
    );
}

/// `on_success: return_phase` only stops the current phase; subsequent phases run normally.
#[test]
fn on_success_return_phase_subsequent_phases_run() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: phase_a
    mount: [app]
    steps:
      - intent: return early from phase_a
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        on_success: return_phase
  - name: phase_b
    steps: []
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        [
            "started:phase_a",
            "completed:phase_a",
            "started:phase_b",
            "completed:phase_b",
            "Completed",
        ],
    );
}

// ── on_failure: fallback ──────────────────────────────────────────────────────

/// `on_failure: fallback` — primary action times out; fallback action runs and
/// satisfies the expect condition; phase completes successfully.
#[test]
fn on_failure_fallback_runs_when_primary_times_out() {
    // Primary expect looks for a button that doesn't exist (times out).
    // Fallback NoOp is followed by re-polling the App window, which exists.
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: primary times out, fallback satisfies expect
        action:
          type: NoOp
        fallback:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        timeout: 50ms
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"],
    );
}

/// `on_failure: fallback` — primary times out AND fallback also cannot satisfy
/// expect; the step (and phase) fails.
#[test]
fn on_failure_fallback_fails_when_fallback_expect_unsatisfied() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: both primary and fallback timeout — phase fails
        action:
          type: NoOp
        fallback:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_err(), "fallback that cannot fix expect must fail");
    assert_eq!(
        event_names(&events),
        ["started:main", "failed:main", "Failed"],
    );
}

/// `on_failure: fallback` parse — ensure the YAML deserialises without error
/// and the workflow runs (schema round-trip check).
#[test]
fn on_failure_fallback_parses_correctly() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: fallback with explicit scope
        action:
          type: NoOp
        fallback:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        timeout: 50ms
"#;
    // parse() panics on invalid YAML — success here confirms deserialization works.
    parse(yaml);
}

/// Combined: step 1 fails → continue; step 2 succeeds → return_phase; step 3 is skipped.
#[test]
fn on_failure_continue_then_on_success_return_phase() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: step 1 fails, continue
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=Missing]"
        timeout: 50ms
        on_failure: continue
      - intent: step 2 succeeds, return_phase
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        on_success: return_phase
      - intent: step 3 must not run
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=AlsoMissing]"
        timeout: 50ms
"#;
    let desktop = ui_automata::mock::mock_desktop_from_yaml(
        r#"
role: window
name: App
"#,
    );
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"],
    );
}

// ── action error propagation ──────────────────────────────────────────────────

/// `expect: Always` does NOT swallow action errors — the step must fail when
/// the action fails, even though `Always` is immediately satisfied.
#[test]
fn action_error_with_always_aborts_step() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: action fails, Always must not hide it
        action:
          type: Click
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        expect:
          type: Always
        timeout: 50ms
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_err(),
        "action error must propagate even with Always"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "failed:main", "Failed"]
    );
}

/// `on_failure: continue` opts out of action-error propagation: the step
/// continues even when the action fails and expect is Always.
#[test]
fn action_error_always_on_failure_continue_ok() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: action fails but on_failure=continue allows it
        action:
          type: Click
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        expect:
          type: Always
        on_failure: continue
      - intent: must still run
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

/// When the action errors and `expect: Always`, the retry policy fires.
/// Exhausting retries ultimately fails the step.
#[test]
fn action_error_with_always_retries_then_fails() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: action always fails, retry exhausted
        action:
          type: Click
          scope: app
          selector: ">> [role=button][name=DoesNotExist]"
        expect:
          type: Always
        timeout: 50ms
        retry:
          fixed:
            count: 2
            delay: 0s
"#;
    let desktop = app_desktop();
    let (result, _) = run(yaml, desktop);
    assert!(result.is_err(), "exhausted retries must still fail");
    let msg = result.unwrap_err().to_string();
    assert!(msg.contains("timed out after 3 attempt"), "got: {msg}");
}

/// Regression: `last_action_error` must be reset at the start of each retry
/// iteration. If attempt 1 errors but attempt 2 succeeds, the step must
/// succeed — the stale error from attempt 1 must not taint attempt 2.
#[test]
fn action_error_on_retry_1_then_ok_on_retry_2_succeeds() {
    use ui_automata::mock::MockElement;

    let trigger = MockElement::leaf("button", "Trigger");
    trigger.kill(); // attempt 1: element dead → find_required returns None → action error

    let desktop = ui_automata::mock::MockDesktop::new(vec![MockElement::parent(
        "window",
        "App",
        vec![trigger.clone()],
    )]);

    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: element dead on attempt 1, alive on attempt 2
        action:
          type: Click
          scope: app
          selector: ">> [role=button][name=Trigger]"
        expect:
          type: Always
        timeout: 50ms
        retry:
          fixed:
            count: 1
            delay: 200ms
"#;

    // Revive the element midway through the retry delay so attempt 2 finds it.
    let t = trigger.clone();
    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(50));
        t.revive();
    });

    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_ok(),
        "attempt 2 succeeds → step must succeed, not carry stale error: {result:?}"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

// ── step precondition ─────────────────────────────────────────────────────────

/// When the step precondition is satisfied the step runs normally.
#[test]
fn step_precondition_satisfied_step_runs() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: precondition true, step must run
        precondition:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

/// When the step precondition is not satisfied the step is silently skipped.
/// A following step that would time out if reached is used to prove the
/// skipped step did not block execution.
#[test]
fn step_precondition_not_satisfied_step_skipped() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: precondition false → skip; would fail if it ran
        precondition:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=AlsoNeverExists]"
        timeout: 50ms
      - intent: must still run after skip
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

// ── timeout-driven retry (no action error) ────────────────────────────────────

/// When the action succeeds but the expect condition is never satisfied, the
/// step times out. With a retry policy the step is retried; exhausting all
/// retries results in failure with the attempt count in the error message.
#[test]
fn timeout_retry_exhausted_fails_with_attempt_count() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: action ok but expect never satisfied
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
        retry:
          fixed:
            count: 2
            delay: 0s
"#;
    let desktop = app_desktop();
    let (result, _) = run(yaml, desktop);
    assert!(result.is_err(), "must fail when expect never satisfied");
    let msg = result.unwrap_err().to_string();
    assert!(msg.contains("timed out after 3 attempt"), "got: {msg}");
}

// ── recovery handlers ─────────────────────────────────────────────────────────

/// `resume: skip_step` — when recovery fires the failing step is silently
/// skipped and execution advances to the next step.
#[test]
fn recovery_skip_step_skips_and_continues() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
recovery_handlers:
  skip_it:
    trigger:
      type: ElementFound
      scope: app
      selector: "[name=App]"
    actions: []
    resume: skip_step
phases:
  - name: main
    mount: [app]
    recovery:
      handlers: [skip_it]
    steps:
      - intent: will time out, recovery skips it
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
      - intent: must run after skip
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_ok(),
        "skip_step recovery must allow phase to complete: {result:?}"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

/// `resume: fail` — when recovery fires it immediately fails the step.
#[test]
fn recovery_fail_resume_aborts_step() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
recovery_handlers:
  fail_it:
    trigger:
      type: ElementFound
      scope: app
      selector: "[name=App]"
    actions: []
    resume: fail
phases:
  - name: main
    mount: [app]
    recovery:
      handlers: [fail_it]
    steps:
      - intent: will time out, recovery fails it
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(result.is_err(), "fail resume must abort the step");
    assert_eq!(
        event_names(&events),
        ["started:main", "failed:main", "Failed"]
    );
}

/// `resume: retry_step` — recovery fires, re-runs the step. When the element
/// appears between the recovery and the retry the step succeeds.
#[test]
fn recovery_retry_step_reruns_step_and_succeeds() {
    use ui_automata::mock::MockElement;

    let btn = MockElement::leaf("button", "Target");
    btn.kill(); // not present on first attempt → timeout → recovery fires

    let desktop = ui_automata::mock::MockDesktop::new(vec![MockElement::parent(
        "window",
        "App",
        vec![btn.clone()],
    )]);

    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
recovery_handlers:
  revive_handler:
    trigger:
      type: ElementFound
      scope: app
      selector: "[name=App]"
    actions: []
    resume: retry_step
phases:
  - name: main
    mount: [app]
    recovery:
      handlers: [revive_handler]
    steps:
      - intent: target missing on attempt 1; recovery retries; target present on retry
        action:
          type: Click
          scope: app
          selector: ">> [role=button][name=Target]"
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=Target]"
        timeout: 100ms
"#;

    // Revive the element partway through the first timeout window.
    // The inner poll will find it (ElementFound=true) but there's a pending
    // action error → breaks to recovery, which retries. On retry the action
    // also succeeds (element alive) → step passes.
    let b = btn.clone();
    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(40));
        b.revive();
    });

    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_ok(),
        "step must succeed after recovery retry: {result:?}"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}

/// `limit` caps how many times recovery can fire. Once the limit is reached
/// the step is handled by `on_failure` policy (abort by default → error).
#[test]
fn recovery_max_recoveries_reached_then_fails() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
recovery_handlers:
  always_retry:
    trigger:
      type: ElementFound
      scope: app
      selector: "[name=App]"
    actions: []
    resume: retry_step
phases:
  - name: main
    mount: [app]
    recovery:
      handlers: [always_retry]
      limit: 2
    steps:
      - intent: always times out, recovery retries until limit
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_err(),
        "must fail once max_recoveries is exhausted"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "failed:main", "Failed"]
    );
}

// ── fallback + on_success: return_phase ───────────────────────────────────────

/// When the primary action times out but the fallback satisfies the expect,
/// and `on_success: return_phase` is set, the remaining steps are skipped.
#[test]
fn fallback_success_with_return_phase_skips_remaining() {
    let yaml = r#"
name: test
anchors:
  app:
    type: Root
    selector: "[name=App]"
phases:
  - name: main
    mount: [app]
    steps:
      - intent: primary times out; fallback satisfies; return_phase skips step 2
        action:
          type: NoOp
        fallback:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: "[name=App]"
        on_success: return_phase
        timeout: 50ms
      - intent: must NOT run — would fail if reached
        action:
          type: NoOp
        expect:
          type: ElementFound
          scope: app
          selector: ">> [role=button][name=NeverExists]"
        timeout: 50ms
"#;
    let desktop = app_desktop();
    let (result, events) = run(yaml, desktop);
    assert!(
        result.is_ok(),
        "fallback + return_phase must complete the phase: {result:?}"
    );
    assert_eq!(
        event_names(&events),
        ["started:main", "completed:main", "Completed"]
    );
}