gravityfile-tui 0.4.1

Terminal user interface for gravityfile
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
//! End-to-end journeys: key presses and clicks on a real scanned directory,
//! asserting on what a person would see and on the filesystem.

use super::harness::Harness;
use super::state::{LayoutMode, SortMode};
use super::{AppMode, UserSettings};

// --- Deleting -----------------------------------------------------------

#[tokio::test]
async fn deleting_a_selected_file_removes_it_from_disk_and_view() {
    let mut harness = Harness::scanned().await;
    harness.select("copy-b.bin").await;
    harness.press("d").await;
    assert_eq!(harness.app.mode, AppMode::ConfirmDelete);
    harness.press("y").await;
    assert!(!harness.path("copy-b.bin").exists());
    assert!(!harness.tree_shows("copy-b.bin"), "{}", harness.screen());
    assert!(harness.screen().contains("copy-a.bin"));
}

#[tokio::test]
async fn rebound_keys_take_effect_and_the_delete_dialog_keeps_its_own() {
    let mut harness = Harness::scanned().await;
    let file: crate::keymap::KeymapFile = toml::from_str(
        r#"
        [[keymap]]
        on = "<C-n>"
        run = "move_down"

        [[keymap]]
        on = "y"
        run = "none"

        [[keymap]]
        on = "X"
        run = "delete"
        "#,
    )
    .unwrap();
    assert!(harness.app.keymap.apply(&file).is_empty());
    harness.select("copy-a.bin").await;
    harness.press("<c-n>").await;
    assert_eq!(harness.selected(), Some(harness.path("copy-b.bin")));
    // With y unbound, copying has no key, so the footer stops offering it.
    let screen = harness.screen();
    assert!(
        !screen.contains("Copy") && screen.contains("Cut"),
        "{screen}"
    );
    // y is unbound in the explorer but still confirms a delete.
    harness.press("Xy").await;
    assert!(!harness.path("copy-b.bin").exists());
}

#[tokio::test]
async fn the_preview_scrolls_with_keys_and_the_wheel() {
    let mut harness = Harness::scanned_sized(120, 30).await;
    let text: String = (1..=200).map(|n| format!("line {n:03}\n")).collect();
    std::fs::write(harness.path("long.txt"), text).unwrap();
    harness.press("R").await;
    harness.press("v").await;
    harness.select("long.txt").await;
    assert!(
        harness.screen().contains("line 001"),
        "{}",
        harness.screen()
    );

    harness.press("JJ").await;
    let screen = harness.screen();
    assert!(
        !screen.contains("line 010") && screen.contains("line 011"),
        "{screen}"
    );
    harness.press("K").await;
    assert!(
        harness.screen().contains("line 006"),
        "{}",
        harness.screen()
    );

    // The wheel over the preview scrolls it rather than the selection.
    harness.scroll_down(110, 15).await;
    assert!(
        harness.screen().contains("line 011"),
        "{}",
        harness.screen()
    );
    assert_eq!(harness.selected(), Some(harness.path("long.txt")));

    // Scrolling stops at the end, and a new selection starts at the top.
    harness
        .press("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ")
        .await;
    assert!(
        harness.screen().contains("line 200"),
        "{}",
        harness.screen()
    );
    harness.press("kj").await;
    assert!(
        harness.screen().contains("line 001"),
        "{}",
        harness.screen()
    );
}

