turnframe-test 0.1.0

Test kit for Turnframe: fixtures, scripted providers, fake stores, workflow exploration and replay assertions
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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
//! Bounded exploration of both sample workflows (spec §8.5, §27.3).
//!
//! The first two tests are the point of the kit: every state reachable from the
//! samples satisfies the projection invariants. The last two are the control
//! group — a model that lies produces exactly the violation it should, which is
//! what stops the first two from passing vacuously.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use turnframe_core::case::CaseRef;
use turnframe_core::error::DomainRejection;
use turnframe_core::event::{OperationalReceipt, ReceiptEvent};
use turnframe_core::flow::{
    CommandPolicy, InteractionRequirement, PhaseOwnership, ViewOf, WorkflowDefinition,
    WorkflowNotice, WorkflowRegistry, WorkflowView,
};
use turnframe_core::ids::{CaseRevision, WorkflowKey, WorkflowVersion};
use turnframe_core::interaction::InteractionSpec;
use turnframe_core::locale::{Locale, LocalizedText};
use turnframe_core::operation::OperationSpec;
use turnframe_core::response::NoticeSeverity;
use turnframe_core::target::ResolvedAct;
use turnframe_test::assertions::{AssertionFailure, same_phase_in, single_phase};
use turnframe_test::explore::{
    EXPLORATION_REVISIONS, ExplorationLimits, ExplorationViolationKind, SimulatedTransition,
    WorkflowModel, explore, reachable_states,
};
use turnframe_test::workflows::claim::{
    ClaimModel, ClaimOutcome, ClaimPhase, ClaimWorkflow, complete_proposal, partial_proposal,
    under_review,
};
use turnframe_test::workflows::traveler::{
    TravelerCommand, TravelerEvent, TravelerModel, TravelerObligation, TravelerOutcome,
    TravelerPhase, TravelerState, TravelerWorkflow, awaiting_activation,
};
use turnframe_test::workflows::trip::apply::MAX_EXTRAS;
use turnframe_test::workflows::trip::{
    OTHER_TRAVELER_ID, TripCommand, TripEvent, TripExecutor, TripModel, TripOutcome, TripPhase,
    TripState, TripWorkflow, awaiting_rebooking_confirmation, complete_case, incomplete_case,
};

#[test]
fn trip_reachable_states_satisfy_every_invariant() {
    let report = explore(
        &TripWorkflow::default(),
        &TripModel::default(),
        ExplorationLimits::generous(),
    );

    assert!(report.is_clean(), "{}", report.describe());
    assert!(
        !report.truncated,
        "the trip state space should fit in the generous limits: {}",
        report.describe()
    );
    assert!(
        report.states_explored > 400,
        "exploration barely moved: {}",
        report.describe()
    );
    // Printed on failure only; the number is the budget this test guards.
    assert!(
        report.states_explored < 2_500,
        "the trip state space grew past what a fast test can explore: {}",
        report.describe()
    );
    for outcome in [
        TripOutcome::Withdrawn,
        TripOutcome::Notified,
        TripOutcome::NotNotified,
    ] {
        assert!(
            report.reached_outcome(&outcome),
            "{outcome:?} was never reached: {}",
            report.describe()
        );
    }
    // Where it looked, and not only how much. A budget that stops eight steps
    // in reports every invariant clean over half a workflow, so the phases are
    // named here rather than inferred from a state count.
    for phase in [
        TripPhase::Collecting,
        TripPhase::AwaitingRebookingConfirmation,
        TripPhase::Dispatching,
        TripPhase::Refused,
        TripPhase::Ticketed,
        TripPhase::Notified,
        TripPhase::NotNotified,
        TripPhase::Withdrawn,
    ] {
        assert!(
            report.reached_phase(&phase),
            "{phase:?} was never projected: {}",
            report.describe()
        );
    }
}

