ftui-extras 0.4.0

Feature-gated extras for FrankenTUI (markdown, charts, clipboard, themes).
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
#![forbid(unsafe_code)]

//! Guided tour system for step-by-step onboarding walkthroughs.
//!
//! # Invariants
//!
//! 1. A tour can only have one active step at a time.
//! 2. Navigation respects step order: back goes to previous, next goes to next.
//! 3. Skipping a tour marks it as incomplete but ends the tour.
//! 4. Completion is tracked persistently via `TourCompletion`.
//!
//! # Example
//!
//! ```ignore
//! use ftui_extras::help::{Tour, TourStep};
//!
//! let tour = Tour::new("onboarding")
//!     .add_step(TourStep::new("Welcome")
//!         .content("Welcome to the app! Let's get started.")
//!         .target_widget(1))
//!     .add_step(TourStep::new("Search")
//!         .content("Use the search bar to find items.")
//!         .target_widget(2));
//! ```

use ftui_core::geometry::Rect;
use std::collections::HashSet;

/// Unique identifier for a widget to highlight.
pub type WidgetId = u32;

/// A single step in a guided tour.
#[derive(Debug, Clone)]
pub struct TourStep {
    /// Step title displayed in the spotlight.
    pub title: String,
    /// Step content/instructions.
    pub content: String,
    /// Target widget ID to highlight (if any).
    pub target_widget: Option<WidgetId>,
    /// Target bounds override (if widget ID not available).
    pub target_bounds: Option<Rect>,
    /// Whether this step requires user action before continuing.
    pub requires_action: bool,
    /// Custom data for the step (e.g., keybinding to demonstrate).
    pub metadata: Option<String>,
}

impl TourStep {
    /// Create a new tour step with the given title.
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            content: String::new(),
            target_widget: None,
            target_bounds: None,
            requires_action: false,
            metadata: None,
        }
    }

    /// Set the step content/instructions.
    #[must_use]
    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.content = content.into();
        self
    }

    /// Set the target widget to highlight.
    #[must_use]
    pub fn target_widget(mut self, id: WidgetId) -> Self {
        self.target_widget = Some(id);
        self
    }

    /// Set explicit target bounds (overrides widget lookup).
    #[must_use]
    pub fn target_bounds(mut self, bounds: Rect) -> Self {
        self.target_bounds = Some(bounds);
        self
    }

    /// Mark this step as requiring user action before continuing.
    #[must_use]
    pub fn requires_action(mut self, requires: bool) -> Self {
        self.requires_action = requires;
        self
    }

    /// Set custom metadata for the step.
    #[must_use]
    pub fn metadata(mut self, data: impl Into<String>) -> Self {
        self.metadata = Some(data.into());
        self
    }

    /// Get the effective target bounds for highlighting.
    ///
    /// If `target_bounds` is set, returns that directly.
    /// Otherwise, the caller should look up bounds from the widget ID.
    #[must_use]
    pub fn effective_bounds(&self) -> Option<Rect> {
        self.target_bounds
    }
}

/// A guided tour consisting of multiple steps.
#[derive(Debug, Clone)]
pub struct Tour {
    /// Unique tour identifier.
    pub id: String,
    /// Human-readable tour name.
    pub name: String,
    /// Tour steps in order.
    pub steps: Vec<TourStep>,
    /// Whether the tour can be skipped.
    pub skippable: bool,
}

impl Tour {
    /// Create a new tour with the given ID.
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: String::new(),
            steps: Vec::new(),
            skippable: true,
        }
    }

    /// Set the tour name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Add a step to the tour.
    #[must_use]
    pub fn add_step(mut self, step: TourStep) -> Self {
        self.steps.push(step);
        self
    }

    /// Set whether the tour can be skipped.
    #[must_use]
    pub fn skippable(mut self, skippable: bool) -> Self {
        self.skippable = skippable;
        self
    }

    /// Get the number of steps.
    #[must_use]
    pub fn step_count(&self) -> usize {
        self.steps.len()
    }

    /// Get a step by index.
    #[must_use]
    pub fn get_step(&self, index: usize) -> Option<&TourStep> {
        self.steps.get(index)
    }
}