#[tokio::test]
async fn a_snapshot_is_browsed_read_only_without_touching_the_filesystem() {
    let mut harness = Harness::snapshot().await;
    let screen = harness.screen();
    assert!(screen.contains("snapshot home.json, read-only"), "{screen}");
    assert!(harness.tree_shows("copy-a.bin"), "{screen}");

    // Browsing works from the record, though none of these paths exist.
    harness.select("src").await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
    assert!(harness.tree_shows("modified.rs"), "{}", harness.screen());
    harness.press("<bs><bs>").await;
    assert_eq!(harness.app.nav.view_root, harness.root(), "stays inside");
    assert!(harness.app.error.is_none(), "{:?}", harness.app.error);
    harness.press("b").await;
    assert!(harness.screen().contains(" apparent in "));

    // Nothing that would change files or rescan runs.
    let refused = |harness: &Harness| {
        harness
            .app
            .operation_message
            .as_ref()
            .is_some_and(|(ok, message)| !ok && message.contains("snapshot"))
    };
    harness.select("copy-a.bin").await;
    for keys in ["d", "r", "R", "a", "<c-z>"] {
        harness.app.operation_message = None;
        harness.press(keys).await;
        assert_eq!(harness.app.mode, AppMode::Normal, "{keys}");
        assert!(refused(&harness), "{keys}");
    }
    harness.app.operation_message = None;
    harness.press(":").await;
    harness.type_text("mkdir new").await;
    harness.press("<enter>").await;
    assert!(refused(&harness));

    // Duplicates need file contents; the view says so instead of searching.
    harness.press("<tab>").await;
    assert_eq!(harness.app.view, super::View::Duplicates);
    assert!(
        harness.screen().contains("compares file contents"),
        "{}",
        harness.screen()
    );
}

#[tokio::test]
async fn page_down_moves_by_a_screenful() {
    let mut harness = Harness::scanned_sized(100, 20).await;
    for n in 0..60 {
        std::fs::write(harness.path(&format!("file-{n:02}")), b"x").unwrap();
    }
    harness.press("R").await;
    let page = harness.app.viewport_height - 1;
    assert!(
        page > 5,
        "a 20-row terminal shows more than a handful of rows"
    );
    harness.press("<pgdn>").await;
    assert_eq!(harness.app.nav.tree_state.selected, page);
    harness.press("<pgdn><pgup>").await;
    assert_eq!(harness.app.nav.tree_state.selected, page);
}

#[tokio::test]
async fn the_whole_help_can_be_scrolled_into_view() {
    let mut harness = Harness::scanned_sized(70, 24).await;
    harness.press("?").await;
    assert!(
        harness.screen().contains("Move down"),
        "{}",
        harness.screen()
    );
    let (last_command, _) = *crate::event::get_command_help().last().unwrap();
    assert!(!harness.screen().contains(last_command));
    harness.press("G").await;
    assert!(
        harness.screen().contains(last_command),
        "{}",
        harness.screen()
    );
    harness.press("<esc>").await;
    assert_eq!(harness.app.mode, AppMode::Normal);
}

#[tokio::test]
async fn delete_never_targets_the_directory_being_viewed() {
    let mut harness = Harness::scanned().await;
    harness.press("g").await;
    assert_eq!(harness.selected().as_deref(), Some(harness.root()));
    harness.press("dy").await;
    assert!(harness.root().exists());
    assert!(harness.path("copy-a.bin").exists());
}

#[tokio::test]
async fn declining_a_delete_keeps_the_marks_you_made() {
    let mut harness = Harness::scanned().await;
    harness.select("copy-a.bin").await;
    harness.press("<space>").await;
    harness.select("empty").await;
    harness.press("dn").await;
    assert!(harness.app.marked.contains(&harness.path("copy-a.bin")));
    assert!(!harness.app.marked.contains(&harness.path("empty")));
    assert!(harness.path("copy-a.bin").exists());
}

#[tokio::test]
async fn deleting_inside_a_subdirectory_keeps_the_whole_scan() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    harness.select("src").await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
    harness.select("modified.rs").await;
    harness.press("dy").await;
    assert!(!harness.path("src/modified.rs").exists());

    // The scan still covers the root it started from, with real sizes.
    assert_eq!(harness.app.path, root);
    let tree = harness.app.tree.as_ref().expect("a tree");
    assert_eq!(tree.root_path, root);
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, root);
    let screen = harness.screen();
    let copy = screen
        .lines()
        .find(|line| line.contains("copy-a.bin"))
        .unwrap();
    assert!(
        copy.contains("kB"),
        "sizes are known after going back:\n{screen}"
    );
}

// --- Searching and sorting ----------------------------------------------

