fm84 0.38.0

A synthwave dual-pane TUI file manager
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
//! Copying, moving, linking, comparing and changing attributes, from the keys.

use super::*;

/// Shift+F3 compares the file under the cursor with its namesake in the
/// other panel, or two selected in one; each side is the panel its file is
/// in. Files that are the same, or not text, are only spoken of.
#[test]
fn compare_files_from_the_keys() {
    let dir = std::env::temp_dir().join(format!("fm84-compare-files-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let (one, two) = (dir.join("one"), dir.join("two"));
    std::fs::create_dir_all(one.join("sub")).unwrap();
    std::fs::create_dir_all(&two).unwrap();
    std::fs::write(one.join("a.txt"), "same\nleft\nsame\n").unwrap();
    std::fs::write(one.join("b.txt"), "same\nb\nsame\n").unwrap();
    std::fs::write(two.join("a.txt"), "same\nright\nsame\nextra\n").unwrap();
    std::fs::write(two.join("copy.txt"), "same\nleft\nsame\n").unwrap();
    let mut app_state = AppState::new();
    app_state.open_dir(false, two.clone(), Some("copy.txt".as_ref()));
    app_state.is_left_active = true;
    app_state.open_dir(true, one.clone(), Some("a.txt".as_ref()));
    let compare = |app_state: &mut AppState| handle_key(app_state, KeyEvent::new(KeyCode::F(3), KeyModifiers::SHIFT));
    let sides = |app_state: &AppState| match &app_state.screen {
        Screen::Diff(state) => (state.left_path.clone(), state.right_path.clone()),
        _ => panic!("not comparing"),
    };

    // The namesake, not the other panel's cursor.
    compare(&mut app_state);
    assert_eq!(sides(&app_state), (one.join("a.txt"), two.join("a.txt")));
    let Screen::Diff(state) = &app_state.screen else { unreachable!() };
    assert_eq!(state.hunks.len(), 2);

    // Drawn side by side, with the marks between, and its own keys.
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
    crate::ui::render_ui(&mut terminal, &mut app_state);
    let screen: String = terminal.backend().buffer().content().iter().map(|cell| cell.symbol()).collect();
    for shown in [" | ", " > ", "left", "right", "extra", "Difference 1/2", "F3 Next"] {
        assert!(screen.contains(shown), "{shown} not drawn");
    }
    press(&mut app_state, KeyCode::F(3));
    let Screen::Diff(state) = &app_state.screen else { unreachable!() };
    assert_eq!(state.current, Some(1));
    press(&mut app_state, KeyCode::Esc);
    assert!(matches!(app_state.screen, Screen::Panels));

    // = does the same, for a terminal that cannot send Shift+F3.
    press(&mut app_state, KeyCode::Char('='));
    assert_eq!(sides(&app_state), (one.join("a.txt"), two.join("a.txt")));
    press(&mut app_state, KeyCode::Esc);

    // From the right panel, the left file is still on the left.
    app_state.is_left_active = false;
    app_state.open_dir(false, two.clone(), Some("a.txt".as_ref()));
    compare(&mut app_state);
    assert_eq!(sides(&app_state), (one.join("a.txt"), two.join("a.txt")));
    press(&mut app_state, KeyCode::Esc);

    // No namesake: the other panel's cursor, here a file the same.
    app_state.open_dir(false, two.clone(), Some("copy.txt".as_ref()));
    compare(&mut app_state);
    assert!(matches!(app_state.screen, Screen::Panels));
    assert_eq!(app_state.find_note.as_deref(), Some("The files are identical"));

    // Two selected in one panel.
    app_state.is_left_active = true;
    app_state.selected_left.insert("a.txt".into());
    app_state.selected_left.insert("b.txt".into());
    compare(&mut app_state);
    assert_eq!(sides(&app_state), (one.join("a.txt"), one.join("b.txt")));
    press(&mut app_state, KeyCode::Esc);
    app_state.selected_left.clear();

    // A directory is not a file to compare.
    app_state.open_dir(true, one.clone(), Some("sub".as_ref()));
    compare(&mut app_state);
    assert!(app_state.error.as_deref().is_some_and(|error| error.starts_with("Put the cursor on a file")));
    let _ = std::fs::remove_dir_all(&dir);
}

/// Ctrl+Shift+F5 links the entry under the cursor into the other panel,
/// under its own name or one typed; several selected get one each, and one
/// already there is named while the rest are still made.
#[cfg(unix)]
#[test]
fn symlinks_from_the_keys() {
    let dir = std::env::temp_dir().join(format!("fm84-symlink-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let (src, dst) = (dir.join("src"), dir.join("dst"));
    std::fs::create_dir_all(src.join("sub")).unwrap();
    std::fs::create_dir_all(&dst).unwrap();
    for name in ["a.txt", "b.txt"] {
        std::fs::write(src.join(name), name).unwrap();
    }
    let mut app_state = AppState::new();
    app_state.open_dir(false, dst.clone(), None);
    app_state.is_left_active = true;
    app_state.open_dir(true, src.clone(), Some("a.txt".as_ref()));
    let link = |app_state: &mut AppState| handle_key(app_state, KeyEvent::new(KeyCode::F(5), KeyModifiers::CONTROL | KeyModifiers::SHIFT));
    let typed = |app_state: &mut AppState| app_state.dialog_input().unwrap().text.clone();

    link(&mut app_state);
    assert_eq!(typed(&mut app_state), dst.join("a.txt").to_string_lossy());
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.dialog.is_none() && app_state.error.is_none());
    assert_eq!(std::fs::read_link(dst.join("a.txt")).unwrap(), src.join("a.txt"));
    let cursor = app_state.state_right.selected().map(|index| app_state.children_right[index].name_os.clone());
    assert_eq!(cursor.as_deref(), Some("a.txt".as_ref()), "the other panel lands on the new link");

    // Again: refused, nothing written over.
    link(&mut app_state);
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.as_deref().is_some_and(|error| error.starts_with("Cannot link") && error.contains("already exists")));
    app_state.error = None;

    // A bare name is made beside the original.
    link(&mut app_state);
    app_state.dialog_input().unwrap().set("alias".to_string());
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(std::fs::read_link(src.join("alias")).unwrap(), src.join("a.txt"));

    // A lone directory is named, not gone into.
    app_state.open_dir(true, src.clone(), Some("sub".as_ref()));
    link(&mut app_state);
    assert_eq!(typed(&mut app_state), dst.join("sub").to_string_lossy());
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(std::fs::read_link(dst.join("sub")).unwrap(), src.join("sub"));

    // Several: into the directory, one already there passed over.
    app_state.selected_left.insert("a.txt".into());
    app_state.selected_left.insert("b.txt".into());
    link(&mut app_state);
    assert!(typed(&mut app_state).ends_with(std::path::MAIN_SEPARATOR));
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.as_deref().is_some_and(|error| error.starts_with("Could not link 1 of 2")));
    assert_eq!(std::fs::read_link(dst.join("b.txt")).unwrap(), src.join("b.txt"));

    // Esc leaves it be.
    app_state.error = None;
    app_state.selected_left.clear();
    app_state.open_dir(true, src.clone(), Some("b.txt".as_ref()));
    link(&mut app_state);
    app_state.dialog_input().unwrap().set("never".to_string());
    press(&mut app_state, KeyCode::Esc);
    assert!(app_state.dialog.is_none() && !crate::fs_ops::path_exists(&src.join("never")));

    // Alt+F5 does the same.
    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(5), KeyModifiers::ALT));
    assert!(matches!(app_state.dialog, Some(Dialog::Symlink { .. })));
    press(&mut app_state, KeyCode::Esc);

    // Ctrl+F5 alone still sorts.
    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(5), KeyModifiers::CONTROL));
    assert!(app_state.dialog.is_none());
    let _ = std::fs::remove_dir_all(&dir);
}

