plotline 0.3.0

Authored sequences, gates, and unlock trees as plain data for any game engine
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
//! The runner: a cursor over the steps of a library.

use alloc::vec::Vec;
use core::fmt::{Display, Formatter, Result as FmtResult};
use core::mem;

use crate::rules::Has;
use crate::sequence::{Library, SequenceRef, Step};

/// Limits that stop runaway content.
///
/// Content can loop forever without any action for the host, for example
/// two sequences that jump to each other. The runner aborts such a chain
/// instead of hanging.
///
/// ```
/// use plotline::{Limits, Runner};
///
/// let mut limits = Limits::default();
/// limits.call_depth = 8;
/// let runner = Runner::new(limits);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
    /// The most sequences a chain may stack through calls. The first
    /// sequence counts as one. The default is 32.
    pub call_depth: usize,
    /// The most steps one [`Runner::advance`] or [`Runner::resume`] may run.
    /// The count includes the step that hands out the action. The default is
    /// 10 000.
    pub steps_per_advance: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            call_depth: 32,
            steps_per_advance: 10_000,
        }
    }
}

/// What the runner reports after it moves.
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use = "an unread status loses the action the host must perform"]
pub enum Status<'a, A> {
    /// The host performs this action, then calls [`Runner::resume`].
    Act(&'a A),
    /// The runner waits for the answer to an action it already handed out.
    Waiting,
    /// The chain ended.
    Finished,
    /// A limit or a missing sequence stopped the chain.
    Aborted(Abort),
    /// No chain is running.
    Idle,
}

/// Why the runner stopped a chain.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Abort {
    /// A call went deeper than [`Limits::call_depth`].
    CallDepth,
    /// One advance ran more steps than [`Limits::steps_per_advance`].
    StepLimit,
    /// The chain reached a sequence that the library does not hold.
    MissingSequence(SequenceRef),
}

impl Display for Abort {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::CallDepth => f.write_str("the call depth limit was reached"),
            Self::StepLimit => f.write_str("the step limit was reached before an action"),
            Self::MissingSequence(name) => write!(f, "the library has no sequence '{name}'"),
        }
    }
}

/// The host's answer to an action.
///
/// Most actions finish with [`Answer::Done`]. An action can also steer the
/// chain, which is how a dialog choice jumps to the branch the player picked.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Answer {
    /// The action finished. The chain continues with the next step.
    Done,
    /// Runs a sequence, then continues after the action.
    Call(SequenceRef),
    /// Clears the call stack and starts a sequence. `None` ends the chain.
    Goto(Option<SequenceRef>),
    /// Leaves the current sequence, as [`Step::Return`] does.
    Return,
}

impl Answer {
    /// Creates an answer that runs `sequence`, then continues.
    #[must_use]
    pub fn call(sequence: impl Into<SequenceRef>) -> Self {
        Self::Call(sequence.into())
    }

    /// Creates an answer that clears the call stack and starts `sequence`.
    #[must_use]
    pub fn goto(sequence: impl Into<SequenceRef>) -> Self {
        Self::Goto(Some(sequence.into()))
    }

    /// Creates an answer that ends the chain.
    #[must_use]
    pub const fn stop() -> Self {
        Self::Goto(None)
    }

    fn to_move(&self) -> Move<'_> {
        match self {
            Self::Done => Move::Next,
            Self::Call(target) => Move::Call(target),
            Self::Goto(target) => Move::Goto(target.as_ref()),
            Self::Return => Move::Return,
        }
    }
}

/// [`Runner::start`] refused, because a chain is already running.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Busy;

impl Display for Busy {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("a chain is already running")
    }
}

impl core::error::Error for Busy {}