#[tokio::test]
async fn search_uses_smart_case() {
    let mut harness = Harness::scanned().await;
    std::fs::write(harness.path("README.md"), "read me").unwrap();
    harness.press("R").await;
    let mut search = async |query: &str| {
        harness.press("/").await;
        assert_eq!(harness.app.mode, AppMode::Search);
        harness.type_text(query).await;
        let names: Vec<String> = (harness.app.search_state.results.iter())
            .map(|result| result.display.clone())
            .collect();
        harness.press("<esc>").await;
        names
    };
    // Lowercase ignores case; a capital makes the query exact.
    assert!(
        search("readme")
            .await
            .iter()
            .any(|name| name == "README.md")
    );
    assert!(
        search("README")
            .await
            .iter()
            .any(|name| name == "README.md")
    );
    assert!(search("Readme").await.is_empty());
    assert!(search("COPY").await.is_empty());
}

#[tokio::test]
async fn search_facets_narrow_by_size_type_and_extension() {
    let mut harness = Harness::scanned().await;
    let mut search = async |query: &str| {
        harness.press("/").await;
        harness.type_text(query).await;
        let names: Vec<String> = (harness.app.search_state.results.iter())
            .map(|result| result.display.clone())
            .collect();
        harness.press("<esc>").await;
        names
    };
    // Facets alone list matches largest first; directories are excluded by type.
    let large = search("size>=8kb type:f").await;
    assert!(
        large.len() == 2 && large.iter().all(|name| name.starts_with("copy-")),
        "{large:?}"
    );
    // Facets combine with search text.
    assert_eq!(search("copy ext:bin size>1mb").await, Vec::<String>::new());
    let modified = format!("src{}modified.rs", std::path::MAIN_SEPARATOR);
    assert_eq!(search("ext:rs").await, [modified]);
    // A malformed facet is just text, which here matches nothing.
    assert!(search("size>lots").await.is_empty());
}

#[tokio::test]
async fn sort_order_survives_a_rescan() {
    let mut harness = Harness::scanned().await;
    while harness.app.sort_mode != SortMode::NameAscending {
        harness.press("s").await;
    }
    let order = |screen: &str| {
        ["a-very-long", "copy-a.bin", "empty", "src"].map(|name| {
            screen
                .find(name)
                .unwrap_or_else(|| panic!("{name}:\n{screen}"))
        })
    };
    let before = order(&harness.screen());
    assert!(before.is_sorted(), "{}", harness.screen());
    harness.press("R").await;
    assert!(order(&harness.screen()).is_sorted(), "{}", harness.screen());
}

// --- Navigating ---------------------------------------------------------

#[tokio::test]
async fn going_back_selects_the_directory_left_after_a_resort_and_layout_switch() {
    let mut harness = Harness::scanned().await;
    harness.select("src").await;
    harness.press("<enter>").await;
    // Inside src, re-sort by name and switch to Miller columns.
    while harness.app.sort_mode != SortMode::NameAscending {
        harness.press("s").await;
    }
    harness.press("v").await;
    assert_eq!(harness.app.layout_mode, LayoutMode::Miller);
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, harness.root());
    assert_eq!(harness.selected(), Some(harness.path("src")));
    // Entering again and coming back the other way round works too.
    harness.press("<enter>v<bs>").await;
    assert_eq!(harness.app.layout_mode, LayoutMode::Tree);
    assert_eq!(harness.selected(), Some(harness.path("src")));
}

#[tokio::test]
async fn backspace_goes_back_in_the_treemap() {
    let mut harness = Harness::scanned().await;
    harness.select("src").await;
    harness.press("<enter>").await;
    while harness.app.view != super::View::Treemap {
        harness.press("<tab>").await;
    }
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, harness.root());
}

#[tokio::test]
async fn going_to_an_ancestor_of_the_scan_root_lists_it() {
    // The fixture is a hidden `.tmp…` directory.
    let settings = UserSettings {
        show_hidden: true,
        scan_on_startup: false,
        ..UserSettings::default()
    };
    let mut harness = Harness::scanned_with(100, 30, settings).await;
    let parent = harness.root().parent().unwrap().to_path_buf();
    let name = harness
        .root()
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    // The prompt starts with the current path; Ctrl-U clears it.
    harness.press("<c-g><c-u>").await;
    harness.type_text(&parent.to_string_lossy()).await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.nav.view_root, parent);
    // The parent is shared with other tests, so the row may be off-screen.
    let tree = harness.app.tree.as_ref().expect("a listing");
    assert_eq!(tree.root_path, parent);
    assert!(tree.root.children.iter().any(|child| child.name == name));
    // Going back returns to the scanned tree's directory.
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, harness.root());
}

