rcom/tests/ui.rs

196 lines
6.3 KiB
Rust

use std::collections::BTreeMap;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use rcom::actions::{EntryMutationKind, EntryMutationPlan};
use rcom::app::{
AppState, Command, Effect, Overlay, UndoConsequence, UndoInverseKind, UndoRecord, UndoSummary,
};
use rcom::domain::{Entry, EntryKind, Location};
use rcom::operations::{
CollisionPolicy, CommitOutcome, EntryFingerprint, ItemId, ItemOutcome, ItemPhase,
OperationEvent, OperationEventKind, OperationKind, OperationPlan, PlanGeneration, PlanId,
ProgressUpdate, SourceCleanupOutcome, SourceItem, WorkKind, WorkPlan,
};
use rcom::ui::{Theme, render};
fn draw(app: &AppState) -> String {
let backend = TestBackend::new(120, 34);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|frame| render(frame, app, None, &Theme::no_color()))
.unwrap();
let buffer = terminal.backend().buffer();
(0..buffer.area.height)
.map(|y| {
(0..buffer.area.width)
.map(|x| buffer.cell((x, y)).unwrap().symbol())
.collect::<String>()
.trim_end()
.to_owned()
})
.collect::<Vec<_>>()
.join("\n")
}
fn app_with_created_entry() -> AppState {
let mut app = AppState::new(Location::local("/work"));
let plan = EntryMutationPlan::create(
PlanId::new(100),
PlanGeneration::new(100),
EntryMutationKind::CreateFile,
ItemId::new(1),
Location::local("/work"),
"created.txt",
BTreeMap::new(),
)
.unwrap();
app.reduce(Command::RecordUndo(UndoRecord::Create {
plan,
created_fingerprint: EntryFingerprint::from(&Entry::new("created.txt", EntryKind::File)),
outcomes: vec![ItemOutcome::new(
ItemId::new(1),
ItemPhase::Succeeded,
CommitOutcome::Committed,
SourceCleanupOutcome::NotRequired,
)],
}));
app
}
#[test]
fn undo_summary_help_and_task_details_expose_inverse_paths_and_consequences() {
let mut app = app_with_created_entry();
let effects: [Effect; 1] = app.reduce(Command::Undo).try_into().unwrap();
let [Effect::PrepareUndo(request)] = effects else {
panic!("expected undo preparation")
};
assert_eq!(request.plan_id, PlanId::new(1));
assert_eq!(request.generation, PlanGeneration::new(1));
let summary = UndoSummary {
plan_id: request.plan_id,
generation: request.generation,
inverse_kind: UndoInverseKind::DeleteCreated,
source: Location::local("/work/created.txt"),
destination: None,
sources: vec![SourceItem::new(
ItemId::new(1),
Location::local("/work/created.txt"),
)],
consequence: UndoConsequence::RemoveOnlyUnchangedCreatedEntry,
};
assert!(
app.reduce(Command::UndoPrepared(request.into_result(Ok(summary))))
.is_empty()
);
let confirmation = draw(&app);
for expected in [
"CONFIRM UNDO",
"Inverse action: delete created entry",
"Source: local:/work/created.txt",
"Destination: none (created entry is removed)",
"Consequences: remove only the unchanged created file or empty directory",
"Enter Confirm once",
] {
assert!(confirmation.contains(expected), "missing {expected:?}");
}
let effects = app.reduce(Command::ConfirmOperation);
assert_eq!(effects.len(), 1);
assert!(matches!(effects[0], Effect::ExecuteUndo { .. }));
assert!(app.reduce(Command::ConfirmOperation).is_empty());
app.reduce(Command::OpenTasks);
assert!(draw(&app).contains("UNDO"));
app.reduce(Command::OpenTaskDetails);
assert_eq!(app.overlay, Overlay::TaskDetails);
let details = draw(&app);
assert!(details.contains("Task #1 · UNDO"));
assert!(details.contains("Source: local:/work/created.txt"));
assert!(details.contains("only complete success consumes the journal entry"));
app.reduce(Command::CloseOverlay);
app.reduce(Command::CloseOverlay);
app.reduce(Command::ToggleHelp);
app.reduce(Command::SelectHelpCategory(2));
let help = draw(&app);
assert!(help.contains("u / Ctrl-z"));
assert!(help.contains("Review/undo latest undoable action"));
for expected in [
"[Files]",
"Workspace: Create file",
"A / F7",
"r / F2",
"Trash selection (restorable)",
"D / F8",
"NO UNDO",
"e / F4",
] {
assert!(help.contains(expected), "missing help entry {expected:?}");
}
}
#[test]
fn mixed_task_history_keeps_copy_bytes_percent_and_elapsed() {
let mut app = AppState::new(Location::local("/work"));
let copy = OperationPlan::new(
PlanId::new(1),
PlanGeneration::new(1),
OperationKind::Copy,
vec![SourceItem::new(
ItemId::new(1),
Location::local("/source/a"),
)],
Location::local("/destination"),
CollisionPolicy::Ask,
Some(1),
Some(100),
BTreeMap::new(),
)
.unwrap();
let task = app.task_manager.start(copy);
let start = app.task_manager.active_operation().unwrap();
for kind in [
OperationEventKind::ItemStarted(ItemId::new(1)),
OperationEventKind::Progress(ProgressUpdate::new(
1,
10,
Some(ItemId::new(1)),
std::time::Duration::from_secs(65),
)),
] {
app.task_manager
.apply_event(OperationEvent::new(start.operation_id, start.plan_id, kind));
}
let create = WorkPlan::new(
PlanId::new(2),
PlanGeneration::new(2),
WorkKind::Create,
vec![SourceItem::new(
ItemId::new(1),
Location::local("/work/new"),
)],
Location::local("/work/new"),
CollisionPolicy::Skip,
Some(1),
None,
BTreeMap::new(),
)
.unwrap();
app.task_manager.start_work(create);
assert_eq!(
app.task_manager
.task(task)
.unwrap()
.progress()
.processed_bytes(),
10
);
app.reduce(Command::OpenTasks);
let output = draw(&app);
assert!(output.contains("COPY"));
assert!(output.contains("CREATE"));
assert!(output.contains("bytes 10/100"));
assert!(output.contains("10%"));
assert!(output.contains("00:01:05"));
}