neser 0.1.1

NESER - NES Emulator in Rust - is a NES emulator written in Rust. It aims to be a high-quality, hardware-accurate emulator that is also easy to use and extend. It supports a wide range of NES games and features, including various mappers, audio processing, and input handling. NESER is designed to be modular and extensible, allowing developers to easily add new features or support for additional hardware. It can be run using one of two frontends: a native desktop application using SDL2, or a web application using WebAssembly. The desktop application provides a high-performance, feature-rich experience with support for various input devices and display options, while the web application allows users to play NES games directly in their browsers without needing to install any software in a BYOR manner (Bring Your Own Roms).
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
use crate::autorun::{
    AUTORUN_VERSION, AutorunCheckpoint, AutorunFile, AutorunFrame, CHECKPOINT_INTERVAL_FRAMES,
    autorun_path_for_rom, backup_autorun_file, load_autorun_file, save_autorun_file,
};
use crate::console::AutorunMode;
use std::path::PathBuf;

/// Resolves a checkpoint index (possibly negative) to a concrete `usize` index.
///
/// Negative indices count from the second-to-last checkpoint:
/// - `-1` → index `total - 2` (second-to-last)
/// - `-2` → index `total - 3` (third-to-last)
///
/// Non-negative indices are returned as-is.
fn resolve_checkpoint_index(raw: i64, total: usize) -> Result<usize, String> {
    if raw >= 0 {
        Ok(raw as usize)
    } else {
        // -1 → total-2, -2 → total-3, …
        let resolved = (total as i64 - 1) + raw;
        if resolved < 0 {
            Err(format!(
                "Checkpoint index {} out of range (recording has {} checkpoints)",
                raw, total
            ))
        } else {
            Ok(resolved as usize)
        }
    }
}

/// Information needed to restore NES state when starting from a checkpoint.
///
/// Returned by [`AutorunState::new`] when the caller must restore emulator state before
/// beginning playback or extend playback.
pub struct PendingRestore {
    /// Serialized [`crate::console::SaveState`] bytes.
    pub state_bytes: Vec<u8>,
}

/// Manages autorun recording and playback state.
pub struct AutorunState {
    autorun: AutorunFile,
    autorun_path: PathBuf,
    /// Index of the *next* frame to be emulated. In playback, this advances through the
    /// stored frames. In record, it counts newly recorded frames.
    frame_index: usize,
    /// True while in extend-record mode and still replaying the existing recording.
    extending_playback: bool,
    mode: AutorunMode,
    /// Index of the next checkpoint to verify during playback.
    playback_checkpoint_idx: usize,
    /// Accumulated CRC mismatch count during playback.
    crc_mismatches: usize,
    /// Total number of checkpoints verified so far.
    total_checkpoints_verified: usize,
    /// Set to `true` when `next_playback_frame` returned a pre-recorded frame this frame.
    /// Cleared by `begin_frame` at the start of each frame cycle.
    current_frame_prerecorded: bool,
}