/// Alt+A opens on what the entries have in common; what is typed over it
/// is applied to each, and what cannot be read leaves the form open.
#[cfg(unix)]
#[test]
fn attributes_change_from_the_keys() {
    use std::os::unix::fs::PermissionsExt;
    let dir = std::env::temp_dir().join(format!("fm84-alt-a-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    for (name, mode) in [("a.txt", 0o644), ("b.txt", 0o600)] {
        std::fs::write(dir.join(name), "").unwrap();
        std::fs::set_permissions(dir.join(name), std::fs::Permissions::from_mode(mode)).unwrap();
    }
    let mode = |name: &str| std::fs::metadata(dir.join(name)).unwrap().permissions().mode() & 0o7777;
    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(true, dir.clone(), Some("a.txt".as_ref()));
    let alt_a = |app_state: &mut AppState| handle_key(app_state, KeyEvent::new(KeyCode::Char('a'), KeyModifiers::ALT));
    let fields = |app_state: &AppState| match &app_state.dialog {
        Some(Dialog::Attributes(form)) => form.fields.iter().map(|field| field.input.text.clone()).collect::<Vec<_>>(),
        _ => panic!("not open"),
    };
    let retype = |app_state: &mut AppState, text: &str| {
        app_state.dialog_input().unwrap().set(text.to_string());
    };

    alt_a(&mut app_state);
    let opened = fields(&app_state);
    assert_eq!(opened[0], "644");
    assert_eq!(opened[1], crate::fs_ops::user_name(unsafe { libc::getuid() }));
    retype(&mut app_state, "u+x");
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.dialog.is_none() && app_state.error.is_none());
    assert_eq!(mode("a.txt"), 0o744);

    // Two that differ: the mode opens empty, and stays as it is.
    app_state.selected_left.insert("a.txt".into());
    app_state.selected_left.insert("b.txt".into());
    alt_a(&mut app_state);
    assert_eq!(fields(&app_state)[0], "");
    press(&mut app_state, KeyCode::BackTab);
    retype(&mut app_state, "2001-02-03 04:05:06");
    press(&mut app_state, KeyCode::Enter);
    let then = crate::attributes::parse_time("2001-02-03 04:05:06").unwrap();
    for name in ["a.txt", "b.txt"] {
        assert_eq!(std::fs::metadata(dir.join(name)).unwrap().modified().unwrap(), then);
    }
    assert_eq!((mode("a.txt"), mode("b.txt")), (0o744, 0o600));

    // A click on a field takes the keys there, the cursor where it
    // landed; one on the rest of the popup does nothing.
    alt_a(&mut app_state);
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
    crate::ui::render_ui(&mut terminal, &mut app_state);
    let click = |app_state: &mut AppState, column, row| {
        crate::input::mouse_to(
            app_state,
            crossterm::event::MouseEvent {
                kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
                column,
                row,
                modifiers: KeyModifiers::NONE,
            },
        );
    };
    let (group_row, text_x) = app_state.attribute_rows[2];
    click(&mut app_state, text_x + 1, group_row.y);
    let focus = |app_state: &AppState| match &app_state.dialog {
        Some(Dialog::Attributes(form)) => (form.focus, form.fields[form.focus].input.cursor),
        _ => panic!("not open"),
    };
    assert_eq!(focus(&app_state), (2, 1));
    let (modified_row, _) = app_state.attribute_rows[3];
    click(&mut app_state, modified_row.right() - 1, modified_row.y);
    assert_eq!(focus(&app_state), (3, "2001-02-03 04:05:06".len()));
    // Above the first field: the line naming what is changed, or a blank
    // row, whichever the popup's width leaves there.
    let (mode_row, _) = app_state.attribute_rows[0];
    click(&mut app_state, mode_row.x, mode_row.y - 1);
    assert_eq!(focus(&app_state).0, 3);
    press(&mut app_state, KeyCode::Esc);

    // Not a mode: said so, and the form is still there to fix.
    alt_a(&mut app_state);
    retype(&mut app_state, "u+q");
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.is_some() && matches!(app_state.dialog, Some(Dialog::Attributes(_))));
    press(&mut app_state, KeyCode::Esc);
    // Drawn, then put away.
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
    crate::ui::render_ui(&mut terminal, &mut app_state);
    press(&mut app_state, KeyCode::Esc);
    assert!(app_state.dialog.is_none());

    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn a_typed_path_starts_from_the_panel_or_from_home() {
    let base = std::path::Path::new("/panel/dir");
    let home = std::env::home_dir().unwrap_or_default();
    assert_eq!(crate::input::typed_path("/abs/x", base), std::path::Path::new("/abs/x"));
    assert_eq!(crate::input::typed_path("x", base), base.join("x"));
    assert_eq!(crate::input::typed_path("sub/x", base), base.join("sub/x"));
    assert_eq!(crate::input::typed_path("~", base), home);
    assert_eq!(crate::input::typed_path("~/x", base), home.join("x"));
    // Only a `~` that stands for home: one starting a name is the name.
    assert_eq!(crate::input::typed_path("~x", base), base.join("~x"));
}