/// A budget that stops early reports clean over half a workflow.
///
/// This is the argument for `reached_phases`, made executable. The run below
/// finds no violations and is honest that it truncated — and it never projects
/// the phases that come after the rebooking card, so every check the explorer
/// performs was performed nowhere near them. `truncated` said as much and says
/// it for any workflow with a large collection half, which is why it stopped
/// carrying information; the phases say where.
#[test]
fn a_truncated_run_is_clean_about_the_half_it_never_saw() {
    let report = explore(
        &TripWorkflow::default(),
        &TripModel::default(),
        ExplorationLimits::new(24, 2, 8),
    );

    assert!(report.is_clean(), "{}", report.describe());
    assert!(report.truncated, "{}", report.describe());
    assert!(
        report.reached_phase(&TripPhase::Collecting),
        "the search did start: {}",
        report.describe()
    );
    for phase in [
        TripPhase::Dispatching,
        TripPhase::Ticketed,
        TripPhase::Notified,
        TripPhase::NotNotified,
    ] {
        assert!(
            !report.reached_phase(&phase),
            "{phase:?} is out of reach at depth two, and a clean report must not \
             be read as covering it: {}",
            report.describe()
        );
    }
}

#[test]
fn traveler_reachable_states_satisfy_every_invariant() {
    let report = explore(
        &TravelerWorkflow::new().with_cards(),
        &TravelerModel::of(TravelerWorkflow::new().with_cards()),
        ExplorationLimits::generous(),
    );

    assert!(report.is_clean(), "{}", report.describe());
    assert!(!report.truncated, "{}", report.describe());
    assert!(report.states_explored > 8, "{}", report.describe());
    for outcome in [TravelerOutcome::Archived, TravelerOutcome::Deleted] {
        assert!(
            report.reached_outcome(&outcome),
            "{outcome:?} was never reached: {}",
            report.describe()
        );
    }
    for phase in [
        TravelerPhase::AwaitingActivation,
        TravelerPhase::Active,
        TravelerPhase::Archived,
        TravelerPhase::Deleted,
    ] {
        assert!(
            report.reached_phase(&phase),
            "{phase:?} was never projected: {}",
            report.describe()
        );
    }
}

#[test]
fn claim_reachable_states_satisfy_every_invariant() {
    let report = explore(
        &ClaimWorkflow::default(),
        &ClaimModel::default(),
        ExplorationLimits::generous(),
    );

    assert!(report.is_clean(), "{}", report.describe());
    assert!(!report.truncated, "{}", report.describe());
    assert!(report.states_explored > 8, "{}", report.describe());
    for outcome in [ClaimOutcome::Recorded, ClaimOutcome::Discarded] {
        assert!(
            report.reached_outcome(&outcome),
            "{outcome:?} was never reached: {}",
            report.describe()
        );
    }
    for phase in [
        ClaimPhase::AwaitingDocument,
        ClaimPhase::Extracting,
        ClaimPhase::AwaitingReview,
        ClaimPhase::Recorded,
        ClaimPhase::Discarded,
    ] {
        assert!(
            report.reached_phase(&phase),
            "{phase:?} was never projected: {}",
            report.describe()
        );
    }
}

#[test]
fn the_review_phase_is_reached_with_and_without_a_complete_proposal() {
    let states: Vec<turnframe_test::workflows::claim::ClaimState> =
        reachable_states(&ClaimModel::default(), ExplorationLimits::generous())
            .into_iter()
            .flatten()
            .collect();

    let reviewing: Vec<_> = states
        .iter()
        .filter(|state| ClaimWorkflow::phase_of(state) == ClaimPhase::AwaitingReview)
        .collect();
    assert!(
        reviewing
            .iter()
            .any(|state| state.proposal.as_ref().is_some_and(|p| p.is_complete())),
        "a complete proposal must be reachable"
    );
    assert!(
        reviewing
            .iter()
            .any(|state| state.proposal.as_ref().is_some_and(|p| !p.is_complete())),
        "a proposal short of a required field must be reachable"
    );
    assert!(
        reviewing.iter().any(|state| state
            .proposal
            .as_ref()
            .is_some_and(|p| p.edited_count() > 0)),
        "a proposal a human corrected must be reachable"
    );
    assert!(
        states
            .iter()
            .any(|state| state.abandoned_from.is_some() && state.proposal.is_none()),
        "a review the user threw away must be reachable"
    );
    assert!(
        states
            .iter()
            .any(|state| state.recorded.iter().any(|recorded| recorded.corrected)),
        "a recorded value that a human corrected must be reachable: that is what \
         makes provenance worth keeping"
    );
}