/// Runs one chain of sequences at a time.
///
/// The runner is a cursor. [`Runner::advance`] runs control flow until it
/// reaches an action, then hands that action to the host and waits. The
/// host performs the action in its own code and calls [`Runner::resume`]
/// with its [`Answer`]. The wait is the gap between those two calls, so the
/// runner needs no clock, callback, or async runtime.
///
/// The runner holds only its limits and a stack of positions, so it clones,
/// compares, and, with the `serde` feature, saves in the middle of a chain.
/// A save holds the positions and not the limits, which belong to the build
/// that loads it: a loaded runner has the default limits until
/// [`Runner::set_limits`]. A saved runner resumes against the same library
/// it ran on.
///
/// ```
/// use std::collections::BTreeSet;
/// use plotline::{Answer, Library, Requirement, Runner, Status, Step};
///
/// enum Action {
///     Say(&'static str),
/// }
///
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// enum Key {
///     HasRing,
/// }
///
/// let mut library = Library::new();
/// library.insert(
///     "hub",
///     [
///         Step::act(Action::Say("Have you found my ring?")),
///         Step::branch(Requirement::has(Key::HasRing), "thanks", "later"),
///     ],
/// );
/// library.insert("thanks", [Step::act(Action::Say("You have my thanks."))]);
/// library.insert("later", [Step::act(Action::Say("Come back when you have."))]);
///
/// let held = BTreeSet::from([Key::HasRing]);
/// let mut runner = Runner::default();
/// runner.start("hub").unwrap();
///
/// let mut said = Vec::new();
/// let mut status = runner.advance(&library, &held);
/// while let Status::Act(Action::Say(line)) = status {
///     said.push(*line);
///     status = runner.resume(Answer::Done, &library, &held);
/// }
///
/// assert!(matches!(status, Status::Finished));
/// assert_eq!(said, ["Have you found my ring?", "You have my thanks."]);
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct Runner {
    #[cfg_attr(feature = "serde", serde(skip))]
    limits: Limits,
    chain: Option<Chain>,
}

impl Runner {
    /// Creates an idle runner with these limits.
    #[must_use]
    pub fn new(limits: Limits) -> Self {
        Self {
            limits,
            chain: None,
        }
    }

    /// Replaces the limits. A running chain uses them from its next step.
    pub fn set_limits(&mut self, limits: Limits) {
        self.limits = limits;
    }

    /// Starts a chain at the first step of `sequence`.
    ///
    /// Nothing runs until [`Runner::advance`]. A missing sequence aborts the
    /// chain there.
    ///
    /// # Errors
    ///
    /// Returns [`Busy`] when a chain is already running.
    pub fn start(&mut self, sequence: impl Into<SequenceRef>) -> Result<(), Busy> {
        if self.chain.is_some() {
            return Err(Busy);
        }
        self.chain = Some(Chain {
            current: Frame::first_step_of(sequence.into()),
            callers: Vec::new(),
            waiting: false,
        });
        Ok(())
    }

    /// Runs the chain until it reaches an action or ends.
    ///
    /// While an action waits for its answer, this returns
    /// [`Status::Waiting`] and changes nothing, so a host can call it once
    /// per frame. `holder` answers the rules of `When` and `Branch` steps.
    pub fn advance<'l, A, K>(
        &mut self,
        library: &'l Library<A, K>,
        holder: &(impl Has<K> + ?Sized),
    ) -> Status<'l, A> {
        match &self.chain {
            None => Status::Idle,
            Some(chain) if chain.waiting => Status::Waiting,
            Some(_) => self.run(library, holder),
        }
    }

    /// Answers the waiting action, then runs the chain as
    /// [`Runner::advance`] does.
    ///
    /// Call it only after [`Status::Act`]. With no chain, it returns
    /// [`Status::Idle`]. While a chain runs with no waiting action, a debug
    /// build panics, and a release build ignores the answer and advances.
    #[expect(
        clippy::needless_pass_by_value,
        reason = "an answer is a command the host hands over, like a message"
    )]
    pub fn resume<'l, A, K>(
        &mut self,
        answer: Answer,
        library: &'l Library<A, K>,
        holder: &(impl Has<K> + ?Sized),
    ) -> Status<'l, A> {
        let Some(chain) = self.chain.as_mut() else {
            return Status::Idle;
        };
        debug_assert!(chain.waiting, "resume was called with no waiting action");
        if chain.waiting {
            chain.waiting = false;
            if let Some(end) = chain.apply(answer.to_move(), &self.limits) {
                self.chain = None;
                return end.into_status();
            }
        }
        self.run(library, holder)
    }

    /// Ends the chain. Returns whether one was running.
    pub fn stop(&mut self) -> bool {
        self.chain.take().is_some()
    }

    /// Returns whether a chain is running.
    #[must_use]
    pub fn is_running(&self) -> bool {
        self.chain.is_some()
    }

    /// Returns the sequence and step index the cursor is at.
    #[must_use]
    pub fn current(&self) -> Option<(&SequenceRef, usize)> {
        let frame = &self.chain.as_ref()?.current;
        Some((&frame.sequence, frame.index))
    }

    /// Returns the action that waits for an answer.
    ///
    /// A host that loads a saved runner calls this to show the pending action
    /// again. It returns `None` when no action waits, or when `library` no
    /// longer holds an action at the cursor.
    #[must_use]
    pub fn waiting_on<'l, A, K>(&self, library: &'l Library<A, K>) -> Option<&'l A> {
        let chain = self.chain.as_ref().filter(|chain| chain.waiting)?;
        let steps = library.get(chain.current.sequence.as_str())?;
        innermost_action(steps.get(chain.current.index)?)
    }

    fn run<'l, A, K>(
        &mut self,
        library: &'l Library<A, K>,
        holder: &(impl Has<K> + ?Sized),
    ) -> Status<'l, A> {
        let Some(chain) = self.chain.as_mut() else {
            return Status::Idle;
        };
        let status = chain.run(library, holder, &self.limits);
        if !matches!(status, Status::Act(_)) {
            self.chain = None;
        }
        status
    }
}

