mindset 0.1.1

A pure functional state machine library built on Stillwater's Effect system
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
//! State machine that executes effectful transitions.

use crate::checkpoint::MachineMetadata;
use crate::core::{State, StateHistory, StateTransition};
use crate::effects::transition::{Transition, TransitionError, TransitionResult};
use chrono::Utc;
use stillwater::effect::Effect;
use stillwater::prelude::*;

/// Result of executing a single step
#[derive(Clone, Debug, PartialEq)]
pub enum StepResult<S: State> {
    /// Successfully transitioned to new state
    Transitioned(S),

    /// Transition should be retried
    Retry { feedback: String, attempts: usize },

    /// Transition aborted permanently
    Aborted { reason: String, error_state: S },
}

/// State machine that executes effectful transitions.
pub struct StateMachine<S: State + 'static, Env: Clone + Send + Sync + 'static> {
    initial: S,
    current: S,
    transitions: Vec<Transition<S, Env>>,
    history: StateHistory<S>,
    attempt_count: usize,
    metadata: MachineMetadata,
}

impl<S: State + 'static, Env: Clone + Send + Sync + 'static> StateMachine<S, Env> {
    /// Create a new state machine in the initial state
    pub fn new(initial: S) -> Self {
        Self {
            initial: initial.clone(),
            current: initial,
            transitions: Vec::new(),
            history: StateHistory::new(),
            attempt_count: 0,
            metadata: MachineMetadata::default(),
        }
    }

    /// Add a transition to the machine
    pub fn add_transition(&mut self, transition: Transition<S, Env>) {
        self.transitions.push(transition);
    }

    /// Get current state (pure)
    pub fn current_state(&self) -> &S {
        &self.current
    }

    /// Check if machine is in a final state (pure)
    pub fn is_final(&self) -> bool {
        self.current.is_final()
    }

    /// Get state history (pure)
    pub fn history(&self) -> &StateHistory<S> {
        &self.history
    }

    /// Execute one step of the state machine.
    /// Returns impl Effect for zero-cost composition.
    /// After running the effect, call apply_result() to update the machine state.
    pub fn step(
        &self,
    ) -> impl Effect<Output = (S, StepResult<S>, usize), Error = TransitionError, Env = Env> + '_
    {
        // Find applicable transition (pure)
        let transition_opt = self
            .transitions
            .iter()
            .find(|t| t.can_execute(&self.current));

        let Some(transition) = transition_opt else {
            return fail(TransitionError::NoTransition {
                from: self.current.name().to_string(),
            })
            .boxed();
        };

        // Get fresh effect from action factory
        let from_state = self.current.clone();
        let attempt_count = self.attempt_count;
        let action = (transition.action)();

        // Execute action and return result with context
        action
            .map(move |result| {
                let step_result = match &result {
                    TransitionResult::Success(new_state) => {
                        StepResult::Transitioned(new_state.clone())
                    }
                    TransitionResult::Retry {
                        feedback,
                        current_state: _,
                    } => StepResult::Retry {
                        feedback: feedback.clone(),
                        attempts: attempt_count + 1,
                    },
                    TransitionResult::Abort {
                        reason,
                        error_state,
                    } => StepResult::Aborted {
                        reason: reason.clone(),
                        error_state: error_state.clone(),
                    },
                };
                (from_state.clone(), step_result, attempt_count)
            })
            .boxed()
    }

    /// Apply the result from step() to update machine state.
    /// Call this after running the effect.
    pub fn apply_result(&mut self, from_state: S, result: StepResult<S>, attempt_count: usize) {
        match result {
            StepResult::Transitioned(new_state) => {
                let transition_record = StateTransition {
                    from: from_state.clone(),
                    to: new_state.clone(),
                    timestamp: Utc::now(),
                    attempt: attempt_count,
                };
                self.history = self.history.record(transition_record);
                self.current = new_state;
                self.attempt_count = 0;
                self.update_metadata(from_state.name().to_string());
            }
            StepResult::Retry { .. } => {
                self.attempt_count += 1;
            }
            StepResult::Aborted { error_state, .. } => {
                self.current = error_state;
            }
        }
    }

    /// Update metadata after transition
    fn update_metadata(&mut self, transition_name: String) {
        self.metadata.updated_at = Utc::now();
        *self
            .metadata
            .total_attempts
            .entry(transition_name)
            .or_insert(0) += 1;
    }

    /// Create a checkpoint of current machine state.
    /// Pure function - does not modify machine.
    pub fn checkpoint(&self) -> crate::checkpoint::Checkpoint<S> {
        use crate::checkpoint::Checkpoint;
        use uuid::Uuid;

        Checkpoint {
            version: crate::checkpoint::CHECKPOINT_VERSION,
            id: Uuid::new_v4().to_string(),
            timestamp: Utc::now(),
            initial_state: self.initial.clone(),
            current_state: self.current.clone(),
            history: self.history.clone(),
            metadata: self.metadata.clone(),
        }
    }