#[test]
fn every_sample_projection_carries_one_phase() {
    let trip = TripWorkflow::default();
    for state in [
        None,
        Some(incomplete_case()),
        Some(complete_case()),
        Some(awaiting_rebooking_confirmation()),
    ] {
        let view = trip.project(
            CaseRef::new("trip", "trip-1", CaseRevision(3)),
            state.as_ref(),
        );
        let erased = view
            .erase(trip.phase_ownership(&view.phase))
            .expect("the sample view is serializable");
        single_phase(&erased).expect("one phase per projection");
    }

    let traveler = TravelerWorkflow::new().with_cards();
    for state in [None, Some(awaiting_activation())] {
        let view = traveler.project(
            CaseRef::new("traveler", "c-1", CaseRevision(1)),
            state.as_ref(),
        );
        let erased = view
            .erase(traveler.phase_ownership(&view.phase))
            .expect("the sample view is serializable");
        single_phase(&erased).expect("one phase per projection");
    }

    let claim = ClaimWorkflow::default();
    for state in [
        None,
        Some(under_review(complete_proposal())),
        Some(under_review(partial_proposal())),
    ] {
        let view = claim.project(
            CaseRef::new("claim", "claim-1", CaseRevision(2)),
            state.as_ref(),
        );
        let erased = view
            .erase(claim.phase_ownership(&view.phase))
            .expect("the sample view is serializable");
        single_phase(&erased).expect("one phase per projection");
    }
}

/// A model that never offers `Withdraw`, while still declaring `Withdrawn`
/// reachable.
struct NeverWithdraws(TripModel);

impl WorkflowModel<TripWorkflow> for NeverWithdraws {
    fn initial_states(&self) -> Vec<Option<TripState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TripState>) -> Vec<TripCommand> {
        self.0
            .candidate_commands(state)
            .into_iter()
            .filter(|command| !matches!(command, TripCommand::Withdraw))
            .collect()
    }

    fn simulate(
        &self,
        state: Option<&TripState>,
        command: &TripCommand,
    ) -> SimulatedTransition<TripState, TripEvent> {
        self.0.simulate(state, command)
    }

    fn declared_outcomes(&self) -> Vec<TripOutcome> {
        self.0.declared_outcomes()
    }
}

#[test]
fn an_outcome_no_path_reaches_is_reported() {
    let report = explore(
        &TripWorkflow::default(),
        &NeverWithdraws(TripModel::default()),
        ExplorationLimits::generous(),
    );

    assert!(!report.truncated, "{}", report.describe());
    let unreachable: Vec<_> = report
        .violations
        .iter()
        .filter(|violation| {
            matches!(
                violation.kind,
                ExplorationViolationKind::UnreachableOutcome { .. }
            )
        })
        .collect();
    assert_eq!(unreachable.len(), 1, "{}", report.describe());
    assert!(report.reached_outcome(&TripOutcome::Notified));
}

/// A model that stops offering commands as soon as the case exists.
struct StopsAfterCreation(TravelerModel);

impl WorkflowModel<TravelerWorkflow> for StopsAfterCreation {
    fn initial_states(&self) -> Vec<Option<TravelerState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TravelerState>) -> Vec<TravelerCommand> {
        match state {
            None => self.0.candidate_commands(state),
            Some(_) => Vec::new(),
        }
    }

    fn simulate(
        &self,
        state: Option<&TravelerState>,
        command: &TravelerCommand,
    ) -> SimulatedTransition<TravelerState, TravelerEvent> {
        self.0.simulate(state, command)
    }
}

#[test]
fn a_state_with_nowhere_to_go_is_reported_as_a_dead_end() {
    let report = explore(
        &TravelerWorkflow::new().with_cards(),
        &StopsAfterCreation(TravelerModel::of(TravelerWorkflow::new().with_cards())),
        ExplorationLimits::smoke(),
    );

    assert!(
        report
            .violations
            .iter()
            .any(|violation| violation.kind == ExplorationViolationKind::DeadEnd),
        "{}",
        report.describe()
    );
    let dead_end = report
        .violations
        .iter()
        .find(|violation| violation.kind == ExplorationViolationKind::DeadEnd)
        .unwrap();
    assert_eq!(
        dead_end.path.len(),
        1,
        "the shortest path to the dead end is one command: {}",
        dead_end.describe()
    );
}