/// Completion status for a tour.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionStatus {
    /// Tour has not been started.
    NotStarted,
    /// Tour was started but not finished.
    InProgress,
    /// Tour was completed successfully.
    Completed,
    /// Tour was skipped.
    Skipped,
}

/// Tracks completion status for multiple tours.
#[derive(Debug, Clone, Default)]
pub struct TourCompletion {
    /// Set of completed tour IDs.
    completed: HashSet<String>,
    /// Set of skipped tour IDs.
    skipped: HashSet<String>,
}

impl TourCompletion {
    /// Create a new tour completion tracker.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Check the completion status of a tour.
    #[must_use]
    pub fn status(&self, tour_id: &str) -> CompletionStatus {
        if self.completed.contains(tour_id) {
            CompletionStatus::Completed
        } else if self.skipped.contains(tour_id) {
            CompletionStatus::Skipped
        } else {
            CompletionStatus::NotStarted
        }
    }

    /// Mark a tour as completed.
    pub fn mark_completed(&mut self, tour_id: impl Into<String>) {
        let id = tour_id.into();
        self.skipped.remove(&id);
        self.completed.insert(id);
    }

    /// Mark a tour as skipped.
    pub fn mark_skipped(&mut self, tour_id: impl Into<String>) {
        let id = tour_id.into();
        self.completed.remove(&id);
        self.skipped.insert(id);
    }

    /// Reset a tour to not started.
    pub fn reset(&mut self, tour_id: &str) {
        self.completed.remove(tour_id);
        self.skipped.remove(tour_id);
    }

    /// Check if a tour has been completed.
    #[must_use]
    pub fn is_completed(&self, tour_id: &str) -> bool {
        self.completed.contains(tour_id)
    }

    /// Get all completed tour IDs.
    pub fn completed_tours(&self) -> impl Iterator<Item = &str> {
        self.completed.iter().map(String::as_str)
    }
}

/// Navigation action in a tour.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TourAction {
    /// Move to the next step.
    Next,
    /// Move to the previous step.
    Back,
    /// Skip the entire tour.
    Skip,
    /// Complete the current step's required action.
    Complete,
}

/// Events emitted by the tour state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TourEvent {
    /// Tour started.
    Started { tour_id: String },
    /// Moved to a new step.
    StepChanged { step_index: usize },
    /// Tour was completed successfully.
    Completed { tour_id: String },
    /// Tour was skipped.
    Skipped { tour_id: String },
}

/// Active tour state and navigation.
#[derive(Debug, Clone)]
pub struct TourState {
    /// The active tour (if any).
    tour: Option<Tour>,
    /// Current step index.
    current_step: usize,
    /// Whether the current step's action has been completed.
    action_completed: bool,
    /// Pending event to emit.
    pending_event: Option<TourEvent>,
}

impl Default for TourState {
    fn default() -> Self {
        Self::new()
    }
}

impl TourState {
    /// Create a new tour state.
    #[must_use]
    pub fn new() -> Self {
        Self {
            tour: None,
            current_step: 0,
            action_completed: false,
            pending_event: None,
        }
    }