#[tokio::test]
async fn each_tab_keeps_its_own_place_selection_and_history() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    // Tab 1: into src, selecting modified.rs.
    harness.select("src").await;
    harness.press("<enter>").await;
    harness.select("modified.rs").await;
    // Tab 2 opens on src too, then goes into nested.
    harness.press("<c-t>").await;
    harness.select("nested").await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src/nested"));

    // Back on tab 1, the selection survived and Backspace follows tab 1's
    // own history, not tab 2's.
    harness.press("1").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
    assert_eq!(harness.selected(), Some(harness.path("src/modified.rs")));
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, root);

    // Tab 2 is still where it was left.
    harness.press("2").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src/nested"));
    harness.press("<bs>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
}

#[tokio::test]
async fn a_tab_whose_directory_was_deleted_elsewhere_moves_to_what_remains() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    harness.press("<c-t>").await;
    harness.select("src").await;
    harness.press("<enter>").await;
    harness.select("nested").await;
    harness.press("<enter>").await;
    // Tab 1 deletes src, and tab 2's directory with it.
    harness.press("1").await;
    harness.select("src").await;
    harness.press("dy").await;
    assert!(!harness.path("src").exists());
    harness.press("2").await;
    assert_eq!(harness.app.nav.view_root, root);
    assert!(harness.tree_shows("copy-a.bin"), "{}", harness.screen());
}

#[tokio::test]
async fn closing_a_tab_returns_to_the_other_tabs_directory() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    harness.press("<c-t>").await;
    assert_eq!(harness.app.tab_manager.len(), 2);
    harness.select("src").await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
    harness.press("<c-w>").await;
    assert_eq!(harness.app.tab_manager.len(), 1);
    assert_eq!(harness.app.nav.view_root, root);
    assert!(
        harness.screen().contains("copy-a.bin"),
        "{}",
        harness.screen()
    );
}

#[tokio::test]
async fn the_saved_default_layout_is_used_at_startup() {
    let settings = UserSettings {
        default_layout: LayoutMode::Miller,
        ..UserSettings::default()
    };
    let harness = Harness::scanned_with(100, 30, settings).await;
    assert_eq!(harness.app.layout_mode, LayoutMode::Miller);
}

// --- Drawing and the mouse ----------------------------------------------

#[tokio::test]
async fn sizes_line_up_at_every_depth() {
    let harness = Harness::scanned().await;
    let screen = harness.screen();
    let column = |name: &str| {
        let line = screen.lines().find(|line| line.contains(name)).unwrap();
        line.chars().position(|c| c == 'k').expect("a size in kB")
    };
    assert_eq!(column("copy-a.bin"), column(" src"), "\n{screen}");
}

#[tokio::test]
async fn double_clicking_a_directory_enters_it() {
    let mut harness = Harness::scanned().await;
    let row = harness.row_of(" src");
    harness.click(10, row).await;
    harness.click(10, row).await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
}

#[tokio::test]
async fn clicks_after_scrolling_select_the_row_under_the_pointer() {
    // Five rows of entries fit; the sixth needs scrolling.
    let mut harness = Harness::scanned_sized(100, 10).await;
    harness.select("empty").await;
    let row = harness.row_of("copy-b.bin");
    harness.click(10, row).await;
    assert_eq!(harness.selected(), Some(harness.path("copy-b.bin")));
}

#[tokio::test]
async fn right_clicking_marks_the_row_under_the_pointer_after_scrolling() {
    let mut harness = Harness::scanned_sized(100, 10).await;
    let row = harness.row_of("copy-a.bin");
    // Scrolling moves the selection three rows and the view with it.
    harness.scroll_down(10, row).await;
    harness.scroll_down(10, row).await;
    let row = harness.row_of("copy-b.bin");
    harness.right_click(10, row).await;
    assert!(harness.app.marked.contains(&harness.path("copy-b.bin")));
    assert_eq!(harness.app.marked.len(), 1);
}