/// A workflow whose projection reads the case revision: at an odd revision it
/// adds a notice, at an even one it does not. Nothing else changes.
///
/// This is the defect the depth-derived revision could not see. When every
/// state was projected at exactly one revision — its own depth — a projector
/// that varied with the revision produced one consistent view per state and
/// looked perfectly deterministic.
struct RevisionSensitive(TripWorkflow);

impl WorkflowDefinition for RevisionSensitive {
    type State = TripState;
    type Phase = turnframe_test::workflows::trip::TripPhase;
    type Obligation = turnframe_test::workflows::trip::TripObligation;
    type Command = TripCommand;
    type Event = TripEvent;
    type Outcome = TripOutcome;

    fn key(&self) -> WorkflowKey {
        self.0.key()
    }

    fn version(&self) -> WorkflowVersion {
        self.0.version()
    }

    fn phase_ownership(&self, phase: &Self::Phase) -> PhaseOwnership {
        self.0.phase_ownership(phase)
    }

    fn project(&self, case_ref: CaseRef, state: Option<&Self::State>) -> ViewOf<Self> {
        let odd = case_ref.expected_revision.value() % 2 == 1;
        let mut view = self.0.project(case_ref, state);
        if odd {
            view.notices.push(WorkflowNotice {
                code: "trip.odd_revision".to_owned(),
                severity: NoticeSeverity::Info,
                text: LocalizedText::new("An odd revision."),
            });
        }
        view
    }

    fn operations(&self, view: &ViewOf<Self>) -> Vec<OperationSpec> {
        self.0.operations(view)
    }

    fn compile_act(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        act: &ResolvedAct,
    ) -> Result<Vec<Self::Command>, DomainRejection> {
        self.0.compile_act(state, view, act)
    }

    fn command_policy(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> CommandPolicy {
        self.0.command_policy(state, command)
    }

    fn validate_command(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> Result<(), DomainRejection> {
        self.0.validate_command(state, command)
    }

    fn receipts(
        &self,
        events: &[ReceiptEvent<Self::Event>],
        locale: &Locale,
    ) -> Vec<OperationalReceipt> {
        self.0.receipts(events, locale)
    }

    fn build_interaction(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        requirement: &InteractionRequirement,
    ) -> Result<InteractionSpec, DomainRejection> {
        self.0.build_interaction(state, view, requirement)
    }
}

/// The trip model, retargeted at the wrapper. The associated types are the
/// same, so every method is a plain delegation.
struct SensitiveModel(TripModel);

impl WorkflowModel<RevisionSensitive> for SensitiveModel {
    fn initial_states(&self) -> Vec<Option<TripState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TripState>) -> Vec<TripCommand> {
        self.0.candidate_commands(state)
    }

    fn simulate(
        &self,
        state: Option<&TripState>,
        command: &TripCommand,
    ) -> SimulatedTransition<TripState, TripEvent> {
        self.0.simulate(state, command)
    }
}

struct OffersWhatItCannotCompile(TripWorkflow);

impl WorkflowDefinition for OffersWhatItCannotCompile {
    type State = TripState;
    type Phase = turnframe_test::workflows::trip::TripPhase;
    type Obligation = turnframe_test::workflows::trip::TripObligation;
    type Command = TripCommand;
    type Event = TripEvent;
    type Outcome = TripOutcome;

    fn key(&self) -> WorkflowKey {
        self.0.key()
    }

    fn version(&self) -> WorkflowVersion {
        self.0.version()
    }

    fn phase_ownership(&self, phase: &Self::Phase) -> PhaseOwnership {
        self.0.phase_ownership(phase)
    }

    fn project(&self, case_ref: CaseRef, state: Option<&Self::State>) -> ViewOf<Self> {
        self.0.project(case_ref, state)
    }

