ratado 0.2.0

A fast, keyboard-driven terminal task manager built with Rust and Ratatui
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
//! Keyboard input to command mapping.
//!
//! This module provides the key mapping system that translates keyboard input
//! into application commands. The mapping is context-aware, taking into account
//! the current view and input mode.
//!
//! ## Keybinding Design
//!
//! The keybindings follow Vim conventions where possible:
//! - `j/k` for up/down navigation
//! - `h/l` for left/right panel switching
//! - `g/G` for top/bottom
//! - `Ctrl+d/u` for page down/up
//!
//! ## Example
//!
//! ```rust,no_run
//! use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
//! use ratado::handlers::input::map_key_to_command;
//! use ratado::handlers::commands::Command;
//! use ratado::app::App;
//!
//! # fn example(app: &App) {
//! let key = KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE);
//! let cmd = map_key_to_command(key, app);
//! assert!(matches!(cmd, Some(Command::Quit)));
//! # }
//! ```

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

use crate::app::{App, FocusPanel, InputMode, View};
use crate::handlers::commands::{Command, TuiLoggerEvent};
use crate::models::Priority;

/// Maps a keyboard event to a command based on the current application state.
///
/// The mapping considers:
/// - The current view (Main, Help, DebugLogs, etc.)
/// - The current input mode (Normal, Editing, Search)
///
/// # Arguments
///
/// * `key` - The keyboard event to map
/// * `app` - The current application state for context
///
/// # Returns
///
/// The corresponding command, or `None` if the key has no mapping in the
/// current context.
pub fn map_key_to_command(key: KeyEvent, app: &App) -> Option<Command> {
    // Global keybindings that work everywhere
    match key.code {
        // Force quit with Ctrl+C
        KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            return Some(Command::ForceQuit);
        }
        // Toggle debug logs with F12 (works everywhere)
        KeyCode::F(12) => {
            return Some(Command::ShowDebugLogs);
        }
        _ => {}
    }

    // Handle view-specific bindings
    match app.current_view {
        View::Help => return map_help_view_key(key),
        View::DebugLogs => return map_debug_view_key(key),
        View::Calendar => return map_calendar_view_key(key, app),
        View::TaskDetail => return map_task_detail_view_key(key),
        View::Search => {
            // In search view, handle search-specific keys
            if app.input_mode == InputMode::Search {
                return map_search_mode_key(key);
            }
        }
        _ => {}
    }

    // Handle mode-specific bindings
    match app.input_mode {
        InputMode::Normal => map_normal_mode_key(key, app),
        InputMode::Editing => map_editing_mode_key(key),
        InputMode::Search => map_search_mode_key(key),
    }
}

/// Maps keys in the Help view.
///
/// Any key closes the help screen and returns to the main view.
fn map_help_view_key(key: KeyEvent) -> Option<Command> {
    match key.code {
        // Explicit escape goes back to main
        KeyCode::Esc => Some(Command::ShowMain),
        // Any other key also closes help
        _ => Some(Command::ShowMain),
    }
}

/// Maps keys in the Debug Logs view.
///
/// Handles tui-logger widget navigation and log level controls.
fn map_debug_view_key(key: KeyEvent) -> Option<Command> {
    match key.code {
        // Escape returns to main view
        KeyCode::Esc => Some(Command::ShowMain),

        // tui-logger navigation
        KeyCode::Char(' ') => Some(Command::LoggerEvent(TuiLoggerEvent::SpaceBar)),
        KeyCode::Up | KeyCode::Char('k') => Some(Command::LoggerEvent(TuiLoggerEvent::Up)),
        KeyCode::Down | KeyCode::Char('j') => Some(Command::LoggerEvent(TuiLoggerEvent::Down)),
        KeyCode::PageUp => Some(Command::LoggerEvent(TuiLoggerEvent::PrevPage)),
        KeyCode::PageDown => Some(Command::LoggerEvent(TuiLoggerEvent::NextPage)),
        KeyCode::Left | KeyCode::Char('h') => Some(Command::LoggerEvent(TuiLoggerEvent::Left)),
        KeyCode::Right | KeyCode::Char('l') => Some(Command::LoggerEvent(TuiLoggerEvent::Right)),
        KeyCode::Char('+') | KeyCode::Char('=') => {
            Some(Command::LoggerEvent(TuiLoggerEvent::Plus))
        }
        KeyCode::Char('-') => Some(Command::LoggerEvent(TuiLoggerEvent::Minus)),
        KeyCode::Char('H') => Some(Command::LoggerEvent(TuiLoggerEvent::Hide)),

        _ => None,
    }
}