// --- Changing files keeps the rest of the scan --------------------------

/// The size shown for `name` in the tree, as opposed to the details pane.
fn size_on_screen(harness: &Harness, name: &str) -> String {
    let screen = harness.screen();
    let after = screen
        .lines()
        .filter_map(|line| {
            let (before, after) = line.split_once(name)?;
            (before.contains("├─") || before.contains("└─")).then_some(after)
        })
        .next()
        .unwrap_or_else(|| panic!("{name} is not a tree row:\n{screen}"));
    after
        .split_whitespace()
        .take(2)
        .collect::<Vec<_>>()
        .join(" ")
}

#[tokio::test]
async fn b_switches_between_disk_usage_and_apparent_sizes_without_rescanning() {
    let mut harness = Harness::scanned().await;
    std::fs::write(harness.path("small.txt"), b"0123456789").unwrap();
    harness.press("R").await;
    harness.select("small.txt").await;
    let on_disk = size_on_screen(&harness, "small.txt");
    assert_ne!(on_disk, "10 B", "a 10-byte file occupies a whole block");

    harness.press("b").await;
    assert_eq!(size_on_screen(&harness, "small.txt"), "10 B");
    assert!(
        harness.screen().contains(" apparent in "),
        "{}",
        harness.screen()
    );
    assert_eq!(harness.selected(), Some(harness.path("small.txt")));
    // A rescan keeps the chosen measure.
    harness.press("R").await;
    assert_eq!(size_on_screen(&harness, "small.txt"), "10 B");

    harness.press("b").await;
    assert_eq!(size_on_screen(&harness, "small.txt"), on_disk);
}

#[tokio::test]
async fn pasting_into_a_subdirectory_updates_sizes_in_place() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    let before = harness.app.tree.as_ref().unwrap().root.size;
    harness.select("copy-a.bin").await;
    harness.press("y").await;
    harness.select("src").await;
    harness.press("<enter>p").await;
    assert!(harness.path("src/copy-a.bin").is_file());
    assert_eq!(harness.app.path, root, "the scan root is unchanged");
    assert!(
        harness.screen().contains("copy-a.bin"),
        "{}",
        harness.screen()
    );
    let tree = harness.app.tree.as_ref().unwrap();
    let copied = tree.find(&harness.path("src/copy-a.bin")).unwrap().size;
    assert!(copied > 0);
    assert_eq!(tree.root.size, before + copied);
    assert_eq!(
        tree.find(&harness.path("src")).unwrap().file_count(),
        3,
        "nested/deep.txt, modified.rs and the copy"
    );
    // Nothing else was rescanned away: the sibling still has its size.
    harness.press("<bs>").await;
    assert_eq!(size_on_screen(&harness, "copy-b.bin"), "8.19 kB");
}

#[tokio::test]
async fn renaming_updates_the_entry_in_place() {
    let mut harness = Harness::scanned().await;
    harness.select("copy-b.bin").await;
    harness.press("r<c-u>").await;
    harness.type_text("renamed.bin").await;
    harness.press("<enter>").await;
    assert!(harness.path("renamed.bin").is_file());
    let screen = harness.screen();
    assert!(
        screen.contains("renamed.bin") && !screen.contains("copy-b.bin"),
        "{screen}"
    );
    assert_eq!(size_on_screen(&harness, "renamed.bin"), "8.19 kB");
}

#[tokio::test]
async fn creating_a_file_adds_it_in_place() {
    let mut harness = Harness::scanned().await;
    let files = harness.app.tree.as_ref().unwrap().root.file_count();
    harness.press("a").await;
    harness.type_text("notes.txt").await;
    harness.press("<enter>").await;
    assert!(harness.path("notes.txt").is_file());
    assert!(
        harness.screen().contains("notes.txt"),
        "{}",
        harness.screen()
    );
    assert_eq!(
        harness.app.tree.as_ref().unwrap().root.file_count(),
        files + 1
    );
}