impl AutorunState {
    /// Create a new AutorunState for the given mode and ROM path.
    ///
    /// Returns `(state, Some(PendingRestore))` when the caller must restore NES state before
    /// beginning emulation (extend mode with checkpoints, or `from_checkpoint` playback).
    ///
    /// # Parameters
    /// * `mode` - Operating mode (`Record` or `Playback`).
    /// * `rom_path` - Path to the loaded ROM; the `.autorun` file lives alongside it.
    /// * `overwrite` - In `Record` mode: back up and overwrite any existing recording.
    /// * `extend` - In `Record` mode: play back the existing recording from the second-to-last
    ///   checkpoint, then continue recording.
    /// * `from_checkpoint` - In `Playback` mode: start from this checkpoint index instead of
    ///   frame 0.
    pub fn new(
        mode: AutorunMode,
        rom_path: &str,
        overwrite: bool,
        extend: bool,
        from_checkpoint: Option<i64>,
    ) -> Result<(Self, Option<PendingRestore>), String> {
        let autorun_path = autorun_path_for_rom(&PathBuf::from(rom_path));

        match mode {
            AutorunMode::None => Err("AutorunState requires Record or Playback mode".to_string()),

            AutorunMode::Record => {
                if extend && autorun_path.exists() {
                    Self::new_extend(autorun_path)
                } else {
                    // New recording — back up or clear any existing file
                    if autorun_path.exists() {
                        if overwrite {
                            backup_autorun_file(&autorun_path)?;
                        } else {
                            return Err(format!(
                                "Recording already exists: {} (use --create-recording to replace)",
                                autorun_path.display()
                            ));
                        }
                    }
                    let state = Self {
                        autorun: AutorunFile {
                            version: AUTORUN_VERSION,
                            frames: Vec::new(),
                            checkpoints: Vec::new(),
                        },
                        autorun_path,
                        frame_index: 0,
                        extending_playback: false,
                        mode,
                        playback_checkpoint_idx: 0,
                        crc_mismatches: 0,
                        total_checkpoints_verified: 0,
                        current_frame_prerecorded: false,
                    };
                    Ok((state, None))
                }
            }

            AutorunMode::Playback => {
                let autorun = load_autorun_file(&autorun_path)?;
                let (frame_index, playback_checkpoint_idx, pending) = if let Some(cp_idx_raw) =
                    from_checkpoint
                {
                    let cp_idx = resolve_checkpoint_index(cp_idx_raw, autorun.checkpoints.len())?;
                    let cp = autorun.checkpoints.get(cp_idx).ok_or_else(|| {
                        format!(
                            "Checkpoint index {cp_idx} out of range (recording has {})",
                            autorun.checkpoints.len()
                        )
                    })?;
                    let pending = PendingRestore {
                        state_bytes: cp.state_bytes.clone(),
                    };
                    (cp.frame_index as usize + 1, cp_idx + 1, Some(pending))
                } else {
                    (0, 0, None)
                };

                let state = Self {
                    autorun,
                    autorun_path,
                    frame_index,
                    extending_playback: false,
                    mode,
                    playback_checkpoint_idx,
                    crc_mismatches: 0,
                    total_checkpoints_verified: 0,
                    current_frame_prerecorded: false,
                };
                Ok((state, pending))
            }
        }
    }

    /// Construct extend-mode state: load an existing recording and prepare to continue it.
    ///
    /// * **n ≥ 2 checkpoints**: restores from the second-to-last checkpoint and replays frames
    ///   up to the last checkpoint, so the transition into recording is verifiable.
    /// * **n = 1 checkpoint**: restores directly from the single checkpoint and starts recording
    ///   immediately — no extend playback is needed.
    /// * **n = 0 checkpoints**: starts from scratch (no state to restore).
    fn new_extend(autorun_path: PathBuf) -> Result<(Self, Option<PendingRestore>), String> {
        let existing = load_autorun_file(&autorun_path)?;

        let n = existing.checkpoints.len();
        let (frame_index, playback_checkpoint_idx, pending) = if n >= 2 {
            let second_to_last = &existing.checkpoints[n - 2];
            let pending = PendingRestore {
                state_bytes: second_to_last.state_bytes.clone(),
            };
            // Start playing from the frame after the second-to-last checkpoint.
            // The last checkpoint (n-1) will be verified during this playback.
            (
                second_to_last.frame_index as usize + 1,
                n - 1,
                Some(pending),
            )
        } else if n == 1 {
            // Single checkpoint: restore from it and start recording immediately after.
            // No extend playback is needed — state restoration alone is sufficient.
            let cp = &existing.checkpoints[0];
            let pending = PendingRestore {
                state_bytes: cp.state_bytes.clone(),
            };
            (cp.frame_index as usize + 1, 1, Some(pending))
        } else {
            // No checkpoints: start from scratch.
            (0, 0, None)
        };

        let state = Self {
            autorun: existing,
            autorun_path,
            frame_index,
            extending_playback: true,
            mode: AutorunMode::Record,
            playback_checkpoint_idx,
            crc_mismatches: 0,
            total_checkpoints_verified: 0,
            current_frame_prerecorded: false,
        };
        Ok((state, pending))
    }

    /// Get the total number of frames in the recording.
    pub fn total_frames(&self) -> usize {
        self.autorun.frames.len()
    }

    /// Get the current frame index.
    pub fn current_frame_index(&self) -> usize {
        self.frame_index
    }

    /// Check if we're in extend mode and still playing back.
    pub fn is_extending_playback(&self) -> bool {
        self.extending_playback && self.frame_index < self.autorun.frames.len()
    }

    /// Signal the start of a new frame cycle.
    ///
    /// Must be called at the very beginning of each frame before any call to
    /// [`next_playback_frame`]. Clears the pre-recorded flag so that
    /// [`current_frame_was_prerecorded`] correctly reflects only the current frame.
    pub fn begin_frame(&mut self) {
        self.current_frame_prerecorded = false;
    }