    /// One operation more than the compiler knows: the drift this reports.
    fn operations(&self, view: &ViewOf<Self>) -> Vec<OperationSpec> {
        let mut offered = self.0.operations(view);
        if !offered.is_empty() {
            offered.push(
                OperationSpec::new("trip.forgotten")
                    .summary("Offered, and never wired to a command."),
            );
        }
        offered
    }

    fn compile_act(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        act: &ResolvedAct,
    ) -> Result<Vec<Self::Command>, DomainRejection> {
        self.0.compile_act(state, view, act)
    }

    fn command_policy(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> CommandPolicy {
        self.0.command_policy(state, command)
    }

    fn validate_command(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> Result<(), DomainRejection> {
        self.0.validate_command(state, command)
    }

    fn receipts(
        &self,
        events: &[ReceiptEvent<Self::Event>],
        locale: &Locale,
    ) -> Vec<OperationalReceipt> {
        self.0.receipts(events, locale)
    }

    fn build_interaction(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        requirement: &InteractionRequirement,
    ) -> Result<InteractionSpec, DomainRejection> {
        self.0.build_interaction(state, view, requirement)
    }
}

/// The trip model, retargeted at the wrapper. The associated types are the
/// same, so every method is a plain delegation.
struct ForgottenModel(TripModel);

impl WorkflowModel<OffersWhatItCannotCompile> for ForgottenModel {
    fn initial_states(&self) -> Vec<Option<TripState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TripState>) -> Vec<TripCommand> {
        self.0.candidate_commands(state)
    }

    fn simulate(
        &self,
        state: Option<&TripState>,
        command: &TripCommand,
    ) -> SimulatedTransition<TripState, TripEvent> {
        self.0.simulate(state, command)
    }
}

/// The control group for the compile check: a catalogue that offers one
/// operation more than the compiler knows.
///
/// This is the mistake as an adopter meets it — the declaration in one
/// function, the translation in another, and nothing relating them until now.
/// It fails as far downstream as a mistake can: the catalogue offers it, the
/// interpreter proposes it correctly with the right arguments, and the user is
/// told his request could not be carried out on a sentence that was understood
/// perfectly.
#[test]
fn a_catalogue_that_offers_what_it_cannot_compile_is_reported() {
    let report = explore(
        &OffersWhatItCannotCompile(TripWorkflow::default()),
        &ForgottenModel(TripModel::default()),
        ExplorationLimits::smoke(),
    );

    let offered: Vec<String> = report
        .violations
        .iter()
        .filter_map(|violation| match &violation.kind {
            ExplorationViolationKind::CatalogedOperationDoesNotCompile { operation } => {
                Some(operation.as_str().to_owned())
            }
            _ => None,
        })
        .collect();
    assert!(
        offered
            .iter()
            .any(|operation| operation == "trip.forgotten"),
        "the operation nobody wired is named: {offered:?}"
    );
    assert!(
        !offered
            .iter()
            .any(|operation| operation.contains("set_name")),
        "and an operation that refuses because the explorer could not invent \
         its arguments is left alone: {offered:?}"
    );
}

#[test]
fn a_projection_that_reads_the_case_revision_is_reported() {
    let report = explore(
        &RevisionSensitive(TripWorkflow::default()),
        &SensitiveModel(TripModel::default()),
        ExplorationLimits::smoke(),
    );

    let varying: Vec<_> = report
        .violations
        .iter()
        .filter_map(|violation| match &violation.kind {
            ExplorationViolationKind::ProjectionVariesWithRevision {
                left,
                right,
                detail,
            } => Some((*left, *right, detail.clone())),
            _ => None,
        })
        .collect();

    assert!(
        !varying.is_empty(),
        "a projector that reads the revision must be caught: {}",
        report.describe()
    );
    let (left, right, detail) = &varying[0];
    assert_ne!(left, right, "the two revisions must be different");
    assert_ne!(
        left % 2,
        right % 2,
        "the table must pair revisions of different parity for this defect to show"
    );
    assert_eq!(detail, "notices");
    // The revisions come from the fixed table, not from the depth.
    assert!(EXPLORATION_REVISIONS.contains(left));
    assert!(EXPLORATION_REVISIONS.contains(right));
}

#[test]
fn the_sample_workflows_do_not_read_the_case_revision() {
    for report in [
        explore(
            &TripWorkflow::default(),
            &TripModel::default(),
            ExplorationLimits::generous(),
        ),
        explore(
            &TravelerWorkflow::new().with_cards(),
            &TravelerModel::of(TravelerWorkflow::new().with_cards()),
            ExplorationLimits::generous(),
        ),
        explore(
            &ClaimWorkflow::default(),
            &ClaimModel::default(),
            ExplorationLimits::generous(),
        ),
    ] {
        assert!(
            !report.violations.iter().any(|violation| matches!(
                violation.kind,
                ExplorationViolationKind::ProjectionVariesWithRevision { .. }
            )),
            "{}",
            report.describe()
        );
    }
}

#[test]
fn the_trip_model_fills_the_extra_budget_and_revisits_the_second_traveler() {
    let states: Vec<TripState> =
        reachable_states(&TripModel::default(), ExplorationLimits::generous())
            .into_iter()
            .flatten()
            .collect();

    let longest = states
        .iter()
        .map(|state| state.extras.len())
        .max()
        .unwrap_or(0);
    assert_eq!(
        longest, MAX_EXTRAS,
        "the model must reach the domain's own extra cap"
    );
    assert!(
        states.iter().any(|state| state.extras.len() == MAX_EXTRAS
            && state.extras.iter().all(|extra| extra.payer.is_some())),
        "every extra of a full trip must get a payer"
    );

    let with_second_traveler = states
        .iter()
        .filter(|state| {
            state
                .traveler
                .as_ref()
                .is_some_and(|traveler| traveler.traveler_id == OTHER_TRAVELER_ID)
        })
        .count();
    assert!(
        with_second_traveler > 1,
        "the second traveler must be exercised more than once, saw {with_second_traveler} state(s)"
    );
    // Bounded: three traveler values, four extras, a paid prefix, a quote and a kept leg.
    assert!(
        states.len() < 2_500,
        "the state space must stay small enough to explore fast, saw {}",
        states.len()
    );
}

#[test]
fn one_case_resolves_to_the_same_phase_in_two_projections() {
    let workflow = TripWorkflow::default();
    let registry = WorkflowRegistry::builder()
        .register(TripWorkflow::default(), TripExecutor::default())
        .build()
        .expect("one workflow, one key");
    let erased = &registry
        .require(&WorkflowKey::from("trip"))
        .expect("the trip workflow is registered")
        .definition;

    for state in [complete_case(), awaiting_rebooking_confirmation()] {
        let case_ref = CaseRef::new("trip", "trip-1", CaseRevision(4));
        let typed = workflow.project(case_ref.clone(), Some(&state));
        let typed = typed
            .erase(workflow.phase_ownership(&typed.phase))
            .expect("the sample view is serializable");
        let json = serde_json::to_value(&state).expect("the sample state is serializable");
        let through_registry = erased
            .project(case_ref, Some(&json))
            .expect("the erased projection round-trips the state");

        same_phase_in(&typed, &through_registry)
            .expect("the typed and erased projections agree on the phase");
    }
}

#[test]
fn two_projections_that_disagree_about_the_phase_are_reported() {
    let workflow = TripWorkflow::default();
    let case_ref = CaseRef::new("trip", "trip-1", CaseRevision(4));
    let view = |state: &TripState| {
        let view = workflow.project(case_ref.clone(), Some(state));
        view.erase(workflow.phase_ownership(&view.phase))
            .expect("the sample view is serializable")
    };

    let collecting = view(&complete_case());
    let awaiting = view(&awaiting_rebooking_confirmation());
    assert!(matches!(
        same_phase_in(&collecting, &awaiting).unwrap_err(),
        AssertionFailure::PhaseDiffers { .. }
    ));

    let other_case = {
        let view = workflow.project(
            CaseRef::new("trip", "trip-2", CaseRevision(4)),
            Some(&complete_case()),
        );
        view.erase(workflow.phase_ownership(&view.phase))
            .expect("the sample view is serializable")
    };
    assert!(matches!(
        same_phase_in(&collecting, &other_case).unwrap_err(),
        AssertionFailure::DifferentCases { .. }
    ));
}

/// A projector that takes the opposite position: absence *is* the terminal
/// state, so a case whose row was deleted on completion projects as finished.
///
/// This is the shape an adopter arriving from a delete-on-completion design
/// writes, and nothing else in the kit notices it. The executor answers `None`
/// both for a case nobody has created and for a case that was consumed on
/// success, so one view has to serve both, and the assistant congratulates the
/// user and then offers them a brand-new empty draft.
struct EndsByDisappearing(TravelerWorkflow);

impl WorkflowDefinition for EndsByDisappearing {
    type State = TravelerState;
    type Phase = TravelerPhase;
    type Obligation = TravelerObligation;
    type Command = TravelerCommand;
    type Event = TravelerEvent;
    type Outcome = TravelerOutcome;