/// Maps keys in the Calendar view.
///
/// Handles navigation between days and weeks when focused on day grid,
/// or task navigation when focused on task list.
fn map_calendar_view_key(key: KeyEvent, app: &App) -> Option<Command> {
    use crate::ui::calendar::CalendarFocus;

    // Tab toggles focus regardless of current focus
    if key.code == KeyCode::Tab {
        return Some(Command::CalendarToggleFocus);
    }

    // 'f' toggles completed filter in both focus states
    if key.code == KeyCode::Char('f') {
        return Some(Command::CalendarToggleCompleted);
    }

    match app.calendar_state.focus {
        CalendarFocus::DayGrid => match key.code {
            // Escape returns to main view
            KeyCode::Esc => Some(Command::ShowMain),

            // Day navigation
            KeyCode::Left | KeyCode::Char('h') => Some(Command::CalendarPrevDay),
            KeyCode::Right | KeyCode::Char('l') => Some(Command::CalendarNextDay),

            // Week navigation
            KeyCode::Up | KeyCode::Char('k') => Some(Command::CalendarPrevWeek),
            KeyCode::Down | KeyCode::Char('j') => Some(Command::CalendarNextWeek),

            // Jump to today
            KeyCode::Char('t') => Some(Command::CalendarToday),

            // Select current day and return to main
            KeyCode::Enter => Some(Command::CalendarSelectDay),

            // Quit
            KeyCode::Char('q') => Some(Command::Quit),

            _ => None,
        },
        CalendarFocus::TaskList => match key.code {
            // Escape returns focus to day grid
            KeyCode::Esc => Some(Command::CalendarToggleFocus),

            // Task navigation
            KeyCode::Up | KeyCode::Char('k') => Some(Command::CalendarTaskUp),
            KeyCode::Down | KeyCode::Char('j') => Some(Command::CalendarTaskDown),

            // Task actions
            KeyCode::Char(' ') => Some(Command::CalendarToggleTask),
            KeyCode::Char('p') => Some(Command::CalendarCyclePriority),
            KeyCode::Char('e') => Some(Command::CalendarEditTask),
            KeyCode::Char('d') => Some(Command::DeleteTask),

            // Go to task in its project
            KeyCode::Enter => Some(Command::CalendarGoToTask),

            // Quit
            KeyCode::Char('q') => Some(Command::Quit),

            _ => None,
        },
    }
}

/// Maps keys in the Task Detail view.
///
/// Handles quick actions on the displayed task.
fn map_task_detail_view_key(key: KeyEvent) -> Option<Command> {
    match key.code {
        // Escape returns to main view
        KeyCode::Esc => Some(Command::ShowMain),

        // Quick task actions
        KeyCode::Char(' ') => Some(Command::ToggleTaskStatus),
        KeyCode::Char('p') => Some(Command::CyclePriority),
        KeyCode::Char('e') | KeyCode::Enter => Some(Command::EditTask),
        KeyCode::Char('d') => Some(Command::DeleteTask),

        // Quit
        KeyCode::Char('q') => Some(Command::Quit),

        _ => None,
    }
}