    /// Returns `true` if the current frame's inputs came from a pre-recorded source
    /// (i.e., [`next_playback_frame`] returned `Some` this frame cycle).
    pub fn current_frame_was_prerecorded(&self) -> bool {
        self.current_frame_prerecorded
    }

    /// Get the next frame to play back. Returns None if playback is complete.
    pub fn next_playback_frame(&mut self) -> Option<AutorunFrame> {
        if self.frame_index < self.autorun.frames.len() {
            let frame = self.autorun.frames[self.frame_index].clone();
            self.frame_index += 1;
            self.current_frame_prerecorded = true;
            Some(frame)
        } else {
            None
        }
    }

    /// Record a new frame during record mode (or extend mode after playback).
    ///
    /// Returns `true` if a checkpoint should be captured immediately after this call
    /// (i.e., the frame completes a checkpoint interval).
    pub fn record_frame(&mut self, player1: u8, player2: u8) -> bool {
        self.autorun.frames.push(AutorunFrame { player1, player2 });
        self.frame_index += 1;
        // Checkpoint every CHECKPOINT_INTERVAL_FRAMES frames
        (self.frame_index as u32).is_multiple_of(CHECKPOINT_INTERVAL_FRAMES)
    }

    /// Store a checkpoint for the frame just recorded.
    ///
    /// Call this immediately after [`record_frame`] returns `true`.
    pub fn record_checkpoint(&mut self, screen_crc: u32, state_bytes: Vec<u8>) {
        self.autorun.checkpoints.push(AutorunCheckpoint {
            frame_index: (self.frame_index - 1) as u32,
            screen_crc,
            state_bytes,
        });
    }

    /// Check whether the current frame has a playback checkpoint and verify its CRC.
    ///
    /// Must be called after each frame is rendered during playback.
    /// Returns `Some(true)` if the CRC matched, `Some(false)` if it mismatched,
    /// or `None` if no checkpoint falls on this frame.
    pub fn check_playback_checkpoint(&mut self, screen_crc: u32) -> Option<bool> {
        let current = (self.frame_index - 1) as u32; // frame just completed
        let mut matched = None;
        while self.playback_checkpoint_idx < self.autorun.checkpoints.len()
            && self.autorun.checkpoints[self.playback_checkpoint_idx].frame_index == current
        {
            let expected = self.autorun.checkpoints[self.playback_checkpoint_idx].screen_crc;
            let ok = screen_crc == expected;
            if !ok {
                self.crc_mismatches += 1;
            }
            self.total_checkpoints_verified += 1;
            self.playback_checkpoint_idx += 1;
            matched = Some(ok);
        }
        matched
    }

    /// Save the recording. Appends a final checkpoint with the given CRC and state.
    pub fn save_with_final_checkpoint(
        &mut self,
        screen_crc: u32,
        state_bytes: Vec<u8>,
    ) -> Result<(), String> {
        self.autorun.checkpoints.push(AutorunCheckpoint {
            frame_index: self.frame_index.saturating_sub(1) as u32,
            screen_crc,
            state_bytes,
        });
        save_autorun_file(&self.autorun_path, &self.autorun)
    }

    /// Number of CRC mismatches detected during playback so far.
    pub fn crc_mismatches(&self) -> usize {
        self.crc_mismatches
    }

    /// Total number of checkpoints verified so far during playback.
    pub fn total_checkpoints_verified(&self) -> usize {
        self.total_checkpoints_verified
    }

    /// Total number of checkpoints in the loaded autorun file.
    pub fn total_checkpoints(&self) -> usize {
        self.autorun.checkpoints.len()
    }

