elio 1.3.0

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
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
use super::super::*;
use super::helpers::{temp_path, wait_for_background_preview, wait_for_directory_load};
use crate::config::Action;
use std::{
    fs, thread,
    time::{Duration, Instant},
};

#[cfg(all(unix, not(target_os = "macos")))]
struct OpenInSystemCaptureGuard;

#[cfg(all(unix, not(target_os = "macos")))]
impl OpenInSystemCaptureGuard {
    fn install(path: std::path::PathBuf) -> Self {
        crate::fs::set_open_in_system_capture_for_test(Some(path));
        Self
    }
}

#[cfg(all(unix, not(target_os = "macos")))]
impl Drop for OpenInSystemCaptureGuard {
    fn drop(&mut self) {
        crate::fs::set_open_in_system_capture_for_test(None);
    }
}

#[test]
fn shift_slash_opens_and_closes_help_overlay() {
    let root = temp_path("help-shift-slash");
    fs::create_dir_all(&root).expect("failed to create temp root");

    let mut app = App::new_at(root.clone()).expect("failed to create app");

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('/'),
        KeyModifiers::SHIFT,
    )))
    .expect("shift-slash should open help");
    assert!(app.overlays.help);

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('/'),
        KeyModifiers::SHIFT,
    )))
    .expect("shift-slash should close help");
    assert!(!app.overlays.help);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn q_sets_should_quit() {
    let root = temp_path("quit-shortcut");
    fs::create_dir_all(&root).expect("failed to create temp root");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    assert!(!app.should_quit);

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('q'))))
        .expect("q should request quit");

    assert!(app.should_quit);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn enter_opens_selected_file_with_system_opener() {
    let root = temp_path("enter-opens-file");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let file_path = root.join("note.txt");
    fs::write(&file_path, "hello").expect("failed to write temp file");
    let capture = root.join("capture.txt");
    let _capture_guard = OpenInSystemCaptureGuard::install(capture.clone());

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    wait_for_directory_load(&mut app);

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Enter,
        KeyModifiers::NONE,
    )))
    .expect("enter should open selected file");

    let deadline = Instant::now() + Duration::from_millis(1000);
    while !capture.exists() && Instant::now() < deadline {
        thread::sleep(Duration::from_millis(10));
    }

    let opened = fs::read_to_string(&capture).expect("capture should exist");
    assert_eq!(opened, file_path.display().to_string());

    fs::remove_dir_all(root).ok();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn newline_key_event_also_opens_selected_file() {
    let root = temp_path("newline-opens-file");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let file_path = root.join("note.txt");
    fs::write(&file_path, "hello").expect("failed to write temp file");
    let capture = root.join("capture.txt");
    let _capture_guard = OpenInSystemCaptureGuard::install(capture.clone());

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    wait_for_directory_load(&mut app);

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('\n'),
        KeyModifiers::NONE,
    )))
    .expect("newline key event should open selected file");

    let deadline = Instant::now() + Duration::from_millis(1000);
    while !capture.exists() && Instant::now() < deadline {
        thread::sleep(Duration::from_millis(10));
    }

    let opened = fs::read_to_string(&capture).expect("capture should exist");
    assert_eq!(opened, file_path.display().to_string());

    fs::remove_dir_all(root).ok();
}