    fn key(&self) -> WorkflowKey {
        self.0.key()
    }

    fn version(&self) -> WorkflowVersion {
        self.0.version()
    }

    fn phase_ownership(&self, phase: &Self::Phase) -> PhaseOwnership {
        self.0.phase_ownership(phase)
    }

    fn project(&self, case_ref: CaseRef, state: Option<&Self::State>) -> ViewOf<Self> {
        if state.is_none() {
            return WorkflowView::new(case_ref, self.version(), TravelerPhase::Deleted)
                .with_outcome(TravelerOutcome::Deleted);
        }
        self.0.project(case_ref, state)
    }

    fn operations(&self, view: &ViewOf<Self>) -> Vec<OperationSpec> {
        self.0.operations(view)
    }

    fn compile_act(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        act: &ResolvedAct,
    ) -> Result<Vec<Self::Command>, DomainRejection> {
        self.0.compile_act(state, view, act)
    }

    fn command_policy(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> CommandPolicy {
        self.0.command_policy(state, command)
    }

    fn validate_command(
        &self,
        state: Option<&Self::State>,
        command: &Self::Command,
    ) -> Result<(), DomainRejection> {
        self.0.validate_command(state, command)
    }

    fn receipts(
        &self,
        events: &[ReceiptEvent<Self::Event>],
        locale: &Locale,
    ) -> Vec<OperationalReceipt> {
        self.0.receipts(events, locale)
    }

    fn build_interaction(
        &self,
        state: Option<&Self::State>,
        view: &ViewOf<Self>,
        requirement: &InteractionRequirement,
    ) -> Result<InteractionSpec, DomainRejection> {
        self.0.build_interaction(state, view, requirement)
    }
}

/// The traveler model, retargeted at the wrapper. The associated types are the
/// same, so every method is a plain delegation.
struct DisappearingModel(TravelerModel);

impl WorkflowModel<EndsByDisappearing> for DisappearingModel {
    fn initial_states(&self) -> Vec<Option<TravelerState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TravelerState>) -> Vec<TravelerCommand> {
        self.0.candidate_commands(state)
    }

