382 lines
13 KiB
Rust
382 lines
13 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use rcom::app::{
|
|
AppState, Command, DestinationFingerprint, Effect, ItemDiagnostic, PaneMode,
|
|
PlanOperationOutput, PlanOperationRequest, TaskDiagnostic,
|
|
};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::input::{InputOutcome, InputState};
|
|
use rcom::operations::{
|
|
ItemId, OperationKind, OperationPlan, PlanId, ProgressUpdate, SourceItem, TaskId,
|
|
};
|
|
use rcom::ui::{Theme, render};
|
|
|
|
fn app_with_plan() -> (AppState, PlanOperationRequest) {
|
|
app_with_command(Command::Copy)
|
|
}
|
|
|
|
fn app_with_command(command: Command) -> (AppState, PlanOperationRequest) {
|
|
let mut app = AppState::new(Location::local("/source"));
|
|
app.mode = PaneMode::Dual;
|
|
app.active_pane_mut().entries = vec![Entry::new("a\n.txt", EntryKind::File)];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
app.panes[1].location = Location::local("/destination");
|
|
let effects: [Effect; 1] = app.reduce(command).try_into().unwrap();
|
|
let [Effect::PlanOperation(request)] = effects else {
|
|
panic!("planner effect expected");
|
|
};
|
|
(app, request)
|
|
}
|
|
|
|
fn output(request: &PlanOperationRequest) -> PlanOperationOutput {
|
|
let sources = request
|
|
.sources
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, source)| {
|
|
SourceItem::new(
|
|
ItemId::new(index as u64 + 1),
|
|
Location {
|
|
source: source.source.clone(),
|
|
path: source.path.clone(),
|
|
},
|
|
)
|
|
})
|
|
.collect();
|
|
PlanOperationOutput {
|
|
prepared: None,
|
|
plan: OperationPlan::new(
|
|
request.plan_id,
|
|
request.generation,
|
|
request.kind,
|
|
sources,
|
|
request.destination.clone(),
|
|
request.policy,
|
|
None,
|
|
None,
|
|
BTreeMap::new(),
|
|
)
|
|
.unwrap(),
|
|
destination_fingerprint: DestinationFingerprint::new("dest-v1"),
|
|
collisions: Vec::new(),
|
|
item_diagnostics: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn confirmed_app() -> (AppState, rcom::app::ExecuteOperationRequest) {
|
|
let (mut app, request) = app_with_plan();
|
|
app.reduce(Command::PlanFinished(
|
|
request.clone().into_result(Ok(output(&request))),
|
|
));
|
|
let effects: [Effect; 1] = app.reduce(Command::ConfirmOperation).try_into().unwrap();
|
|
let [Effect::ExecuteOperation(execution)] = effects else {
|
|
panic!("execution expected");
|
|
};
|
|
(app, execution)
|
|
}
|
|
|
|
fn draw(app: &AppState, width: u16, height: u16) -> String {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, &Theme::default()))
|
|
.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 key(code: KeyCode) -> KeyEvent {
|
|
KeyEvent::new(code, KeyModifiers::NONE)
|
|
}
|
|
|
|
#[test]
|
|
fn confirmation_shows_kind_paths_unknown_totals_move_consequences_and_sanitizes_text() {
|
|
let (mut app, request) = app_with_command(Command::Move);
|
|
let mut output = output(&request);
|
|
output.item_diagnostics.push(ItemDiagnostic {
|
|
source: Some(request.sources[0].clone()),
|
|
message: "metadata\u{1b}[2J unavailable".into(),
|
|
partial: Some(Location::local("/destination/.a.part")),
|
|
});
|
|
app.reduce(Command::PlanFinished(request.into_result(Ok(output))));
|
|
let rendered = draw(&app, 120, 36);
|
|
|
|
for expected in [
|
|
"CONFIRM MOVE",
|
|
"Items: unknown",
|
|
"Size: unknown",
|
|
"local:/destination",
|
|
"MOVE:",
|
|
"dest-v1",
|
|
".part",
|
|
"Enter Confirm once",
|
|
] {
|
|
assert!(
|
|
rendered.contains(expected),
|
|
"missing {expected:?}\n{rendered}"
|
|
);
|
|
}
|
|
assert!(rendered.contains("a\\n.txt"));
|
|
assert!(!rendered.contains("\u{1b}[2J"));
|
|
}
|
|
|
|
#[test]
|
|
fn collision_overlay_defaults_to_cancel_and_rejects_key_repeat_actions() {
|
|
let (mut app, request) = app_with_plan();
|
|
let mut output = output(&request);
|
|
output.collisions.push(rcom::app::OperationCollision {
|
|
source: request.sources[0].clone(),
|
|
destination: Location::local("/destination/a.txt"),
|
|
existing_kind: EntryKind::File,
|
|
});
|
|
app.reduce(Command::PlanFinished(request.into_result(Ok(output))));
|
|
let rendered = draw(&app, 120, 36);
|
|
assert!(rendered.contains("CANCEL IS DEFAULT"));
|
|
assert!(rendered.contains("Existing directories cannot be merged"));
|
|
assert!(rendered.contains("Esc / Enter / q Cancel"));
|
|
|
|
let repeat = KeyEvent {
|
|
code: KeyCode::Enter,
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Repeat,
|
|
state: KeyEventState::NONE,
|
|
};
|
|
assert_eq!(
|
|
InputState::new().handle_confirmation_key(repeat),
|
|
InputOutcome::Ignored
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_collision_key(repeat),
|
|
InputOutcome::Ignored
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn destination_prompt_captures_global_shortcuts_as_path_text() {
|
|
let mut app = AppState::new(Location::local("/source"));
|
|
app.active_pane_mut().entries = vec![Entry::new("a", EntryKind::File)];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
app.reduce(Command::Copy);
|
|
let mut input = InputState::new();
|
|
for character in "qpyxt".chars() {
|
|
assert!(matches!(
|
|
input.handle_destination_key(key(KeyCode::Char(character))),
|
|
InputOutcome::Command(Command::DestinationInput(actual)) if actual == character
|
|
));
|
|
}
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().kind,
|
|
OperationKind::Copy
|
|
);
|
|
let rendered = draw(&app, 100, 30);
|
|
assert!(rendered.contains("Destination path"));
|
|
assert!(rendered.contains("> /source"));
|
|
}
|
|
|
|
#[test]
|
|
fn tasks_and_quit_views_show_progress_part_errors_and_safe_choices_without_nan() {
|
|
let (mut app, execution) = confirmed_app();
|
|
let task_id = execution.start.task_id;
|
|
app.reduce(Command::OperationEvent(
|
|
rcom::operations::OperationEvent::new(
|
|
execution.start.operation_id,
|
|
execution.start.plan_id,
|
|
rcom::operations::OperationEventKind::Progress(ProgressUpdate::new(
|
|
0,
|
|
0,
|
|
Some(execution.operation.plan.sources()[0].id()),
|
|
Duration::from_secs(65),
|
|
)),
|
|
),
|
|
));
|
|
app.reduce(Command::OperationDiagnostic(TaskDiagnostic {
|
|
task_id,
|
|
message: "close failed; outcome unknown".into(),
|
|
partial: Some(Location::local("/destination/.a.txt.part")),
|
|
}));
|
|
app.reduce(Command::OpenTasks);
|
|
let tasks = draw(&app, 120, 36);
|
|
assert!(tasks.contains("running"));
|
|
assert!(tasks.contains("bytes 0/?"));
|
|
assert!(tasks.contains("00:01:05"));
|
|
assert!(!tasks.contains("NaN"));
|
|
|
|
app.reduce(Command::OpenTaskDetails);
|
|
let details = draw(&app, 120, 36);
|
|
assert!(details.contains("ERRORS / .PART DIAGNOSTICS"));
|
|
assert!(details.contains("close failed; outcome unknown"));
|
|
assert!(details.contains("/destination/.a.txt.part"));
|
|
assert!(details.contains("Source: local:/source/a\\n.txt"));
|
|
|
|
app.reduce(Command::Quit);
|
|
let quit = draw(&app, 120, 36);
|
|
assert!(quit.contains("OPERATIONS STILL ACTIVE"));
|
|
assert!(quit.contains("Cancel and wait"));
|
|
assert!(quit.contains("Exit with warning"));
|
|
assert!(!app.should_quit);
|
|
}
|
|
|
|
#[test]
|
|
fn task_input_cancel_and_details_do_not_repeat() {
|
|
let mut input = InputState::new();
|
|
assert_eq!(
|
|
input.handle_key(key(KeyCode::Char('t')), 10),
|
|
InputOutcome::Command(Command::OpenTasks)
|
|
);
|
|
let repeat = |code| KeyEvent {
|
|
code,
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Repeat,
|
|
state: KeyEventState::NONE,
|
|
};
|
|
assert_eq!(
|
|
input.handle_tasks_key(repeat(KeyCode::Char('c')), 10),
|
|
InputOutcome::Ignored
|
|
);
|
|
assert_eq!(
|
|
input.handle_tasks_key(repeat(KeyCode::Enter), 10),
|
|
InputOutcome::Ignored
|
|
);
|
|
assert_eq!(
|
|
input.handle_quit_key(repeat(KeyCode::Char('x'))),
|
|
InputOutcome::Ignored
|
|
);
|
|
assert_eq!(
|
|
input.handle_quit_key(key(KeyCode::Enter)),
|
|
InputOutcome::Command(Command::DecideQuit(rcom::app::QuitDecision::Stay))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn task_details_vertical_aliases_scroll_without_selecting_behind_overlay() {
|
|
let cases = [
|
|
(KeyCode::Char('j'), KeyModifiers::NONE, 1),
|
|
(KeyCode::Down, KeyModifiers::NONE, 1),
|
|
(KeyCode::Char('k'), KeyModifiers::NONE, -1),
|
|
(KeyCode::Up, KeyModifiers::NONE, -1),
|
|
(KeyCode::PageDown, KeyModifiers::NONE, 8),
|
|
(KeyCode::PageUp, KeyModifiers::NONE, -8),
|
|
(KeyCode::Char('f'), KeyModifiers::CONTROL, 8),
|
|
(KeyCode::Char('b'), KeyModifiers::CONTROL, -8),
|
|
(KeyCode::Char('d'), KeyModifiers::CONTROL, 4),
|
|
(KeyCode::Char('u'), KeyModifiers::CONTROL, -4),
|
|
];
|
|
let mut input = InputState::new();
|
|
for (code, modifiers, delta) in cases {
|
|
for kind in [KeyEventKind::Press, KeyEventKind::Repeat] {
|
|
let key = KeyEvent {
|
|
code,
|
|
modifiers,
|
|
kind,
|
|
state: KeyEventState::NONE,
|
|
};
|
|
let mut app = AppState::new(Location::local("/source"));
|
|
app.overlay = rcom::app::Overlay::TaskDetails;
|
|
app.selected_task = Some(TaskId::new(7));
|
|
app.task_details = Some(TaskId::new(7));
|
|
assert_eq!(
|
|
input.handle_task_details_key(key, 8),
|
|
InputOutcome::Command(Command::ScrollOverlay(delta))
|
|
);
|
|
app.reduce(Command::ScrollOverlay(delta));
|
|
assert_eq!(app.selected_task, Some(TaskId::new(7)));
|
|
assert_eq!(app.task_details, Some(TaskId::new(7)));
|
|
}
|
|
}
|
|
|
|
for code in [
|
|
KeyCode::Char('m'),
|
|
KeyCode::Enter,
|
|
KeyCode::Esc,
|
|
KeyCode::Char('q'),
|
|
KeyCode::Char('t'),
|
|
] {
|
|
assert_eq!(
|
|
input.handle_task_details_key(key(code), 8),
|
|
InputOutcome::Command(Command::CloseOverlay)
|
|
);
|
|
}
|
|
assert_eq!(
|
|
input.handle_task_details_key(key(KeyCode::Char('c')), 8),
|
|
InputOutcome::Command(Command::CancelSelectedTask)
|
|
);
|
|
assert_eq!(
|
|
input.handle_task_details_key(
|
|
KeyEvent {
|
|
code: KeyCode::Char('j'),
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Release,
|
|
state: KeyEventState::NONE,
|
|
},
|
|
8,
|
|
),
|
|
InputOutcome::Ignored
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn help_and_header_expose_operation_keys_without_persistent_hints() {
|
|
let mut app = AppState::new(Location::local("/source"));
|
|
app.reduce(Command::ToggleHelp);
|
|
let rendered = draw(&app, 120, 40);
|
|
assert!(rendered.contains("dual-pane file manager"));
|
|
assert!(rendered.contains("? Help"));
|
|
assert!(!rendered.contains("read-only workspace"));
|
|
assert!(!rendered.contains("Space Select · y/x/p Yank/Cut/Paste"));
|
|
|
|
app.reduce(Command::SelectHelpCategory(1));
|
|
let selection = draw(&app, 120, 40);
|
|
assert!(selection.contains("Space / Insert"));
|
|
app.reduce(Command::SelectHelpCategory(1));
|
|
let files = draw(&app, 120, 40);
|
|
for expected in ["Yank/copy selection", "F5", "F6", "NO UNDO"] {
|
|
assert!(files.contains(expected), "missing {expected:?}\n{files}");
|
|
}
|
|
app.reduce(Command::SelectHelpCategory(1));
|
|
let tasks = draw(&app, 120, 40);
|
|
assert!(tasks.contains("Open the task list"));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_confirmation_120x36() {
|
|
let (mut app, request) = app_with_plan();
|
|
app.reduce(Command::PlanFinished(
|
|
request.clone().into_result(Ok(output(&request))),
|
|
));
|
|
insta::assert_snapshot!(draw(&app, 120, 36));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_tasks_with_part_diagnostic_120x36() {
|
|
let (mut app, _execution) = confirmed_app();
|
|
app.reduce(Command::OperationDiagnostic(TaskDiagnostic {
|
|
task_id: TaskId::new(1),
|
|
message: "flush failed".into(),
|
|
partial: Some(Location::local("/destination/.a.part")),
|
|
}));
|
|
app.reduce(Command::OpenTasks);
|
|
app.reduce(Command::OpenTaskDetails);
|
|
insta::assert_snapshot!(draw(&app, 120, 36));
|
|
}
|
|
|
|
#[test]
|
|
fn no_plan_id_can_be_confirmed_before_preflight_finishes() {
|
|
let (mut app, request) = app_with_plan();
|
|
assert!(
|
|
app.reduce(Command::ConfirmPlan(PlanId::new(request.plan_id.get())))
|
|
.is_empty()
|
|
);
|
|
assert!(app.task_manager.is_empty());
|
|
}
|