/// F5 and F6 go where the field says. A single file opens on its path in
/// the other panel, anything else on the other panel's directory, and
/// either goes there when it is left as it opened. A name edited in
/// renames it on the way, and a bare one puts it beside the original. Y
/// and N are typed, not taken for an answer.
#[test]
fn copy_and_move_go_where_the_destination_says() {
    let dir = std::env::temp_dir().join(format!("fm84-dest-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let (left, right) = (dir.join("left"), dir.join("right"));
    std::fs::create_dir_all(&left).unwrap();
    std::fs::create_dir_all(&right).unwrap();
    std::fs::write(left.join("a.txt"), "a").unwrap();
    std::fs::write(left.join("z.txt"), "z").unwrap();
    std::fs::create_dir(left.join("sub")).unwrap();

    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(false, right.clone(), None);
    let on_a = |app_state: &mut AppState| {
        app_state.open_dir(true, left.clone(), Some("a.txt".as_ref()));
    };
    let finish = |app_state: &mut AppState| {
        let started = std::time::Instant::now();
        while app_state.job.is_some() {
            assert!(started.elapsed() < std::time::Duration::from_secs(10), "the job never finished");
            std::thread::sleep(std::time::Duration::from_millis(5));
            app_state.poll_transfer();
        }
    };
    let type_in = |app_state: &mut AppState, text: &str| {
        app_state.dialog_input().unwrap().set(text.to_string());
        press(app_state, KeyCode::Enter);
    };

    // A single file opens on its path in the other panel, and Enter alone
    // copies it there.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    assert_eq!(std::path::PathBuf::from(&app_state.dialog_input().unwrap().text), right.join("a.txt"));
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert_eq!(std::fs::read_to_string(right.join("a.txt")).unwrap(), "a");

    // Again, onto the copy now there: the job stops to ask about it, and
    // Esc leaves it as it was.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    press(&mut app_state, KeyCode::Enter);
    let started = std::time::Instant::now();
    while !app_state.job.as_ref().is_some_and(|job| job.clash.is_some()) {
        assert!(started.elapsed() < std::time::Duration::from_secs(10), "never asked");
        std::thread::sleep(std::time::Duration::from_millis(5));
        app_state.poll_transfer();
    }
    press(&mut app_state, KeyCode::Esc);
    finish(&mut app_state);
    assert!(app_state.error.as_deref().is_some_and(|error| error.starts_with("Copy cancelled")), "{:?}", app_state.error);
    assert_eq!(std::fs::read_to_string(right.join("a.txt")).unwrap(), "a");
    app_state.reset_error();

    // Again, and K keeps both: the question offers it, and the copy goes in
    // beside the one there.
    std::fs::write(right.join("a.txt"), "theirs").unwrap();
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    press(&mut app_state, KeyCode::Enter);
    let started = std::time::Instant::now();
    while !app_state.job.as_ref().is_some_and(|job| job.clash.is_some()) {
        assert!(started.elapsed() < std::time::Duration::from_secs(10), "never asked");
        std::thread::sleep(std::time::Duration::from_millis(5));
        app_state.poll_transfer();
    }
    // Drawn once the job has run long enough to be worth showing.
    app_state.job.as_mut().unwrap().started -= std::time::Duration::from_secs(5);
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap();
    crate::ui::render_ui(&mut terminal, &mut app_state);
    let screen: String = terminal.backend().buffer().content().iter().map(|cell| cell.symbol()).collect();
    assert!(screen.contains("K - Keep both") && screen.contains("B - Keep both all"), "not offered");
    press(&mut app_state, KeyCode::Char('k'));
    finish(&mut app_state);
    assert_eq!(app_state.error, None);
    assert_eq!(std::fs::read_to_string(right.join("a.txt")).unwrap(), "theirs");
    assert_eq!(std::fs::read_to_string(right.join("a (2).txt")).unwrap(), "a");
    std::fs::remove_file(right.join("a (2).txt")).unwrap();
    std::fs::write(right.join("a.txt"), "a").unwrap();

    // The name edited, one key at a time.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    for _ in "a.txt".chars() {
        press(&mut app_state, KeyCode::Backspace);
    }
    for c in "b.txt".chars() {
        press(&mut app_state, KeyCode::Char(c));
    }
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert!(right.join("b.txt").exists());

    // N is typed into the field, and Esc leaves it.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    press(&mut app_state, KeyCode::Char('n'));
    assert!(app_state.dialog_input().unwrap().text.ends_with('n'));
    press(&mut app_state, KeyCode::Esc);
    assert!(app_state.dialog.is_none() && app_state.job.is_none());

    // A bare name is beside the original.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(5));
    type_in(&mut app_state, "dup.txt");
    finish(&mut app_state);
    assert_eq!(std::fs::read_to_string(left.join("dup.txt")).unwrap(), "a");

    // F6 to a new name in the same place is a rename.
    on_a(&mut app_state);
    press(&mut app_state, KeyCode::F(6));
    type_in(&mut app_state, "moved.txt");
    finish(&mut app_state);
    assert!(!left.join("a.txt").exists() && left.join("moved.txt").exists());

    // A directory in the way of a file is refused, not copied into, when
    // the field is left as it opened.
    std::fs::create_dir(right.join("z.txt")).unwrap();
    app_state.open_dir(true, left.clone(), Some("z.txt".as_ref()));
    press(&mut app_state, KeyCode::F(5));
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.as_deref().is_some_and(|error| error.contains("Cannot replace directory")), "{:?}", app_state.error);
    assert!(!right.join("z.txt").join("z.txt").exists());
    app_state.reset_error();

    // A directory, and several entries, open on the other panel's
    // directory alone.
    let opened_on_directory = |app_state: &mut AppState| {
        let text = app_state.dialog_input().unwrap().text.clone();
        std::path::Path::new(&text) == right && text.ends_with(std::path::MAIN_SEPARATOR)
    };
    app_state.open_dir(true, left.clone(), Some("sub".as_ref()));
    press(&mut app_state, KeyCode::F(5));
    assert!(opened_on_directory(&mut app_state));
    press(&mut app_state, KeyCode::Esc);
    app_state.open_dir(true, left.clone(), None);
    app_state.selected_left.extend(["dup.txt".into(), "z.txt".into()]);
    press(&mut app_state, KeyCode::F(5));
    assert!(opened_on_directory(&mut app_state));

    // And several cannot share one name.
    type_in(&mut app_state, "one.txt");
    assert!(app_state.error.as_deref().is_some_and(|error| error.contains("2 items need a directory")), "{:?}", app_state.error);
    assert!(!left.join("one.txt").exists());

    std::fs::remove_dir_all(&dir).unwrap();
}

