From b8fefe9c18e7205b48ef8631d13d56b17b53c69f Mon Sep 17 00:00:00 2001 From: agent Date: Sat, 19 Sep 2026 00:31:42 -0400 Subject: [PATCH] tui: F9 works on the peer-payments screen, and frames dump the real footer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1 on the F9 `payments` row found two false affordances. 1. `footer_hints(Screen::PeerPayments)` prints `F9 menu`, and docs/TUI.md says F9 reopens the menu, but the `KeyCode::F(9)` arm opened it only from `Screen::Main`, so pressing F9 on the peer history did nothing. The function keys are now routed through one table, `function_key_for(screen, code)`, and the F9 arm no longer has the Main-only guard: it opens (or reopens) the menu from `Main`, `WalletsSettings` and `PeerPayments`. A screen that names a key has to answer it while it is open — that is what the table encodes, and `mod function_key_tests` locks it, including a test that reads the screen's own hint and asserts every `F` it names is answered there. 2. The PR's frame dumps passed `"Esc back up/down select ? help"` to `status_footer` by hand, so every dumped frame showed a footer the app never renders. `peer_payments_footer_hint()` is now the single source both `footer_hints` and the fixture read, and `peer_payments_frame_shows_the_shipped_footer` pins the dumped frame's last line to the shipped one. Also restores the rustfmt-only reflow of unrelated tests in `tests/tui_spec.rs` (`queued_row`, a `failed_row` call, `rgb_tokens_…`, `alert_line_…`), keeping this card's real edits there. Presentation only: no wire, store, session, send-path or `Node` change. --- docs/TUI.md | 6 ++ src/tui.rs | 189 ++++++++++++++++++++++++++++++++++-------- tests/tui_payments.rs | 19 ++++- tests/tui_spec.rs | 25 ++---- 4 files changed, 185 insertions(+), 54 deletions(-) diff --git a/docs/TUI.md b/docs/TUI.md index 943512d..83986ef 100644 --- a/docs/TUI.md +++ b/docs/TUI.md @@ -749,6 +749,12 @@ dumps the screen at 80x24, 120x40 and 200x60, populated and empty. - The focused row is marked with `>` **and** bold, so focus survives a monochrome terminal. `up`/`down` (or `k`/`j`) move it, `Esc` returns to the main screen, `F9` reopens the menu. +- `F9` is honoured **while this screen is open**, not only from the main screen: + the function-key table (`function_key_for`) answers it from `Main`, + `WalletsSettings` and `PeerPayments` alike, so the hint the screen prints + cannot be a no-op there. `peer_payments_footer_hint()` is the single place that + hint string is written, and the frame-dump fixture is driven from it — a dumped + frame cannot show a footer the app does not render. - The selected row's `tx` and `addr` are spelled out under the rows: a 64-hex txid is unusable truncated, and a 95-character base58 address is **wrapped** over two lines rather than clipped to a prefix. diff --git a/src/tui.rs b/src/tui.rs index 38f868a..6812a71 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1257,6 +1257,44 @@ enum WalletSetup { ImportPass { seed: Zeroizing, entry: PassphraseEntry }, } +/// A function key the TUI answers before the per-screen `match`; the screen's +/// own key handler never sees it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum FunctionKey { + Share, + Paste, + Rotate, + Who, + Wallet, + RotatePrekey, + OpenMenu, +} + +/// The function key `screen` answers, or `None` when the key belongs to the +/// per-screen handler. +/// +/// The function keys stay live on every screen that names one — in its footer +/// or in its body: a shortcut the user can read has to work while that screen +/// is open. +fn function_key_for(screen: &Screen, code: KeyCode) -> Option { + if !matches!( + screen, + Screen::Main | Screen::WalletsSettings { .. } | Screen::PeerPayments { .. } + ) { + return None; + } + match code { + KeyCode::F(2) => Some(FunctionKey::Share), + KeyCode::F(3) => Some(FunctionKey::Paste), + KeyCode::F(4) => Some(FunctionKey::Rotate), + KeyCode::F(5) => Some(FunctionKey::Who), + KeyCode::F(6) => Some(FunctionKey::Wallet), + KeyCode::F(8) => Some(FunctionKey::RotatePrekey), + KeyCode::F(9) => Some(FunctionKey::OpenMenu), + _ => None, + } +} + impl App { fn new(node: Arc, rt: tokio::runtime::Handle) -> Result { let friends = node.list_friends()?; @@ -1345,52 +1383,36 @@ impl App { let mut payments_move = 0i32; // The function-key shortcuts stay live on the WALLETS & SETTINGS screen - // too: that screen's whole point is naming the key that fires each row, - // so a key it names has to work while it is open. - if matches!( - self.screen, - Screen::Main | Screen::WalletsSettings { .. } | Screen::PeerPayments { .. } - ) { - match key.code { - KeyCode::F(2) => { - self.open_share()?; - return Ok(None); - } - KeyCode::F(3) => { + // and the peer history too: the settings screen's whole point is naming + // the key that fires each row, and the history's footer names `F9 + // menu`, so a key a screen names has to work while it is open. + if let Some(fk) = function_key_for(&self.screen, key.code) { + match fk { + FunctionKey::Share => self.open_share()?, + FunctionKey::Paste => { self.screen = Screen::Paste { buf: String::new(), err: None, }; - return Ok(None); } - KeyCode::F(4) => { + FunctionKey::Rotate => { self.screen = Screen::Rotate { prompt: RotatePrompt::new(), }; - return Ok(None); } - KeyCode::F(5) => { - self.who_open = true; - return Ok(None); - } - KeyCode::F(6) => { - self.open_wallet()?; - return Ok(None); - } - KeyCode::F(8) => { + FunctionKey::Who => self.who_open = true, + FunctionKey::Wallet => self.open_wallet()?, + FunctionKey::RotatePrekey => { self.screen = Screen::RotatePrekey { prompt: RotatePrompt::new(), }; - return Ok(None); } - KeyCode::F(9) => { - if matches!(self.screen, Screen::Main) { - self.open_wallets_settings()?; - } - return Ok(None); - } - _ => {} + // One level above the screen it is pressed on: F9 from Main + // opens the menu, and from the menu or the peer history it + // reopens it rather than silently doing nothing. + FunctionKey::OpenMenu => self.open_wallets_settings()?, } + return Ok(None); } match &mut self.screen { @@ -2803,11 +2825,20 @@ match screen { Screen::WalletSetup(WalletSetup::CreateSeed { .. }) => "type SEEN Esc cancel ? help", Screen::WalletSetup(WalletSetup::ImportSeed { .. }) => "paste seed Enter Esc ? help", Screen::WalletsSettings { .. } => "Esc back c copy ? help", - Screen::PeerPayments { .. } => "Esc back up/down select F9 menu ? help", + Screen::PeerPayments { .. } => peer_payments_footer_hint(), Screen::Quit { .. } => "CLEAR or QUIT then Enter Esc cancel ? help", } } +/// The hint line the peer-payment-history screen prints. One source only: the +/// screen's frame tests build their fixture from this, so a dumped frame can +/// never show a footer the app does not render. +pub fn peer_payments_footer_hint() -> &'static str { + PEER_PAYMENTS_FOOTER_HINT +} + +const PEER_PAYMENTS_FOOTER_HINT: &str = "Esc back up/down select F9 menu ? help"; + fn apply_chrome_borders(block: Block<'_>) -> Block<'_> { let block = block.borders(Borders::ALL); if ascii_chrome() { @@ -4105,6 +4136,98 @@ fn io_err(e: io::Error) -> String { e.to_string() } +#[cfg(test)] +mod function_key_tests { + use super::*; + + fn peer_payments_screen() -> Screen { + Screen::PeerPayments { + state: PeerPaymentsState::default(), + } + } + + /// The peer history's footer prints `F9 menu`, so F9 has to reach the menu + /// while that screen is open — not only from Main. + #[test] + fn f9_opens_the_menu_from_the_peer_payments_screen() { + assert_eq!( + function_key_for(&peer_payments_screen(), KeyCode::F(9)), + Some(FunctionKey::OpenMenu), + "the screen's footer names F9 but F9 does nothing there" + ); + } + + /// Copy and behaviour agree: every `F` the screen's own hint names must + /// be a key the screen answers. This is the test the defect class needs — + /// the hint string and the routing table are the two halves of the claim. + #[test] + fn every_function_key_named_by_the_peer_payments_hint_is_answered_there() { + let hint = peer_payments_footer_hint(); + let screen = peer_payments_screen(); + let mut named = 0; + for (at, _) in hint.match_indices('F') { + let digits: String = hint[at + 1..] + .chars() + .take_while(char::is_ascii_digit) + .collect(); + let Ok(number) = digits.parse::() else { + continue; + }; + named += 1; + assert!( + function_key_for(&screen, KeyCode::F(number)).is_some(), + "hint names F{number} ({hint:?}) but the screen does not answer it" + ); + } + assert!(named > 0, "the hint names no function key: {hint:?}"); + } + + /// The menu screen names `c` and `Esc`, not the function keys, but it is + /// the screen the `payments` row came from — its own keys stay its own. + #[test] + fn function_keys_stay_live_on_the_wallets_settings_screen() { + let screen = Screen::WalletsSettings { + state: WalletMenuState::default(), + }; + for key in [ + KeyCode::F(2), + KeyCode::F(3), + KeyCode::F(4), + KeyCode::F(5), + KeyCode::F(6), + KeyCode::F(8), + KeyCode::F(9), + ] { + assert!( + function_key_for(&screen, key).is_some(), + "{key:?} is named on the menu but is not answered there" + ); + } + } + + /// A screen that names no function key keeps the key to itself: the + /// composer's `F`-less screens must not steal F-keys they never advertise. + #[test] + fn screens_that_name_no_function_key_do_not_answer_one() { + for screen in [ + Screen::Paste { + buf: String::new(), + err: None, + }, + Screen::WalletSetup(WalletSetup::ImportSeed { + buf: Zeroizing::new(String::new()), + }), + ] { + for key in [KeyCode::F(9), KeyCode::F(6)] { + assert!( + function_key_for(&screen, key).is_none(), + "{key:?} was answered on a screen that never names it" + ); + } + } + } +} + #[cfg(test)] mod passphrase_buffer_tests { use super::*; diff --git a/tests/tui_payments.rs b/tests/tui_payments.rs index c0e43ef..9b59667 100644 --- a/tests/tui_payments.rs +++ b/tests/tui_payments.rs @@ -12,7 +12,8 @@ use onionwire::Payment; use onionwire::tui::{ PeerPaymentRow, PeerPaymentsState, WalletMenuState, cell_width, draw_peer_payments, - peer_payments_lines, peer_payments_text, status_footer, wallets_settings_menu_text, + peer_payments_footer_hint, peer_payments_lines, peer_payments_text, status_footer, + wallets_settings_menu_text, }; use ratatui::Terminal; use ratatui::backend::TestBackend; @@ -59,7 +60,9 @@ fn peer_state() -> PeerPaymentsState { fn render(w: u16, h: u16, st: &PeerPaymentsState) -> String { let backend = TestBackend::new(w, h); let mut terminal = Terminal::new(backend).expect("terminal"); - let footer = status_footer(w, FP, ONION, None, "Esc back up/down select ? help"); + // The same two calls the app makes, so a dumped frame can never show a + // footer (or a hint) the app does not render. + let footer = status_footer(w, FP, ONION, None, peer_payments_footer_hint()); terminal .draw(|f| draw_peer_payments(f, st, &footer)) .expect("draw"); @@ -74,6 +77,18 @@ fn render(w: u16, h: u16, st: &PeerPaymentsState) -> String { screen } +#[test] +fn peer_payments_frame_shows_the_shipped_footer() { + let shipped = status_footer(80, FP, ONION, None, peer_payments_footer_hint()); + let frame = render(80, 24, &peer_state()); + let last = frame.lines().nth(23).unwrap_or_default(); + assert_eq!( + last.trim_end(), + shipped.trim_end(), + "the dumped frame's footer is not the one the app renders" + ); +} + #[test] fn peer_payments_lists_every_required_field() { let body = peer_payments_text(78, 21, &peer_state()); diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs index 05cc668..d6ab135 100644 --- a/tests/tui_spec.rs +++ b/tests/tui_spec.rs @@ -429,14 +429,7 @@ fn pending_row( } fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { - pending_row( - message_id, - body, - OutboxStatus::Queued, - attempts, - next_in, - now, - ) + pending_row(message_id, body, OutboxStatus::Queued, attempts, next_in, now) } fn sending_row(message_id: i64, body: &str, now: i64) -> OutboxEntry { @@ -562,12 +555,7 @@ fn delivery_markers_have_exercised_ascii_fallbacks() { in_msg(5, "epsilon"), ]; let pending = vec![ - failed_row( - 1, - "alpha", - "connect peer.onion:80: connection timed out", - now, - ), + failed_row(1, "alpha", "connect peer.onion:80: connection timed out", now), queued_row(2, "beta", 1, 30, now), sending_row(3, "gamma", now), ]; @@ -853,7 +841,9 @@ fn rgb_tokens_resolve_to_palette_fg() { assert_eq!(t.highlight.fg, Some(Color::Rgb(232, 165, 75))); assert_eq!(t.highlight.bg, Some(Color::Rgb(48, 42, 28))); assert!( - t.highlight.add_modifier.contains(Modifier::BOLD), + t.highlight + .add_modifier + .contains(Modifier::BOLD), "RGB highlight must be bold" ); } @@ -916,10 +906,7 @@ fn alert_line_prefixes_when_no_color() { let _guard = std::env::var_os("NO_COLOR"); unsafe { std::env::set_var("NO_COLOR", "1") }; let line = alert_line("peer down", 80); - assert!( - line.starts_with("! "), - "expected '! ' prefix in None mode: {line:?}" - ); + assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}"); unsafe { match _guard { Some(v) => std::env::set_var("NO_COLOR", v),