#[tokio::test]
async fn an_invalid_name_keeps_the_prompt_open_with_the_reason() {
    let mut harness = Harness::scanned().await;
    harness.press("a").await;
    harness.type_text("a/b").await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.mode, AppMode::CreatingFile);
    assert!(
        harness.screen().contains("Name cannot contain '/'"),
        "{}",
        harness.screen()
    );
    // Fixing the name in the same prompt creates the file. A backslash is an
    // ordinary character on Unix and a separator on Windows.
    let fixed = if cfg!(windows) {
        "fixed"
    } else {
        "back\\slash"
    };
    harness.press("<bs><bs><bs>").await;
    harness.type_text(fixed).await;
    harness.press("<enter>").await;
    assert_eq!(harness.app.mode, AppMode::Normal);
    assert!(harness.path(fixed).is_file());
}

#[tokio::test]
async fn unread_directories_are_listed_in_the_background() {
    let mut harness = Harness::scanned().await;
    // A quick listing, as before a scan finishes: subdirectories are unread.
    let quick = gravityfile_scan::quick_list(harness.root(), None).unwrap();
    harness.app.install_tree(quick);
    harness.app.has_full_scan = false;
    harness.app.view_changed();
    harness.press("v").await;
    harness.select("src").await;
    harness.press_unsettled("<enter>").await;
    assert_eq!(harness.app.nav.view_root, harness.path("src"));
    assert!(
        !harness.app.listings.is_empty(),
        "the listing runs off the UI thread"
    );
    assert!(
        harness.screen().contains("(loading…)"),
        "{}",
        harness.screen()
    );
    harness.settle().await;
    assert!(harness.app.listings.is_empty());
    assert!(
        harness.screen().contains("modified.rs"),
        "{}",
        harness.screen()
    );
}

#[tokio::test]
async fn full_scans_are_recorded_and_the_header_shows_growth_since_the_last() {
    use gravityfile_core::history::{self, Record};
    let mut harness = Harness::scanned().await;
    let place = tempfile::tempdir().unwrap();
    let file = place.path().join("history.jsonl");
    let mut earlier = Record::of(harness.app.tree.as_ref().unwrap());
    earlier.at -= std::time::Duration::from_secs(86_400);
    earlier.size -= 1_000;
    history::append(&file, &earlier).unwrap();
    harness.app.history_file = Some(file.clone());

    harness.press("R").await;
    assert!(
        harness.screen().contains("+1.00 kB since"),
        "{}",
        harness.screen()
    );
    let records = history::read(&file, Some(harness.root())).unwrap();
    assert_eq!(records.len(), 2);
    assert_eq!(records[1].size, earlier.size + 1_000);
}

#[tokio::test]
async fn rescanning_a_subdirectory_keeps_the_rest_of_the_scan() {
    let mut harness = Harness::scanned().await;
    let root = harness.root().to_path_buf();
    harness.select("src").await;
    harness.press("<enter>").await;
    std::fs::write(harness.path("src/added.bin"), vec![0u8; 50_000]).unwrap();
    harness.press("R").await;
    assert_eq!(harness.app.path, root);
    assert!(
        harness.screen().contains("added.bin"),
        "{}",
        harness.screen()
    );
    harness.press("<bs>").await;
    assert_eq!(size_on_screen(&harness, "copy-a.bin"), "8.19 kB");
}

#[tokio::test]
async fn an_operation_during_a_scan_leaves_the_scan_running() {
    let mut harness = Harness::scanned().await;
    harness.select("copy-b.bin").await;
    harness.press_unsettled("R").await;
    assert!(harness.app.scan_rx.is_some(), "a full scan is running");
    let scan = harness.app.scan_token.clone();
    harness.press_unsettled("dy").await;
    assert!(harness.app.op_rx.is_some(), "the delete is running");
    assert!(
        !scan.is_cancelled(),
        "starting the delete must not cancel the scan"
    );
    harness.settle().await;
    assert!(!harness.path("copy-b.bin").exists());
    assert!(!harness.tree_shows("copy-b.bin"), "{}", harness.screen());
    assert!(harness.screen().contains("copy-a.bin"));
}