Lines
100 %
Functions
Branches
//! The viewer windows: fixed, one per source, and never above a dialog.
use super::report_chart::env;
use super::transaction_detail::{TX, detail_text, front_viewer, open_detail, reply_of, started};
use crate::app::{App, MenuId};
use crate::hits::Target;
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::test_support::{draw_frame, flat, key, press, redraw};
use crate::view::ViewId;
use crate::viewer::Source;
use crate::window::WindowId;
use ratatui::crossterm::event::KeyCode;
use ratatui::layout::Position;
use rpc::chart::ChartBackend;
use tvui::Caps;
fn detail_of(id: &str) -> Source {
Source::Detail { id: id.to_string() }
}
fn lines(count: usize) -> Vec<String> {
(0..count).map(|n| format!("line {n}")).collect()
fn on_80x24(app: &mut App) {
app.terminal.replace(env(80, 24, ChartBackend::Halfblocks));
/// The targets recorded on the cells of window `id`, bottom to top as the mouse finds them.
fn targets_of(app: &App, id: WindowId) -> Vec<Target> {
let place = app.desktop.place_of(id, app.desktop_rect());
let rect = flat(place.expect("the window is open"));
let cells = (rect.y..rect.bottom()).flat_map(|y| (rect.x..rect.right()).map(move |x| (x, y)));
cells
.filter_map(|(x, y)| app.hits.hit_at(Position::new(x, y)))
.map(|(_, target, _)| target)
.collect()
#[tokio::test]
async fn a_detail_window_is_fixed_with_no_zoom_icon_or_grow_corner() {
let (mut app, _) = started();
on_80x24(&mut app);
open_detail(&mut app, &detail_text("", 1));
redraw(&mut app);
let id = app.front_window().expect("the detail is in front");
assert_eq!(
app.desktop.window(id).map(|slot| slot.caps),
Some(Caps::FIXED)
);
let targets = targets_of(&app, id);
assert!(targets.contains(&Target::Close));
assert!(!targets.contains(&Target::Zoom), "{targets:?}");
assert!(!targets.contains(&Target::Grow), "{targets:?}");
async fn a_refused_zoom_changes_and_loads_nothing() {
app.close_all_windows();
app.open_extra(ViewId::Accounts);
app.show_viewer(detail_of(TX), lines(3));
let (id, requests) = (app.front_window(), app.pending_routes.len());
app.toggle_zoom();
assert_eq!(app.front_window(), id);
assert!(
app.desktop
.window(id.expect("a front window"))
.is_some_and(|slot| !slot.zoomed)
app.pending_routes.len(),
requests,
"the window behind loaded"
async fn a_second_detail_of_one_id_raises_the_open_one() {
let detail = app.front_window();
app.show_viewer(detail_of(TX), lines(5));
assert_eq!(app.windows_of(ViewId::Viewer), 1);
assert_eq!(app.front_window(), detail);
assert_eq!(front_viewer(&app).lines.len(), 5);
async fn a_detail_of_another_id_opens_a_second_window() {
app.show_viewer(detail_of("other"), lines(3));
assert_eq!(app.windows_of(ViewId::Viewer), 2);
let labels: Vec<String> = app
.window_entries()
.into_iter()
.map(|(_, label)| label)
.collect();
for id in [TX, "other"] {
assert!(labels.contains(&format!("Transaction {id}")), "{labels:?}");
async fn a_detail_asked_for_under_a_dialog_opens_behind_it() {
press(&mut app, key(KeyCode::Enter));
let id = app
.pending_routes
.keys()
.copied()
.max()
.expect("a detail request");
crate::test_support::open_overlay(&mut app);
app.route_reply(id, &reply_of(id, &detail_text("", 1)));
assert!(matches!(app.overlays.top(), Some(Modal::Confirm { .. })));
assert!(app.status.is_empty(), "{}", app.status);
async fn the_edit_of_a_detail_belongs_to_the_detail_window() {
press(&mut app, key(KeyCode::Char('e')));
let route = app
.values()
.find(|route| matches!(route.ctx, RouteCtx::TransactionEdit));
assert_eq!(route.and_then(|route| route.target), app.front_window());
#[test]
fn the_size_of_a_wide_line_is_not_capped() {
assert_eq!(tvui::viewer_size(&["x".repeat(500)]).0, 502);
fn row_text(buffer: &ratatui::buffer::Buffer, rect: ratatui::layout::Rect, y: u16) -> String {
(rect.x..rect.right())
.map(|x| buffer[(x, y)].symbol())
fn a_multi_line_note_draws_on_separate_rows_and_end_reaches_the_last_row() {
let mut text: Vec<String> = (0..30).map(|n| format!("line{n:02}")).collect();
text[0] = "Note: a\nb\nc".to_string();
app.show_viewer(detail_of(TX), text);
let rect = flat(app.desktop.place_of(id, app.desktop_rect()).expect("open"));
let buffer = draw_frame(&app, 80, 24).0;
assert!(row_text(&buffer, rect, rect.y + 1).contains("Note: a"));
assert!(row_text(&buffer, rect, rect.y + 2).contains('b'));
assert!(!row_text(&buffer, rect, rect.y + 2).contains("Note"));
press(&mut app, key(KeyCode::End));
assert!(row_text(&buffer, rect, rect.bottom() - 2).contains("line29"));
fn the_view_menu_opens_on_the_first_entry_over_a_viewer() {
app.open_menu();
assert_eq!(app.menu_item(), Some(0));
assert_eq!(app.menu_title(), Some(MenuId::View));
fn a_new_window_over_a_fixed_viewer_follows_the_zoom_of_the_window_behind() {
let id = app.open_extra(ViewId::Accounts);
assert!(app.desktop.window(id).is_some_and(|slot| slot.zoomed));
async fn a_short_detail_is_a_small_window_on_a_big_screen() {
app.terminal.replace(env(200, 50, ChartBackend::Halfblocks));
app.show_viewer(detail_of(TX), lines(4));
let width = place.expect("the window is open").width;
assert!(width < 60, "{width}");