keybr-tui 0.2.1

Adaptive terminal (TUI) typing trainer in Rust that ports the keybr.com algorithm, runs offline, and imports your keybr.com data
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
use std::time::Instant;

use crossterm::event::{KeyCode, KeyModifiers};

use crate::app::{App, AppScreen, ErrorMode};
use crate::components::menu::MENU_ITEMS;
use crate::components::settings::SETTINGS_COUNT;
use crate::events::AppEvent;
use crate::persistence::today_date_string;

/// Request a stats save. `update` never touches the disk itself — main's
/// event loop performs the actual write after the event is processed.
/// This keeps the MVU update layer pure and, crucially, stops unit tests
/// that drive `update` from clobbering the user's real stats file.
fn request_stats_save(app: &mut App) {
    app.pending_stats_save = true;
}

/// Request a config save (see `request_stats_save` for why it's deferred).
fn request_config_save(app: &mut App) {
    app.pending_config_save = true;
}

/// Increment `today_seconds_practiced` by `lesson_seconds`, after checking
/// for a day-rollover that may have happened mid-session.
/// Tracking in seconds (display as minutes) avoids the sub-minute floor-to-zero
/// that made the daily-goal bar appear stuck at 0.
fn tick_daily_goal(app: &mut App, lesson_seconds: u32) {
    let today = today_date_string();
    if app.today_date != today {
        app.today_date = today;
        app.today_seconds_practiced = 0;
    }
    app.today_seconds_practiced = app.today_seconds_practiced.saturating_add(lesson_seconds);
}

pub fn update(app: &mut App, event: AppEvent) {
    match event {
        AppEvent::Key(key) => handle_key(app, key),
        AppEvent::Tick => {}
        AppEvent::Resize => {}
    }
}

fn handle_key(app: &mut App, key: crossterm::event::KeyEvent) {
    use KeyCode::*;

    // Global: Ctrl+C always quits
    if key.code == Char('c') && key.modifiers == KeyModifiers::CONTROL {
        request_stats_save(app);
        app.running = false;
        return;
    }

    match app.screen {
        AppScreen::Menu => handle_menu_key(app, key),
        AppScreen::Typing => handle_typing_key(app, key),
        AppScreen::Progress => handle_progress_key(app, key),
        AppScreen::Settings => handle_settings_key(app, key),
    }
}

// --- Menu screen ---

fn handle_menu_key(app: &mut App, key: crossterm::event::KeyEvent) {
    use KeyCode::*;

    match key.code {
        Up => {
            if app.menu_selection > 0 {
                app.menu_selection -= 1;
            } else {
                app.menu_selection = MENU_ITEMS.len() - 1;
            }
        }
        Down => {
            app.menu_selection = (app.menu_selection + 1) % MENU_ITEMS.len();
        }
        Enter => {
            match app.menu_selection {
                0 => {
                    // Start Practice
                    app.start_next_lesson();
                }
                1 => {
                    // View Progress
                    app.screen = AppScreen::Progress;
                }
                2 => {
                    // Settings
                    app.screen = AppScreen::Settings;
                }
                3 => {
                    // Quit
                    request_stats_save(app);
                    app.running = false;
                }
                _ => {}
            }
        }
        Char('q') | Esc => {
            request_stats_save(app);
            app.running = false;
        }
        _ => {}
    }
}

// --- Typing screen ---

fn handle_typing_key(app: &mut App, key: crossterm::event::KeyEvent) {
    use KeyCode::*;

    match key.code {
        Esc => {
            // Esc during typing goes to menu, not quit
            request_stats_save(app);
            app.screen = AppScreen::Menu;
        }

        // Toggle error mode
        Tab => {
            app.error_mode = match app.error_mode {
                ErrorMode::ForgiveMistakes => ErrorMode::StopOnError,
                ErrorMode::StopOnError => ErrorMode::ForgiveMistakes,
            };
            request_config_save(app);
        }

        // Backspace — move cursor back and clear error mark
        Backspace if app.cursor_pos > 0 => {
            app.cursor_pos -= 1;
            app.error_positions.remove(&app.cursor_pos);
            app.first_attempt_correct.remove(&app.cursor_pos);
            app.recovered_positions.remove(&app.cursor_pos);
            app.key_target_start = Some(Instant::now());
        }

        // Typing input — includes space
        Char(typed) => {
            handle_typed_char(app, typed);
        }

        _ => {}
    }
}