#[test]
fn c_opens_and_esc_closes_copy_overlay() {
    let root = temp_path("copy-overlay-shortcut");
    fs::create_dir_all(&root).expect("failed to create temp root");
    fs::write(root.join("report.txt"), "hello").expect("failed to write temp file");

    let mut app = App::new_at(root.clone()).expect("failed to create app");

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('c'))))
        .expect("c should open copy overlay");
    assert!(app.copy_is_open());

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Esc)))
        .expect("esc should close copy overlay");
    assert!(!app.copy_is_open());

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn g_opens_goto_overlay_and_goto_shortcuts_keep_g_for_top() {
    let root = temp_path("goto-overlay-shortcut");
    fs::create_dir_all(&root).expect("failed to create temp root");
    for name in ["a.txt", "b.txt", "c.txt"] {
        fs::write(root.join(name), name).expect("failed to write temp file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.select_last();
    assert_eq!(
        app.navigation.selected, 2,
        "G behavior should still reach the last item"
    );

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('g'))))
        .expect("g should open go-to overlay");
    assert!(app.goto_is_open());
    assert_eq!(
        app.navigation.selected, 2,
        "opening the go-to overlay should not move selection"
    );

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('g'))))
        .expect("g inside go-to overlay should jump to top");
    assert!(!app.goto_is_open());
    assert_eq!(
        app.navigation.selected, 0,
        "go-to g shortcut should move to the top item"
    );

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('G'))))
        .expect("capital G should still move to the last item");
    assert_eq!(
        app.navigation.selected, 2,
        "capital G should keep the old bottom-jump behavior"
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn tab_and_shift_tab_cycle_sidebar_locations_and_skip_section_rows() {
    let root = temp_path("tab-cycles-pinned-places");
    let downloads = root.join("downloads");
    let usb = root.join("usb");
    fs::create_dir_all(&downloads).expect("failed to create downloads dir");
    fs::create_dir_all(&usb).expect("failed to create usb dir");

    let sidebar_rows = || {
        vec![
            SidebarRow::Item(SidebarItem::new(
                SidebarItemKind::Home,
                "Home",
                "H",
                root.clone(),
            )),
            SidebarRow::Item(SidebarItem::new(
                SidebarItemKind::Downloads,
                "Downloads",
                "D",
                downloads.clone(),
            )),
            SidebarRow::Section { title: "Devices" },
            SidebarRow::Item(SidebarItem::new(
                SidebarItemKind::Device { removable: true },
                "USB",
                "U",
                usb.clone(),
            )),
        ]
    };

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.sidebar = sidebar_rows();

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Tab)))
        .expect("tab should cycle sidebar locations");
    wait_for_directory_load(&mut app);
    assert_eq!(app.navigation.cwd, downloads);

    app.navigation.sidebar = sidebar_rows();
    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Tab)))
        .expect("tab should continue into device rows");
    wait_for_directory_load(&mut app);
    assert_eq!(app.navigation.cwd, usb);

    app.navigation.sidebar = sidebar_rows();
    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Tab)))
        .expect("tab should wrap back to the first sidebar location");
    wait_for_directory_load(&mut app);
    assert_eq!(app.navigation.cwd, root);

    app.navigation.sidebar = sidebar_rows();
    app.set_dir(usb.clone()).expect("device path should open");
    wait_for_directory_load(&mut app);

    app.navigation.sidebar = sidebar_rows();
    app.handle_event(Event::Key(KeyEvent::from(KeyCode::BackTab)))
        .expect("shift-tab should walk sidebar locations in reverse");
    wait_for_directory_load(&mut app);
    assert_eq!(app.navigation.cwd, downloads);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn repeated_down_arrow_is_throttled_without_starving_hold_repeat() {
    let root = temp_path("down-arrow-debounce");
    fs::create_dir_all(&root).expect("failed to create temp root");
    for name in ["a.txt", "b.txt", "c.txt"] {
        fs::write(root.join(name), name).expect("failed to write temp file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.select_index(0);

    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)))
        .expect("first down arrow should be handled");

    let throttled_at = app
        .input
        .last_navigation_key
        .expect("accepted navigation key should be tracked")
        .1;
    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)))
        .expect("second down arrow should be handled");

    assert_eq!(app.navigation.selected, 1);
    assert_eq!(
        app.input
            .last_navigation_key
            .expect("throttled navigation key should keep prior timestamp")
            .1,
        throttled_at
    );

    app.input.last_navigation_key = Some((
        NavigationRepeatKey::Down,
        Instant::now() - KEY_REPEAT_NAV_INTERVAL - Duration::from_millis(1),
    ));
    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)))
        .expect("third down arrow should be handled");

    assert_eq!(app.navigation.selected, 2);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn high_frequency_alt_right_scrolls_preview_instead_of_history() {
    let root = temp_path("preview-horizontal-alt-right");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let file_path = root.join("long.rs");
    fs::write(
        &file_path,
        "fn main() { let preview_line = \"this line is intentionally long for horizontal preview scrolling\"; }\n",
    )
    .expect("failed to write temp file");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.input.wheel_profile = WheelProfile::HighFrequency;
    app.input.last_wheel_target = Some(WheelTarget::Entries);
    app.select_index(0);
    app.input.last_selection_change_at =
        Instant::now() - PREVIEW_AUTO_FOCUS_DELAY - Duration::from_millis(1);
    app.set_frame_state(FrameState {
        preview_panel: Some(Rect {
            x: 21,
            y: 0,
            width: 20,
            height: 8,
        }),
        preview_rows_visible: 6,
        preview_cols_visible: 12,
        ..FrameState::default()
    });

    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Right, KeyModifiers::ALT)))
        .expect("alt-right should be handled");

    assert!(app.preview.state.horizontal_scroll > 0);
    assert_eq!(app.navigation.selected, 0);
    assert_eq!(app.input.last_wheel_target, Some(WheelTarget::Preview));

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn high_frequency_down_arrow_keeps_browser_navigation() {
    let root = temp_path("high-frequency-down-keeps-browser");
    fs::create_dir_all(&root).expect("failed to create temp root");
    for name in ["a.txt", "b.txt", "c.txt"] {
        fs::write(root.join(name), name).expect("failed to write temp file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.input.wheel_profile = WheelProfile::HighFrequency;
    app.select_index(0);
    app.input.last_wheel_target = Some(WheelTarget::Preview);
    app.input.last_selection_change_at =
        Instant::now() - PREVIEW_AUTO_FOCUS_DELAY - Duration::from_millis(1);
    app.set_frame_state(FrameState {
        preview_panel: Some(Rect {
            x: 21,
            y: 0,
            width: 20,
            height: 8,
        }),
        preview_rows_visible: 4,
        preview_cols_visible: 20,
        ..FrameState::default()
    });

    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)))
        .expect("down arrow should be handled");

    assert_eq!(app.navigation.selected, 1);
    assert_eq!(app.preview.state.scroll, 0);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn high_frequency_right_arrow_in_list_view_still_enters_directory() {
    let root = temp_path("high-frequency-right-enters");
    let child = root.join("child");
    fs::create_dir_all(&child).expect("failed to create child dir");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.input.wheel_profile = WheelProfile::HighFrequency;
    app.select_index(0);
    app.input.last_wheel_target = Some(WheelTarget::Preview);

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Right,
        KeyModifiers::NONE,
    )))
    .expect("right arrow should be handled");
    wait_for_directory_load(&mut app);

    assert_eq!(app.navigation.cwd, child);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn rapid_audio_navigation_defers_second_cold_heavy_preview_refresh() {
    let root = temp_path("rapid-audio-navigation-preview-defer");
    fs::create_dir_all(&root).expect("failed to create temp root");
    for name in ["a.mp3", "b.mp3", "c.mp3"] {
        fs::write(root.join(name), name).expect("failed to write temp audio file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.set_media_ffprobe_available_for_tests(false);
    app.set_media_ffmpeg_available_for_tests(false);
    app.input.last_selection_change_at =
        Instant::now() - WHEEL_SCROLL_BURST_WINDOW - Duration::from_millis(1);

    let initial_token = app.preview.state.token;
    app.move_vertical(1);

    // Cold heavy audio is always deferred regardless of burst window state.
    assert_eq!(app.navigation.selected, 1);
    assert_eq!(app.preview.state.token, initial_token);
    assert!(app.preview.state.deferred_refresh_at.is_some());

    app.move_vertical(1);

    assert_eq!(app.navigation.selected, 2);
    assert_eq!(app.preview.state.token, initial_token);
    assert!(app.preview.state.deferred_refresh_at.is_some());

    thread::sleep(HIGH_FREQUENCY_PREVIEW_REFRESH_DELAY + Duration::from_millis(20));
    assert!(app.process_preview_refresh_timers());
    assert!(app.preview.state.token > initial_token);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn rapid_key_navigation_defers_preview_for_non_heavy_files() {
    let root = temp_path("rapid-key-nav-preview-defer");
    fs::create_dir_all(&root).expect("failed to create temp root");
    for name in ["a.txt", "b.txt", "c.txt"] {
        fs::write(root.join(name), name).expect("failed to write temp file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.select_index(0);

    // First move: last_key_nav_at is in the past → Immediate preview.
    let token_before = app.preview.state.token;
    app.move_vertical_keyboard(1);
    assert_eq!(app.navigation.selected, 1);
    assert!(
        app.preview.state.token > token_before,
        "first move should trigger an immediate preview refresh"
    );
    assert!(
        app.preview.state.deferred_refresh_at.is_none(),
        "first move should not leave a deferred timer"
    );

    // Second move within KEY_NAV_RAPID_THRESHOLD → Deferred preview.
    let token_before = app.preview.state.token;
    app.move_vertical_keyboard(1);
    assert_eq!(app.navigation.selected, 2);
    assert_eq!(
        app.preview.state.token, token_before,
        "second rapid move should not immediately refresh preview"
    );
    assert!(
        app.preview.state.deferred_refresh_at.is_some(),
        "second rapid move should schedule a deferred refresh"
    );

    // After the deferred delay the preview fires.
    thread::sleep(HIGH_FREQUENCY_PREVIEW_REFRESH_DELAY + Duration::from_millis(20));
    assert!(app.process_preview_refresh_timers());
    assert!(
        app.preview.state.token > token_before,
        "deferred preview should fire after pause"
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn rapid_key_navigation_clears_directory_totals_until_deferred_refresh_runs() {
    let root = temp_path("rapid-key-nav-directory-stats");
    for dir in ["a-dir", "b-dir"] {
        let path = root.join(dir);
        fs::create_dir_all(&path).expect("failed to create temp dir");
        fs::write(path.join("file.txt"), vec![b'x'; 100]).expect("failed to write temp file");
    }

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.select_index(0);
    wait_for_background_preview(&mut app);
    for _ in 0..100 {
        let _ = app.process_directory_stats_timer();
        let _ = app.process_background_jobs();
        if matches!(
            app.preview.state.directory_stats,
            Some(PreviewDirectoryStatsState::Complete { .. })
        ) {
            break;
        }
        thread::sleep(Duration::from_millis(10));
    }
    assert!(matches!(
        app.preview.state.directory_stats,
        Some(PreviewDirectoryStatsState::Complete { .. })
    ));

    app.input.last_key_nav_at = Instant::now();
    let token_before = app.preview.state.token;
    app.move_vertical_keyboard(1);

    assert_eq!(app.navigation.selected, 1);
    assert_eq!(app.preview.state.token, token_before);
    assert!(app.preview.state.deferred_refresh_at.is_some());
    assert!(app.preview.state.directory_stats.is_none());

    thread::sleep(HIGH_FREQUENCY_PREVIEW_REFRESH_DELAY + Duration::from_millis(20));
    assert!(app.process_preview_refresh_timers());
    for _ in 0..100 {
        let _ = app.process_directory_stats_timer();
        let _ = app.process_background_jobs();
        if app.preview_header_detail_for_width(8, 80).as_deref()
            == Some(&format!("1 item • {}", crate::app::format_size(100)))
        {
            break;
        }
        thread::sleep(Duration::from_millis(10));
    }
    assert_eq!(
        app.preview_header_detail_for_width(8, 80).as_deref(),
        Some(format!("1 item • {}", crate::app::format_size(100)).as_str())
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn high_frequency_alt_right_does_not_trigger_history_navigation() {
    let root = temp_path("high-frequency-alt-right-no-history");
    let child = root.join("child");
    fs::create_dir_all(&child).expect("failed to create child dir");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.navigation.view_mode = ViewMode::List;
    app.input.wheel_profile = WheelProfile::HighFrequency;
    app.select_index(0);
    app.open_selected()
        .expect("opening selected directory should succeed");
    wait_for_directory_load(&mut app);
    app.go_back().expect("go back should succeed");
    wait_for_directory_load(&mut app);

    app.handle_event(Event::Key(KeyEvent::new(KeyCode::Right, KeyModifiers::ALT)))
        .expect("alt-right should be handled");

    assert_eq!(app.navigation.cwd, root);
    assert_eq!(
        app.selected_entry().map(|entry| entry.path.as_path()),
        Some(child.as_path())
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

// --- keybindings integration tests ---

/// Parses a [keys] override, derives an action via KeyBindings::action_for,
/// dispatches it through App::dispatch_action, and checks the resulting state.
/// This covers the full config-to-runtime pipeline:
///   TOML string → KeyBindings → action_for → dispatch_action → app state
///
/// Note: App::handle_event reads from the process-wide config singleton, so we
/// cannot inject per-test overrides there.  The dispatch_action path (which
/// handle_event delegates to for every configurable key) is tested here
/// directly with a parsed KeyBindings, giving equivalent coverage.
#[test]
fn rebound_yank_key_dispatches_yank_action() {
    use crate::config::KeyBindings;

    // Parse a config that rebinds yank from "y" to "Y".
    let kb = KeyBindings::from_toml_str("[keys]\nyank = \"Y\"");
    assert_eq!(
        kb.action_for('Y'),
        Some(Action::Yank),
        "new key should map to Yank"
    );
    assert_eq!(
        kb.action_for('y'),
        None,
        "old key should no longer map to Yank"
    );

    // Create an app with a file so yank has something to act on.
    let root = temp_path("rebind-yank-e2e");
    fs::write(root.join("file.txt"), "hello").expect("failed to write file");
    let mut app = App::new_at(root.clone()).expect("failed to create app");
    wait_for_directory_load(&mut app);
    app.select_index(0);

    assert!(app.jobs.clipboard.is_none(), "clipboard should start empty");

    // Dispatch the action the rebound key would trigger.
    let action = kb.action_for('Y').expect("Y should be bound");
    app.dispatch_action(action)
        .expect("dispatch should succeed");

    assert!(
        app.jobs.clipboard.is_some(),
        "yank should have populated the clipboard"
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn rebound_quit_key_sets_should_quit() {
    use crate::config::KeyBindings;

    let kb = KeyBindings::from_toml_str("[keys]\nquit = \"Q\"");
    assert_eq!(kb.action_for('Q'), Some(Action::Quit));
    assert_eq!(kb.action_for('q'), None);

    let root = temp_path("rebind-quit-e2e");
    let mut app = App::new_at(root.clone()).expect("failed to create app");
    assert!(!app.should_quit);

    app.dispatch_action(kb.action_for('Q').unwrap())
        .expect("dispatch should succeed");
    assert!(app.should_quit);

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn capital_o_opens_open_with_overlay_for_selected_file() {
    let root = temp_path("open-with-overlay-file");
    fs::write(root.join("document.txt"), "hello").expect("failed to write temp file");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    wait_for_directory_load(&mut app);
    app.select_index(0);

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('O'))))
        .expect("O should be handled");

    let overlay_opened = app.overlays.open_with.is_some();

    // The file was either opened directly (single handler, status cleared),
    // the overlay was shown (multiple handlers), or no handlers were found
    // and the system default was used.  All three are valid outcomes.
    let no_apps = app.status == "No apps found, opened with default";
    assert!(
        overlay_opened || no_apps || app.status.is_empty(),
        "O on a file should open overlay, report no apps, or auto-launch; got status: {:?}",
        app.status
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn capital_o_on_directory_sets_status_without_opening_overlay() {
    let root = temp_path("open-with-overlay-dir");
    let child = root.join("subdir");
    fs::create_dir_all(&child).expect("failed to create child dir");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    wait_for_directory_load(&mut app);
    app.select_index(0);

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('O'))))
        .expect("O on a directory should not fail");

    assert!(
        app.overlays.open_with.is_none(),
        "overlay should not open for a directory"
    );
    assert_eq!(app.status, "Open With is for files");

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn esc_closes_open_with_overlay() {
    let root = temp_path("open-with-overlay-esc");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.inject_open_with_for_test("Fake App", "/usr/bin/true", vec![], false);

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Esc)))
        .expect("Esc should close the overlay");
    assert!(app.overlays.open_with.is_none());

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn open_with_shortcut_confirms_row_and_closes_overlay() {
    let root = temp_path("open-with-overlay-confirm");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.inject_open_with_for_test("Fake App", "/usr/bin/true", vec![], false);

    let shortcut = app
        .open_with_row_shortcut(0)
        .expect("first row should have a shortcut");

    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char(shortcut))))
        .expect("shortcut should confirm the row");

    assert!(
        app.overlays.open_with.is_none(),
        "overlay should close after confirming"
    );
    // Confirming a row launches the app — status is cleared on success or shows
    // a failure message if the process could not be spawned.
    assert!(
        app.status.is_empty() || app.status.starts_with("Failed to open with"),
        "unexpected status after confirm: {:?}",
        app.status
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn ctrl_c_closes_open_with_overlay() {
    let root = temp_path("open-with-overlay-ctrl-c");

    let mut app = App::new_at(root.clone()).expect("failed to create app");
    app.inject_open_with_for_test("Fake App", "/usr/bin/true", vec![], false);

    app.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('c'),
        KeyModifiers::CONTROL,
    )))
    .expect("Ctrl-C should close the overlay");
    assert!(app.overlays.open_with.is_none());

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

// ── open-with launch behavior ─────────────────────────────────────────────────

/// Creates a shell script in `dir` that touches `sentinel` when run,
/// makes it executable, and returns its path.
#[cfg(unix)]
fn write_sentinel_script(dir: &std::path::Path, sentinel: &std::path::Path) -> std::path::PathBuf {
    use std::os::unix::fs::PermissionsExt;
    let script = dir.join("fake-app.sh");
    fs::write(
        &script,
        format!("#!/bin/sh\ntouch '{}'\n", sentinel.display()),
    )
    .expect("write sentinel script");
    fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).expect("chmod sentinel script");
    script
}

#[cfg(unix)]
#[test]
fn detached_open_command_executes_program() {
    let dir = temp_path("detached-open-cmd");
    let sentinel = dir.join("ran");
    let script = write_sentinel_script(&dir, &sentinel);

    crate::fs::detached_open_command(script.to_str().unwrap(), &[])
        .expect("detached_open_command should succeed");

    let deadline = std::time::Instant::now() + std::time::Duration::from_millis(1000);
    while !sentinel.exists() && std::time::Instant::now() < deadline {
        thread::sleep(std::time::Duration::from_millis(10));
    }

    let ran = sentinel.exists(); // capture before cleanup
    fs::remove_dir_all(&dir).ok();
    assert!(ran, "script must have run");
}

#[cfg(unix)]
#[test]
fn confirm_open_with_launches_program_and_closes_overlay() {
    let dir = temp_path("open-with-launch");
    let sentinel = dir.join("launched");
    let script = write_sentinel_script(&dir, &sentinel);

    let root = temp_path("open-with-launch-root");
    fs::write(root.join("file.txt"), "hello").expect("write file");
    let mut app = App::new_at(root.clone()).expect("create app");
    wait_for_directory_load(&mut app);

    app.inject_open_with_for_test("Fake App", script.to_str().unwrap(), vec![], false);
    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('1'))))
        .expect("shortcut should fire");

    assert!(
        app.overlays.open_with.is_none(),
        "overlay must close after launch"
    );
    assert!(
        app.status.is_empty(),
        "status should be empty after successful launch; got: {:?}",
        app.status
    );

    let deadline = std::time::Instant::now() + std::time::Duration::from_millis(1000);
    while !sentinel.exists() && std::time::Instant::now() < deadline {
        thread::sleep(std::time::Duration::from_millis(10));
    }

    let launched = sentinel.exists(); // capture before cleanup
    fs::remove_dir_all(&dir).ok();
    fs::remove_dir_all(&root).ok();
    assert!(launched, "fake app must have been executed");
}

#[test]
fn confirm_open_with_launch_failure_sets_status() {
    let root = temp_path("open-with-fail");
    fs::write(root.join("file.txt"), "hello").expect("write file");
    let mut app = App::new_at(root.clone()).expect("create app");
    wait_for_directory_load(&mut app);

    // Point at a program that does not exist — spawn will fail.
    app.inject_open_with_for_test(
        "Ghost App",
        "/this/program/absolutely/does/not/exist",
        vec![],
        false,
    );
    app.handle_event(Event::Key(KeyEvent::from(KeyCode::Char('1'))))
        .expect("shortcut should fire");

    assert!(
        app.overlays.open_with.is_none(),
        "overlay must close even on failure"
    );
    assert_eq!(app.status, "Failed to open with Ghost App");

    fs::remove_dir_all(&root).ok();
}