vertep-core 0.1.1

Chart IR, validation and the pure SCXML step kernel for Vertep (no_std)
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
//! One test per scxml-interpretation scenario.

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use super::builder::*;
use crate::datamodel::{Environment, Event, EventType, Value};
use crate::ir::*;
use crate::kernel::descriptor_matches;
use crate::null::NullDatamodel;
use crate::session::{Effect, Input, Options, StartError, StepError, parse_delay, start, step};
use crate::tape::{Request, TapeDatamodel};
use crate::trace::TransitionRef;

fn invoke_effects(effects: &[Effect]) -> Vec<String> {
    effects
        .iter()
        .filter_map(|e| match e {
            Effect::Invoke(r) => Some(r.invokeid.clone()),
            _ => None,
        })
        .collect()
}

// ----- Pure Step Function -------------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Pure Step Function#Same input, same output]] {level: unit}
#[test]
fn same_input_same_output() {
    let cc = compile(chart(vec![
        atomic("s0").on(tr("go", "s1").body(vec![
            send(Send {
                delay: Some("1s".into()),
                ..send_event("later")
            }),
            raise("inner"),
        ])),
        atomic("s1").on(tr("inner", "s2").body(vec![log("inner seen")])),
        atomic("s2"),
    ]));
    let session = run_null(&cc).session;
    let a = step(
        &cc,
        session.clone(),
        Input::event(ev("go"), Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    let b = step(
        &cc,
        session,
        Input::event(ev("go"), Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    assert_eq!(a, b);
    assert_eq!(active(&cc, &a), ids(&["s2"]));
    assert_eq!(a.effects.len(), 2);
}

// @tg: verifies:: [[openspec:scxml-interpretation#Pure Step Function#No side effects outside the result]] {level: unit}
#[test]
fn no_side_effects_outside_the_result() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![send(send_event("out")), log("hello")])
            .invoke(Invoke::default()),
    ]));
    let o = run_null(&cc);
    assert_eq!(o.effects.len(), 3);
    assert!(matches!(&o.effects[0], Effect::Send(r) if r.event.name == "out"));
    assert!(matches!(&o.effects[1], Effect::Log { label: Some(l), .. } if l == "hello"));
    assert!(matches!(&o.effects[2], Effect::Invoke(r) if r.state == "s"));
    // The trace carries the same effects, and nothing else happened.
    assert_eq!(o.trace.effects, o.effects);
}