    /// Serialize to JSON string
    pub fn to_json(&self) -> Result<String, crate::checkpoint::CheckpointError> {
        let checkpoint = self.checkpoint();
        serde_json::to_string_pretty(&checkpoint)
            .map_err(|e| crate::checkpoint::CheckpointError::SerializationFailed(e.to_string()))
    }

    /// Serialize to binary format
    pub fn to_binary(&self) -> Result<Vec<u8>, crate::checkpoint::CheckpointError> {
        let checkpoint = self.checkpoint();
        bincode::serialize(&checkpoint)
            .map_err(|e| crate::checkpoint::CheckpointError::SerializationFailed(e.to_string()))
    }

    /// Create state machine from checkpoint.
    /// Transitions must be provided (not serializable).
    pub fn from_checkpoint(
        checkpoint: crate::checkpoint::Checkpoint<S>,
        transitions: Vec<Transition<S, Env>>,
    ) -> Result<Self, crate::checkpoint::CheckpointError> {
        use crate::checkpoint::CHECKPOINT_VERSION;

        // Validate version
        if checkpoint.version > CHECKPOINT_VERSION {
            return Err(crate::checkpoint::CheckpointError::UnsupportedVersion {
                found: checkpoint.version,
                supported: CHECKPOINT_VERSION,
            });
        }

        Ok(Self {
            initial: checkpoint.initial_state,
            current: checkpoint.current_state,
            transitions,
            history: checkpoint.history,
            attempt_count: 0,
            metadata: checkpoint.metadata,
        })
    }

    /// Deserialize from JSON string
    pub fn from_json(
        json: &str,
        transitions: Vec<Transition<S, Env>>,
    ) -> Result<Self, crate::checkpoint::CheckpointError> {
        let checkpoint: crate::checkpoint::Checkpoint<S> =
            serde_json::from_str(json).map_err(|e| {
                crate::checkpoint::CheckpointError::DeserializationFailed(e.to_string())
            })?;

        Self::from_checkpoint(checkpoint, transitions)
    }

    /// Deserialize from binary format
    pub fn from_binary(
        bytes: &[u8],
        transitions: Vec<Transition<S, Env>>,
    ) -> Result<Self, crate::checkpoint::CheckpointError> {
        let checkpoint: crate::checkpoint::Checkpoint<S> =
            bincode::deserialize(bytes).map_err(|e| {
                crate::checkpoint::CheckpointError::DeserializationFailed(e.to_string())
            })?;

        Self::from_checkpoint(checkpoint, transitions)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Guard;
    use crate::effects::transition::{Transition, TransitionResult};
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;

    #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
    enum WorkflowState {
        Initial,
        Processing,
        Complete,
        Failed,
    }

    impl State for WorkflowState {
        fn name(&self) -> &str {
            match self {
                Self::Initial => "Initial",
                Self::Processing => "Processing",
                Self::Complete => "Complete",
                Self::Failed => "Failed",
            }
        }

        fn is_final(&self) -> bool {
            matches!(self, Self::Complete | Self::Failed)
        }
    }

    #[derive(Clone)]
    struct TestEnv {
        _should_succeed: bool,
    }

    #[tokio::test]
    async fn simple_transition_succeeds() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        let transition = Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        };

        machine.add_transition(transition);

        let env = TestEnv {
            _should_succeed: true,
        };
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();
        machine.apply_result(from, result, attempt);