/// The other panel on its way to a directory slow to read: F5 offers the
/// one it is going to, not the one it is leaving.
#[test]
fn a_copy_goes_where_the_other_panel_is_going() {
    let dir = std::env::temp_dir().join(format!("fm84-copy-pending-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    for sub in ["from", "leaving", "going"] {
        std::fs::create_dir_all(dir.join(sub)).unwrap();
    }
    std::fs::write(dir.join("from").join("a.txt"), "").unwrap();
    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(true, dir.join("from"), Some("a.txt".as_ref()));
    app_state.open_dir(false, dir.join("leaving"), None);
    app_state.listing_right = Some(crate::app::PendingListing::going_to(dir.join("going")));

    press(&mut app_state, KeyCode::F(5));
    assert_eq!(std::path::PathBuf::from(&app_state.dialog_input().unwrap().text), dir.join("going").join("a.txt"));
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Tab on the pack dialog: whatever case the ending is in, and whatever
/// comes before it, even a character whose lower case is shorter.
#[test]
fn the_pack_format_goes_round_by_its_ending() {
    use super::super::file_ops::next_pack_format;
    assert_eq!(next_pack_format("a.ZIP"), "a.7z");
    assert_eq!(next_pack_format("a.TAR.GZ"), "a.tar.xz");
    assert_eq!(next_pack_format("a.tar"), "a.zip");
    assert_eq!(next_pack_format("plain"), "plain.zip");
    // The Kelvin sign is three bytes, and its lower case one.
    assert_eq!(next_pack_format("\u{212A}.zip"), "\u{212A}.7z");
    assert_eq!(next_pack_format("x\u{212A}"), "x\u{212A}.zip");
}

/// Alt+H works out a file's SHA-256 and shows it; C copies the checksum
/// alone, Tab goes on to SHA-512 - drawn whole, however long - and W writes
/// it beside the file and lands there. On a selection it writes SHA256SUMS,
/// and on that, checks it - saying what fails.
#[test]
fn alt_h_works_out_writes_and_checks_checksums() {
    use crate::app::ChecksumView;
    use crate::checksum::Report;
    let dir = std::env::temp_dir().join(format!("fm84-alt-h-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("photos")).unwrap();
    std::fs::write(dir.join("notes.txt"), "abc").unwrap();
    std::fs::write(dir.join("photos/a.jpg"), "a").unwrap();
    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(true, dir.clone(), Some("notes.txt".as_ref()));
    let alt_h = |app_state: &mut AppState| handle_key(app_state, KeyEvent::new(KeyCode::Char('h'), KeyModifiers::ALT));
    let finish = |app_state: &mut AppState| {
        let started = std::time::Instant::now();
        while app_state.job.is_some() {
            assert!(started.elapsed() < std::time::Duration::from_secs(10), "the job never finished");
            std::thread::sleep(std::time::Duration::from_millis(5));
            app_state.poll_transfer();
        }
    };
    fn report(app_state: &AppState) -> &ChecksumView {
        match &app_state.dialog {
            Some(Dialog::Checksums(view)) => view,
            _ => panic!("no report: {:?}", app_state.error),
        }
    }

    alt_h(&mut app_state);
    finish(&mut app_state);
    assert_eq!(report(&app_state).report.lines(), ["ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  notes.txt"]);
    press(&mut app_state, KeyCode::Char('c'));
    assert_eq!(app_state.clipboard, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");

    // All worked out already: Tab changes what is shown in place, with no
    // job and the same popup, and Shift+Tab or Left goes back.
    press(&mut app_state, KeyCode::Tab);
    assert!(app_state.job.is_none());
    press(&mut app_state, KeyCode::BackTab);
    assert_eq!(report(&app_state).report.shown(), Some(crate::checksum::Algorithm::Sha256));
    press(&mut app_state, KeyCode::Left);
    assert_eq!(report(&app_state).report.shown(), Some(crate::checksum::Algorithm::Md5));
    press(&mut app_state, KeyCode::Char('c'));
    assert_eq!(app_state.clipboard, "900150983cd24fb0d6963f7d28e17f72");
    press(&mut app_state, KeyCode::Right);
    press(&mut app_state, KeyCode::Tab);
    let sha512 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
    assert!(matches!(
        &report(&app_state).report,
        Report::Computed {
            algorithm: crate::checksum::Algorithm::Sha512,
            ..
        }
    ));
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
    crate::ui::render_ui(&mut terminal, &mut app_state);
    let screen: String = terminal.backend().buffer().content().iter().map(|cell| cell.symbol()).collect();
    // Wrapped at the popup's width - 60% of 100 columns, less its border
    // and margin - and drawn whole, a piece to a row.
    for piece in sha512.as_bytes().chunks(56) {
        assert!(screen.contains(std::str::from_utf8(piece).unwrap()), "{} not drawn", std::str::from_utf8(piece).unwrap());
    }
    for part in [" SHA-256 ", " SHA-512 ", " MD5 ", "Enter - Write", "Tab - Algorithm", "Esc - Close"] {
        assert!(screen.contains(part), "{part} not drawn");
    }
    // One size whatever is shown: as wide as every popup, as tall as the
    // larger ones - the same with SHA-512's long rows as with MD5's short
    // ones, and with one file as with several.
    fn popup_box(app_state: &mut AppState) -> (u16, u16, u16, u16) {
        let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
        crate::ui::render_ui(&mut terminal, app_state);
        let buffer = terminal.backend().buffer();
        let row = |y: u16| (0..100).map(|x| buffer[(x, y)].symbol().to_string()).collect::<String>();
        let top = (0..30).find(|&y| row(y).contains(" Checksums ")).expect("no popup");
        let left = (1..100).find(|&x| buffer[(x, top)].symbol() == "┌").unwrap();
        let right = (left..100).find(|&x| buffer[(x, top)].symbol() == "┐").unwrap();
        let bottom = (top..30).find(|&y| buffer[(left, y)].symbol() == "└").unwrap();
        (left, top, right - left + 1, bottom - top + 1)
    }
    let sized = popup_box(&mut app_state);
    assert_eq!(sized.2, 60, "not the width every popup has");
    assert_eq!(sized.3, 30 * 60 / 100);
    for _ in 0..3 {
        press(&mut app_state, KeyCode::Tab);
        assert_eq!(popup_box(&mut app_state), sized);
    }
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.dialog.is_none());
    assert_eq!(std::fs::read_to_string(dir.join("notes.txt.sha512")).unwrap(), format!("{sha512}  notes.txt\n"));
    assert_eq!(app_state.children_left[app_state.state_left.selected().unwrap()].name_full, "notes.txt.sha512");

    // Several, a directory's files among them: SHA256SUMS, checked after.
    app_state.selected_left.extend(["notes.txt", "photos"].map(std::ffi::OsString::from));
    alt_h(&mut app_state);
    finish(&mut app_state);
    assert_eq!(report(&app_state).report.summary(), "2 files");
    assert_eq!(popup_box(&mut app_state), sized);
    press(&mut app_state, KeyCode::Enter);
    app_state.selected_left.clear();
    alt_h(&mut app_state);
    finish(&mut app_state);
    assert_eq!(report(&app_state).report.summary(), "2 OK");
    press(&mut app_state, KeyCode::Esc);
    std::fs::write(dir.join("photos/a.jpg"), "changed").unwrap();
    alt_h(&mut app_state);
    finish(&mut app_state);
    assert_eq!(report(&app_state).report.summary(), "1 OK, 1 FAILED");
    assert_eq!(report(&app_state).report.lines()[0], "FAILED   photos/a.jpg");
    // A check has nothing to write: Enter only closes it.
    let before: Vec<_> = std::fs::read_dir(&dir).unwrap().map(|entry| entry.unwrap().file_name()).collect();
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.dialog.is_none());
    assert_eq!(std::fs::read_dir(&dir).unwrap().count(), before.len());
    std::fs::remove_dir_all(&dir).unwrap();
}