/// One running chain: a call stack of positions.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct Chain {
    current: Frame,
    callers: Vec<Frame>,
    waiting: bool,
}

/// A position in one sequence. A caller's index stays on its calling step
/// until the callee returns.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct Frame {
    sequence: SequenceRef,
    index: usize,
}

impl Frame {
    fn first_step_of(sequence: SequenceRef) -> Self {
        Self { sequence, index: 0 }
    }
}

/// A change to the cursor, decided by a step or by the host's answer.
#[derive(Clone, Copy)]
enum Move<'a> {
    Next,
    Call(&'a SequenceRef),
    Goto(Option<&'a SequenceRef>),
    Return,
}

/// What one step asks for, once its rules are read.
enum Decision<'l, A> {
    Act(&'l A),
    Move(Move<'l>),
}

/// Why a chain ended.
enum End {
    Finished,
    Aborted(Abort),
}

impl End {
    fn into_status<'l, A>(self) -> Status<'l, A> {
        match self {
            Self::Finished => Status::Finished,
            Self::Aborted(abort) => Status::Aborted(abort),
        }
    }
}

impl Chain {
    /// Runs steps until one hands out an action or the chain ends. Never
    /// returns `Waiting` or `Idle`.
    fn run<'l, A, K>(
        &mut self,
        library: &'l Library<A, K>,
        holder: &(impl Has<K> + ?Sized),
        limits: &Limits,
    ) -> Status<'l, A> {
        let mut steps = 0;
        loop {
            let Some(sequence) = library.get(self.current.sequence.as_str()) else {
                let missing = self.current.sequence.clone();
                return Status::Aborted(Abort::MissingSequence(missing));
            };
            let next = match sequence.get(self.current.index) {
                // Falling off the end of a sequence returns from it.
                None => Move::Return,
                Some(step) => {
                    steps += 1;
                    if steps > limits.steps_per_advance {
                        return Status::Aborted(Abort::StepLimit);
                    }
                    match decide(step, holder) {
                        Decision::Act(action) => {
                            self.waiting = true;
                            return Status::Act(action);
                        }
                        Decision::Move(next) => next,
                    }
                }
            };
            if let Some(end) = self.apply(next, limits) {
                return end.into_status();
            }
        }
    }

    /// Moves the cursor. Returns why the chain ended, if it did.
    fn apply(&mut self, next: Move<'_>, limits: &Limits) -> Option<End> {
        // A save file can hold any index, so a corrupt one must not overflow.
        match next {
            Move::Next => self.current.index = self.current.index.saturating_add(1),
            Move::Call(target) => {
                let depth = self.callers.len() + 1;
                if depth >= limits.call_depth {
                    return Some(End::Aborted(Abort::CallDepth));
                }
                let entry = Frame::first_step_of(target.clone());
                let caller = mem::replace(&mut self.current, entry);
                self.callers.push(caller);
            }
            Move::Goto(Some(target)) => {
                self.callers.clear();
                self.current = Frame::first_step_of(target.clone());
            }
            Move::Goto(None) => return Some(End::Finished),
            Move::Return => match self.callers.pop() {
                Some(caller) => {
                    self.current = caller;
                    self.current.index = self.current.index.saturating_add(1);
                }
                None => return Some(End::Finished),
            },
        }
        None
    }
}