        assert_eq!(machine.current_state(), &WorkflowState::Processing);
        assert_eq!(machine.history().transitions().len(), 1);
    }

    #[tokio::test]
    async fn guard_blocks_transition() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        let guard = Guard::new(|s: &WorkflowState| s.is_final());

        let transition = Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: Some(guard),
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        };

        machine.add_transition(transition);

        let env = TestEnv {
            _should_succeed: true,
        };
        let result = machine.step().run(&env).await;

        // Should fail because Initial is not final
        assert!(result.is_err());
        assert_eq!(machine.current_state(), &WorkflowState::Initial);
    }

    #[tokio::test]
    async fn retry_increments_attempt_count() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        let transition = Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| {
                pure(TransitionResult::Retry {
                    feedback: "Not ready yet".to_string(),
                    current_state: WorkflowState::Initial,
                })
                .boxed()
            }),
        };

        machine.add_transition(transition);

        let env = TestEnv {
            _should_succeed: false,
        };
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();

        match &result {
            StepResult::Retry { attempts, .. } => assert_eq!(*attempts, 1),
            _ => panic!("Expected Retry result"),
        }
        machine.apply_result(from, result, attempt);

        // Second attempt
        let (from2, result2, attempt2) = machine.step().run(&env).await.unwrap();
        match &result2 {
            StepResult::Retry { attempts, .. } => assert_eq!(*attempts, 2),
            _ => panic!("Expected Retry result"),
        }
        machine.apply_result(from2, result2, attempt2);
    }

    #[tokio::test]
    async fn effectful_action_with_environment() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        let transition = Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| {
                from_fn(|env: &TestEnv| {
                    if env._should_succeed {
                        Ok(TransitionResult::Success(WorkflowState::Processing))
                    } else {
                        Ok(TransitionResult::Abort {
                            reason: "Environment not ready".to_string(),
                            error_state: WorkflowState::Failed,
                        })
                    }
                })
                .boxed()
            }),
        };

        machine.add_transition(transition);

        let env = TestEnv {
            _should_succeed: true,
        };
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();

        assert!(matches!(result, StepResult::Transitioned(_)));
        machine.apply_result(from, result, attempt);
        assert_eq!(machine.current_state(), &WorkflowState::Processing);
    }

    #[tokio::test]
    async fn abort_changes_state() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        let transition = Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| {
                pure(TransitionResult::Abort {
                    reason: "Something went wrong".to_string(),
                    error_state: WorkflowState::Failed,
                })
                .boxed()
            }),
        };

        machine.add_transition(transition);

        let env = TestEnv {
            _should_succeed: false,
        };
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();

        match &result {
            StepResult::Aborted { error_state, .. } => {
                assert_eq!(*error_state, WorkflowState::Failed);
            }
            _ => panic!("Expected Aborted result"),
        }
        machine.apply_result(from, result, attempt);
        assert_eq!(machine.current_state(), &WorkflowState::Failed);
    }

    #[tokio::test]
    async fn checkpoint_serializes_to_json() {
        let machine = StateMachine::<WorkflowState, TestEnv>::new(WorkflowState::Initial);
        let json = machine.to_json().unwrap();

        // Verify it's valid JSON
        assert!(serde_json::from_str::<serde_json::Value>(&json).is_ok());

        // Verify contains expected fields
        assert!(json.contains("version"));
        assert!(json.contains("current_state"));
        assert!(json.contains("history"));
    }

    #[tokio::test]
    async fn checkpoint_roundtrip_preserves_state() {
        let mut machine1 = StateMachine::new(WorkflowState::Initial);

        machine1.add_transition(Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        });

        machine1.add_transition(Transition {
            from: WorkflowState::Processing,
            to: WorkflowState::Complete,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Complete)).boxed()),
        });

        // Execute some transitions
        let env = TestEnv {
            _should_succeed: true,
        };
        let (from, result, attempt) = machine1.step().run(&env).await.unwrap();
        machine1.apply_result(from, result, attempt);

        let (from2, result2, attempt2) = machine1.step().run(&env).await.unwrap();
        machine1.apply_result(from2, result2, attempt2);

        // Checkpoint and restore
        let json = machine1.to_json().unwrap();

        let transitions: Vec<Transition<WorkflowState, TestEnv>> = vec![
            Transition {
                from: WorkflowState::Initial,
                to: WorkflowState::Processing,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Processing)).boxed()
                }),
            },
            Transition {
                from: WorkflowState::Processing,
                to: WorkflowState::Complete,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Complete)).boxed()
                }),
            },
        ];

        let machine2 = StateMachine::from_json(&json, transitions).unwrap();

        // Verify state preserved
        assert_eq!(machine1.current_state(), machine2.current_state());
        assert_eq!(
            machine1.history().transitions().len(),
            machine2.history().transitions().len()
        );
    }

    #[test]
    fn binary_format_smaller_than_json() {
        let machine = StateMachine::<WorkflowState, TestEnv>::new(WorkflowState::Initial);

        let json = machine.to_json().unwrap();
        let binary = machine.to_binary().unwrap();

        // Binary should be significantly smaller
        assert!(binary.len() < json.len() / 2);
    }

    #[tokio::test]
    async fn resumed_machine_can_continue_execution() {
        let mut machine1 = StateMachine::new(WorkflowState::Initial);
        let env = TestEnv {
            _should_succeed: true,
        };

        machine1.add_transition(Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        });

        machine1.add_transition(Transition {
            from: WorkflowState::Processing,
            to: WorkflowState::Complete,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Complete)).boxed()),
        });

        // Execute first transition
        let (from, result, attempt) = machine1.step().run(&env).await.unwrap();
        machine1.apply_result(from, result, attempt);
        assert_eq!(machine1.current_state(), &WorkflowState::Processing);

        // Checkpoint
        let json = machine1.to_json().unwrap();

        // Resume
        let transitions: Vec<Transition<WorkflowState, TestEnv>> = vec![
            Transition {
                from: WorkflowState::Initial,
                to: WorkflowState::Processing,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Processing)).boxed()
                }),
            },
            Transition {
                from: WorkflowState::Processing,
                to: WorkflowState::Complete,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Complete)).boxed()
                }),
            },
        ];
        let mut machine2 = StateMachine::from_json(&json, transitions).unwrap();

        // Should be able to continue from where we left off
        let (from2, result2, attempt2) = machine2.step().run(&env).await.unwrap();
        machine2.apply_result(from2, result2, attempt2);
        assert_eq!(machine2.current_state(), &WorkflowState::Complete);
    }

    #[test]
    fn unsupported_version_returns_error() {
        use crate::checkpoint::Checkpoint;
        use uuid::Uuid;

        let checkpoint = Checkpoint {
            version: 999,
            id: Uuid::new_v4().to_string(),
            timestamp: Utc::now(),
            initial_state: WorkflowState::Initial,
            current_state: WorkflowState::Initial,
            history: crate::core::StateHistory::new(),
            metadata: crate::checkpoint::MachineMetadata::default(),
        };

        let json = serde_json::to_string(&checkpoint).unwrap();
        let result = StateMachine::<WorkflowState, TestEnv>::from_json(&json, vec![]);

        assert!(matches!(
            result,
            Err(crate::checkpoint::CheckpointError::UnsupportedVersion { .. })
        ));
    }
}