    fn simulate(
        &self,
        state: Option<&TravelerState>,
        command: &TravelerCommand,
    ) -> SimulatedTransition<TravelerState, TravelerEvent> {
        self.0.simulate(state, command)
    }

    fn declared_outcomes(&self) -> Vec<TravelerOutcome> {
        self.0.declared_outcomes()
    }
}

#[test]
fn a_projector_that_ends_a_case_by_disappearing_is_reported() {
    let report = explore(
        &EndsByDisappearing(TravelerWorkflow::new().with_cards()),
        &DisappearingModel(TravelerModel::of(TravelerWorkflow::new().with_cards())),
        ExplorationLimits::generous(),
    );

    let disappearing: Vec<_> = report
        .violations
        .iter()
        .filter(|violation| {
            matches!(
                violation.kind,
                ExplorationViolationKind::CaseEndsByDisappearing { .. }
            )
        })
        .collect();
    assert_eq!(
        disappearing.len(),
        1,
        "the absent state is visited once, and projecting it as finished must be reported: {}",
        report.describe()
    );
    let violation = disappearing[0];
    assert!(
        violation.path.is_empty() && violation.state.is_null(),
        "the offending state is the absent one, reached without a single command: {}",
        violation.describe()
    );
    let ExplorationViolationKind::CaseEndsByDisappearing { phase, outcome } = &violation.kind
    else {
        unreachable!("filtered above");
    };
    assert_eq!(phase, &serde_json::json!("deleted"));
    assert_eq!(outcome.as_ref(), Some(&serde_json::json!("deleted")));
    // The rule is about the absent state alone: every state that exists still
    // projects exactly as the sample does.
    assert!(
        !report.violations.iter().any(|other| !matches!(
            other.kind,
            ExplorationViolationKind::CaseEndsByDisappearing { .. }
        )),
        "only the absent state should be at fault: {}",
        report.describe()
    );
}

/// The traveler model with one change: deleting drops the case instead of
/// moving it to the `Deleted` status.
struct DeletesTheRow(TravelerModel);

impl WorkflowModel<TravelerWorkflow> for DeletesTheRow {
    fn initial_states(&self) -> Vec<Option<TravelerState>> {
        self.0.initial_states()
    }

