Lines
98.15 %
Functions
100 %
Branches
//! `Enter` on the Transactions tab opens a read-only detail window.
use super::edit::options_wire;
use crate::app::App;
use crate::route::RouteCtx;
use crate::tabs::fetch::Fetch;
use crate::tabs::list::ListTab;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use crate::view::{Tab, ViewId};
use crate::viewer::{Source, Viewer};
use cli_core::render::ListRow;
use ratatui::crossterm::event::KeyCode;
pub(super) const TX: &str = "550e8400-e29b-41d4-a716-446655440071";
const CASH: &str = "550e8400-e29b-41d4-a716-446655440072";
const USD: &str = "550e8400-e29b-41d4-a716-446655440073";
pub(super) fn started() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
id: Some(TX.to_string()),
cells: vec!["2024-06-01".to_string()],
}]);
app.switch_tab(Tab::Transactions);
(app, sent)
}
fn detail_request(app: &App) -> Option<(i64, u64)> {
app.pending_routes
.iter()
.find_map(|(id, route)| match route.ctx {
RouteCtx::TransactionDetail { seq, .. } => Some((*id, seq)),
_ => None,
})
pub(super) fn detail_text(tags: &str, splits: usize) -> String {
let split =
format!(r#"(:id "s" :account-id "{CASH}" :commodity-id "{USD}" :value 5 :tags ())"#);
let splits = vec![split; splits].join(" ");
format!(
r#"(:id "{TX}" :post-date "2024-06-01" :post-date-text "2024-06-01 00:00" :enter-date "x" :note "lunch" :splits ({splits}) :prices () :tags ({tags}))"#
)
fn quoted(text: &str) -> String {
format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
/// The combined reply: the options wires carry their list as a quoted string.
pub(super) fn reply_of(id: i64, detail: &str) -> String {
let value_of = |wire: String| {
wire.split_once(":value ")
.map(|(_, value)| value.trim_end_matches(')').to_string())
.unwrap_or_default()
};
let accounts = value_of(options_wire(&[(CASH, "Assets:Cash")]));
let commodities = value_of(options_wire(&[(USD, "USD")]));
"(:id {id} :value ({} {accounts} {commodities}))",
quoted(detail)
pub(super) fn open_detail(app: &mut App, detail: &str) {
press(app, key(KeyCode::Enter));
let (id, _) = detail_request(app).expect("a detail request");
app.route_reply(id, &reply_of(id, detail));
pub(super) fn front_viewer(app: &App) -> &Viewer {
match app.front::<Viewer>() {
Some(viewer) => viewer,
None => panic!("the front window is not a viewer"),
pub(super) fn detail_lines(app: &App, id: &str) -> Vec<String> {
let source = Source::Detail { id: id.to_string() };
let viewer = app
.viewer_of(&source)
.and_then(|window| app.win::<Viewer>(window));
viewer
.map(|viewer| viewer.lines.clone())
/// The request id of the detail request that carried `seq`; the test fails when there is none.
pub(super) fn request_of(app: &App, seq: u64) -> i64 {
let found = app.pending_routes.iter().find_map(|(id, route)| {
matches!(route.ctx, RouteCtx::TransactionDetail { seq: s, .. } if s == seq).then_some(*id)
});
match found {
Some(id) => id,
None => panic!("no detail request with seq {seq}"),
fn view_of(app: &App) -> usize {
front_viewer(app).scroll.rows()
fn scroll_of(app: &App) -> usize {
front_viewer(app).scroll.top()
#[tokio::test]
async fn enter_dispatches_the_detail_request_for_the_selected_id() {
let (mut app, sent) = started();
press(&mut app, key(KeyCode::Enter));
let (_, seq) = detail_request(&app).expect("a detail request");
assert_eq!(seq, 0, "a first open has no viewer to count it");
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(
echoed.contains(&format!(r#"(get-transaction-detail "{TX}")"#)),
"frame: {echoed}"
);
async fn the_reply_opens_the_window_with_labelled_splits_and_tags() {
let (mut app, _) = started();
open_detail(&mut app, &detail_text(r#"(:name "trip" :value "rome")"#, 1));
let text = detail_lines(&app, TX).join("\n");
for needle in [
"Date: 2024-06-01 00:00",
"Note: lunch",
"trip=rome",
"Assets:Cash 5.00 USD",
] {
assert!(text.contains(needle), "{needle} missing in\n{text}");
assert!(app.pending_routes.is_empty());
#[test]
fn enter_without_a_selection_reports_it() {
assert_eq!(app.status, "no transaction selected");
assert!(detail_request(&app).is_none());
async fn an_error_reply_shows_in_the_status_line() {
let (id, _) = detail_request(&app).expect("a detail request");
app.route_reply(
id,
&format!(r#"(:id {id} :error (:code command-failed :message "no such transaction"))"#),
assert!(app.status.starts_with("command error:"), "{}", app.status);
assert!(app.front::<Viewer>().is_none());
async fn a_stale_reply_is_dropped() {
open_detail(&mut app, &detail_text("", 1));
app.activate_window(app.window_of(ViewId::Transactions));
let first = request_of(&app, 1);
let second = request_of(&app, 2);
let before = detail_lines(&app, TX);
app.route_reply(first, &reply_of(first, &detail_text("", 3)));
assert_eq!(
detail_lines(&app, TX),
before,
"the superseded reply is dropped"
app.route_reply(second, &reply_of(second, &detail_text("", 3)));
assert_ne!(detail_lines(&app, TX), before);
fn enter_without_a_console_reports_it() {
assert_eq!(app.status, "console not connected");
async fn escape_and_q_close_the_window_and_enter_does_not() {
for (code, closes) in [
(KeyCode::Esc, true),
(KeyCode::Char('q'), true),
(KeyCode::Enter, false),
press(&mut app, key(code));
assert_eq!(app.front::<Viewer>().is_none(), closes, "{code:?}");
async fn the_window_scrolls_within_its_lines() {
open_detail(&mut app, &detail_text("", 70));
crate::test_support::draw_once(&app, 80, 24);
let view = view_of(&app);
assert!(view > 0);
press(&mut app, key(KeyCode::Down));
press(&mut app, key(KeyCode::PageDown));
assert_eq!(scroll_of(&app), 1 + view);
(0..4).for_each(|_| press(&mut app, key(KeyCode::PageDown)));
let last = scroll_of(&app);
let rows = detail_lines(&app, TX).len();
(rows - 1..=rows).contains(&(last + view)),
"{last} {view} {rows}"
press(&mut app, key(KeyCode::PageUp));
press(&mut app, key(KeyCode::Up));
assert_eq!(scroll_of(&app), last - view - 1);
async fn edit_inside_the_window_opens_the_edit_form_request_and_keeps_it() {
let detail = app.front_window();
press(&mut app, key(KeyCode::Char('e')));
assert_eq!(app.front_window(), detail, "the detail stays");
.values()
.any(|route| matches!(route.ctx, RouteCtx::TransactionEdit))
echoed.contains(&format!(r#"(load-form "transaction-edit" "{TX}")"#)),
async fn the_latest_detail_request_wins_across_windows() {
let first = app.window_of(ViewId::Transactions);
let second = app.open_extra(ViewId::Transactions);
if let Some(list) = app.win_mut::<ListTab>(second) {
list.state = Fetch::Loaded(vec![ListRow {
app.activate_window(first);
let older = request_of(&app, 1);
app.activate_window(second);
let newer = request_of(&app, 2);
app.route_reply(older, &reply_of(older, &detail_text("", 3)));
"the superseded detail applied"
app.route_reply(newer, &reply_of(newer, &detail_text("", 3)));