Lines
98.76 %
Functions
100 %
Branches
//! Down on the last loaded row and Up on the first continue into the neighbouring server page.
use super::overlap::{FRONT_AT, REAR_AT, place_window, wheel_down};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::hits::{Surface, Target};
use crate::route::{Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::tabs::list::ListTab;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::redraw;
use crate::test_support::{app, emacs_app, key, press, vim_app};
use crate::view::{Tab, ViewId};
use ratatui::crossterm::event::{
KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};
fn page_wire(id: u64, ids: &[&str], has_more: bool) -> String {
let rows: String = ids
.iter()
.map(|i| format!(r#"(:ID "{i}" :DATE "2026-10-01T00:00:00Z" :DATE-TEXT "2026-10-01 00:00" :NOTE "n" :AMOUNT "5")"#))
.collect::<Vec<_>>()
.join(" ");
let more = if has_more { "t" } else { "nil" };
format!(r#"(:id {id} :value (:ROWS ({rows}) :HAS-MORE {more} :TOTAL 45))"#)
}
fn deliver(app: &mut App, wire: &str) {
let route = Route {
target: Some(app.window_of(ViewId::Transactions)),
ctx: RouteCtx::None,
app.deliver_reply(route, wire);
async fn loaded(mut app: App) -> (App, Sent) {
let sent = app.attach_echo();
app.switch_tab(Tab::Transactions);
deliver(&mut app, &page_wire(1, &["a", "b"], true));
(app, sent)
async fn requests(app: &mut App, sent: &Sent, offset: u32) -> usize {
tokio::task::yield_now().await;
app.drain_eval();
let needle = format!(r#":offset \"{offset}\""#);
sent.frames()
.filter(|line| line.contains(&needle))
.count()
fn selected_id(app: &App) -> Option<&str> {
app.transactions().selected_id()
async fn walk_both_ways(mut app: App, sent: Sent, down: KeyEvent, up: KeyEvent) {
press(&mut app, down);
assert_eq!(selected_id(&app), Some("b"));
assert!(matches!(app.transactions().state, Fetch::Loaded(_)));
assert!(matches!(app.transactions().state, Fetch::Loading { .. }));
assert_eq!(requests(&mut app, &sent, 20).await, 1);
assert_eq!(
requests(&mut app, &sent, 20).await,
1,
"no duplicate load in flight"
);
deliver(&mut app, &page_wire(2, &["c", "d"], true));
assert_eq!(selected_id(&app), Some("c"));
press(&mut app, up);
requests(&mut app, &sent, 0).await,
2,
"initial fetch and the way back"
deliver(&mut app, &page_wire(3, &["a", "b"], true));
#[tokio::test]
async fn cua_down_and_up_cross_the_page_edge() {
let (app, sent) = loaded(app()).await;
walk_both_ways(app, sent, key(KeyCode::Down), key(KeyCode::Up)).await;
async fn emacs_down_and_up_cross_the_page_edge() {
let (app, sent) = loaded(emacs_app()).await;
async fn vim_j_and_k_cross_the_page_edge() {
let (mut app, sent) = loaded(vim_app()).await;
press(&mut app, key(KeyCode::Esc));
walk_both_ways(app, sent, key(KeyCode::Char('j')), key(KeyCode::Char('k'))).await;
async fn the_first_row_overall_stays_put_on_up() {
let (mut app, _) = loaded(app()).await;
apply(&mut app, Intent::ListSelectPrev);
assert_eq!(selected_id(&app), Some("a"));
async fn the_last_row_overall_stays_put_on_down() {
let mut app = app();
app.attach_echo();
deliver(&mut app, &page_wire(1, &["a", "b"], false));
apply(&mut app, Intent::ListSelectNext);
fn mouse_at(app: &mut App, kind: MouseEventKind, target: Target) {
crate::test_support::redraw(app);
let area = crate::test_support::area_of(app, target);
let mouse = MouseEvent {
kind,
column: area.x,
row: area.y,
modifiers: KeyModifiers::NONE,
crate::mouse::handle_mouse(app, mouse, std::time::Instant::now());
async fn the_wheel_and_the_scrollbar_arrow_cross_the_page_edge() {
let (mut app, sent) = loaded(app()).await;
press(&mut app, key(KeyCode::Down));
mouse_at(&mut app, MouseEventKind::ScrollDown, Target::Row(0));
let down = Target::Scroll(Surface::List, tvui::BarPart::Down);
mouse_at(&mut app, MouseEventKind::Down(MouseButton::Left), down);
assert_eq!(requests(&mut app, &sent, 40).await, 1);
async fn the_wheel_over_a_rear_window_continues_that_windows_page() {
let Some(rear) = app.front_window() else {
panic!("a window");
app.open_view(ViewId::Transactions);
let Some(front) = app.front_window() else {
place_window(&mut app, rear, REAR_AT);
place_window(&mut app, front, FRONT_AT);
redraw(&mut app);
let at = {
let entries = app.hits.entries();
let found = entries.iter().find(|(_, t)| *t == Target::Row(0));
found.map_or((0, 0), |(rect, _)| (rect.x, rect.y))
wheel_down(&mut app, at, std::time::Instant::now());
let list = app.win::<ListTab>(rear);
assert!(list.is_some_and(|list| matches!(list.state, Fetch::Loading { .. })));
assert_eq!(app.front_window(), Some(front));
async fn an_empty_next_page_lands_on_the_last_row_of_the_last_one() {
deliver(&mut app, &page_wire(2, &[], false));
requests(&mut app, &sent, 40).await,
"the last page is fetched"
deliver(&mut app, &page_wire(3, &["x", "y"], false));
assert_eq!(selected_id(&app), Some("y"));
async fn marking_the_last_loaded_row_advances_into_the_next_page() {
apply(&mut app, Intent::ListMark);
assert_eq!(app.transactions().marked_ids(), vec!["b".to_string()]);
async fn marking_a_middle_row_only_moves_the_selection() {
async fn a_page_key_and_an_edge_move_share_one_load_in_flight() {
apply(&mut app, Intent::ListPageNext);
assert_eq!(requests(&mut app, &sent, 40).await, 0);