    /// Check if a tour is active.
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.tour.is_some()
    }

    /// Get the active tour.
    #[must_use]
    pub fn tour(&self) -> Option<&Tour> {
        self.tour.as_ref()
    }

    /// Get the current step index.
    #[must_use]
    pub fn current_step_index(&self) -> usize {
        self.current_step
    }

    /// Get the current step.
    #[must_use]
    pub fn current_step(&self) -> Option<&TourStep> {
        self.tour.as_ref()?.get_step(self.current_step)
    }

    /// Get tour progress as (current, total).
    #[must_use]
    pub fn progress(&self) -> (usize, usize) {
        match &self.tour {
            Some(tour) => (self.current_step + 1, tour.step_count()),
            None => (0, 0),
        }
    }

    /// Start a tour.
    pub fn start(&mut self, tour: Tour) {
        let tour_id = tour.id.clone();
        self.tour = Some(tour);
        self.current_step = 0;
        self.action_completed = false;
        self.pending_event = Some(TourEvent::Started { tour_id });
    }

    /// Stop the current tour without completion.
    pub fn stop(&mut self) {
        self.tour = None;
        self.current_step = 0;
        self.action_completed = false;
    }

    /// Navigate the tour based on an action.
    ///
    /// Returns `true` if the action was handled.
    pub fn navigate(&mut self, action: TourAction) -> bool {
        let Some(tour) = &self.tour else {
            return false;
        };

        match action {
            TourAction::Next => {
                let step = tour.get_step(self.current_step);
                if let Some(s) = step
                    && s.requires_action
                    && !self.action_completed
                {
                    // Cannot proceed without completing the action.
                    return false;
                }

                if self.current_step + 1 < tour.step_count() {
                    self.current_step += 1;
                    self.action_completed = false;
                    self.pending_event = Some(TourEvent::StepChanged {
                        step_index: self.current_step,
                    });
                    true
                } else {
                    // Tour complete.
                    let tour_id = tour.id.clone();
                    self.pending_event = Some(TourEvent::Completed { tour_id });
                    self.tour = None;
                    self.current_step = 0;
                    true
                }
            }
            TourAction::Back => {
                if self.current_step > 0 {
                    self.current_step -= 1;
                    self.action_completed = false;
                    self.pending_event = Some(TourEvent::StepChanged {
                        step_index: self.current_step,
                    });
                    true
                } else {
                    false
                }
            }
            TourAction::Skip => {
                if tour.skippable {
                    let tour_id = tour.id.clone();
                    self.pending_event = Some(TourEvent::Skipped { tour_id });
                    self.tour = None;
                    self.current_step = 0;
                    true
                } else {
                    false
                }
            }
            TourAction::Complete => {
                self.action_completed = true;
                true
            }
        }
    }

    /// Mark the current step's action as completed.
    pub fn complete_action(&mut self) {
        self.action_completed = true;
    }

    /// Check if the current step's action is completed.
    #[must_use]
    pub fn is_action_completed(&self) -> bool {
        self.action_completed
    }

    /// Take the pending event (if any).
    pub fn take_event(&mut self) -> Option<TourEvent> {
        self.pending_event.take()
    }

    /// Check if we can go to the next step.
    #[must_use]
    pub fn can_go_next(&self) -> bool {
        let Some(tour) = &self.tour else {
            return false;
        };
        let step = tour.get_step(self.current_step);
        if let Some(s) = step
            && s.requires_action
            && !self.action_completed
        {
            return false;
        }
        true
    }

    /// Check if we can go back.
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        self.tour.is_some() && self.current_step > 0
    }

    /// Check if we can skip.
    #[must_use]
    pub fn can_skip(&self) -> bool {
        self.tour.as_ref().map(|t| t.skippable).unwrap_or(false)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_tour() -> Tour {
        Tour::new("test-tour")
            .name("Test Tour")
            .add_step(TourStep::new("Step 1").content("First step"))
            .add_step(TourStep::new("Step 2").content("Second step"))
            .add_step(TourStep::new("Step 3").content("Third step"))
    }

    // ── Tour construction ────────────────────────────────────────────────

    #[test]
    fn tour_construction() {
        let tour = sample_tour();
        assert_eq!(tour.id, "test-tour");
        assert_eq!(tour.name, "Test Tour");
        assert_eq!(tour.step_count(), 3);
    }

    #[test]
    fn step_construction() {
        let step = TourStep::new("Welcome")
            .content("Hello!")
            .target_widget(42)
            .requires_action(true)
            .metadata("press Enter");

        assert_eq!(step.title, "Welcome");
        assert_eq!(step.content, "Hello!");
        assert_eq!(step.target_widget, Some(42));
        assert!(step.requires_action);
        assert_eq!(step.metadata, Some("press Enter".into()));
    }

    // ── TourState navigation ─────────────────────────────────────────────

    #[test]
    fn state_start_tour() {
        let mut state = TourState::new();
        assert!(!state.is_active());

        state.start(sample_tour());
        assert!(state.is_active());
        assert_eq!(state.current_step_index(), 0);
        assert_eq!(state.progress(), (1, 3));

        let event = state.take_event();
        assert_eq!(
            event,
            Some(TourEvent::Started {
                tour_id: "test-tour".into()
            })
        );
    }

    #[test]
    fn state_navigate_next() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.take_event(); // Clear start event

        assert!(state.navigate(TourAction::Next));
        assert_eq!(state.current_step_index(), 1);
        assert_eq!(
            state.take_event(),
            Some(TourEvent::StepChanged { step_index: 1 })
        );
    }

    #[test]
    fn state_navigate_back() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.navigate(TourAction::Next);
        state.take_event();

        assert!(state.navigate(TourAction::Back));
        assert_eq!(state.current_step_index(), 0);

        // Cannot go back from first step
        assert!(!state.navigate(TourAction::Back));
    }

    #[test]
    fn state_navigate_to_completion() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.take_event();

        state.navigate(TourAction::Next);
        state.navigate(TourAction::Next);
        let completed = state.navigate(TourAction::Next);

        assert!(completed);
        assert!(!state.is_active());
        assert_eq!(
            state.take_event(),
            Some(TourEvent::Completed {
                tour_id: "test-tour".into()
            })
        );
    }

    #[test]
    fn state_skip_tour() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.take_event();

        assert!(state.navigate(TourAction::Skip));
        assert!(!state.is_active());
        assert_eq!(
            state.take_event(),
            Some(TourEvent::Skipped {
                tour_id: "test-tour".into()
            })
        );
    }

    #[test]
    fn state_skip_disabled() {
        let mut state = TourState::new();
        state.start(sample_tour().skippable(false));
        state.take_event();

        assert!(!state.navigate(TourAction::Skip));
        assert!(state.is_active());
    }

    #[test]
    fn state_requires_action() {
        let tour = Tour::new("action-tour").add_step(
            TourStep::new("Action Step")
                .content("Do something")
                .requires_action(true),
        );

        let mut state = TourState::new();
        state.start(tour);
        state.take_event();

        // Cannot proceed without completing action
        assert!(!state.can_go_next());
        assert!(!state.navigate(TourAction::Next));

        // Complete the action
        state.complete_action();
        assert!(state.can_go_next());
        assert!(state.navigate(TourAction::Next));
    }

    // ── TourCompletion tracking ──────────────────────────────────────────

    #[test]
    fn completion_tracking() {
        let mut completion = TourCompletion::new();

        assert_eq!(completion.status("tour1"), CompletionStatus::NotStarted);

        completion.mark_completed("tour1");
        assert_eq!(completion.status("tour1"), CompletionStatus::Completed);
        assert!(completion.is_completed("tour1"));

        completion.mark_skipped("tour1");
        assert_eq!(completion.status("tour1"), CompletionStatus::Skipped);
        assert!(!completion.is_completed("tour1"));

        completion.reset("tour1");
        assert_eq!(completion.status("tour1"), CompletionStatus::NotStarted);
    }

    #[test]
    fn completion_iterator() {
        let mut completion = TourCompletion::new();
        completion.mark_completed("tour1");
        completion.mark_completed("tour2");
        completion.mark_skipped("tour3");

        let completed: Vec<_> = completion.completed_tours().collect();
        assert_eq!(completed.len(), 2);
        assert!(completed.contains(&"tour1"));
        assert!(completed.contains(&"tour2"));
    }

    // ── Edge cases ───────────────────────────────────────────────────────

    #[test]
    fn navigate_no_active_tour() {
        let mut state = TourState::new();
        assert!(!state.navigate(TourAction::Next));
        assert!(!state.navigate(TourAction::Back));
        assert!(!state.navigate(TourAction::Skip));
    }

    #[test]
    fn empty_tour() {
        let tour = Tour::new("empty");
        assert_eq!(tour.step_count(), 0);
        assert!(tour.get_step(0).is_none());

        let mut state = TourState::new();
        state.start(tour);
        assert!(state.current_step().is_none());

        // Immediate completion on next
        assert!(state.navigate(TourAction::Next));
        assert!(!state.is_active());
    }

    #[test]
    fn step_bounds_override() {
        let bounds = Rect::new(10, 20, 30, 40);
        let step = TourStep::new("Test").target_widget(5).target_bounds(bounds);

        assert_eq!(step.target_widget, Some(5));
        assert_eq!(step.effective_bounds(), Some(bounds));
    }

    #[test]
    fn step_defaults_no_target_or_bounds() {
        let step = TourStep::new("Plain");
        assert_eq!(step.title, "Plain");
        assert!(step.content.is_empty());
        assert!(step.target_widget.is_none());
        assert!(step.target_bounds.is_none());
        assert!(!step.requires_action);
        assert!(step.metadata.is_none());
        assert!(step.effective_bounds().is_none());
    }

    #[test]
    fn tour_is_skippable_by_default() {
        let tour = Tour::new("id");
        assert!(tour.skippable);
    }

    #[test]
    fn tour_state_stop_ends_tour() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.take_event();
        assert!(state.is_active());

        state.stop();
        assert!(!state.is_active());
        assert_eq!(state.current_step_index(), 0);
        assert!(state.take_event().is_none());
    }

    #[test]
    fn progress_no_active_tour_returns_zero() {
        let state = TourState::new();
        assert_eq!(state.progress(), (0, 0));
    }

    #[test]
    fn can_go_back_no_tour() {
        let state = TourState::new();
        assert!(!state.can_go_back());
    }

    #[test]
    fn can_skip_no_tour() {
        let state = TourState::new();
        assert!(!state.can_skip());
    }

    #[test]
    fn completion_mark_completed_idempotent() {
        let mut completion = TourCompletion::new();
        completion.mark_completed("t");
        completion.mark_completed("t");
        assert_eq!(completion.status("t"), CompletionStatus::Completed);
        assert_eq!(completion.completed_tours().count(), 1);
    }

    #[test]
    fn navigate_complete_sets_flag() {
        let tour = Tour::new("a").add_step(TourStep::new("S").requires_action(true));
        let mut state = TourState::new();
        state.start(tour);
        state.take_event();

        assert!(!state.is_action_completed());
        assert!(state.navigate(TourAction::Complete));
        assert!(state.is_action_completed());
    }

    // --- Additional edge case tests (bd-m3gnl) ---

    #[test]
    fn tour_step_debug_clone() {
        let step = TourStep::new("Dbg").content("body").metadata("meta");
        let cloned = step.clone();
        assert_eq!(cloned.title, "Dbg");
        assert_eq!(cloned.metadata, Some("meta".into()));
        assert!(!format!("{:?}", step).is_empty());
    }

    #[test]
    fn tour_debug_clone() {
        let tour = sample_tour();
        let cloned = tour.clone();
        assert_eq!(cloned.id, "test-tour");
        assert_eq!(cloned.step_count(), 3);
        assert!(!format!("{:?}", tour).is_empty());
    }

    #[test]
    fn tour_get_step_valid_and_oob() {
        let tour = sample_tour();
        assert!(tour.get_step(0).is_some());
        assert_eq!(tour.get_step(0).unwrap().title, "Step 1");
        assert!(tour.get_step(2).is_some());
        assert!(tour.get_step(3).is_none());
        assert!(tour.get_step(usize::MAX).is_none());
    }

    #[test]
    fn completion_status_debug_clone_copy_eq() {
        let status = CompletionStatus::Completed;
        let copied = status;
        assert_eq!(status, copied);
        assert_ne!(CompletionStatus::Completed, CompletionStatus::Skipped);
        assert_ne!(CompletionStatus::InProgress, CompletionStatus::NotStarted);
        assert!(!format!("{:?}", status).is_empty());
    }

    #[test]
    fn tour_action_debug_clone_copy_eq() {
        let action = TourAction::Next;
        let copied = action;
        assert_eq!(action, copied);
        assert_ne!(TourAction::Next, TourAction::Back);
        assert_ne!(TourAction::Skip, TourAction::Complete);
        assert!(!format!("{:?}", action).is_empty());
    }

    #[test]
    fn tour_event_debug_clone_eq() {
        let event = TourEvent::Started {
            tour_id: "t".into(),
        };
        let cloned = event.clone();
        assert_eq!(event, cloned);
        assert_ne!(
            TourEvent::Started {
                tour_id: "a".into()
            },
            TourEvent::Completed {
                tour_id: "a".into()
            }
        );
        assert!(!format!("{:?}", event).is_empty());
    }

    #[test]
    fn tour_completion_debug_clone_default() {
        let comp = TourCompletion::default();
        let cloned = comp.clone();
        assert_eq!(cloned.status("x"), CompletionStatus::NotStarted);
        assert!(!format!("{:?}", comp).is_empty());
    }

    #[test]
    fn tour_state_default_equals_new() {
        let a = TourState::default();
        let b = TourState::new();
        assert!(!a.is_active());
        assert!(!b.is_active());
        assert_eq!(a.current_step_index(), b.current_step_index());
    }

    #[test]
    fn tour_state_debug_clone() {
        let mut state = TourState::new();
        state.start(sample_tour());
        let cloned = state.clone();
        assert!(cloned.is_active());
        assert!(!format!("{:?}", state).is_empty());
    }

    #[test]
    fn tour_state_tour_returns_ref() {
        let mut state = TourState::new();
        assert!(state.tour().is_none());
        state.start(sample_tour());
        let tour_ref = state.tour().unwrap();
        assert_eq!(tour_ref.id, "test-tour");
    }

    #[test]
    fn current_step_at_different_indices() {
        let mut state = TourState::new();
        state.start(sample_tour());
        assert_eq!(state.current_step().unwrap().title, "Step 1");

        state.navigate(TourAction::Next);
        assert_eq!(state.current_step().unwrap().title, "Step 2");

        state.navigate(TourAction::Next);
        assert_eq!(state.current_step().unwrap().title, "Step 3");
    }

    #[test]
    fn can_go_next_no_action_required() {
        let mut state = TourState::new();
        state.start(sample_tour());
        assert!(
            state.can_go_next(),
            "should be able to go next without action requirement"
        );
    }

    #[test]
    fn can_go_next_no_tour_returns_false() {
        let state = TourState::new();
        assert!(!state.can_go_next());
    }

    #[test]
    fn action_completed_resets_on_step_change() {
        let tour = Tour::new("t")
            .add_step(TourStep::new("S1").requires_action(true))
            .add_step(TourStep::new("S2").requires_action(true));
        let mut state = TourState::new();
        state.start(tour);
        state.take_event();

        state.complete_action();
        assert!(state.is_action_completed());

        state.navigate(TourAction::Next);
        assert!(
            !state.is_action_completed(),
            "action_completed should reset on step change"
        );
    }

    #[test]
    fn single_step_tour_completes_on_next() {
        let tour = Tour::new("single").add_step(TourStep::new("Only"));
        let mut state = TourState::new();
        state.start(tour);
        state.take_event();

        assert!(state.navigate(TourAction::Next));
        assert!(!state.is_active());
        assert_eq!(
            state.take_event(),
            Some(TourEvent::Completed {
                tour_id: "single".into()
            })
        );
    }

    #[test]
    fn mark_completed_removes_from_skipped() {
        let mut comp = TourCompletion::new();
        comp.mark_skipped("t");
        assert_eq!(comp.status("t"), CompletionStatus::Skipped);

        comp.mark_completed("t");
        assert_eq!(comp.status("t"), CompletionStatus::Completed);
    }

    #[test]
    fn reset_nonexistent_tour_is_noop() {
        let mut comp = TourCompletion::new();
        comp.reset("nonexistent"); // Should not panic
        assert_eq!(comp.status("nonexistent"), CompletionStatus::NotStarted);
    }

    #[test]
    fn restart_replaces_active_tour() {
        let mut state = TourState::new();
        state.start(sample_tour());
        state.navigate(TourAction::Next);

        // Start a different tour
        let tour2 = Tour::new("tour2").add_step(TourStep::new("New"));
        state.start(tour2);
        assert_eq!(state.current_step_index(), 0);
        assert_eq!(state.tour().unwrap().id, "tour2");
    }

    #[test]
    fn take_event_clears_pending() {
        let mut state = TourState::new();
        state.start(sample_tour());
        assert!(state.take_event().is_some());
        assert!(state.take_event().is_none(), "event should be consumed");
    }
}