fn handle_typed_char(app: &mut App, typed: char) {
    if app.cursor_pos >= app.generated_text.chars().count() {
        return;
    }

    // Start lesson timer on first keystroke
    if app.lesson_start.is_none() {
        app.lesson_start = Some(Instant::now());
        app.key_target_start = Some(Instant::now());
    }

    let target = match app.generated_text.chars().nth(app.cursor_pos) {
        Some(c) => c,
        None => return,
    };

    let reaction_ms = app
        .key_target_start
        .map(|t| t.elapsed().as_millis() as u64)
        .unwrap_or(0);

    if typed == target {
        let pos = app.cursor_pos;

        if app.ever_error_positions.contains(&pos) {
            app.recovered_positions.insert(pos);
        } else {
            app.first_attempt_correct.insert(pos);
        }

        if target != ' ' {
            if app.first_attempt_correct.contains(&pos) {
                let stats = app.per_key_stats.entry(target).or_default();
                if (40..=12000).contains(&reaction_ms) {
                    stats.record_hit(reaction_ms);
                }
            }
            app.lesson_positions += 1;
        }
        app.lesson_correct += 1;
        app.cursor_pos += 1;
        app.key_target_start = Some(Instant::now());

        if app.cursor_pos >= app.generated_text.chars().count() {
            let lesson_seconds = app
                .lesson_start
                .map(|s| s.elapsed().as_secs() as u32)
                .unwrap_or(0);
            app.finish_lesson();
            tick_daily_goal(app, lesson_seconds);
            request_stats_save(app);
        }
    } else {
        let pos = app.cursor_pos;

        if target != ' ' {
            let stats = app.per_key_stats.entry(target).or_default();
            stats.record_error();
            if !app.error_positions.contains(&pos) {
                app.lesson_positions += 1;
                app.lesson_errors += 1;
            }
        }

        app.ever_error_positions.insert(pos);

        match app.error_mode {
            ErrorMode::ForgiveMistakes => {
                app.error_positions.insert(pos);
                app.cursor_pos += 1;
                app.key_target_start = Some(Instant::now());

                if app.cursor_pos >= app.generated_text.chars().count() {
                    let lesson_seconds = app
                        .lesson_start
                        .map(|s| s.elapsed().as_secs() as u32)
                        .unwrap_or(0);
                    app.finish_lesson();
                    tick_daily_goal(app, lesson_seconds);
                    request_stats_save(app);
                }
            }
            ErrorMode::StopOnError => {
                app.error_positions.insert(pos);
                app.key_target_start = Some(Instant::now());
            }
        }
    }
}

// --- Progress screen ---

fn handle_progress_key(app: &mut App, key: crossterm::event::KeyEvent) {
    if key.code == KeyCode::Esc {
        app.screen = AppScreen::Menu;
    }
}

// --- Settings screen ---