fn decide<'l, A, K>(step: &'l Step<A, K>, holder: &(impl Has<K> + ?Sized)) -> Decision<'l, A> {
    match step {
        Step::Act(action) => Decision::Act(action),
        Step::When { rule, step } => {
            if rule.satisfies(holder) {
                decide(step, holder)
            } else {
                Decision::Move(Move::Next)
            }
        }
        Step::Branch {
            rule,
            if_true,
            if_false,
        } => {
            let target = if rule.satisfies(holder) {
                if_true
            } else {
                if_false
            };
            Decision::Move(Move::Goto(target.as_ref()))
        }
        Step::Call(target) => Decision::Move(Move::Call(target)),
        Step::Goto(target) => Decision::Move(Move::Goto(target.as_ref())),
        Step::Return => Decision::Move(Move::Return),
    }
}

fn innermost_action<A, K>(step: &Step<A, K>) -> Option<&A> {
    match step {
        Step::Act(action) => Some(action),
        Step::When { step, .. } => innermost_action(step),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use alloc::collections::BTreeSet;
    use alloc::format;
    use alloc::string::ToString;
    use alloc::vec;

    use super::*;
    use crate::rules::Requirement;

    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
    enum Key {
        Ring,
    }

    type Script = Library<&'static str, Key>;

    fn acts(names: &[&'static str]) -> Vec<Step<&'static str, Key>> {
        names.iter().map(|name| Step::act(*name)).collect()
    }

    /// Drives a chain to its end and records every action the host saw.
    fn drive<'l>(
        runner: &mut Runner,
        library: &'l Script,
        held: &BTreeSet<Key>,
        answer: impl Fn(&str) -> Answer,
    ) -> (Vec<&'static str>, Status<'l, &'static str>) {
        let mut seen = Vec::new();
        let mut status = runner.advance(library, held);
        while let Status::Act(action) = status {
            seen.push(*action);
            status = runner.resume(answer(action), library, held);
        }
        (seen, status)
    }

    fn run(
        library: &Script,
        start: &str,
        held: &[Key],
    ) -> (Vec<&'static str>, Status<'static, &'static str>) {
        let mut runner = Runner::default();
        runner.start(start).unwrap();
        let held = held.iter().copied().collect();
        let (seen, status) = drive(&mut runner, library, &held, |_| Answer::Done);
        let status = match status {
            Status::Finished => Status::Finished,
            Status::Aborted(abort) => Status::Aborted(abort),
            other => panic!("a driven chain ended with {other:?}"),
        };
        (seen, status)
    }

    #[test]
    fn a_sequence_hands_out_its_actions_in_order() {
        let mut library = Script::new();
        library.insert("main", acts(&["a", "b", "c"]));
        assert_eq!(
            run(&library, "main", &[]),
            (vec!["a", "b", "c"], Status::Finished)
        );

        library.insert("empty", []);
        assert_eq!(run(&library, "empty", &[]), (vec![], Status::Finished));
    }

    #[test]
    fn a_when_step_runs_only_when_its_rule_holds() {
        let mut library = Script::new();
        library.insert(
            "main",
            [
                Step::act("greet"),
                Step::when(Requirement::has(Key::Ring), Step::act("thank")),
                Step::act("leave"),
            ],
        );
        assert_eq!(run(&library, "main", &[]).0, ["greet", "leave"]);
        assert_eq!(
            run(&library, "main", &[Key::Ring]).0,
            ["greet", "thank", "leave"]
        );
    }

    #[test]
    fn a_branch_starts_the_side_its_rule_picks() {
        let mut library = Script::new();
        library.insert(
            "main",
            [
                Step::branch(Requirement::has(Key::Ring), "yes", "no"),
                Step::act("never"),
            ],
        );
        library.insert("yes", acts(&["yes"]));
        library.insert("no", acts(&["no"]));
        assert_eq!(run(&library, "main", &[Key::Ring]).0, ["yes"]);
        assert_eq!(run(&library, "main", &[]).0, ["no"]);

        library.insert(
            "ending",
            [Step::Branch {
                rule: Requirement::has(Key::Ring),
                if_true: Some("yes".into()),
                if_false: None,
            }],
        );
        assert_eq!(run(&library, "ending", &[]), (vec![], Status::Finished));
    }

    #[test]
    fn a_call_returns_to_the_step_after_it() {
        let mut library = Script::new();
        library.insert(
            "main",
            [
                Step::act("before"),
                Step::call("sub"),
                Step::when(Requirement::has(Key::Ring), Step::call("sub")),
                Step::act("after"),
            ],
        );
        library.insert("sub", acts(&["inside"]));
        assert_eq!(
            run(&library, "main", &[Key::Ring]).0,
            ["before", "inside", "inside", "after"]
        );
    }

    #[test]
    fn return_leaves_a_sequence_early() {
        let mut library = Script::new();
        library.insert(
            "main",
            [Step::call("sub"), Step::act("after"), Step::Return],
        );
        library.insert(
            "sub",
            [Step::act("inside"), Step::Return, Step::act("skipped")],
        );
        library.insert("root", [Step::Return, Step::act("skipped")]);

        assert_eq!(run(&library, "main", &[]).0, ["inside", "after"]);
        assert_eq!(run(&library, "root", &[]), (vec![], Status::Finished));
    }

    #[test]
    fn goto_clears_the_call_stack() {
        let mut library = Script::new();
        library.insert("main", [Step::call("sub"), Step::act("never")]);
        library.insert("sub", [Step::goto("other")]);
        library.insert("other", acts(&["other"]));
        library.insert(
            "stop",
            [Step::act("last"), Step::stop(), Step::act("never")],
        );

        assert_eq!(
            run(&library, "main", &[]),
            (vec!["other"], Status::Finished)
        );
        assert_eq!(run(&library, "stop", &[]), (vec!["last"], Status::Finished));
    }

    #[test]
    fn an_answer_steers_the_chain() {
        let mut library = Script::new();
        library.insert(
            "main",
            [Step::act("call"), Step::act("goto"), Step::act("never")],
        );
        library.insert("sub", [Step::act("return"), Step::act("never")]);
        library.insert("other", [Step::act("stop"), Step::act("never")]);

        let mut runner = Runner::default();
        runner.start("main").unwrap();
        let (seen, status) = drive(
            &mut runner,
            &library,
            &BTreeSet::new(),
            |action| match action {
                "call" => Answer::call("sub"),
                "return" => Answer::Return,
                "goto" => Answer::goto("other"),
                "stop" => Answer::stop(),
                _ => Answer::Done,
            },
        );
        assert_eq!(seen, ["call", "return", "goto", "stop"]);
        assert_eq!(status, Status::Finished);
    }

    #[test]
    fn the_runner_waits_for_an_answer() {
        let mut library = Script::new();
        library.insert("main", [Step::call("sub"), Step::act("after")]);
        library.insert(
            "sub",
            [Step::when(Requirement::has(Key::Ring), Step::act("ask"))],
        );
        let held = BTreeSet::from([Key::Ring]);

        let mut runner = Runner::default();
        runner.start("main").unwrap();
        assert_eq!(runner.waiting_on(&library), None, "nothing handed out yet");
        assert_eq!(runner.advance(&library, &held), Status::Act(&"ask"));
        assert_eq!(runner.advance(&library, &held), Status::Waiting);
        assert_eq!(runner.waiting_on(&library), Some(&"ask"));
        assert_eq!(runner.current(), Some((&"sub".into(), 0)));

        assert_eq!(
            runner.resume(Answer::Done, &library, &held),
            Status::Act(&"after")
        );
        assert_eq!(runner.current(), Some((&"main".into(), 1)));
    }

    #[test]
    fn one_chain_runs_at_a_time() {
        let mut library = Script::new();
        library.insert("main", acts(&["a"]));
        let held = BTreeSet::new();

        let mut runner = Runner::default();
        assert_eq!(runner.advance(&library, &held), Status::Idle);
        assert!(!runner.stop());

        runner.start("main").unwrap();
        assert_eq!(runner.start("main"), Err(Busy));
        assert!(runner.is_running());
        assert!(runner.stop());
        assert!(!runner.is_running());
        assert_eq!(runner.waiting_on(&library), None);

        runner.start("main").unwrap();
        assert_eq!(runner.advance(&library, &held), Status::Act(&"a"));
        assert_eq!(
            runner.resume(Answer::Done, &library, &held),
            Status::Finished
        );
        assert_eq!(runner.advance(&library, &held), Status::Idle);
        assert_eq!(runner.resume(Answer::Done, &library, &held), Status::Idle);
        assert_eq!(runner.start("main"), Ok(()));
    }

    #[test]
    fn a_missing_sequence_aborts_with_its_name() {
        let mut library = Script::new();
        library.insert("main", [Step::act("a"), Step::call("absent")]);

        let missing = Status::Aborted(Abort::MissingSequence("absent".into()));
        assert_eq!(run(&library, "main", &[]), (vec!["a"], missing.clone()));
        assert_eq!(run(&library, "absent", &[]), (vec![], missing));
    }

    #[test]
    fn a_deep_call_aborts_at_the_call_depth() {
        let mut library = Script::new();
        library.insert("a", [Step::call("b")]);
        library.insert("b", [Step::call("c")]);
        library.insert("c", acts(&["deep"]));
        let held = BTreeSet::new();

        let mut runner = Runner::new(Limits {
            call_depth: 3,
            ..Limits::default()
        });
        runner.start("a").unwrap();
        assert_eq!(runner.advance(&library, &held), Status::Act(&"deep"));

        let mut runner = Runner::new(Limits {
            call_depth: 2,
            ..Limits::default()
        });
        runner.start("a").unwrap();
        assert_eq!(
            runner.advance(&library, &held),
            Status::Aborted(Abort::CallDepth)
        );
        assert!(!runner.is_running());

        library.insert("forever", [Step::call("forever")]);
        assert_eq!(
            run(&library, "forever", &[]).1,
            Status::Aborted(Abort::CallDepth)
        );
    }

    #[test]
    fn a_loop_without_actions_aborts_at_the_step_limit() {
        let mut library = Script::new();
        library.insert("ping", [Step::goto("pong")]);
        library.insert("pong", [Step::goto("ping")]);
        assert_eq!(
            run(&library, "ping", &[]).1,
            Status::Aborted(Abort::StepLimit)
        );
    }

    #[test]
    fn a_wide_call_tree_without_a_loop_is_bounded_too() {
        // Each level calls the next one twice, so the tree holds 2^20 calls.
        let mut library = Script::new();
        for level in 0..20 {
            let next = format!("level-{}", level + 1);
            library.insert(
                format!("level-{level}"),
                [Step::call(next.as_str()), Step::call(next.as_str())],
            );
        }
        library.insert("level-20", []);
        assert_eq!(
            run(&library, "level-0", &[]).1,
            Status::Aborted(Abort::StepLimit)
        );
    }

    #[test]
    fn the_step_limit_counts_steps_between_actions() {
        let mut library = Script::new();
        let mut steps = Vec::new();
        for _ in 0..10 {
            steps.push(Step::when(
                Requirement::has(Key::Ring),
                Step::act("skipped"),
            ));
            steps.push(Step::act("shown"));
        }
        library.insert("main", steps);

        let mut runner = Runner::new(Limits {
            steps_per_advance: 2,
            ..Limits::default()
        });
        runner.start("main").unwrap();
        let (seen, status) = drive(&mut runner, &library, &BTreeSet::new(), |_| Answer::Done);
        assert_eq!(seen.len(), 10);
        assert_eq!(status, Status::Finished);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "no waiting action")]
    fn resume_without_a_waiting_action_panics_in_debug() {
        let mut library = Script::new();
        library.insert("main", acts(&["a"]));
        let mut runner = Runner::default();
        runner.start("main").unwrap();
        let _ = runner.resume(Answer::stop(), &library, &BTreeSet::new());
    }

    #[test]
    #[cfg(not(debug_assertions))]
    fn resume_without_a_waiting_action_ignores_the_answer_in_release() {
        let mut library = Script::new();
        library.insert("main", acts(&["a"]));
        let mut runner = Runner::default();
        runner.start("main").unwrap();
        assert_eq!(
            runner.resume(Answer::stop(), &library, &BTreeSet::new()),
            Status::Act(&"a")
        );
    }

    #[test]
    fn an_abort_explains_itself() {
        let abort = Abort::MissingSequence("absent".into());
        assert_eq!(abort.to_string(), "the library has no sequence 'absent'");
        assert_eq!(Busy.to_string(), "a chain is already running");
    }

    #[test]
    fn an_answer_can_abort_the_chain() {
        let mut library = Script::new();
        library.insert("main", acts(&["ask"]));
        let held = BTreeSet::new();

        let mut runner = Runner::default();
        runner.start("main").unwrap();
        let _ = runner.advance(&library, &held);
        assert_eq!(
            runner.resume(Answer::call("absent"), &library, &held),
            Status::Aborted(Abort::MissingSequence("absent".into()))
        );

        let mut runner = Runner::new(Limits {
            call_depth: 1,
            ..Limits::default()
        });
        runner.start("main").unwrap();
        let _ = runner.advance(&library, &held);
        assert_eq!(
            runner.resume(Answer::call("main"), &library, &held),
            Status::Aborted(Abort::CallDepth)
        );
        assert!(!runner.is_running());
    }

    #[test]
    #[cfg(feature = "serde")]
    fn a_loaded_runner_takes_the_hosts_limits() {
        let mut library = Script::new();
        library.insert("a", [Step::act("start"), Step::call("b")]);
        library.insert("b", [Step::call("c")]);
        library.insert("c", acts(&["deep"]));
        let held = BTreeSet::new();

        let mut runner = Runner::new(Limits {
            call_depth: 2,
            ..Limits::default()
        });
        runner.start("a").unwrap();
        let _ = runner.advance(&library, &held);

        let saved = serde_json::to_string(&runner).unwrap();
        assert!(!saved.contains("call_depth"), "a save holds no limits");

        // The loaded runner has the default limits, not the saved ones.
        let loaded: Runner = serde_json::from_str(&saved).unwrap();
        let mut with_defaults = loaded.clone();
        assert_eq!(
            with_defaults.resume(Answer::Done, &library, &held),
            Status::Act(&"deep")
        );

        // The host's own limits apply from the next step.
        let mut with_host_limits = loaded;
        with_host_limits.set_limits(Limits {
            call_depth: 2,
            ..Limits::default()
        });
        assert_eq!(
            with_host_limits.resume(Answer::Done, &library, &held),
            Status::Aborted(Abort::CallDepth)
        );
    }

    #[test]
    #[cfg(feature = "serde")]
    fn a_corrupt_index_in_a_save_does_not_panic() {
        let mut library = Script::new();
        library.insert("main", acts(&["one", "two"]));
        let held = BTreeSet::new();

        let current = r#"{"sequence":"main","index":18446744073709551615}"#;
        let waiting = format!(r#"{{"chain":{{"current":{current},"callers":[],"waiting":true}}}}"#);
        let mut runner: Runner = serde_json::from_str(&waiting).unwrap();
        assert_eq!(
            runner.resume(Answer::Done, &library, &held),
            Status::Finished
        );

        let calling = format!(
            r#"{{"chain":{{"current":{{"sequence":"main","index":5}},"callers":[{current}],"waiting":false}}}}"#
        );
        let mut runner: Runner = serde_json::from_str(&calling).unwrap();
        assert_eq!(runner.advance(&library, &held), Status::Finished);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn a_save_with_an_unknown_field_is_an_error() {
        // Without the check, the misspelled key loads as an idle runner.
        let misspelled =
            r#"{"chian":{"current":{"sequence":"main","index":0},"callers":[],"waiting":true}}"#;
        let error = serde_json::from_str::<Runner>(misspelled).unwrap_err();
        assert!(error.to_string().contains("chian"), "{error}");

        let extra = r#"{"chain":{"current":{"sequence":"main","index":0,"extra":1},"callers":[],"waiting":true}}"#;
        assert!(serde_json::from_str::<Runner>(extra).is_err());
    }

    #[test]
    #[cfg(feature = "serde")]
    fn a_saved_chain_finishes_from_the_copy() {
        let mut library = Script::new();
        library.insert(
            "main",
            [Step::act("one"), Step::call("sub"), Step::act("four")],
        );
        library.insert("sub", acts(&["two", "three"]));
        let held = BTreeSet::new();

        let mut runner = Runner::default();
        runner.start("main").unwrap();
        let _ = runner.advance(&library, &held);
        assert_eq!(
            runner.resume(Answer::Done, &library, &held),
            Status::Act(&"two")
        );

        let saved = serde_json::to_string(&runner).unwrap();
        let mut loaded: Runner = serde_json::from_str(&saved).unwrap();
        assert_eq!(loaded, runner);
        assert_eq!(loaded.waiting_on(&library), Some(&"two"));

        let rest = |runner: &mut Runner| {
            let mut seen = Vec::new();
            let mut status = runner.resume(Answer::Done, &library, &held);
            while let Status::Act(action) = status {
                seen.push(*action);
                status = runner.resume(Answer::Done, &library, &held);
            }
            (seen, status)
        };
        assert_eq!(rest(&mut loaded), (vec!["three", "four"], Status::Finished));
        assert_eq!(rest(&mut runner), (vec!["three", "four"], Status::Finished));
    }
}