    /// Get the autorun mode.
    pub fn mode(&self) -> AutorunMode {
        self.mode
    }
}

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

    fn make_file_with_checkpoints(num_frames: u32, checkpoint_crcs: &[(u32, u32)]) -> AutorunFile {
        AutorunFile {
            version: AUTORUN_VERSION,
            frames: (0..num_frames)
                .map(|_| AutorunFrame {
                    player1: 0,
                    player2: 0,
                })
                .collect(),
            checkpoints: checkpoint_crcs
                .iter()
                .map(|(frame_idx, crc)| AutorunCheckpoint {
                    frame_index: *frame_idx,
                    screen_crc: *crc,
                    state_bytes: vec![],
                })
                .collect(),
        }
    }

    #[test]
    fn test_total_frames_empty() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let rom_path_str = rom_file.path().to_str().expect("rom path to string");

        let (state, _) = AutorunState::new(AutorunMode::Record, rom_path_str, false, false, None)
            .expect("create autorun state");

        assert_eq!(state.total_frames(), 0);
    }

    #[test]
    fn test_total_frames_with_data() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_file = make_file_with_checkpoints(3, &[(2, 0xABCD)]);
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save file");

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (state, _) = AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
            .expect("create autorun state");

        assert_eq!(state.total_frames(), 3);
    }

    #[test]
    fn test_record_frame_returns_true_at_checkpoint_interval() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Record, rom_path_str, false, false, None)
                .expect("create autorun state");

        // Record 299 frames – none should trigger a checkpoint
        for _ in 0..299 {
            let checkpoint_due = state.record_frame(0, 0);
            assert!(!checkpoint_due);
        }
        // Frame 300 (index 299) triggers a checkpoint
        let checkpoint_due = state.record_frame(0, 0);
        assert!(checkpoint_due, "frame 300 should trigger a checkpoint");
    }

    #[test]
    fn test_record_checkpoint_stores_crc_and_state() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Record, rom_path_str, false, false, None)
                .expect("create autorun state");

        for _ in 0..300 {
            state.record_frame(0, 0);
        }
        state.record_checkpoint(0xCAFEBABE, vec![1, 2, 3]);

        assert_eq!(state.autorun.checkpoints.len(), 1);
        assert_eq!(state.autorun.checkpoints[0].frame_index, 299);
        assert_eq!(state.autorun.checkpoints[0].screen_crc, 0xCAFEBABE);
        assert_eq!(state.autorun.checkpoints[0].state_bytes, vec![1, 2, 3]);
    }

    #[test]
    fn test_check_playback_checkpoint_matching_crc() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let file = make_file_with_checkpoints(300, &[(299, 0x1234)]);
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
                .expect("create state");

        // Consume all 300 frames
        for _ in 0..300 {
            state.next_playback_frame();
        }

        let result = state.check_playback_checkpoint(0x1234);
        assert_eq!(result, Some(true));
        assert_eq!(state.crc_mismatches(), 0);
        assert_eq!(state.total_checkpoints_verified(), 1);
    }

    #[test]
    fn test_check_playback_checkpoint_mismatching_crc() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let file = make_file_with_checkpoints(300, &[(299, 0x1234)]);
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
                .expect("create state");

        for _ in 0..300 {
            state.next_playback_frame();
        }

        let result = state.check_playback_checkpoint(0xBADBAD00);
        assert_eq!(result, Some(false));
        assert_eq!(state.crc_mismatches(), 1);
    }

    #[test]
    fn test_overwrite_record_creates_backup() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let existing_file = make_file_with_checkpoints(1, &[(0, 0xABCD)]);
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &existing_file).expect("save existing");
        assert!(autorun_path.exists());

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let _ = AutorunState::new(AutorunMode::Record, rom_path_str, true, false, None)
            .expect("create state with overwrite");

        let bak_path = autorun_path.with_extension("autorun.bak");
        assert!(
            bak_path.exists(),
            "backup file should be created on overwrite"
        );
        let _ = std::fs::remove_file(&bak_path);
    }

    #[test]
    fn test_is_extending_playback_starts_true_in_extend_mode() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        // Need >=2 checkpoints to get full extend behavior; use >=2
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![
                AutorunFrame {
                    player1: 1,
                    player2: 2,
                },
                AutorunFrame {
                    player1: 3,
                    player2: 4,
                },
            ],
            checkpoints: vec![
                AutorunCheckpoint {
                    frame_index: 0,
                    screen_crc: 0,
                    state_bytes: vec![],
                },
                AutorunCheckpoint {
                    frame_index: 1,
                    screen_crc: 0,
                    state_bytes: vec![],
                },
            ],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save file");

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (state, _) = AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
            .expect("create autorun state");

        assert!(state.is_extending_playback());
    }

    #[test]
    fn test_extend_mode_starts_from_second_to_last_checkpoint() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        // 4 checkpoints at frames 299, 599, 899, 1199
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![
                AutorunFrame {
                    player1: 0,
                    player2: 0
                };
                1200
            ],
            checkpoints: vec![
                AutorunCheckpoint {
                    frame_index: 299,
                    screen_crc: 0,
                    state_bytes: b"state0".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 599,
                    screen_crc: 0,
                    state_bytes: b"state1".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 899,
                    screen_crc: 0,
                    state_bytes: b"state2".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 1199,
                    screen_crc: 0,
                    state_bytes: b"state3".to_vec(),
                },
            ],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (state, pending) =
            AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
                .expect("create state");

        // Should start from second-to-last checkpoint (frame 899) + 1 = 900
        assert_eq!(state.current_frame_index(), 900);
        assert!(
            pending.is_some(),
            "should have pending restore for second-to-last checkpoint"
        );
        assert_eq!(
            pending.unwrap().state_bytes,
            b"state2".to_vec(),
            "should restore state from second-to-last checkpoint"
        );
    }

    #[test]
    fn test_playback_from_checkpoint_sets_frame_index() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![
                AutorunFrame {
                    player1: 0,
                    player2: 0
                };
                600
            ],
            checkpoints: vec![
                AutorunCheckpoint {
                    frame_index: 299,
                    screen_crc: 0x1111,
                    state_bytes: b"s0".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 599,
                    screen_crc: 0x2222,
                    state_bytes: b"s1".to_vec(),
                },
            ],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (state, pending) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, Some(0))
                .expect("create state");

        // frame_index should be 299 + 1 = 300
        assert_eq!(state.current_frame_index(), 300);
        assert!(pending.is_some());
        assert_eq!(pending.unwrap().state_bytes, b"s0".to_vec());
    }

    #[test]
    fn test_is_extending_playback_in_normal_record_mode() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let rom_path_str = rom_file.path().to_str().expect("rom path to string");

        let (state, _) = AutorunState::new(AutorunMode::Record, rom_path_str, false, false, None)
            .expect("create autorun state");

        assert!(!state.is_extending_playback());
    }

    #[test]
    fn test_is_extending_playback_in_playback_mode() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![AutorunFrame {
                player1: 1,
                player2: 2,
            }],
            checkpoints: vec![AutorunCheckpoint {
                frame_index: 0,
                screen_crc: 0,
                state_bytes: vec![],
            }],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save file");

        let rom_path_str = rom_file.path().to_str().expect("rom path to string");
        let (state, _) = AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
            .expect("create autorun state");

        assert!(!state.is_extending_playback());
    }

    fn make_file_with_4_checkpoints() -> (tempfile::NamedTempFile, AutorunFile) {
        // 4 checkpoints at frame indices 0, 1, 2, 3
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![
                AutorunFrame {
                    player1: 0,
                    player2: 0
                };
                4
            ],
            checkpoints: vec![
                AutorunCheckpoint {
                    frame_index: 0,
                    screen_crc: 0,
                    state_bytes: b"s0".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 1,
                    screen_crc: 0,
                    state_bytes: b"s1".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 2,
                    screen_crc: 0,
                    state_bytes: b"s2".to_vec(),
                },
                AutorunCheckpoint {
                    frame_index: 3,
                    screen_crc: 0,
                    state_bytes: b"s3".to_vec(),
                },
            ],
        };
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");
        (rom_file, autorun_file)
    }

    #[test]
    fn test_playback_from_negative_checkpoint_minus_1_is_second_to_last() {
        // -1 = second-to-last checkpoint (index 2 of 4)
        let (rom_file, _) = make_file_with_4_checkpoints();
        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (state, pending) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, Some(-1))
                .expect("create state");

        // Second-to-last is index 2, frame_index=2, so playback starts at frame 3
        assert_eq!(state.current_frame_index(), 3);
        assert_eq!(pending.unwrap().state_bytes, b"s2".to_vec());
    }

    #[test]
    fn test_playback_from_negative_checkpoint_minus_2_is_third_from_end() {
        // -2 = third from end (index 1 of 4)
        let (rom_file, _) = make_file_with_4_checkpoints();
        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (state, pending) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, Some(-2))
                .expect("create state");

        assert_eq!(state.current_frame_index(), 2);
        assert_eq!(pending.unwrap().state_bytes, b"s1".to_vec());
    }

    #[test]
    fn test_playback_from_negative_checkpoint_out_of_range_is_error() {
        // -4 with 4 checkpoints would resolve to index -1 which is out of range
        let (rom_file, _) = make_file_with_4_checkpoints();
        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let result = AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, Some(-4));
        assert!(result.is_err(), "negative index beyond range should fail");
    }

    fn make_recording_with_single_checkpoint() -> (tempfile::NamedTempFile, AutorunFile) {
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![
                AutorunFrame {
                    player1: 0,
                    player2: 0
                };
                300
            ],
            checkpoints: vec![AutorunCheckpoint {
                frame_index: 299,
                screen_crc: 0xABCD,
                state_bytes: b"single_checkpoint_state".to_vec(),
            }],
        };
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");
        (rom_file, autorun_file)
    }

    fn make_recording_with_no_checkpoints() -> tempfile::NamedTempFile {
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![],
            checkpoints: vec![],
        };
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");
        rom_file
    }

    // --- Bug 2: n=1 extend restores from the single checkpoint ---

    #[test]
    fn test_extend_with_single_checkpoint_restores_from_it() {
        let (rom_file, _) = make_recording_with_single_checkpoint();
        let rom_path_str = rom_file.path().to_str().expect("rom path");

        let (_, pending) = AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
            .expect("create extend state");

        assert!(
            pending.is_some(),
            "extending with 1 checkpoint must restore from that checkpoint"
        );
        assert_eq!(
            pending.unwrap().state_bytes,
            b"single_checkpoint_state".to_vec()
        );
    }

    #[test]
    fn test_extend_with_single_checkpoint_starts_at_frame_after_checkpoint() {
        let (rom_file, _) = make_recording_with_single_checkpoint();
        let rom_path_str = rom_file.path().to_str().expect("rom path");

        let (state, _) = AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
            .expect("create extend state");

        // CP[0].frame_index = 299, so recording starts at 300
        assert_eq!(
            state.current_frame_index(),
            300,
            "extend with 1 checkpoint should start at CP[0].frame_index + 1"
        );
    }

    #[test]
    fn test_extend_with_single_checkpoint_has_no_extend_playback() {
        let (rom_file, _) = make_recording_with_single_checkpoint();
        let rom_path_str = rom_file.path().to_str().expect("rom path");

        let (state, _) = AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
            .expect("create extend state");

        assert!(
            !state.is_extending_playback(),
            "extend with 1 checkpoint should not replay frames — restoration alone is sufficient"
        );
    }

    #[test]
    fn test_extend_with_no_checkpoints_starts_from_scratch() {
        let rom_file = make_recording_with_no_checkpoints();
        let rom_path_str = rom_file.path().to_str().expect("rom path");

        let (state, pending) =
            AutorunState::new(AutorunMode::Record, rom_path_str, false, true, None)
                .expect("create extend state");

        assert!(
            pending.is_none(),
            "no checkpoints means no state to restore"
        );
        assert_eq!(state.current_frame_index(), 0, "should start from frame 0");
    }

    // --- Bug 1: pre-recorded frame flag prevents duplicate at extend transition ---

    #[test]
    fn test_begin_frame_clears_prerecorded_flag() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Record, rom_path_str, false, false, None)
                .expect("create state");

        state.begin_frame();
        assert!(!state.current_frame_was_prerecorded());
    }

    #[test]
    fn test_prerecorded_flag_true_after_next_playback_frame_returns_some() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![AutorunFrame {
                player1: 7,
                player2: 0,
            }],
            checkpoints: vec![AutorunCheckpoint {
                frame_index: 0,
                screen_crc: 0,
                state_bytes: vec![],
            }],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
                .expect("create state");

        state.begin_frame();
        let frame = state.next_playback_frame();
        assert!(frame.is_some(), "should return the pre-recorded frame");
        assert!(
            state.current_frame_was_prerecorded(),
            "flag must be true after next_playback_frame returned Some"
        );
    }

    #[test]
    fn test_prerecorded_flag_false_after_next_playback_frame_returns_none() {
        let rom_file = NamedTempFile::new().expect("create temp file");
        // Empty recording — next_playback_frame always returns None
        let autorun_file = AutorunFile {
            version: AUTORUN_VERSION,
            frames: vec![],
            checkpoints: vec![],
        };
        let autorun_path = autorun_path_for_rom(rom_file.path());
        save_autorun_file(&autorun_path, &autorun_file).expect("save");

        let rom_path_str = rom_file.path().to_str().expect("rom path");
        let (mut state, _) =
            AutorunState::new(AutorunMode::Playback, rom_path_str, false, false, None)
                .expect("create state");

        state.begin_frame();
        let frame = state.next_playback_frame();
        assert!(frame.is_none());
        assert!(
            !state.current_frame_was_prerecorded(),
            "flag must remain false when next_playback_frame returns None"
        );
    }
}