/// Maps keys in Normal mode (main task list view).
///
/// This is where most keybindings are defined for navigating and
/// managing tasks.
fn map_normal_mode_key(key: KeyEvent, app: &App) -> Option<Command> {
    match key.code {
        // === Quit ===
        KeyCode::Char('q') => Some(Command::Quit),

        // === Navigation ===
        KeyCode::Char('j') | KeyCode::Down => Some(Command::NavigateDown),
        KeyCode::Char('k') | KeyCode::Up => Some(Command::NavigateUp),
        KeyCode::Char('g') | KeyCode::Home => Some(Command::NavigateTop),
        KeyCode::Char('G') | KeyCode::End => Some(Command::NavigateBottom),

        // Page navigation with Ctrl+d/u
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            Some(Command::PageDown)
        }
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            Some(Command::PageUp)
        }

        // === Panel Switching ===
        KeyCode::Tab => Some(Command::SwitchPanel),
        KeyCode::Char('h') | KeyCode::Left => Some(Command::FocusSidebar),
        KeyCode::Char('l') | KeyCode::Right => Some(Command::FocusTaskList),

        // === Context-sensitive actions (tasks or projects based on focus) ===
        KeyCode::Char('a') => {
            if app.focus == FocusPanel::Sidebar {
                Some(Command::AddProject)
            } else {
                Some(Command::QuickCapture)
            }
        }
        KeyCode::Char('e') | KeyCode::Enter => {
            if app.focus == FocusPanel::Sidebar {
                Some(Command::EditProject)
            } else {
                Some(Command::EditTask)
            }
        }
        KeyCode::Char('d') => {
            if app.focus == FocusPanel::Sidebar {
                Some(Command::DeleteProject)
            } else {
                Some(Command::DeleteTask)
            }
        }

        // === Task-specific actions ===
        KeyCode::Char(' ') => Some(Command::ToggleTaskStatus),
        KeyCode::Char('p') => Some(Command::CyclePriority),
        KeyCode::Char('t') => Some(Command::EditTags),
        KeyCode::Char('m') => Some(Command::MoveToProject),

        // === Full Add Task form ===
        KeyCode::Char('A') => Some(Command::AddTask),

        // === Views ===
        KeyCode::Char('?') => Some(Command::ShowHelp),
        KeyCode::Char('/') => Some(Command::ShowSearch),
        KeyCode::Char('c') => Some(Command::ShowCalendar),
        KeyCode::Char('v') => Some(Command::ShowTaskDetail),

        // === Quick Filters ===
        KeyCode::Char('T') => Some(Command::FilterToday),
        KeyCode::Char('W') => Some(Command::FilterThisWeek),

        // Priority filters (1-4 keys)
        KeyCode::Char('1') => Some(Command::FilterByPriority(Priority::Low)),
        KeyCode::Char('2') => Some(Command::FilterByPriority(Priority::Medium)),
        KeyCode::Char('3') => Some(Command::FilterByPriority(Priority::High)),
        KeyCode::Char('4') => Some(Command::FilterByPriority(Priority::Urgent)),

        // === Filter/Sort ===
        KeyCode::Char('f') => Some(Command::ShowFilterSort),

        // === Settings ===
        KeyCode::Char('S') => Some(Command::ShowSettings),

        // === Other ===
        KeyCode::Char('r') => Some(Command::Refresh),

        _ => None,
    }
}

/// Maps keys in Editing mode (text input for task title, etc.).
///
/// Standard text editing keys plus escape to cancel and enter to submit.
fn map_editing_mode_key(key: KeyEvent) -> Option<Command> {
    // Check for Ctrl modifiers first (Emacs-style shortcuts)
    if key.modifiers.contains(KeyModifiers::CONTROL) {
        return match key.code {
            KeyCode::Char('a') => Some(Command::MoveCursorStart),
            KeyCode::Char('e') => Some(Command::MoveCursorEnd),
            _ => None,
        };
    }

    match key.code {
        // Exit editing
        KeyCode::Esc => Some(Command::CancelInput),
        KeyCode::Enter => Some(Command::SubmitInput),

        // Text editing
        KeyCode::Char(c) => Some(Command::InsertChar(c)),
        KeyCode::Backspace => Some(Command::DeleteCharBackward),
        KeyCode::Delete => Some(Command::DeleteCharForward),

        // Cursor movement
        KeyCode::Left => Some(Command::MoveCursorLeft),
        KeyCode::Right => Some(Command::MoveCursorRight),
        KeyCode::Home => Some(Command::MoveCursorStart),
        KeyCode::End => Some(Command::MoveCursorEnd),

        _ => None,
    }
}

