302 lines
13 KiB
Rust
302 lines
13 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::style::Color;
|
|
use rcom::actions::{EntryMutationKind, EntryMutationNode, EntryMutationPlan};
|
|
use rcom::app::{
|
|
AppState, Command, Effect, Overlay, PrepareEntryActionRequest, PreparedEntryAction,
|
|
};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::operations::{EntryFingerprint, ItemId};
|
|
use rcom::ui::{Theme, render};
|
|
|
|
fn app_with_file() -> AppState {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.active_pane_mut().entries = vec![
|
|
Entry::new("old\nname", EntryKind::File).with_metadata(Some(4), None, Some(0o600)),
|
|
Entry::new("directory", EntryKind::Directory),
|
|
];
|
|
app.active_pane_mut().selection.set(Some(0), 2);
|
|
app
|
|
}
|
|
|
|
fn draw_with_theme(app: &AppState, width: u16, height: u16, theme: &Theme) -> String {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, theme))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
(0..buffer.area.height)
|
|
.map(|y| {
|
|
let row: String = (0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect();
|
|
row.trim_end().to_owned()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
fn draw(app: &AppState, width: u16, height: u16) -> String {
|
|
draw_with_theme(app, width, height, &Theme::no_color())
|
|
}
|
|
|
|
fn prepare_request(app: &mut AppState, command: Command) -> PrepareEntryActionRequest {
|
|
let effects: [Effect; 1] = app.reduce(command).try_into().unwrap();
|
|
let [Effect::PrepareEntryAction(request)] = effects else {
|
|
panic!("expected preparation");
|
|
};
|
|
request
|
|
}
|
|
|
|
fn prepared_plan(request: &PrepareEntryActionRequest) -> EntryMutationPlan {
|
|
let nodes = request
|
|
.sources
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, source)| {
|
|
let location = Location {
|
|
source: source.source.clone(),
|
|
path: source.path.clone(),
|
|
};
|
|
let name = location.file_name_owned().unwrap();
|
|
EntryMutationNode::new(
|
|
ItemId::new(index as u64 + 1),
|
|
location,
|
|
name,
|
|
Some(EntryFingerprint::from(&Entry::new(
|
|
"snapshot",
|
|
EntryKind::File,
|
|
))),
|
|
)
|
|
})
|
|
.collect();
|
|
EntryMutationPlan::new(
|
|
request.plan_id,
|
|
request.generation,
|
|
request.kind,
|
|
request.pane.location.clone(),
|
|
(request.kind == EntryMutationKind::Trash).then(|| Location::local("/trash")),
|
|
nodes,
|
|
BTreeMap::new(),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn create_prompt_and_confirmation_show_safe_type_location_and_paths() {
|
|
let mut app = app_with_file();
|
|
app.reduce(Command::CreateFile);
|
|
for character in "qaure\nnew".chars() {
|
|
app.reduce(Command::EntryInput(character));
|
|
}
|
|
let prompt = draw(&app, 100, 26);
|
|
assert!(prompt.contains("CREATE FILE"));
|
|
assert!(prompt.contains("regular file"));
|
|
assert!(prompt.contains("local:/work"));
|
|
assert!(prompt.contains("qaure\\nnew"));
|
|
assert!(!prompt.contains('\u{1b}'));
|
|
|
|
for _ in 0..4 {
|
|
app.reduce(Command::EntryBackspace);
|
|
}
|
|
app.reduce(Command::SubmitEntry);
|
|
let confirmation = draw(&app, 100, 26);
|
|
assert!(confirmation.contains("CONFIRM CREATE FILE"));
|
|
assert!(confirmation.contains("Source: local:/work"));
|
|
assert!(confirmation.contains("Destination: local:/work/qaure"));
|
|
assert!(confirmation.contains("Enter Confirm once"));
|
|
assert!(confirmation.contains("Esc/q Cancel (default)"));
|
|
}
|
|
|
|
#[test]
|
|
fn rename_confirmation_identifies_hovered_type_source_and_destination() {
|
|
let mut app = app_with_file();
|
|
app.active_pane_mut()
|
|
.selection_set
|
|
.insert(Location::local("/work").entry_id("directory"));
|
|
app.reduce(Command::Rename);
|
|
while !app.entry_action.as_ref().unwrap().input.is_empty() {
|
|
app.reduce(Command::EntryBackspace);
|
|
}
|
|
for character in "renamed.txt".chars() {
|
|
app.reduce(Command::EntryInput(character));
|
|
}
|
|
app.reduce(Command::SubmitEntry);
|
|
let output = draw(&app, 110, 30);
|
|
for expected in [
|
|
"CONFIRM RENAME",
|
|
"Type: regular file",
|
|
"Source: local:/work/old\\nname",
|
|
"Destination: local:/work/renamed.txt",
|
|
"Items: 1",
|
|
] {
|
|
assert!(output.contains(expected), "missing {expected:?}\n{output}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn trash_and_permanent_delete_have_distinct_explicit_confirmations() {
|
|
let mut trash = app_with_file();
|
|
let request = prepare_request(&mut trash, Command::Trash);
|
|
let plan = prepared_plan(&request);
|
|
trash.reduce(Command::EntryActionPrepared(
|
|
request.into_result(Ok(PreparedEntryAction::without_trash_records(plan))),
|
|
));
|
|
let output = draw(&trash, 110, 30);
|
|
assert!(output.contains("CONFIRM TRASH"));
|
|
assert!(output.contains("TRASH:"));
|
|
assert!(output.contains("Destination: local:/trash"));
|
|
assert!(!output.contains("NOT UNDOABLE"));
|
|
|
|
let mut delete = app_with_file();
|
|
let request = prepare_request(&mut delete, Command::PermanentDelete);
|
|
let plan = prepared_plan(&request);
|
|
delete.reduce(Command::EntryActionPrepared(
|
|
request.into_result(Ok(PreparedEntryAction::without_trash_records(plan))),
|
|
));
|
|
let output = draw(&delete, 120, 30);
|
|
assert!(output.contains("CONFIRM PERMANENT DELETE"));
|
|
assert!(output.contains("PERMANENT DELETE: this is permanent and NOT UNDOABLE."));
|
|
assert!(output.contains("Cancel is the safe default"));
|
|
assert!(output.contains("none (permanent delete)"));
|
|
}
|
|
|
|
#[test]
|
|
fn no_color_confirmation_uses_reset_colors_and_textual_safety_markers() {
|
|
let mut app = app_with_file();
|
|
let request = prepare_request(&mut app, Command::PermanentDelete);
|
|
let plan = prepared_plan(&request);
|
|
app.reduce(Command::EntryActionPrepared(
|
|
request.into_result(Ok(PreparedEntryAction::without_trash_records(plan))),
|
|
));
|
|
let backend = TestBackend::new(120, 30);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, &app, None, &Theme::no_color()))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
for cell in buffer.content() {
|
|
assert_eq!(cell.fg, Color::Reset);
|
|
assert_eq!(cell.bg, Color::Reset);
|
|
}
|
|
let output = draw(&app, 120, 30);
|
|
assert!(output.contains("NOT UNDOABLE"));
|
|
assert!(output.contains("Cancel is the safe default"));
|
|
}
|
|
|
|
#[test]
|
|
fn empty_undo_and_editor_error_overlays_do_not_claim_execution() {
|
|
let mut app = app_with_file();
|
|
app.reduce(Command::Undo);
|
|
assert_eq!(app.overlay, Overlay::Undo);
|
|
let undo = draw(&app, 90, 20);
|
|
assert!(undo.contains("UNDO — EMPTY"));
|
|
assert!(undo.contains("Undo history is empty"));
|
|
assert!(undo.contains("No action was performed"));
|
|
assert!(undo.contains("No write, undo, or editor execution was requested"));
|
|
assert!(!undo.contains("NOT CONNECTED"));
|
|
|
|
app.reduce(Command::CloseOverlay);
|
|
app.active_pane_mut().selection.set(Some(1), 2);
|
|
app.reduce(Command::Edit);
|
|
let edit = draw(&app, 90, 20);
|
|
assert!(edit.contains("EXTERNAL EDITOR — NOT CONNECTED"));
|
|
assert!(edit.contains("Type: directory"));
|
|
assert!(edit.contains("supports only regular files"));
|
|
assert!(edit.contains("No editor was started"));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_create_file_prompt_no_color_100x24() {
|
|
let mut app = app_with_file();
|
|
app.reduce(Command::CreateFile);
|
|
for character in "notes.txt".chars() {
|
|
app.reduce(Command::EntryInput(character));
|
|
}
|
|
insta::assert_snapshot!(draw(&app, 100, 24), @r#"
|
|
RCOM dual-pane file manager SINGLE ? Help
|
|
╭▸ ACTIVE · LOCAL · /work───────────────────╮╭PREVIEW · 📄 old\nname───────────────────────────────╮
|
|
│ NAME SIZE││NO PREVIEW │
|
|
│▸╭CREATE FILE───────────────────────────────────────────────────────────────────────────────────╮ │
|
|
│ │Type: regular file │ │
|
|
│ │Location: local:/work │ │
|
|
│ │ │ │
|
|
│ │One normal, non-empty name; paths and traversal are rejected: │ │
|
|
│ │> notes.txt │ │
|
|
│ │ │ │
|
|
│ │Enter Review · Esc Cancel (default) │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ │ │ │
|
|
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
|
|
│ ││ │
|
|
╰───────────────────────────────────────────╯╰─────────────────────────────────────────────────────╯
|
|
2 entries · Selected 1/2 · Dotfiles hidden
|
|
"#);
|
|
}
|
|
|
|
#[test]
|
|
fn task_views_use_work_kind_and_source_paths_for_non_copy_actions() {
|
|
let mut create = app_with_file();
|
|
create.reduce(Command::CreateFile);
|
|
create.reduce(Command::EntryInput('x'));
|
|
create.reduce(Command::SubmitEntry);
|
|
create.reduce(Command::ConfirmEntryAction);
|
|
create.reduce(Command::OpenTasks);
|
|
create.reduce(Command::OpenTaskDetails);
|
|
let output = draw(&create, 100, 24);
|
|
assert!(output.contains("CREATE"));
|
|
assert!(output.contains("local:/work/x"));
|
|
assert!(!output.contains("Task #1 · COPY"));
|
|
|
|
let mut delete = app_with_file();
|
|
let request = prepare_request(&mut delete, Command::PermanentDelete);
|
|
let plan = prepared_plan(&request);
|
|
delete.reduce(Command::EntryActionPrepared(
|
|
request.into_result(Ok(PreparedEntryAction::without_trash_records(plan))),
|
|
));
|
|
delete.reduce(Command::ConfirmEntryAction);
|
|
delete.reduce(Command::OpenTasks);
|
|
delete.reduce(Command::OpenTaskDetails);
|
|
let output = draw(&delete, 100, 26);
|
|
assert!(output.contains("DELETE"));
|
|
assert!(output.contains("local:/work/old\\nname"));
|
|
assert!(output.contains("ITEM OUTCOMES"));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_permanent_delete_no_color_small_48x16() {
|
|
let mut app = app_with_file();
|
|
let request = prepare_request(&mut app, Command::PermanentDelete);
|
|
let plan = prepared_plan(&request);
|
|
app.reduce(Command::EntryActionPrepared(
|
|
request.into_result(Ok(PreparedEntryAction::without_trash_records(plan))),
|
|
));
|
|
insta::assert_snapshot!(draw(&app, 48, 16), @r#"
|
|
╭CONFIRM PERMANENT DELETE — NOT UNDOABLE───────╮
|
|
│Plan #1 / generation 1 │
|
|
│Type: permanent delete │
|
|
│Source: local:/work │
|
|
│Destination: none (permanent delete) │
|
|
│Items: 1 │
|
|
│Paths: │
|
|
│ local:/work/old\nname │
|
|
│ │
|
|
│PERMANENT DELETE: this is permanent and NOT UN│
|
|
│DOABLE. │
|
|
│Cancel is the safe default; no recovery is pro│
|
|
│mised. │
|
|
│ │
|
|
│Enter Confirm once · Esc/q Cancel (default) │
|
|
╰──────────────────────────────────────────────╯
|
|
"#);
|
|
}
|