rcom/tests/pty_smoke.rs

527 lines
20 KiB
Rust

#![cfg(unix)]
use std::fs;
use std::process::Command;
use expectrl::process::unix::WaitStatus;
use expectrl::{Eof, Expect, Session};
use tempfile::TempDir;
fn spawn_app(path: &std::path::Path) -> expectrl::session::OsSession {
spawn_app_with_editor(path, None)
}
fn spawn_app_with_editor(
path: &std::path::Path,
editor: Option<&std::path::Path>,
) -> expectrl::session::OsSession {
let mut command = Command::new(env!("CARGO_BIN_EXE_rcom"));
command
.arg(path)
.env("TERM", "xterm-256color")
.env("NO_COLOR", "1");
if let Some(editor) = editor {
command.env("VISUAL", editor).env_remove("EDITOR");
}
Session::spawn(command).unwrap()
}
#[cfg(unix)]
fn fake_editor(directory: &std::path::Path, body: &str) -> std::path::PathBuf {
use std::os::unix::fs::PermissionsExt;
let path = directory.join("fake-editor");
fs::write(&path, format!("#!/bin/sh\nset -eu\n{body}\n")).unwrap();
let mut permissions = fs::metadata(&path).unwrap().permissions();
permissions.set_mode(0o700);
fs::set_permissions(&path, permissions).unwrap();
path
}
fn spawn_app_with_config(
path: &std::path::Path,
config_root: &std::path::Path,
) -> expectrl::session::OsSession {
let mut command = Command::new(env!("CARGO_BIN_EXE_rcom"));
command
.arg(path)
.env("TERM", "xterm-256color")
.env("NO_COLOR", "1")
.env("RCOM_CONFIG_HOME", config_root);
Session::spawn(command).unwrap()
}
fn assert_contains(output: &[u8], expected: &[u8]) {
assert!(
output
.windows(expected.len())
.any(|window| window == expected),
"missing {:?} in terminal output",
String::from_utf8_lossy(expected)
);
}
fn quit_and_expect_restore(
session: &mut expectrl::session::OsSession,
expected_code: i32,
) -> Vec<u8> {
session.send("q").unwrap();
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, code) if code == expected_code));
assert_contains(output.as_bytes(), b"\x1b[?1049l");
assert_contains(output.as_bytes(), b"\x1b[?25h");
output.as_bytes().to_vec()
}
#[test]
fn quit_restores_terminal_and_returns_success() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("readme.txt"), "fixture").unwrap();
let mut session = spawn_app(fixture.path());
session.expect("RCOM").unwrap();
session.expect("readme.txt").unwrap();
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn help_close_reopen_aliases_preserve_terminal_lifecycle() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("readme.txt"), "fixture").unwrap();
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(40, 8).unwrap();
session.expect("readme.txt").unwrap();
for (open, close) in [
("?", "\x1b"),
("\x1bOP", "\x1bOP"),
("?", "?"),
("\x1bOP", "q"),
] {
session.send(open).unwrap();
session.expect("< 1/6 Navigation >").unwrap();
session.send(close).unwrap();
session.expect("readme.txt").unwrap();
}
session.send("\x1b[21~").unwrap(); // F10
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, 0)));
assert_contains(output.as_bytes(), b"\x1b[?1049l");
assert_contains(output.as_bytes(), b"\x1b[?25h");
}
#[test]
fn filter_input_apply_match_clear_and_f10_restore_terminal() {
let fixture = TempDir::new().unwrap();
for name in ["alpha.txt", "alphabet.txt", "beta.txt"] {
fs::write(fixture.path().join(name), format!("contents-{name}")).unwrap();
}
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(120, 30).unwrap();
session.expect("contents-alpha.txt").unwrap();
session.send("/").unwrap();
session.expect("FILTER>").unwrap();
session.send("alpha").unwrap();
session.expect("Enter Apply").unwrap();
session.send("\r").unwrap();
std::thread::sleep(std::time::Duration::from_millis(80));
session.send("n").unwrap();
session.expect("contents-alphabet.txt").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.send("\x1b[21~").unwrap();
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, 0)));
assert_contains(output.as_bytes(), b"\x1b[?1049l");
assert_contains(output.as_bytes(), b"\x1b[?25h");
}
#[test]
fn filter_no_matches_cancel_and_quit_restore_workspace() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("alpha.txt"), "contents-alpha").unwrap();
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(120, 30).unwrap();
session.expect("contents-alpha").unwrap();
session.send("/").unwrap();
session.expect("FILTER>").unwrap();
session.send("z").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn image_halfblocks_render_in_pty_without_calling_substitute_tmux() {
use std::io::Cursor;
use std::os::unix::fs::PermissionsExt;
let fixture = TempDir::new().unwrap();
let tools = TempDir::new().unwrap();
let marker = tools.path().join("tmux-called");
let fake_tmux = tools.path().join("tmux");
fs::write(
&fake_tmux,
format!("#!/bin/sh\ntouch '{}'\n", marker.display()),
)
.unwrap();
let mut permissions = fs::metadata(&fake_tmux).unwrap().permissions();
permissions.set_mode(0o700);
fs::set_permissions(&fake_tmux, permissions).unwrap();
let mut pixels = image::ImageBuffer::from_pixel(10, 20, image::Rgba([255, 0, 0, 255]));
for (_, y, pixel) in pixels.enumerate_pixels_mut() {
if y >= 10 {
*pixel = image::Rgba([0, 0, 255, 255]);
}
}
let mut bytes = Cursor::new(Vec::new());
image::DynamicImage::ImageRgba8(pixels)
.write_to(&mut bytes, image::ImageFormat::Png)
.unwrap();
fs::write(fixture.path().join("pixel.png"), bytes.into_inner()).unwrap();
let mut command = Command::new(env!("CARGO_BIN_EXE_rcom"));
command
.arg(fixture.path())
.env("TERM", "xterm-256color")
.env_remove("TMUX")
.env("TERM_PROGRAM", "tmux")
.env(
"PATH",
format!(
"{}:{}",
tools.path().display(),
std::env::var("PATH").unwrap()
),
)
.env_remove("NO_COLOR");
let mut session = Session::spawn(command).unwrap();
session.get_process_mut().set_window_size(120, 30).unwrap();
session.expect("pixel.png").unwrap();
session.expect("▀").unwrap();
let _ = quit_and_expect_restore(&mut session, 0);
assert!(
!marker.exists(),
"rcom must not invoke tmux or change allow-passthrough"
);
}
#[test]
fn rapid_selection_burst_coalesces_preview_and_restores_terminal() {
let fixture = TempDir::new().unwrap();
for name in ["a.txt", "b.txt", "c.txt"] {
fs::write(fixture.path().join(name), format!("contents-{name}")).unwrap();
}
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(120, 30).unwrap();
session.expect("contents-a.txt").unwrap();
session.send("jj").unwrap();
let output = session.expect("contents-c.txt").unwrap();
let output = output.as_bytes();
let loading_frames = output
.windows(b"LOADING PREVIEW".len())
.filter(|window| *window == b"LOADING PREVIEW")
.count();
assert!(loading_frames <= 1, "loading frames: {loading_frames}");
assert_contains(output, b"c.txt");
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn theme_picker_escape_rolls_back_and_f10_quits_without_saving() {
let fixture = TempDir::new().unwrap();
let config = TempDir::new().unwrap();
fs::write(fixture.path().join("readme.txt"), "fixture").unwrap();
let config_path = config.path().join("config.toml");
fs::write(&config_path, "icons = \"ascii\"\ntheme = \"night-ops\"\n").unwrap();
let original = fs::read(&config_path).unwrap();
let mut session = spawn_app_with_config(fixture.path(), config.path());
session.expect("readme.txt").unwrap();
session.send("T").unwrap();
session.expect("THEMES").unwrap();
session.send("k").unwrap();
session.expect("Gruvbox Light").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.send("T").unwrap();
session.expect("12/20").unwrap();
session.send("j").unwrap();
session.expect("One Half Dark").unwrap();
session.send("\x1b[21~").unwrap(); // F10
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, 0)));
assert_eq!(fs::read(&config_path).unwrap(), original);
assert_contains(output.as_bytes(), b"\x1b[?1049l");
assert_contains(output.as_bytes(), b"\x1b[?25h");
}
#[test]
fn theme_picker_enter_persists_only_the_selected_theme_and_restores_terminal() {
let fixture = TempDir::new().unwrap();
let config = TempDir::new().unwrap();
fs::write(fixture.path().join("readme.txt"), "fixture").unwrap();
let config_path = config.path().join("config.toml");
fs::write(&config_path, "icons = \"none\"\nfuture_setting = true\n").unwrap();
let mut session = spawn_app_with_config(fixture.path(), config.path());
session.expect("readme.txt").unwrap();
session.send("T").unwrap();
session.expect("12/20").unwrap();
session.send("j").unwrap();
session.expect("One Half Dark").unwrap();
session.send("\r").unwrap();
session.expect("readme.txt").unwrap();
session.send("T").unwrap();
session.expect("13/20").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
let _ = quit_and_expect_restore(&mut session, 0);
let saved = fs::read_to_string(&config_path).unwrap();
assert!(saved.contains("theme = \"one-half-dark\""), "{saved}");
assert!(saved.contains("icons = \"none\""), "{saved}");
assert!(saved.contains("future_setting = true"), "{saved}");
assert_eq!(fs::read_dir(config.path()).unwrap().count(), 1);
}
#[test]
fn backend_error_is_visible_and_does_not_prevent_clean_quit() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("readme-unique.txt"), "fixture").unwrap();
let mut session = spawn_app(fixture.path());
session.expect("readme-unique.txt").unwrap();
fs::remove_dir_all(fixture.path()).unwrap();
session.send([0x12_u8]).unwrap(); // Ctrl-r
std::thread::sleep(std::time::Duration::from_millis(300));
session.send("q").unwrap();
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, 0)));
assert_contains(output.as_bytes(), b"LISTING ERROR");
assert_contains(output.as_bytes(), b"no longer exists");
assert_contains(output.as_bytes(), b"\x1b[?1049l");
assert_contains(output.as_bytes(), b"\x1b[?25h");
}
#[test]
fn local_dual_survives_resize_and_pane_switch() {
let fixture = TempDir::new().unwrap();
fs::create_dir(fixture.path().join("left-dir")).unwrap();
fs::write(fixture.path().join("right-file.txt"), "fixture").unwrap();
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(120, 40).unwrap();
session.expect("RCOM").unwrap();
session.send("\\").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.expect("DUAL").unwrap();
session.get_process_mut().set_window_size(80, 24).unwrap();
session.send("\t").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.expect("PANE 2/2").unwrap();
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn single_pane_f5_shows_destination_prompt_and_escape_cancels_without_writes() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("readme.txt"), "fixture").unwrap();
let mut session = spawn_app(fixture.path());
session.get_process_mut().set_window_size(100, 30).unwrap();
session.expect("readme.txt").unwrap();
session.send("\x1b[15~").unwrap(); // F5
session.expect("OPERATION DESTINATION").unwrap();
session.send("\x1b").unwrap(); // Esc: safe cancellation
std::thread::sleep(std::time::Duration::from_millis(100));
assert_eq!(
fs::read_dir(fixture.path()).unwrap().count(),
1,
"planning/cancellation must not create files"
);
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn editor_success_suspends_resumes_and_keeps_edited_local_file() {
let fixture = TempDir::new().unwrap();
let editor_fixture = TempDir::new().unwrap();
let file = fixture.path().join("note.txt");
fs::write(&file, "before").unwrap();
let editor = fake_editor(
editor_fixture.path(),
"printf 'edited-by-pty' > \"$1\"\nprintf 'PTY_EDITOR_SUCCESS\\n'",
);
let mut session = spawn_app_with_editor(fixture.path(), Some(&editor));
session.expect("note.txt").unwrap();
session.send("e").unwrap();
session.expect("\x1b[?1049l").unwrap();
session.expect("PTY_EDITOR_SUCCESS").unwrap();
session.expect("RCOM").unwrap();
let _ = quit_and_expect_restore(&mut session, 0);
assert_eq!(fs::read_to_string(file).unwrap(), "edited-by-pty");
}
#[test]
fn editor_nonzero_restores_terminal_without_changing_source() {
let fixture = TempDir::new().unwrap();
let editor_fixture = TempDir::new().unwrap();
let file = fixture.path().join("note.txt");
fs::write(&file, "before").unwrap();
let editor = fake_editor(
editor_fixture.path(),
"printf 'PTY_EDITOR_NONZERO\\n'\nexit 7",
);
let mut session = spawn_app_with_editor(fixture.path(), Some(&editor));
session.expect("note.txt").unwrap();
session.send("e").unwrap();
session.expect("\x1b[?1049l").unwrap();
session.expect("PTY_EDITOR_NONZERO").unwrap();
session.expect("RCOM").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
let output = quit_and_expect_restore(&mut session, 0);
assert_contains(&output, b"exited");
assert_contains(&output, b"status");
assert_eq!(fs::read_to_string(file).unwrap(), "before");
}
#[test]
fn editor_spawn_error_restores_terminal_and_preserves_source() {
let fixture = TempDir::new().unwrap();
let editor_fixture = TempDir::new().unwrap();
let file = fixture.path().join("note.txt");
fs::write(&file, "before").unwrap();
let missing = editor_fixture.path().join("missing-editor");
let mut session = spawn_app_with_editor(fixture.path(), Some(&missing));
session.expect("note.txt").unwrap();
session.send("e").unwrap();
session.expect("\x1b[?1049l").unwrap();
session.expect("RCOM").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
let output = quit_and_expect_restore(&mut session, 0);
assert_contains(&output, b"Cannot");
assert_contains(&output, b"run");
assert_contains(&output, b"editor");
assert_eq!(fs::read_to_string(file).unwrap(), "before");
}
#[test]
fn create_prompt_cancel_restores_terminal_without_creating_a_file() {
let fixture = TempDir::new().unwrap();
let mut session = spawn_app(fixture.path());
session.expect("RCOM").unwrap();
session.send("a").unwrap();
session.expect("CREATE FILE").unwrap();
session.send("new.txt").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
assert_eq!(fs::read_dir(fixture.path()).unwrap().count(), 0);
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn permanent_delete_confirmation_cancels_without_preparing_a_mutation() {
let fixture = TempDir::new().unwrap();
let victim = fixture.path().join("victim.txt");
fs::write(&victim, "keep").unwrap();
let mut session = spawn_app(fixture.path());
session.expect("victim.txt").unwrap();
session.send("D").unwrap();
session.expect("CONFIRM PERMANENT DELETE").unwrap();
session.expect("NOT UNDOABLE").unwrap();
session.send("\x1b").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
assert_eq!(fs::read_to_string(victim).unwrap(), "keep");
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn multi_select_navigate_and_paste_never_shows_a_false_empty_plan() {
let fixture = TempDir::new().unwrap();
fs::write(fixture.path().join("a.txt"), "a").unwrap();
fs::write(fixture.path().join("b.txt"), "b").unwrap();
fs::create_dir(fixture.path().join("destination")).unwrap();
fs::create_dir(fixture.path().join("other")).unwrap();
let mut session = spawn_app(fixture.path());
session.expect("RCOM").unwrap();
session.expect("a.txt").unwrap();
session.send("\\").unwrap();
session.send("\t").unwrap();
session.send("\r").unwrap();
session.expect("EMPTY DIRECTORY").unwrap();
session.send("\t").unwrap();
session.send("G").unwrap();
session.send(" ").unwrap();
session.send("k").unwrap();
session.send(" ").unwrap();
session.send("y").unwrap();
session.send("gg").unwrap();
session.send("j").unwrap();
session.send("\r").unwrap();
session.expect("EMPTY DIRECTORY").unwrap();
session.send("p").unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
session.send("\r").unwrap();
session.send("\r").unwrap(); // duplicate confirmation must be harmless
std::thread::sleep(std::time::Duration::from_millis(300));
assert_eq!(
fs::read_to_string(fixture.path().join("destination/a.txt")).unwrap(),
"a"
);
assert_eq!(
fs::read_to_string(fixture.path().join("destination/b.txt")).unwrap(),
"b"
);
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn external_create_rename_and_delete_appear_without_keyboard_input() {
let fixture = TempDir::new().unwrap();
let original = fixture.path().join("original.txt");
fs::write(&original, "original").unwrap();
let mut session = spawn_app(fixture.path());
session.expect("original.txt").unwrap();
let external = fixture.path().join("external.txt");
fs::write(&external, "external").unwrap();
session.expect("external.txt").unwrap();
let renamed = fixture.path().join("renamed.txt");
fs::rename(&external, &renamed).unwrap();
session.expect("renamed.txt").unwrap();
fs::remove_file(&original).unwrap();
fs::remove_file(&renamed).unwrap();
session.expect("EMPTY DIRECTORY").unwrap();
let _ = quit_and_expect_restore(&mut session, 0);
}
#[test]
fn fatal_initial_path_error_returns_nonzero_without_alternate_screen_leak() {
let fixture = TempDir::new().unwrap();
let file = fixture.path().join("file.txt");
fs::write(&file, "not a directory").unwrap();
let mut session = spawn_app(&file);
let output = session.expect(Eof).unwrap();
let status = session.get_process_mut().wait().unwrap();
assert!(matches!(status, WaitStatus::Exited(_, code) if code != 0));
assert!(
!output
.as_bytes()
.windows(9)
.any(|window| window == b"\x1b[?1049h")
);
}