/// Maps keys in Search mode.
///
/// Similar to editing mode but with search-specific behavior.
fn map_search_mode_key(key: KeyEvent) -> Option<Command> {
    // Check for Ctrl modifiers first (Emacs-style shortcuts)
    if key.modifiers.contains(KeyModifiers::CONTROL) {
        return match key.code {
            KeyCode::Char('a') => Some(Command::MoveCursorStart),
            KeyCode::Char('e') => Some(Command::MoveCursorEnd),
            KeyCode::Char('n') => Some(Command::SearchNavigateDown),
            KeyCode::Char('p') => Some(Command::SearchNavigateUp),
            _ => None,
        };
    }

    match key.code {
        // Exit search
        KeyCode::Esc => Some(Command::CancelInput),
        // Select current search result
        KeyCode::Enter => Some(Command::SearchSelectTask),

        // Navigate search results
        KeyCode::Down => Some(Command::SearchNavigateDown),
        KeyCode::Up => Some(Command::SearchNavigateUp),

        // Text editing
        KeyCode::Char(c) => Some(Command::InsertChar(c)),
        KeyCode::Backspace => Some(Command::DeleteCharBackward),
        KeyCode::Delete => Some(Command::DeleteCharForward),

        // Cursor movement within search text
        KeyCode::Left => Some(Command::MoveCursorLeft),
        KeyCode::Right => Some(Command::MoveCursorRight),
        KeyCode::Home => Some(Command::MoveCursorStart),
        KeyCode::End => Some(Command::MoveCursorEnd),

        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::{run_migrations, Database};

    async fn setup_app() -> App {
        let db = Database::open_in_memory().await.unwrap();
        run_migrations(&db).await.unwrap();
        App::new(db).await.unwrap()
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn key_with_mod(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
        KeyEvent::new(code, modifiers)
    }

    // === Normal Mode Tests ===

    #[tokio::test]
    async fn test_quit_keybinding() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('q')), &app);
        assert!(matches!(cmd, Some(Command::Quit)));
    }

    #[tokio::test]
    async fn test_force_quit_ctrl_c() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key_with_mod(KeyCode::Char('c'), KeyModifiers::CONTROL), &app);
        assert!(matches!(cmd, Some(Command::ForceQuit)));
    }

    #[tokio::test]
    async fn test_vim_navigation_j() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('j')), &app);
        assert!(matches!(cmd, Some(Command::NavigateDown)));
    }

    #[tokio::test]
    async fn test_vim_navigation_k() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('k')), &app);
        assert!(matches!(cmd, Some(Command::NavigateUp)));
    }

    #[tokio::test]
    async fn test_arrow_navigation_down() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Down), &app);
        assert!(matches!(cmd, Some(Command::NavigateDown)));
    }

    #[tokio::test]
    async fn test_arrow_navigation_up() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Up), &app);
        assert!(matches!(cmd, Some(Command::NavigateUp)));
    }

    #[tokio::test]
    async fn test_navigate_top_g() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('g')), &app);
        assert!(matches!(cmd, Some(Command::NavigateTop)));
    }

    #[tokio::test]
    async fn test_navigate_bottom_shift_g() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('G')), &app);
        assert!(matches!(cmd, Some(Command::NavigateBottom)));
    }

    #[tokio::test]
    async fn test_page_down_ctrl_d() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key_with_mod(KeyCode::Char('d'), KeyModifiers::CONTROL), &app);
        assert!(matches!(cmd, Some(Command::PageDown)));
    }

    #[tokio::test]
    async fn test_page_up_ctrl_u() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key_with_mod(KeyCode::Char('u'), KeyModifiers::CONTROL), &app);
        assert!(matches!(cmd, Some(Command::PageUp)));
    }

    #[tokio::test]
    async fn test_tab_switches_panel() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Tab), &app);
        assert!(matches!(cmd, Some(Command::SwitchPanel)));
    }

    #[tokio::test]
    async fn test_h_focuses_sidebar() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('h')), &app);
        assert!(matches!(cmd, Some(Command::FocusSidebar)));
    }

    #[tokio::test]
    async fn test_l_focuses_tasklist() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('l')), &app);
        assert!(matches!(cmd, Some(Command::FocusTaskList)));
    }

    #[tokio::test]
    async fn test_quick_capture_a() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('a')), &app);
        assert!(matches!(cmd, Some(Command::QuickCapture)));
    }

    #[tokio::test]
    async fn test_add_task_shift_a() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('A')), &app);
        assert!(matches!(cmd, Some(Command::AddTask)));
    }

    #[tokio::test]
    async fn test_edit_task_e() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('e')), &app);
        assert!(matches!(cmd, Some(Command::EditTask)));
    }

    #[tokio::test]
    async fn test_edit_task_enter() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Enter), &app);
        assert!(matches!(cmd, Some(Command::EditTask)));
    }

    #[tokio::test]
    async fn test_delete_task_d() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('d')), &app);
        assert!(matches!(cmd, Some(Command::DeleteTask)));
    }

    #[tokio::test]
    async fn test_toggle_status_space() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char(' ')), &app);
        assert!(matches!(cmd, Some(Command::ToggleTaskStatus)));
    }

    #[tokio::test]
    async fn test_cycle_priority_p() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('p')), &app);
        assert!(matches!(cmd, Some(Command::CyclePriority)));
    }

    #[tokio::test]
    async fn test_show_help_question_mark() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('?')), &app);
        assert!(matches!(cmd, Some(Command::ShowHelp)));
    }

    #[tokio::test]
    async fn test_show_search_slash() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('/')), &app);
        assert!(matches!(cmd, Some(Command::ShowSearch)));
    }

    #[tokio::test]
    async fn test_show_calendar_c() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('c')), &app);
        assert!(matches!(cmd, Some(Command::ShowCalendar)));
    }

    #[tokio::test]
    async fn test_filter_today_shift_t() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('T')), &app);
        assert!(matches!(cmd, Some(Command::FilterToday)));
    }

    #[tokio::test]
    async fn test_filter_week_shift_w() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('W')), &app);
        assert!(matches!(cmd, Some(Command::FilterThisWeek)));
    }

    #[tokio::test]
    async fn test_priority_filters() {
        let app = setup_app().await;

        let cmd = map_key_to_command(key(KeyCode::Char('1')), &app);
        assert!(matches!(cmd, Some(Command::FilterByPriority(Priority::Low))));

        let cmd = map_key_to_command(key(KeyCode::Char('2')), &app);
        assert!(matches!(
            cmd,
            Some(Command::FilterByPriority(Priority::Medium))
        ));

        let cmd = map_key_to_command(key(KeyCode::Char('3')), &app);
        assert!(matches!(
            cmd,
            Some(Command::FilterByPriority(Priority::High))
        ));

        let cmd = map_key_to_command(key(KeyCode::Char('4')), &app);
        assert!(matches!(
            cmd,
            Some(Command::FilterByPriority(Priority::Urgent))
        ));
    }

    #[tokio::test]
    async fn test_refresh_r() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('r')), &app);
        assert!(matches!(cmd, Some(Command::Refresh)));
    }

    #[tokio::test]
    async fn test_show_filter_sort_f() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Char('f')), &app);
        assert!(matches!(cmd, Some(Command::ShowFilterSort)));
    }

    #[tokio::test]
    async fn test_esc_does_nothing_in_normal_mode() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(cmd.is_none());
    }

    #[tokio::test]
    async fn test_f12_toggles_debug() {
        let app = setup_app().await;
        let cmd = map_key_to_command(key(KeyCode::F(12)), &app);
        assert!(matches!(cmd, Some(Command::ShowDebugLogs)));
    }

    // === Help View Tests ===

    #[tokio::test]
    async fn test_help_view_any_key_closes() {
        let mut app = setup_app().await;
        app.current_view = View::Help;

        let cmd = map_key_to_command(key(KeyCode::Char('x')), &app);
        assert!(matches!(cmd, Some(Command::ShowMain)));
    }

    #[tokio::test]
    async fn test_help_view_esc_closes() {
        let mut app = setup_app().await;
        app.current_view = View::Help;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::ShowMain)));
    }

    // === Debug View Tests ===

    #[tokio::test]
    async fn test_debug_view_esc_closes() {
        let mut app = setup_app().await;
        app.current_view = View::DebugLogs;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::ShowMain)));
    }

    #[tokio::test]
    async fn test_debug_view_navigation() {
        let mut app = setup_app().await;
        app.current_view = View::DebugLogs;

        // Space toggles focus
        let cmd = map_key_to_command(key(KeyCode::Char(' ')), &app);
        assert!(matches!(
            cmd,
            Some(Command::LoggerEvent(TuiLoggerEvent::SpaceBar))
        ));

        // j/k for navigation
        let cmd = map_key_to_command(key(KeyCode::Char('j')), &app);
        assert!(matches!(
            cmd,
            Some(Command::LoggerEvent(TuiLoggerEvent::Down))
        ));

        let cmd = map_key_to_command(key(KeyCode::Char('k')), &app);
        assert!(matches!(
            cmd,
            Some(Command::LoggerEvent(TuiLoggerEvent::Up))
        ));
    }

    // === Editing Mode Tests ===

    #[tokio::test]
    async fn test_editing_mode_chars() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key(KeyCode::Char('x')), &app);
        assert!(matches!(cmd, Some(Command::InsertChar('x'))));
    }

    #[tokio::test]
    async fn test_editing_mode_backspace() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key(KeyCode::Backspace), &app);
        assert!(matches!(cmd, Some(Command::DeleteCharBackward)));
    }

    #[tokio::test]
    async fn test_editing_mode_enter_submits() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key(KeyCode::Enter), &app);
        assert!(matches!(cmd, Some(Command::SubmitInput)));
    }

    #[tokio::test]
    async fn test_editing_mode_esc_cancels() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::CancelInput)));
    }

    #[tokio::test]
    async fn test_editing_mode_cursor_movement() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key(KeyCode::Left), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorLeft)));

        let cmd = map_key_to_command(key(KeyCode::Right), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorRight)));

        let cmd = map_key_to_command(key(KeyCode::Home), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorStart)));

        let cmd = map_key_to_command(key(KeyCode::End), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorEnd)));
    }

    #[tokio::test]
    async fn test_editing_mode_emacs_keys() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Editing;

        let cmd = map_key_to_command(key_with_mod(KeyCode::Char('a'), KeyModifiers::CONTROL), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorStart)));

        let cmd = map_key_to_command(key_with_mod(KeyCode::Char('e'), KeyModifiers::CONTROL), &app);
        assert!(matches!(cmd, Some(Command::MoveCursorEnd)));
    }

    // === Search Mode Tests ===

    #[tokio::test]
    async fn test_search_mode_chars() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Search;

        let cmd = map_key_to_command(key(KeyCode::Char('a')), &app);
        assert!(matches!(cmd, Some(Command::InsertChar('a'))));
    }

    #[tokio::test]
    async fn test_search_mode_esc_cancels() {
        let mut app = setup_app().await;
        app.input_mode = InputMode::Search;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::CancelInput)));
    }

    // === Calendar View Tests ===

    #[tokio::test]
    async fn test_calendar_view_esc_returns() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::ShowMain)));
    }

    #[tokio::test]
    async fn test_calendar_view_left_prev_day() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Left), &app);
        assert!(matches!(cmd, Some(Command::CalendarPrevDay)));
    }

    #[tokio::test]
    async fn test_calendar_view_right_next_day() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Right), &app);
        assert!(matches!(cmd, Some(Command::CalendarNextDay)));
    }

    #[tokio::test]
    async fn test_calendar_view_up_prev_week() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Up), &app);
        assert!(matches!(cmd, Some(Command::CalendarPrevWeek)));
    }

    #[tokio::test]
    async fn test_calendar_view_down_next_week() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Down), &app);
        assert!(matches!(cmd, Some(Command::CalendarNextWeek)));
    }

    #[tokio::test]
    async fn test_calendar_view_t_today() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Char('t')), &app);
        assert!(matches!(cmd, Some(Command::CalendarToday)));
    }

    #[tokio::test]
    async fn test_calendar_view_vim_navigation() {
        let mut app = setup_app().await;
        app.current_view = View::Calendar;

        let cmd = map_key_to_command(key(KeyCode::Char('h')), &app);
        assert!(matches!(cmd, Some(Command::CalendarPrevDay)));

        let cmd = map_key_to_command(key(KeyCode::Char('l')), &app);
        assert!(matches!(cmd, Some(Command::CalendarNextDay)));

        let cmd = map_key_to_command(key(KeyCode::Char('j')), &app);
        assert!(matches!(cmd, Some(Command::CalendarNextWeek)));

        let cmd = map_key_to_command(key(KeyCode::Char('k')), &app);
        assert!(matches!(cmd, Some(Command::CalendarPrevWeek)));
    }

    // === Task Detail View Tests ===

    #[tokio::test]
    async fn test_task_detail_view_esc_returns() {
        let mut app = setup_app().await;
        app.current_view = View::TaskDetail;

        let cmd = map_key_to_command(key(KeyCode::Esc), &app);
        assert!(matches!(cmd, Some(Command::ShowMain)));
    }

    #[tokio::test]
    async fn test_task_detail_view_space_toggles() {
        let mut app = setup_app().await;
        app.current_view = View::TaskDetail;

        let cmd = map_key_to_command(key(KeyCode::Char(' ')), &app);
        assert!(matches!(cmd, Some(Command::ToggleTaskStatus)));
    }

    #[tokio::test]
    async fn test_task_detail_view_p_cycles_priority() {
        let mut app = setup_app().await;
        app.current_view = View::TaskDetail;

        let cmd = map_key_to_command(key(KeyCode::Char('p')), &app);
        assert!(matches!(cmd, Some(Command::CyclePriority)));
    }

    #[tokio::test]
    async fn test_task_detail_view_e_edits() {
        let mut app = setup_app().await;
        app.current_view = View::TaskDetail;

        let cmd = map_key_to_command(key(KeyCode::Char('e')), &app);
        assert!(matches!(cmd, Some(Command::EditTask)));
    }

    #[tokio::test]
    async fn test_task_detail_view_d_deletes() {
        let mut app = setup_app().await;
        app.current_view = View::TaskDetail;

        let cmd = map_key_to_command(key(KeyCode::Char('d')), &app);
        assert!(matches!(cmd, Some(Command::DeleteTask)));
    }
}