Lines
99.31 %
Functions
100 %
Branches
//! An open detail follows its transaction: it refills in place, never raises, and goes with it.
use super::edit::{PAYLOAD, deliver_options, options_wire, wire_of};
use super::report_chart::env;
use super::transaction_detail::{
TX, detail_lines, detail_text, open_detail, reply_of, request_of, started,
};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::modal::Modal;
use crate::route::{FormOptionsSource, RouteCtx};
use crate::test_support::{draw_frame, flat, key, press};
use crate::view::ViewId;
use crate::viewer::{Source, Viewer};
use crate::window::WindowId;
use ratatui::crossterm::event::KeyCode;
use rpc::chart::ChartBackend;
const OTHER: &str = "other";
fn detail_of(id: &str) -> Source {
Source::Detail { id: id.to_string() }
}
fn detail_window(app: &App, id: &str) -> WindowId {
match app.viewer_of(&detail_of(id)) {
Some(window) => window,
None => panic!("no detail of {id}"),
fn mutation_request(app: &App) -> Option<i64> {
app.pending_routes
.iter()
.find_map(|(id, route)| matches!(route.ctx, RouteCtx::Mutation { .. }).then_some(*id))
fn open_edit_from_detail(app: &mut App) {
press(app, key(KeyCode::Char('e')));
let id = app
.pending_routes
.find_map(|(id, route)| matches!(route.ctx, RouteCtx::TransactionEdit).then_some(*id))
.expect("the edit request");
app.route_reply(id, &wire_of(PAYLOAD));
let accounts = options_wire(&[("a", "Assets:Cash")]);
deliver_options(app, FormOptionsSource::FormAccounts, &accounts);
#[tokio::test]
async fn a_saved_edit_refreshes_the_detail_in_place_without_raising_it() {
let (mut app, _) = started();
open_detail(&mut app, &detail_text("", 1));
let detail = detail_window(&app, TX);
open_edit_from_detail(&mut app);
assert_eq!(
app.overlays.origin().map(|origin| origin.window),
Some(detail)
);
apply(&mut app, Intent::Submit);
let save = mutation_request(&app).expect("the save request");
let accounts = app.open_extra(ViewId::Accounts);
app.route_reply(save, &format!(r#"(:id {save} :value "ok")"#));
let lists = app.window_ids(ViewId::Transactions);
assert!(
lists
.all(|list| app.win_by(*list).is_some_and(|win| win.view.loading()))
let refresh = request_of(&app, 1);
let before = detail_lines(&app, TX);
app.route_reply(refresh, &reply_of(refresh, &detail_text("", 3)));
assert_ne!(detail_lines(&app, TX), before);
app.front_window(),
Some(accounts),
"the refill raised the detail"
async fn a_detail_above_the_origin_of_a_form_leaves_the_dialog_working() {
let list = app.window_of(ViewId::Transactions);
app.deliver_transaction_edit_reply(Some(list), &wire_of(PAYLOAD));
deliver_options(&mut app, FormOptionsSource::FormAccounts, &accounts);
app.show_viewer(detail_of(TX), vec!["a".into()]);
assert_eq!(app.dialog_window(), Some(list));
assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
assert!(mutation_request(&app).is_some(), "{}", app.status);
async fn a_form_of_a_closed_detail_names_no_viewer() {
app.close_window(detail_window(&app, TX));
assert_eq!(app.dialog_window(), None);
assert_eq!(app.status, "the window was closed");
#[test]
fn a_pty_shrunk_below_a_fixed_viewer_still_draws_and_scrolls_to_the_end() {
app.sync_terminal(env(80, 24, ChartBackend::Halfblocks));
let text: Vec<String> = (0..30).map(|n| format!("line{n:02}")).collect();
app.show_viewer(detail_of(TX), text);
app.sync_terminal(env(40, 10, ChartBackend::Halfblocks));
let window = detail_window(&app, TX);
let rect = flat(
app.desktop
.place_of(window, app.desktop_rect())
.expect("open"),
draw_frame(&app, 40, 10);
press(&mut app, key(KeyCode::End));
let buffer = draw_frame(&app, 40, 10).0;
let row: String = (rect.x..rect.right())
.map(|x| buffer[(x, rect.bottom() - 2)].symbol())
.collect();
assert!(row.contains("line29"), "{row:?}");
async fn each_detail_awaits_only_its_own_requests() {
app.show_viewer(detail_of(OTHER), vec!["a".into()]);
app.refresh_details();
let [tx_request, other_request] = [TX, OTHER].map(|id| {
let found = app
.find_map(|(request, route)| match &route.ctx {
RouteCtx::TransactionDetail {
id: asked, seq: 1, ..
} if asked == id => Some(*request),
_ => None,
});
found.expect("a refresh of the detail")
app.route_reply(other_request, &reply_of(other_request, &detail_text("", 2)));
app.route_reply(tx_request, &reply_of(tx_request, &detail_text("", 3)));
let sizes = [TX, OTHER].map(|id| detail_lines(&app, id).len());
assert!(sizes[0] > sizes[1] && sizes[1] > 1, "{sizes:?}");
async fn a_refill_refits_the_fixed_window_to_its_lines() {
let lines: Vec<String> = (0..12).map(|n| format!("line {n}")).collect();
let wanted = tvui::viewer_size(&lines);
app.fill_viewer(window, lines);
let place = app.desktop.window(window).map(|slot| slot.place);
assert_eq!(place.map(|place| (place.width, place.height)), Some(wanted));
const MISSING: &str = r#"(nil "()" "()")"#;
async fn a_transaction_that_is_gone_closes_its_window_and_says_so() {
app.route_reply(refresh, &format!("(:id {refresh} :value {MISSING})"));
assert_eq!(app.windows_of(ViewId::Viewer), 0);
assert_eq!(app.status, format!("Transaction {TX} no longer exists"));
async fn enter_on_a_row_that_is_gone_opens_nothing_and_says_so() {
press(&mut app, key(KeyCode::Enter));
let request = request_of(&app, 0);
app.route_reply(request, &format!("(:id {request} :value {MISSING})"));
async fn enter_on_an_open_detail_raises_it_and_a_refresh_in_flight_keeps_it_there() {
app.activate_window(app.window_of(ViewId::Transactions));
assert_eq!(app.front_window(), Some(detail), "raised at once");
let refresh = request_of(&app, 2);
assert_eq!(app.front_window(), Some(detail));
async fn a_refresh_reply_for_a_closed_detail_opens_nothing() {
app.route_reply(refresh, &reply_of(refresh, &detail_text("", 1)));
assert!(app.front::<Viewer>().is_none());
fn two_rows(app: &mut App) {
let row = |id: &str| cli_core::render::ListRow {
id: Some(id.to_string()),
cells: vec!["2024-06-01".to_string()],
app.transactions_mut().state = crate::tabs::fetch::Fetch::Loaded(vec![row(TX), row(OTHER)]);
fn detail_requests_of(app: &App, wanted: &str) -> usize {
let asked = app.pending_routes.values();
asked
.filter(
|route| matches!(&route.ctx, RouteCtx::TransactionDetail { id, .. } if id == wanted),
)
.count()
async fn a_bulk_delete_closes_the_deleted_details_and_keeps_its_summary() {
two_rows(&mut app);
press(&mut app, key(KeyCode::Char('m')));
press(&mut app, key(KeyCode::Char('x')));
apply(&mut app, Intent::ConfirmYes);
let mut routes: Vec<(i64, String)> = app
.filter_map(|(request, route)| match &route.ctx {
RouteCtx::BulkDelete { id, .. } => Some((*request, id.clone())),
})
routes.sort_by(|a, b| a.1.cmp(&b.1));
for (request, id) in routes {
let reply = match id == TX {
true => format!("(:id {request} :value t)"),
false => format!(r#"(:id {request} :error (:code busy :message "busy"))"#),
app.route_reply(request, &reply);
app.viewer_of(&detail_of(TX)).is_none(),
"the deleted detail stays"
assert!(app.viewer_of(&detail_of(OTHER)).is_some());
app.status
.starts_with("deleted 1 of 2 transactions; failed: "),
"{}",
assert_eq!(detail_requests_of(&app, TX), 0);
async fn a_single_delete_closes_its_detail_and_keeps_the_notice() {
app.pending_routes.insert(
7,
crate::route::Route {
target: Some(list),
ctx: RouteCtx::DeleteTransaction { id: TX.to_string() },
},
app.route_reply(7, r#"(:id 7 :value t :notices ("balance moved"))"#);
assert_eq!(app.status, "balance moved");
async fn an_error_reply_to_a_refresh_keeps_the_window_and_its_lines() {
app.route_reply(
refresh,
&format!(r#"(:id {refresh} :error (:code db :message "boom"))"#),
assert_eq!(detail_lines(&app, TX), before);
assert!(app.status.starts_with("command error:"), "{}", app.status);
async fn two_enters_before_the_first_reply_leave_one_window() {
let requests: Vec<i64> = app.pending_routes.keys().copied().collect();
for request in requests {
app.route_reply(request, &reply_of(request, &detail_text("", 1)));
assert_eq!(app.windows_of(ViewId::Viewer), 1);
fn place_detail(app: &mut App, window: WindowId, rect: ratatui::layout::Rect) {
let slot = app.desktop.window(window).expect("it is open");
app.desktop.restore(tvui::Window {
place: tvui::Place::from(rect),
..slot
fn drawn(app: &App, window: WindowId) -> tvui::Place {
place.expect("it is open")
async fn a_refill_of_the_same_size_keeps_the_position_the_user_gave() {
let area = app.desktop_rect();
let size = tvui::viewer_size(&["a"]);
let off = ratatui::layout::Rect::new(area.right() - 3, area.y + 1, size.0, size.1);
place_detail(&mut app, window, off);
app.fill_viewer(window, vec!["b".into()]);
assert_eq!(drawn(&app, window), tvui::Place::from(off));
async fn a_refill_that_grows_at_the_desktop_edge_stays_inside() {
let small = tvui::viewer_size(&["a"]);
let edge = ratatui::layout::Rect::new(
area.right() - small.0,
area.bottom() - small.1,
small.0,
small.1,
place_detail(&mut app, window, edge);
app.fill_viewer(
window,
(0..8).map(|n| format!("{n}{}", "x".repeat(30))).collect(),
let place = drawn(&app, window);
place.x + i32::from(place.width) <= i32::from(area.right()),
"{place:?}"
place.y + i32::from(place.height) <= i32::from(area.bottom()),