    fn candidate_commands(&self, state: Option<&TravelerState>) -> Vec<TravelerCommand> {
        self.0.candidate_commands(state)
    }

    fn simulate(
        &self,
        state: Option<&TravelerState>,
        command: &TravelerCommand,
    ) -> SimulatedTransition<TravelerState, TravelerEvent> {
        let transition = self.0.simulate(state, command);
        match (command, transition.is_applied()) {
            (TravelerCommand::Delete, true) => {
                SimulatedTransition::removed(transition.events().to_vec())
            }
            _ => transition,
        }
    }

    fn declared_outcomes(&self) -> Vec<TravelerOutcome> {
        self.0.declared_outcomes()
    }
}

#[test]
fn a_transition_that_drops_the_case_is_reported() {
    let report = explore(
        &TravelerWorkflow::new().with_cards(),
        &DeletesTheRow(TravelerModel::of(TravelerWorkflow::new().with_cards())),
        ExplorationLimits::generous(),
    );

    assert!(!report.truncated, "{}", report.describe());
    let removals: Vec<_> = report
        .violations
        .iter()
        .filter_map(|violation| match &violation.kind {
            ExplorationViolationKind::TransitionRemovesCase { command_type, .. } => {
                Some(command_type.clone())
            }
            _ => None,
        })
        .collect();
    assert!(
        !removals.is_empty(),
        "a model that deletes the row must be reported: {}",
        report.describe()
    );
    assert!(
        removals.iter().all(|command_type| command_type == "delete"),
        "only deleting drops the case, saw {removals:?}"
    );
    // And the second cost of deleting the row, which the first check cannot
    // show: the terminal outcome the domain declares is no longer reachable,
    // because no state is left to project it.
    assert!(!report.reached_outcome(&TravelerOutcome::Deleted));
    assert!(
        report.violations.iter().any(|violation| matches!(
            &violation.kind,
            ExplorationViolationKind::UnreachableOutcome { outcome }
                if outcome == &serde_json::json!("deleted")
        )),
        "{}",
        report.describe()
    );
    assert!(report.reached_outcome(&TravelerOutcome::Archived));
}

#[test]
fn no_sample_workflow_ends_a_case_by_disappearing() {
    for report in [
        explore(
            &TripWorkflow::default(),
            &TripModel::default(),
            ExplorationLimits::generous(),
        ),
        explore(
            &TravelerWorkflow::new().with_cards(),
            &TravelerModel::of(TravelerWorkflow::new().with_cards()),
            ExplorationLimits::generous(),
        ),
        explore(
            &ClaimWorkflow::default(),
            &ClaimModel::default(),
            ExplorationLimits::generous(),
        ),
    ] {
        assert!(
            !report.violations.iter().any(|violation| matches!(
                violation.kind,
                ExplorationViolationKind::CaseEndsByDisappearing { .. }
                    | ExplorationViolationKind::TransitionRemovesCase { .. }
            )),
            "{}",
            report.describe()
        );
    }
}