#[cfg(test)]
mod integration_tests {
    use super::*;
    use crate::effects::transition::{Transition, TransitionResult};
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;

    #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
    enum WorkflowState {
        Initial,
        Processing,
        Complete,
    }

    impl State for WorkflowState {
        fn name(&self) -> &str {
            match self {
                Self::Initial => "Initial",
                Self::Processing => "Processing",
                Self::Complete => "Complete",
            }
        }

        fn is_final(&self) -> bool {
            matches!(self, Self::Complete)
        }
    }

    #[derive(Clone)]
    struct TestEnv {
        _should_succeed: bool,
    }

    #[tokio::test]
    async fn multi_step_workflow() {
        let mut machine = StateMachine::new(WorkflowState::Initial);

        // Initial -> Processing
        machine.add_transition(Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        });

        // Processing -> Complete
        machine.add_transition(Transition {
            from: WorkflowState::Processing,
            to: WorkflowState::Complete,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Complete)).boxed()),
        });

        let env = TestEnv {
            _should_succeed: true,
        };

        // First step
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();
        machine.apply_result(from, result, attempt);
        assert_eq!(machine.current_state(), &WorkflowState::Processing);

        // Second step
        let (from2, result2, attempt2) = machine.step().run(&env).await.unwrap();
        machine.apply_result(from2, result2, attempt2);
        assert_eq!(machine.current_state(), &WorkflowState::Complete);
        assert!(machine.is_final());

        // Verify history
        let path = machine.history().get_path();
        assert_eq!(path.len(), 3);
        assert_eq!(path[0], &WorkflowState::Initial);
        assert_eq!(path[1], &WorkflowState::Processing);
        assert_eq!(path[2], &WorkflowState::Complete);
    }

    #[tokio::test]
    async fn checkpoint_resume_preserves_history() {
        let mut machine = StateMachine::new(WorkflowState::Initial);
        let env = TestEnv {
            _should_succeed: true,
        };

        // Add all transitions
        machine.add_transition(Transition {
            from: WorkflowState::Initial,
            to: WorkflowState::Processing,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Processing)).boxed()),
        });

        machine.add_transition(Transition {
            from: WorkflowState::Processing,
            to: WorkflowState::Complete,
            guard: None,
            action: Arc::new(|| pure(TransitionResult::Success(WorkflowState::Complete)).boxed()),
        });

        // Execute first step
        let (from, result, attempt) = machine.step().run(&env).await.unwrap();
        machine.apply_result(from, result, attempt);

        // Save original history
        let original_history = machine.history().transitions().to_vec();

        // Checkpoint and resume
        let json = machine.to_json().unwrap();
        let transitions: Vec<Transition<WorkflowState, TestEnv>> = vec![
            Transition {
                from: WorkflowState::Initial,
                to: WorkflowState::Processing,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Processing)).boxed()
                }),
            },
            Transition {
                from: WorkflowState::Processing,
                to: WorkflowState::Complete,
                guard: None,
                action: Arc::new(|| {
                    pure(TransitionResult::Success(WorkflowState::Complete)).boxed()
                }),
            },
        ];
        let restored = StateMachine::from_json(&json, transitions).unwrap();

        let restored_history = restored.history().transitions();

        // History should be identical
        assert_eq!(original_history.len(), restored_history.len());
        for (orig, restored) in original_history.iter().zip(restored_history.iter()) {
            assert_eq!(orig.from, restored.from);
            assert_eq!(orig.to, restored.to);
            assert_eq!(orig.attempt, restored.attempt);
        }
    }
}