fn handle_settings_key(app: &mut App, key: crossterm::event::KeyEvent) {
    use KeyCode::*;

    match key.code {
        Esc => {
            request_config_save(app);
            app.screen = AppScreen::Menu;
        }
        Up => {
            if app.settings_selection > 0 {
                app.settings_selection -= 1;
            } else {
                app.settings_selection = SETTINGS_COUNT - 1;
            }
        }
        Down => {
            app.settings_selection = (app.settings_selection + 1) % SETTINGS_COUNT;
        }
        Left => {
            match app.settings_selection {
                0 => {
                    // Decrease target WPM
                    let new_wpm = app.target_wpm().saturating_sub(5).max(10);
                    app.set_target_wpm(new_wpm);
                }
                1 => {
                    // Toggle error mode
                    app.error_mode = match app.error_mode {
                        ErrorMode::ForgiveMistakes => ErrorMode::StopOnError,
                        ErrorMode::StopOnError => ErrorMode::ForgiveMistakes,
                    };
                }
                2 => {
                    // Decrease fragment length
                    app.fragment_length = app.fragment_length.saturating_sub(10).max(20);
                }
                3 => {
                    // Decrease alphabet size (one forced letter per 0.05 step).
                    // Round to 2 decimals so repeated steps don't drift.
                    app.alphabet_size = ((app.alphabet_size - 0.05).max(0.0) * 100.0).round() / 100.0;
                    app.scheduler.alphabet_size = app.alphabet_size;
                }
                _ => {}
            }
            request_config_save(app);
        }
        Right => {
            match app.settings_selection {
                0 => {
                    // Increase target WPM
                    let new_wpm = (app.target_wpm() + 5).min(200);
                    app.set_target_wpm(new_wpm);
                }
                1 => {
                    // Toggle error mode
                    app.error_mode = match app.error_mode {
                        ErrorMode::ForgiveMistakes => ErrorMode::StopOnError,
                        ErrorMode::StopOnError => ErrorMode::ForgiveMistakes,
                    };
                }
                2 => {
                    // Increase fragment length
                    app.fragment_length = (app.fragment_length + 10).min(500);
                }
                3 => {
                    // Increase alphabet size (one forced letter per 0.05 step).
                    app.alphabet_size = ((app.alphabet_size + 0.05).min(1.0) * 100.0).round() / 100.0;
                    app.scheduler.alphabet_size = app.alphabet_size;
                }
                _ => {}
            }
            request_config_save(app);
        }
        Enter
            // Toggle error mode on Enter when selected
            if app.settings_selection == 1 => {
                app.error_mode = match app.error_mode {
                    ErrorMode::ForgiveMistakes => ErrorMode::StopOnError,
                    ErrorMode::StopOnError => ErrorMode::ForgiveMistakes,
                };
                request_config_save(app);
            }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};

    fn make_key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::NONE,
            kind: KeyEventKind::Press,
            state: KeyEventState::NONE,
        }
    }

    fn make_test_app(text: &str) -> App {
        let mut app = App::new();
        app.screen = AppScreen::Typing;
        app.generated_text = text.to_string();
        app.cursor_pos = 0;
        app.error_positions.clear();
        app.first_attempt_correct.clear();
        app.recovered_positions.clear();
        app.ever_error_positions.clear();
        app.lesson_start = Some(Instant::now());
        app.key_target_start = Some(Instant::now());
        app
    }

    #[test]
    fn backspace_decrements_cursor() {
        let mut app = make_test_app("abc");
        app.cursor_pos = 2;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert_eq!(app.cursor_pos, 1);
    }

    #[test]
    fn backspace_does_not_go_below_zero() {
        let mut app = make_test_app("abc");
        app.cursor_pos = 0;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert_eq!(app.cursor_pos, 0);
    }

    #[test]
    fn backspace_removes_error_mark() {
        let mut app = make_test_app("abc");
        app.cursor_pos = 2;
        app.error_positions.insert(1);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert_eq!(app.cursor_pos, 1);
        assert!(!app.error_positions.contains(&1));
    }

    #[test]
    fn backspace_resets_key_target_start() {
        let mut app = make_test_app("abc");
        app.cursor_pos = 2;
        app.key_target_start = None;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert!(app.key_target_start.is_some());
    }

    #[test]
    fn correct_char_marks_first_attempt() {
        let mut app = make_test_app("ab");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('a'))));
        assert!(app.first_attempt_correct.contains(&0));
        assert!(!app.recovered_positions.contains(&0));
    }

    #[test]
    fn error_then_backspace_then_correct_marks_recovered() {
        let mut app = make_test_app("abc");
        app.error_mode = ErrorMode::ForgiveMistakes;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('x'))));
        assert_eq!(app.cursor_pos, 1);
        assert!(app.error_positions.contains(&0));
        assert!(app.ever_error_positions.contains(&0));
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert_eq!(app.cursor_pos, 0);
        assert!(!app.error_positions.contains(&0));
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('a'))));
        assert!(app.recovered_positions.contains(&0));
        assert!(!app.first_attempt_correct.contains(&0));
    }

    #[test]
    fn stop_on_error_does_not_advance_cursor() {
        let mut app = make_test_app("ab");
        app.error_mode = ErrorMode::StopOnError;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('x'))));
        assert_eq!(app.cursor_pos, 0);
        assert!(app.error_positions.contains(&0));
    }

    #[test]
    fn forgive_mistakes_advances_cursor_on_error() {
        let mut app = make_test_app("ab");
        app.error_mode = ErrorMode::ForgiveMistakes;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('x'))));
        assert_eq!(app.cursor_pos, 1);
        assert!(app.error_positions.contains(&0));
    }

    #[test]
    fn backspace_clears_first_attempt_and_recovered() {
        let mut app = make_test_app("ab");
        app.cursor_pos = 1;
        app.first_attempt_correct.insert(0);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Backspace)));
        assert!(!app.first_attempt_correct.contains(&0));
    }

    #[test]
    fn toggle_error_mode() {
        let mut app = make_test_app("ab");
        app.error_mode = ErrorMode::ForgiveMistakes;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Tab)));
        assert_eq!(app.error_mode, ErrorMode::StopOnError);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Tab)));
        assert_eq!(app.error_mode, ErrorMode::ForgiveMistakes);
    }

    #[test]
    fn esc_during_typing_goes_to_menu() {
        let mut app = make_test_app("abc");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert_eq!(app.screen, AppScreen::Menu);
        assert!(app.running);
    }

    #[test]
    fn esc_on_menu_quits() {
        let mut app = App::new();
        assert_eq!(app.screen, AppScreen::Menu);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert!(!app.running);
    }

    #[test]
    fn q_on_menu_quits() {
        let mut app = App::new();
        assert_eq!(app.screen, AppScreen::Menu);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('q'))));
        assert!(!app.running);
    }

    #[test]
    fn menu_navigation() {
        let mut app = App::new();
        assert_eq!(app.menu_selection, 0);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Down)));
        assert_eq!(app.menu_selection, 1);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Down)));
        assert_eq!(app.menu_selection, 2);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Up)));
        assert_eq!(app.menu_selection, 1);
    }

    #[test]
    fn menu_wraps_around() {
        let mut app = App::new();
        assert_eq!(app.menu_selection, 0);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Up)));
        assert_eq!(app.menu_selection, MENU_ITEMS.len() - 1);
    }

    #[test]
    fn menu_enter_starts_practice() {
        let mut app = App::new();
        app.menu_selection = 0;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Enter)));
        assert_eq!(app.screen, AppScreen::Typing);
    }

    #[test]
    fn menu_enter_opens_progress() {
        let mut app = App::new();
        app.menu_selection = 1;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Enter)));
        assert_eq!(app.screen, AppScreen::Progress);
    }

    #[test]
    fn menu_enter_opens_settings() {
        let mut app = App::new();
        app.menu_selection = 2;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Enter)));
        assert_eq!(app.screen, AppScreen::Settings);
    }

    #[test]
    fn finishing_lesson_stays_on_typing_screen() {
        // Typing the last char of a fragment should keep us on the Typing
        // screen — no separate summary screen anymore — and immediately
        // regenerate a new fragment.
        let mut app = make_test_app("a");
        let old_text = app.generated_text.clone();
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('a'))));
        assert_eq!(app.screen, AppScreen::Typing);
        assert!(app.last_lesson.is_some());
        assert_ne!(app.generated_text, old_text);
        // The cursor must be reset for the new fragment.
        assert_eq!(app.cursor_pos, 0);
    }

    #[test]
    fn finishing_lesson_does_not_require_extra_keystroke() {
        // Verify that the next fragment is ready to be typed straight away,
        // no intervening "press any key" intercept.
        let mut app = make_test_app("a");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('a'))));
        assert_eq!(app.screen, AppScreen::Typing);
        // The first character of the new fragment is the live target —
        // it must be a real char (not empty).
        assert!(!app.generated_text.is_empty());
    }

    #[test]
    fn progress_esc_goes_to_menu() {
        let mut app = App::new();
        app.screen = AppScreen::Progress;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert_eq!(app.screen, AppScreen::Menu);
    }

    #[test]
    fn settings_esc_goes_to_menu() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert_eq!(app.screen, AppScreen::Menu);
    }

    #[test]
    fn settings_adjusts_wpm() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        app.settings_selection = 0;
        let initial_wpm = app.target_wpm();
        update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        assert_eq!(app.target_wpm(), initial_wpm + 5);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Left)));
        assert_eq!(app.target_wpm(), initial_wpm);
    }

    #[test]
    fn settings_toggles_error_mode() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        app.settings_selection = 1;
        app.error_mode = ErrorMode::ForgiveMistakes;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        assert_eq!(app.error_mode, ErrorMode::StopOnError);
    }

    #[test]
    fn settings_adjusts_fragment_length() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        app.settings_selection = 2;
        let initial = app.fragment_length;
        update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        assert_eq!(app.fragment_length, initial + 10);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Left)));
        assert_eq!(app.fragment_length, initial);
    }

    #[test]
    fn settings_adjusts_alphabet_size() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        app.settings_selection = 3;
        assert_eq!(app.alphabet_size, 0.0);

        // One step right: +0.05, mirrored onto the scheduler.
        update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        assert_eq!(app.alphabet_size, 0.05);
        assert_eq!(app.scheduler.alphabet_size, 0.05);

        // Back left to the floor — and never below 0.0.
        update(&mut app, AppEvent::Key(make_key(KeyCode::Left)));
        assert_eq!(app.alphabet_size, 0.0);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Left)));
        assert_eq!(app.alphabet_size, 0.0);
        assert_eq!(app.scheduler.alphabet_size, 0.0);

        // Saturates at 1.0 (20 steps from 0.0, then a few more).
        for _ in 0..25 {
            update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        }
        assert_eq!(app.alphabet_size, 1.0);
        assert_eq!(app.scheduler.alphabet_size, 1.0);

        // And back down to the floor without drift.
        for _ in 0..25 {
            update(&mut app, AppEvent::Key(make_key(KeyCode::Left)));
        }
        assert_eq!(app.alphabet_size, 0.0);
    }

    // --- Deferred persistence: update must only raise flags, never write ---

    #[test]
    fn quit_raises_stats_save_flag() {
        let mut app = App::new();
        assert!(!app.pending_stats_save);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert!(!app.running);
        assert!(app.pending_stats_save, "quit must request a stats save");
    }

    #[test]
    fn settings_change_raises_config_save_flag() {
        let mut app = App::new();
        app.screen = AppScreen::Settings;
        app.settings_selection = 0;
        assert!(!app.pending_config_save);
        update(&mut app, AppEvent::Key(make_key(KeyCode::Right)));
        assert!(app.pending_config_save);
        assert!(!app.pending_stats_save);
    }

    #[test]
    fn error_mode_toggle_raises_config_save_flag() {
        let mut app = make_test_app("ab");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Tab)));
        assert!(app.pending_config_save);
    }

    #[test]
    fn finishing_lesson_raises_stats_save_flag() {
        let mut app = make_test_app("a");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Char('a'))));
        assert!(app.pending_stats_save);
    }

    #[test]
    fn esc_from_typing_raises_stats_save_flag() {
        let mut app = make_test_app("abc");
        update(&mut app, AppEvent::Key(make_key(KeyCode::Esc)));
        assert_eq!(app.screen, AppScreen::Menu);
        assert!(app.pending_stats_save);
    }
}