Lines
97.14 %
Functions
100 %
Branches
//! The windows live on the desktop: its order is the active view, the draw order and the lookup order.
use super::report_chart::{
EMPTY, NO_OVERRIDE, call_for, chart_requests, echoed, env, overrides, png_wire, rendered, show,
stripes, text, with_table,
};
use super::*;
use crate::focus::{FocusTarget, current_focus};
use crate::tabs::reports::{ReportKind, ReportsTab};
use crate::test_support::{key, press};
use forms::FormValues;
use ratatui::crossterm::event::KeyCode;
use ratatui::layout::Rect;
use rpc::chart::ChartBackend;
const AREA: Rect = Rect::new(0, 0, 80, 22);
#[test]
fn every_startup_window_is_on_the_desktop_zoomed_with_the_first_view_in_front() {
let app = crate::test_support::app();
let slots: Vec<_> = app.desktop.iter_back_to_front().collect();
assert_eq!(slots.len(), ViewId::ALL.len());
assert!(slots.iter().all(|slot| slot.zoomed));
assert_eq!(app.desktop.front(), Some(app.window_of(ViewId::ALL[0])));
assert_eq!(app.front_kind(), Some(ViewId::ALL[0]));
}
fn switching_a_tab_raises_its_window_on_the_desktop() {
let mut app = crate::test_support::app();
app.switch_tab(ViewId::Tags);
app.switch_tab(ViewId::Config);
let order: Vec<_> = app
.desktop
.iter_back_to_front()
.map(|slot| slot.id)
.collect();
let [.., tags, config] = order[..] else {
panic!("two windows or more");
assert_eq!(tags, app.window_of(ViewId::Tags));
assert_eq!(config, app.window_of(ViewId::Config));
assert_eq!(app.front_kind(), Some(ViewId::Config));
fn the_drawn_body_is_the_front_window_alone() {
for kind in [ViewId::Accounts, ViewId::Scripts, ViewId::Console] {
app.switch_tab(kind);
let drawn: Vec<_> = app
.visible_windows(AREA)
.into_iter()
.map(|(id, view, place)| (id, view.kind(), place.visible_in(AREA)))
assert_eq!(drawn, [(app.window_of(kind), kind, AREA)]);
fn an_unzoomed_window_shows_the_one_behind_it() {
let front = app.window_of(ViewId::Accounts);
app.desktop.toggle_zoom(front);
.iter()
.map(|(id, _, _)| *id)
assert_eq!(drawn.len(), 2);
assert_eq!(drawn.last(), Some(&front));
fn the_console_prompt_focus_follows_the_front_window() {
app.switch_tab(ViewId::Console);
app.set_console_focused(true);
assert!(app.console_input_focused());
assert_eq!(current_focus(&app), FocusTarget::ConsoleInput);
let accounts = app.window_of(ViewId::Accounts);
app.desktop.raise(accounts);
assert!(!app.console_input_focused());
assert_eq!(current_focus(&app), FocusTarget::ViewPane);
fn a_lookup_by_kind_walks_the_desktop_front_first() {
let first = app.window_of(ViewId::Console);
let second = app.open_new_window(View::Console(ConsoleState::new()));
assert_eq!(app.window_id(ViewId::Console), Some(second));
app.desktop.raise(first);
assert_eq!(app.window_id(ViewId::Console), Some(first));
assert_eq!(app.window_ids(ViewId::Console), [first, second]);
fn a_form_submitted_to_the_console_goes_to_the_front_console() {
app.submit_to_console("(x)".to_string());
let echoed = |id| {
app.win::<ConsoleState>(id)
.is_some_and(|console| console.scrollback.iter().any(|line| line == "> (x)"))
assert!(echoed(first));
assert!(!echoed(second));
/// Opens a second Reports window with a loaded table, on top.
fn open_loaded_reports(app: &mut App) -> WindowId {
let mut reports = ReportsTab::new();
reports.on_reply(EMPTY, ReportKind::Balance, FormValues::default());
app.open_new_window(View::Reports(reports))
#[tokio::test]
async fn two_windows_toggling_the_chart_both_settle() {
let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
let first = app.window_of(ViewId::Reports);
press(&mut app, key(KeyCode::Char('g')));
let second = open_loaded_reports(&mut app);
let [a, b] = overrides(&app)[..] else {
panic!("one override read per window");
app.route_reply(a, NO_OVERRIDE);
app.route_reply(b, NO_OVERRIDE);
let targets: Vec<_> = chart_requests(&app)
.filter_map(|id| app.pending_routes.get(id)?.target)
assert!(targets.contains(&first), "the first window kept Loading");
assert!(targets.contains(&second));
async fn every_reports_window_adopts_the_terminal() {
show(&mut app);
if let Some(reports) = app.win_mut::<ReportsTab>(second) {
reports.chart.toggle()
app.sync_terminal(env(30, 16, ChartBackend::Halfblocks));
assert_eq!(targets.iter().filter(|id| **id == first).count(), 2);
assert_eq!(targets.iter().filter(|id| **id == second).count(), 1);
async fn the_chart_is_sized_to_its_window_not_the_screen() {
let (mut app, sent) = with_table(80, 24, ChartBackend::Halfblocks);
app.toggle_zoom_of(ViewId::Reports);
let echoed = echoed(&mut app, &sent).await;
assert!(echoed.contains(&call_for(580, 280)), "{echoed}");
/// A Reports window behind an unzoomed Accounts one, with its chart ready.
fn rear_chart(backend: ChartBackend) -> App {
let (mut app, _) = with_table(80, 24, backend);
let [id] = chart_requests(&app)[..] else {
panic!("one chart request");
app.route_reply(id, &png_wire(id, stripes(780, 400)));
app.switch_tab(ViewId::Accounts);
app.toggle_zoom_of(ViewId::Accounts);
app
async fn a_halfblocks_chart_draws_in_a_rear_window() {
let app = rear_chart(ChartBackend::Halfblocks);
let terminal = rendered(&app, 80, 24);
assert_ne!(terminal.backend().buffer()[(70, 10)].symbol(), " ");
async fn a_kitty_chart_waits_until_its_window_is_in_front() {
let mut app = rear_chart(ChartBackend::Kitty);
assert!(!text(&rendered(&app, 80, 24)).contains("\x1b_G"));
app.switch_tab(ViewId::Reports);
assert!(text(&rendered(&app, 80, 24)).contains("\x1b_G"));