// ----- Session Initialization ---------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Session Initialization#Initial configuration entered]] {level: unit}
#[test]
fn initial_configuration_entered() {
    let cc = compile(chart(vec![
        compound("a", vec![atomic("a1").logged(), atomic("a2").logged()]).logged(),
    ]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["a", "a1"]));
    assert_eq!(logs(&o.effects), ids(&["enter a", "enter a1"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Session Initialization#Eventless transitions taken at start]] {level: unit}
#[test]
fn eventless_transitions_taken_at_start() {
    let cc = compile(chart(vec![
        atomic("s0").on(tr("", "s1").cond("In('s0')")),
        atomic("s1"),
    ]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["s1"]));
    assert_eq!(o.trace.microsteps.len(), 1);
    assert_eq!(o.trace.microsteps[0].event, None);
}

// ----- Macrostep Execution ------------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Macrostep Execution#Internal events before the next external event]] {level: unit}
#[test]
fn internal_events_before_the_next_external_event() {
    let cc = compile(chart(vec![
        atomic("s0").on(tr("e1", "s1").body(vec![raise("i1"), raise("i2")])),
        atomic("s1")
            .on(tr("i1", "s2").body(vec![log("i1")]))
            .on(tr("e2", "fail")),
        atomic("s2")
            .on(tr("i2", "s3").body(vec![log("i2")]))
            .on(tr("e2", "fail")),
        atomic("s3").on(tr("e2", "s4")),
        atomic("s4"),
        atomic("fail"),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e1");
    assert_eq!(events(&o), ids(&["e1", "i1", "i2"]));
    assert_eq!(logs(&o.effects), ids(&["i1", "i2"]));
    assert!(o.session.internal_queue.is_empty());
    let o = send_null(&cc, o.session, "e2");
    assert_eq!(active(&cc, &o), ids(&["s4"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Macrostep Execution#Eventless transitions take priority over internal events]] {level: unit}
#[test]
fn eventless_transitions_take_priority_over_internal_events() {
    let cc = compile(chart(vec![
        atomic("s0")
            .entry(vec![raise("x")])
            .on(tr("x", "fail"))
            .on(tr("", "s1")),
        atomic("s1").on(tr("x", "pass")),
        atomic("pass"),
        atomic("fail"),
    ]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["pass"]));
    let order: Vec<Option<String>> = o.trace.microsteps.iter().map(|m| m.event.clone()).collect();
    assert_eq!(order, vec![None, Some("x".into())]);
}

// @tg: verifies:: [[openspec:scxml-interpretation#Macrostep Execution#Unbounded eventless loop is cut by a budget]] {level: unit}
#[test]
fn unbounded_eventless_loop_is_cut_by_a_budget() {
    let cc = compile(chart(vec![
        atomic("a").on(tr("", "b")),
        atomic("b").on(tr("", "a")),
    ]));
    let mut input = start_input();
    input.options = Options {
        microstep_budget: 50,
        ..Options::default()
    };
    match start(&cc, input, &mut NullDatamodel) {
        Err(StartError::BudgetExceeded { budget, partial }) => {
            assert_eq!(budget, 50);
            assert_eq!(partial.trace.microsteps.len(), 50);
        }
        other => panic!("expected a budget error, got {other:?}"),
    }
    // The budget also applies to steps.
    let cc = compile(chart(vec![
        atomic("s").on(tr("go", "a")),
        atomic("a").on(tr("", "b")),
        atomic("b").on(tr("", "a")),
    ]));
    let o = run_null(&cc);
    let r = step(
        &cc,
        o.session,
        Input::event(ev("go"), Environment::default()),
        &mut NullDatamodel,
    );
    assert!(matches!(
        r,
        Err(StepError::BudgetExceeded {
            budget: crate::session::DEFAULT_MICROSTEP_BUDGET,
            ..
        })
    ));
}

// ----- Event Descriptor Matching ------------------------------------------------------------

fn error_catcher(descriptor: &str) -> crate::compile::CompiledChart {
    compile(chart(vec![
        atomic("s")
            .entry(vec![log_expr("not an In() expression")])
            .on(tr(descriptor, "caught"))
            .on(tr("*", "other")),
        atomic("caught"),
        atomic("other"),
    ]))
}

// @tg: verifies:: [[openspec:scxml-interpretation#Event Descriptor Matching#Prefix match]] {level: unit}
#[test]
fn prefix_match() {
    assert!(descriptor_matches("error", "error.execution"));
    assert!(descriptor_matches("error.execution", "error.execution"));
    assert!(descriptor_matches("error.*", "error.execution"));
    let cc = error_catcher("error");
    assert_eq!(active(&cc, &run_null(&cc)), ids(&["caught"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Event Descriptor Matching#Partial token does not match]] {level: unit}
#[test]
fn partial_token_does_not_match() {
    assert!(!descriptor_matches("err", "error.execution"));
    assert!(!descriptor_matches("error.exec", "error.execution"));
    assert!(!descriptor_matches(
        "error.execution.more",
        "error.execution"
    ));
    let cc = error_catcher("err");
    assert_eq!(active(&cc, &run_null(&cc)), ids(&["other"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Event Descriptor Matching#Wildcard]] {level: unit}
#[test]
fn wildcard() {
    for name in ["a", "error.execution", "done.state.x", "x.y.z"] {
        assert!(descriptor_matches("*", name));
    }
    let cc = compile(chart(vec![atomic("s").on(tr("*", "t")), atomic("t")]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "anything.at.all");
    assert_eq!(active(&cc, &o), ids(&["t"]));
}

// ----- Optimal Enabled Transition Set -------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Optimal Enabled Transition Set#Descendant wins over ancestor]] {level: unit}
#[test]
fn descendant_wins_over_ancestor() {
    let cc = compile(chart(vec![
        compound("a", vec![atomic("a1").on(tr("e", "x"))]).on(tr("e", "y")),
        atomic("x"),
        atomic("y"),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["x"]));
    assert_eq!(
        o.trace.microsteps[0].transitions,
        vec![TransitionRef {
            source: "a1".into(),
            index: 0
        }]
    );
}

fn two_regions(t1: &str, t2: &str) -> Vec<State> {
    vec![
        parallel(
            "p",
            vec![
                compound("r1", vec![atomic("r1a").on(tr("e", t1)), atomic("r1b")]),
                compound("r2", vec![atomic("r2a").on(tr("e", t2)), atomic("r2b")]),
            ],
        ),
        atomic("out1"),
        atomic("out2"),
    ]
}

// @tg: verifies:: [[openspec:scxml-interpretation#Optimal Enabled Transition Set#Parallel regions take transitions together]] {level: unit}
#[test]
fn parallel_regions_take_transitions_together() {
    let cc = compile(chart(two_regions("r1b", "r2b")));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["p", "r1", "r1b", "r2", "r2b"]));
    assert_eq!(o.trace.microsteps.len(), 1);
    assert_eq!(o.trace.microsteps[0].transitions.len(), 2);
}

// @tg: verifies:: [[openspec:scxml-interpretation#Optimal Enabled Transition Set#Conflict resolved by document order]] {level: unit}
#[test]
fn conflict_resolved_by_document_order() {
    let cc = compile(chart(two_regions("out1", "out2")));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["out1"]));
    assert_eq!(
        o.trace.microsteps[0].transitions,
        vec![TransitionRef {
            source: "r1a".into(),
            index: 0
        }]
    );
}

// @tg: verifies:: [[openspec:scxml-interpretation#Optimal Enabled Transition Set#Condition false skips the transition]] {level: unit}
#[test]
fn condition_false_skips_the_transition() {
    let cc = compile(chart(vec![
        atomic("s0")
            .on(tr("e", "fail").cond("In('pass')"))
            .on(tr("e", "pass")),
        atomic("pass"),
        atomic("fail"),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["pass"]));
}

// ----- Exit And Entry Order -----------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Exit And Entry Order#Exit deepest first]] {level: unit}
#[test]
fn exit_deepest_first() {
    let cc = compile(chart(vec![
        compound("a", vec![atomic("a1").logged().on(tr("e", "b"))]).logged(),
        atomic("b"),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(logs(&o.effects), ids(&["exit a1", "exit a"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Exit And Entry Order#Transition content between exit and entry]] {level: unit}
#[test]
fn transition_content_between_exit_and_entry() {
    let cc = compile(chart(vec![
        atomic("a1")
            .logged()
            .on(tr("e", "b1").body(vec![log("transition")])),
        atomic("b1").logged(),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert_eq!(
        logs(&o.effects),
        ids(&["exit a1", "transition", "enter b1"])
    );
}

// @tg: verifies:: [[openspec:scxml-interpretation#Exit And Entry Order#Parallel regions entered in document order]] {level: unit}
#[test]
fn parallel_regions_entered_in_document_order() {
    let cc = compile(chart(vec![
        parallel(
            "p",
            vec![
                compound("r1", vec![atomic("r1a").logged()]).logged(),
                compound("r2", vec![atomic("r2a").logged()]).logged(),
            ],
        )
        .logged(),
    ]));
    let o = run_null(&cc);
    assert_eq!(
        logs(&o.effects),
        ids(&["enter p", "enter r1", "enter r1a", "enter r2", "enter r2a"])
    );
}

// ----- Internal Transitions -----------------------------------------------------------------

fn internal_chart(internal: bool) -> crate::compile::CompiledChart {
    let t = tr("go", "a2");
    let t = if internal { t.internal() } else { t };
    compile(chart(vec![
        compound("a", vec![atomic("a1").logged(), atomic("a2").logged()])
            .logged()
            .on(t),
    ]))
}

// @tg: verifies:: [[openspec:scxml-interpretation#Internal Transitions#Internal transition keeps the source]] {level: unit}
#[test]
fn internal_transition_keeps_the_source() {
    let cc = internal_chart(true);
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "go");
    assert_eq!(logs(&o.effects), ids(&["exit a1", "enter a2"]));
    assert_eq!(active(&cc, &o), ids(&["a", "a2"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Internal Transitions#External transition re-enters the source]] {level: unit}
#[test]
fn external_transition_re_enters_the_source() {
    let cc = internal_chart(false);
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "go");
    assert_eq!(
        logs(&o.effects),
        ids(&["exit a1", "exit a", "enter a", "enter a2"])
    );
    assert_eq!(active(&cc, &o), ids(&["a", "a2"]));
}

// ----- History Restoration ------------------------------------------------------------------

fn history_chart(ty: HistoryType) -> crate::compile::CompiledChart {
    compile(chart(vec![
        compound(
            "a",
            vec![
                history("h", ty, "a2").on_default_body(vec![log("history default")]),
                atomic("a1").on(tr("to2", "a2")),
                compound("a2", vec![atomic("a2x").on(tr("y", "a2y")), atomic("a2y")]),
            ],
        )
        .on(tr("out", "b")),
        atomic("b").on(tr("back", "h")),
    ]))
}

trait HistoryBody {
    fn on_default_body(self, b: Block) -> Self;
}

impl HistoryBody for State {
    fn on_default_body(mut self, b: Block) -> Self {
        self.transitions[0].body = b;
        self
    }
}

fn leave_from_a2y(cc: &crate::compile::CompiledChart) -> crate::session::Session {
    let o = run_null(cc);
    let o = send_null(cc, o.session, "to2");
    let o = send_null(cc, o.session, "y");
    assert_eq!(active(cc, &o), ids(&["a", "a2", "a2y"]));
    let o = send_null(cc, o.session, "out");
    assert_eq!(active(cc, &o), ids(&["b"]));
    o.session
}

// @tg: verifies:: [[openspec:scxml-interpretation#History Restoration#Shallow history restores the child]] {level: unit}
#[test]
fn shallow_history_restores_the_child() {
    let cc = history_chart(HistoryType::Shallow);
    let s = leave_from_a2y(&cc);
    let o = send_null(&cc, s, "back");
    assert_eq!(active(&cc, &o), ids(&["a", "a2", "a2x"]));
    assert!(logs(&o.effects).is_empty());
}

// @tg: verifies:: [[openspec:scxml-interpretation#History Restoration#Deep history restores the leaf]] {level: unit}
#[test]
fn deep_history_restores_the_leaf() {
    let cc = history_chart(HistoryType::Deep);
    let s = leave_from_a2y(&cc);
    let o = send_null(&cc, s, "back");
    assert_eq!(active(&cc, &o), ids(&["a", "a2", "a2y"]));
    assert!(logs(&o.effects).is_empty());
}

// @tg: verifies:: [[openspec:scxml-interpretation#History Restoration#Default transition when no history recorded]] {level: unit}
#[test]
fn default_transition_when_no_history_recorded() {
    let mut c = chart(vec![]);
    let hc = history_chart(HistoryType::Deep);
    c.states = hc.chart().states.clone();
    c.initial = vec!["b".into()];
    let cc = compile(c);
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["b"]));
    let o = send_null(&cc, o.session, "back");
    assert_eq!(active(&cc, &o), ids(&["a", "a2", "a2x"]));
    assert_eq!(logs(&o.effects), ids(&["history default"]));
}

/// Regression (Lean Configuration Well-Formedness proof): a transition from inside a parallel
/// state with a single region to the parallel state's deep history, taken before any history is
/// recorded, exits the parallel state (its domain, computed from the default targets, lies above
/// it) and must enter it again. Recomputing the domain on entry from the history just recorded
/// gave the region instead, and the parallel state stayed inactive below its active region.
#[test]
fn deep_history_of_single_region_parallel_reenters_the_parallel() {
    let cc = compile(chart(vec![parallel(
        "p",
        vec![
            history("h", HistoryType::Deep, "a"),
            compound("a", vec![atomic("a1").on(tr("e", "h")), atomic("a2")]),
        ],
    )]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["p", "a", "a1"]));
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["p", "a", "a1"]));
}

/// Regression (Lean Configuration Well-Formedness proof): the default of `P`'s history targets
/// `A1`, deep inside region `R1` of the parallel `Q`; the transition `A2 -> h` has domain `A`,
/// below `P`, so only `A2` is exited. Entering the history's default up to its parent `P`
/// (Appendix D) entered `Q` again and completed its region `R2` by default, leaving both `B1`
/// and `B2` active; the entry now stops at the domain.
#[test]
fn history_entry_stops_at_the_transition_domain() {
    let cc = compile(chart(vec![compound(
        "P",
        vec![
            history("h", HistoryType::Deep, "A1"),
            parallel(
                "Q",
                vec![
                    compound(
                        "R1",
                        vec![compound(
                            "A",
                            vec![
                                atomic("A1").on(tr("g", "A2")),
                                atomic("A2").on(tr("e", "h")),
                            ],
                        )],
                    ),
                    compound("R2", vec![atomic("B1").on(tr("f", "B2")), atomic("B2")]),
                ],
            ),
        ],
    )]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "f");
    let o = send_null(&cc, o.session, "g");
    assert_eq!(
        active(&cc, &o),
        ids(&["P", "Q", "R1", "A", "A2", "R2", "B2"])
    );
    let o = send_null(&cc, o.session, "e");
    assert_eq!(
        active(&cc, &o),
        ids(&["P", "Q", "R1", "A", "A1", "R2", "B2"])
    );
}

/// Regression (Lean Configuration Well-Formedness proof): as above with the default of `h`
/// going through a second history state `h2` of `P`. Entering `h2` with its ancestors up to the
/// domain, which is not an ancestor of `h2`, climbed to the root and completed the parallel
/// `Z`'s active region `R` by default.
#[test]
fn history_default_through_a_history_stops_at_the_domain() {
    let cc = compile(chart(vec![parallel(
        "Z",
        vec![
            compound(
                "P",
                vec![
                    history("h", HistoryType::Deep, "h2"),
                    history("h2", HistoryType::Deep, "A1"),
                    compound(
                        "A",
                        vec![
                            atomic("A1").on(tr("g", "A2")),
                            atomic("A2").on(tr("e", "h")),
                        ],
                    ),
                ],
            ),
            compound("R", vec![atomic("B1").on(tr("f", "B2")), atomic("B2")]),
        ],
    )]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "f");
    let o = send_null(&cc, o.session, "g");
    assert_eq!(active(&cc, &o), ids(&["Z", "P", "A", "A2", "R", "B2"]));
    let o = send_null(&cc, o.session, "e");
    assert_eq!(active(&cc, &o), ids(&["Z", "P", "A", "A1", "R", "B2"]));
}

// ----- Final States And Done Events ---------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Final States And Done Events#Compound done event]] {level: unit}
#[test]
fn compound_done_event() {
    let cc = compile(chart(vec![
        compound(
            "a",
            vec![
                atomic("a1").on(tr("e", "f")),
                final_("f").donedata("result"),
            ],
        )
        .on(tr("done.state.a", "pass")),
        atomic("pass"),
    ]));
    let mut null = NullDatamodel;
    let mut dm = TapeDatamodel::new(&mut null);
    let o = start(&cc, start_input(), &mut dm).unwrap();
    let o = step(
        &cc,
        o.session,
        Input::event(ev("e"), Environment::default()),
        &mut dm,
    )
    .unwrap();
    assert_eq!(events(&o), ids(&["e", "done.state.a"]));
    assert_eq!(active(&cc, &o), ids(&["pass"]));
    let done = dm
        .entries()
        .iter()
        .find_map(|e| match &e.request {
            Request::SetEvent(ev) if ev.name == "done.state.a" => Some(ev.clone()),
            _ => None,
        })
        .expect("done.state.a was bound to _event");
    assert_eq!(done.data, Value::String("result".into()));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Final States And Done Events#Parallel done event]] {level: unit}
#[test]
fn parallel_done_event() {
    let cc = compile(chart(vec![
        parallel(
            "p",
            vec![
                compound("r1", vec![atomic("r1a").on(tr("e1", "r1f")), final_("r1f")]),
                compound("r2", vec![atomic("r2a").on(tr("e2", "r2f")), final_("r2f")]),
            ],
        )
        .on(tr("done.state.p", "pass")),
        atomic("pass"),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e1");
    assert_eq!(events(&o), ids(&["e1", "done.state.r1"]));
    assert!(o.session.is_active(&cc, "p"));
    let o = send_null(&cc, o.session, "e2");
    assert_eq!(events(&o), ids(&["e2", "done.state.r2", "done.state.p"]));
    assert_eq!(active(&cc, &o), ids(&["pass"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Final States And Done Events#Top-level final ends the session]] {level: unit}
#[test]
fn top_level_final_ends_the_session() {
    let cc = compile(chart(vec![
        atomic("s").on(tr("e", "end")),
        final_("end").donedata("bye").logged(),
    ]));
    let o = run_null(&cc);
    let o = send_null(&cc, o.session, "e");
    assert!(o.session.is_terminated());
    assert!(o.trace.terminated);
    assert_eq!(logs(&o.effects), ids(&["enter end", "exit end"]));
    assert_eq!(
        o.effects.last(),
        Some(&Effect::SessionDone {
            data: Value::String("bye".into()),
            cancelled: false
        })
    );
}

// ----- Internal And External Queues ---------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Internal And External Queues#Raise is internal]] {level: unit}
#[test]
fn raise_is_internal() {
    let cc = compile(chart(vec![
        atomic("s").entry(vec![raise("x")]).on(tr("x", "pass")),
        atomic("pass"),
    ]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["pass"]));
    assert!(o.effects.is_empty());
    assert_eq!(events(&o), ids(&["x"]));
}

// @tg: verifies:: [[openspec:scxml-interpretation#Internal And External Queues#Send to the internal target]] {level: unit}
#[test]
fn send_to_the_internal_target() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![send(Send {
                target: Some("#_internal".into()),
                ..send_event("x")
            })])
            .on(tr("x", "pass")),
        atomic("pass"),
    ]));
    let mut null = NullDatamodel;
    let mut dm = TapeDatamodel::new(&mut null);
    let o = start(&cc, start_input(), &mut dm).unwrap();
    assert_eq!(active(&cc, &o), ids(&["pass"]));
    assert!(o.effects.is_empty());
    let bound = dm
        .entries()
        .iter()
        .find_map(|e| match &e.request {
            Request::SetEvent(ev) => Some(ev.clone()),
            _ => None,
        })
        .unwrap();
    assert_eq!(bound.name, "x");
    assert_eq!(bound.type_, EventType::Internal);
}

// ----- Execution Errors ---------------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Execution Errors#Failed expression stops the block]] {level: unit}
#[test]
fn failed_expression_stops_the_block() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![log("first"), log_expr("1 + 1"), log("third")])
            .entry(vec![log("next block")])
            .on(tr("error.execution", "pass")),
        atomic("pass"),
    ]));
    let o = run_null(&cc);
    assert_eq!(logs(&o.effects), ids(&["first", "next block"]));
    assert_eq!(events(&o), ids(&["error.execution"]));
    assert_eq!(active(&cc, &o), ids(&["pass"]));
}

/// W3C SCXML 6.2.4 and IRP test 199: an unsupported send type raises `error.execution`.
// @tg: verifies:: [[openspec:scxml-interpretation#Execution Errors#Invalid send type]] {level: unit}
#[test]
fn invalid_send_type() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![
                send(Send {
                    type_: Some("27".into()),
                    ..send_event("event1")
                }),
                log("not reached"),
            ])
            .on(tr("error", "caught")),
        atomic("caught").on(tr("again", "s")),
    ]));
    let mut null = NullDatamodel;
    let mut dm = TapeDatamodel::new(&mut null);
    let o = start(&cc, start_input(), &mut dm).unwrap();
    assert_eq!(active(&cc, &o), ids(&["caught"]));
    assert!(o.effects.is_empty());
    assert_eq!(events(&o), ids(&["error.execution"]));
    // The error event carries the send identifier of the failed send.
    let err = dm
        .entries()
        .iter()
        .find_map(|e| match &e.request {
            Request::SetEvent(ev) => Some(ev.clone()),
            _ => None,
        })
        .unwrap();
    assert_eq!(err.type_, EventType::Platform);
    assert_eq!(err.sendid.as_deref(), Some("sid.send.1"));
    // The session continues: it still processes events.
    let o = step(
        &cc,
        o.session,
        Input::event(ev("again"), Environment::default()),
        &mut dm,
    )
    .unwrap();
    assert!(!o.session.is_terminated());
    assert_eq!(events(&o), ids(&["again", "error.execution"]));
}

// ----- Send And Cancel Effects --------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Send And Cancel Effects#Delayed send becomes an effect]] {level: unit}
#[test]
fn delayed_send_becomes_an_effect() {
    let cc = compile(chart(vec![atomic("s").entry(vec![send(Send {
        delay: Some("5s".into()),
        ..send_event("tick")
    })])]));
    let o = run_null(&cc);
    let [Effect::Send(r)] = o.effects.as_slice() else {
        panic!("expected one send effect, got {:?}", o.effects)
    };
    assert_eq!(r.delay_ms, 5000);
    assert_eq!(r.event.name, "tick");
    assert_eq!(r.sendid, "sid.send.1");
    assert_eq!(r.target, None);
    assert_eq!(r.event.origin.as_deref(), Some("#_scxml_sid"));
    assert_eq!(parse_delay("1.5s"), Some(1500));
    assert_eq!(parse_delay("250ms"), Some(250));
    assert_eq!(parse_delay("soon"), None);
}

// @tg: verifies:: [[openspec:scxml-interpretation#Send And Cancel Effects#Deterministic send identifiers]] {level: unit}
#[test]
fn deterministic_send_identifiers() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![send(send_event("a")), send(send_event("b"))])
            .on(tr("go", "s")),
    ]));
    let run = || {
        let o = run_null(&cc);
        let mut all = o.effects.clone();
        all.extend(send_null(&cc, o.session, "go").effects);
        all.iter()
            .filter_map(|e| match e {
                Effect::Send(r) => Some(r.sendid.clone()),
                _ => None,
            })
            .collect::<Vec<_>>()
    };
    let first = run();
    assert_eq!(
        first,
        ids(&["sid.send.1", "sid.send.2", "sid.send.3", "sid.send.4"])
    );
    assert_eq!(first, run());
}

// @tg: verifies:: [[openspec:scxml-interpretation#Send And Cancel Effects#Cancel by send identifier]] {level: unit}
#[test]
fn cancel_by_send_identifier() {
    let cc = compile(chart(vec![atomic("s").entry(vec![Exec::new(
        ExecKind::Cancel {
            sendid: Some("s1".into()),
            sendidexpr: None,
        },
    )])]));
    let o = run_null(&cc);
    assert_eq!(
        o.effects,
        vec![Effect::Cancel {
            sendid: "s1".into()
        }]
    );
}

// ----- Invoke Lifecycle ---------------------------------------------------------------------

fn finalize_invoke() -> Invoke {
    Invoke {
        finalize: vec![log("finalize")],
        autoforward: true,
        ..Invoke::default()
    }
}

// @tg: verifies:: [[openspec:scxml-interpretation#Invoke Lifecycle#Invoke started after the macrostep]] {level: unit}
#[test]
fn invoke_started_after_the_macrostep() {
    let cc = compile(chart(vec![
        atomic("s")
            .entry(vec![log("enter s"), raise("r")])
            .on(tr("r", "").body(vec![log("handled r")]))
            .invoke(Invoke::default()),
    ]));
    let o = run_null(&cc);
    assert_eq!(logs(&o.effects), ids(&["enter s", "handled r"]));
    // The invoke comes after everything else in the macrostep, including the internal event.
    assert!(matches!(o.effects.last(), Some(Effect::Invoke(r)) if r.state == "s"));
    assert_eq!(o.session.invocations.len(), 1);
}

// @tg: verifies:: [[openspec:scxml-interpretation#Invoke Lifecycle#Invoke not started when the state is left in the same macrostep]] {level: unit}
#[test]
fn invoke_not_started_when_the_state_is_left_in_the_same_macrostep() {
    let cc = compile(chart(vec![
        atomic("s").invoke(Invoke::default()).on(tr("", "t")),
        atomic("t"),
    ]));
    let o = run_null(&cc);
    assert_eq!(active(&cc, &o), ids(&["t"]));
    assert!(invoke_effects(&o.effects).is_empty());
    assert!(
        o.effects
            .iter()
            .all(|e| !matches!(e, Effect::CancelInvoke { .. }))
    );
}

// @tg: verifies:: [[openspec:scxml-interpretation#Invoke Lifecycle#Finalize runs before the event]] {level: unit}
#[test]
fn finalize_runs_before_the_event() {
    let cc = compile(chart(vec![
        atomic("s")
            .invoke(finalize_invoke())
            .on(tr("child.ev", "").body(vec![log("transition")])),
    ]));
    let o = run_null(&cc);
    let id = invoke_effects(&o.effects).pop().unwrap();
    let event = Event {
        invokeid: Some(id.clone()),
        ..Event::named("child.ev", EventType::External)
    };
    let o = step(
        &cc,
        o.session,
        Input::event(event, Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    assert_eq!(logs(&o.effects), ids(&["finalize", "transition"]));
    // Autoforward forwards external events to the invocation.
    assert!(
        o.effects
            .iter()
            .any(|e| matches!(e, Effect::Forward { invokeid, .. } if *invokeid == id))
    );
}

// @tg: verifies:: [[openspec:scxml-interpretation#Invoke Lifecycle#Exiting the state cancels the invocation]] {level: unit}
#[test]
fn exiting_the_state_cancels_the_invocation() {
    let cc = compile(chart(vec![
        atomic("s").invoke(Invoke::default()).on(tr("e", "t")),
        atomic("t"),
    ]));
    let o = run_null(&cc);
    let id = invoke_effects(&o.effects).pop().unwrap();
    let o = send_null(&cc, o.session, "e");
    assert_eq!(
        o.effects,
        vec![Effect::CancelInvoke {
            invokeid: id.clone()
        }]
    );
    assert!(o.session.invocations.is_empty());
    // The host stops delivering the cancelled invocation's events; one it had already queued
    // is processed, but it no longer runs finalize or is forwarded.
    let late = Event {
        invokeid: Some(id),
        ..Event::named("e", EventType::External)
    };
    let o = step(
        &cc,
        o.session,
        Input::event(late, Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    assert!(o.effects.is_empty());
    assert!(o.trace.microsteps[0].transitions.is_empty());
}

#[test]
fn done_invoke_completes_the_invocation() {
    let cc = compile(chart(vec![
        atomic("s")
            .invoke(Invoke::default())
            .on(tr("done.invoke", "t")),
        atomic("t"),
    ]));
    let o = run_null(&cc);
    let id = invoke_effects(&o.effects).pop().unwrap();
    let done = Event {
        invokeid: Some(id.clone()),
        ..Event::named(alloc::format!("done.invoke.{id}"), EventType::External)
    };
    let o = step(
        &cc,
        o.session,
        Input::event(done, Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    assert_eq!(active(&cc, &o), ids(&["t"]));
    // A completed invocation is not cancelled on exit.
    assert!(o.effects.is_empty());
}

// ----- Session Termination ------------------------------------------------------------------

// @tg: verifies:: [[openspec:scxml-interpretation#Session Termination#Exit content runs on termination]] {level: unit}
#[test]
fn exit_content_runs_on_termination() {
    let cc = compile(chart(vec![
        compound("a", vec![atomic("a1").logged()])
            .logged()
            .invoke(Invoke::default()),
    ]));
    let o = run_null(&cc);
    let id = invoke_effects(&o.effects).pop().unwrap();
    let o = step(
        &cc,
        o.session,
        Input::cancel(Environment::default()),
        &mut NullDatamodel,
    )
    .unwrap();
    assert_eq!(logs(&o.effects), ids(&["exit a1", "exit a"]));
    assert_eq!(
        o.effects[2..],
        [
            Effect::CancelInvoke { invokeid: id },
            Effect::SessionDone {
                data: Value::Undefined,
                cancelled: true
            }
        ]
    );
    assert!(o.session.is_terminated());
    assert!(o.session.configuration.is_empty());
}

// @tg: verifies:: [[openspec:scxml-interpretation#Session Termination#Events after termination are rejected]] {level: unit}
#[test]
fn events_after_termination_are_rejected() {
    let cc = compile(chart(vec![final_("end")]));
    let o = run_null(&cc);
    assert!(o.session.is_terminated());
    assert!(matches!(
        o.effects.as_slice(),
        [Effect::SessionDone {
            cancelled: false,
            ..
        }]
    ));
    let r = step(
        &cc,
        o.session,
        Input::event(ev("e"), Environment::default()),
        &mut NullDatamodel,
    );
    assert_eq!(r, Err(StepError::Terminated));
}