onionwire/tests/tui_chrome.rs
agent eefa7e5cc6
All checks were successful
ci / test (pull_request) Successful in 5m42s
ci / sdk (pull_request) Successful in 1m29s
ci / fuzz (pull_request) Successful in 4m41s
android-ci / android (pull_request) Successful in 10m45s
tui: clean-look chrome redesign (one accent, honest tor phase, real frames)
Applies the bitchat-tui clean-look principles to the OnionWire main screen.

- Hierarchy: header, roster + one primary chat pane, one status line, one
  footer hint line. The header gives up the accent (the accent is reserved
  for focus), so it stops competing with the panes.
- Tor phase is a first-class state: Node::tor_state() -> TorPhase (glyph +
  word, header and footer) so connecting / ready / degraded / off are never
  colour-only and never implied. No new data field.
- Footer changes with the focused pane (pane_footer_hints) and the phase; the
  main footer drops the redundant onion (the header already prints it) and
  uses those cells for hints. status_footer keeps its signature and strings.
- Empty roster / empty chat / connecting / degraded / off are designed states
  with their own copy, not blank panes.
- draw_main_chrome + MainView: the real main render path is public and
  Node-free, so tests/tui_frames.rs dumps the actual chrome at 80x24, 120x40
  and 200x60 (12 tests, 5 states, real Message/OutboxEntry rows) instead of a
  reconstructed stand-in.
- Widths in cells everywhere touched: status_footer*, alert_line,
  main_header_line*, draw_text_overlay, roster_row_text. The roster spends its
  budget name-first, so a 22-cell pane reads `bob-the-sta…  stale` instead of
  `…  bbbbbbbb… stale`.
- draw_text_overlay is centered with a one-cell side margin and clears
  underneath; never full-bleed.

Also fixes a pre-existing race in tests/tui_spec.rs (the ellipsis-form
assertion did not tolerate ONIONWIRE_ASCII being set by a parallel test).

Co-Authored-By: Hermes <[email protected]>
2026-09-18 23:18:24 -04:00

344 lines
12 KiB
Rust

//! Main-screen chrome helpers: ASCII wordmark, compact fallback, help text.
use onionwire::hs::TorState;
use onionwire::qr;
use onionwire::tui::{
Pane, TorPhase, banner_for_width, compact_banner, draw_share, help_overlay_text,
main_footer_hints, onion_glyph, pane_footer_hints, status_footer, status_footer_compact,
status_footer_phased, tor_token, wordmark_banner,
};
#[test]
fn wordmark_banner_is_nonempty_and_fits_80() {
let banner = wordmark_banner();
assert!(!banner.is_empty());
for line in banner.lines() {
assert!(
line.chars().count() <= 80,
"wordmark line exceeds 80 chars: {line:?} ({})",
line.chars().count()
);
}
}
#[test]
fn compact_banner_is_nonempty_and_fits_40() {
let compact = compact_banner();
assert!(!compact.is_empty());
assert!(
compact.chars().count() <= 40,
"compact banner exceeds 40 chars ({})",
compact.chars().count()
);
}
#[test]
fn onion_glyph_is_nonempty() {
assert!(!onion_glyph().is_empty());
}
#[test]
fn help_overlay_lists_core_bindings() {
let help = help_overlay_text();
assert!(!help.is_empty());
for needle in [
"Tab",
"F2",
"F3",
"F4",
"F5",
"F6",
"F8",
"F9",
"send chat",
"/profile",
"/pay",
"/tip",
"/wallet create",
"/wallet import",
"/file",
"/backup",
"/restore",
"/wipe-all",
"/settings",
"Ctrl-Q",
"?",
] {
assert!(help.contains(needle), "help overlay missing {needle:?}");
}
for line in help.lines() {
assert!(
line.chars().count() <= 80,
"help line exceeds 80 chars: {line:?}"
);
}
}
#[test]
fn banner_for_width_collapses_when_narrow() {
let full = wordmark_banner();
let max_line = full.lines().map(|l| l.chars().count()).max().unwrap();
assert_eq!(banner_for_width(max_line as u16), full);
assert_eq!(banner_for_width(max_line as u16 - 1), compact_banner());
assert_eq!(banner_for_width(80), full);
assert_eq!(banner_for_width(120), full);
}
#[test]
fn chrome_renders_at_80x24_and_120x40() {
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::layout::{Constraint, Layout};
use ratatui::widgets::Paragraph;
let fp = "abcdef0123456789";
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
for (w, h) in [(80u16, 24u16), (120, 40)] {
let backend = TestBackend::new(w, h);
let mut terminal = Terminal::new(backend).expect("terminal");
terminal
.draw(|f| {
let banner = banner_for_width(f.area().width);
let banner_h = banner.lines().count() as u16;
let chunks = Layout::vertical([
Constraint::Length(banner_h),
Constraint::Min(3),
Constraint::Length(1),
])
.split(f.area());
f.render_widget(Paragraph::new(banner), chunks[0]);
let footer = status_footer(f.area().width, fp, onion, None, main_footer_hints());
f.render_widget(Paragraph::new(footer), chunks[2]);
})
.expect("draw");
let buf = terminal.backend().buffer();
let row0: String = (0..w).map(|x| buf[(x, 0)].symbol().to_string()).collect();
assert!(
row0.contains("ONIONWIRE"),
"banner missing on row 0 at {w}x{h}: {row0:?}"
);
let footer: String = (0..w)
.map(|x| buf[(x, h - 1)].symbol().to_string())
.collect();
assert!(
footer.contains("? help"),
"footer missing ? help at {w}x{h}: {footer:?}"
);
assert!(
footer.contains("TOR"),
"footer missing TOR at {w}x{h}: {footer:?}"
);
}
}
#[test]
fn main_footer_keeps_help_at_80_and_120() {
let fp = "abcdef0123456789deadbeef";
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
let hints = main_footer_hints();
assert!(hints.contains("? help"));
for w in [80u16, 120] {
let line = status_footer(w, fp, onion, None, hints);
assert!(
line.chars().count() <= w as usize,
"footer wider than {w}: {} {line:?}",
line.chars().count()
);
assert!(
line.contains("? help"),
"footer missing ? help at {w}: {line:?}"
);
assert!(line.contains("TOR"), "footer missing TOR at {w}: {line:?}");
assert!(
line.contains("me:"),
"footer missing identity at {w}: {line:?}"
);
}
let crowded = status_footer(80, fp, onion, Some("rotated · notified 1/2 friends"), hints);
assert!(
crowded.chars().count() <= 80,
"crowded footer wider than 80: {} {crowded:?}",
crowded.chars().count()
);
assert!(
crowded.contains("? help"),
"status note crowded out ? help: {crowded:?}"
);
assert!(
crowded.contains("TOR"),
"crowded footer missing TOR: {crowded:?}"
);
}
#[test]
fn pane_focus_cycles_roster_chat_composer() {
assert_eq!(Pane::Roster.next(), Pane::Chat);
assert_eq!(Pane::Chat.next(), Pane::Composer);
assert_eq!(Pane::Composer.next(), Pane::Roster);
assert_eq!(Pane::Roster.prev(), Pane::Composer);
assert_eq!(Pane::Chat.prev(), Pane::Roster);
assert_eq!(Pane::Composer.prev(), Pane::Chat);
}
#[test]
fn menu_key_is_listed_in_help_and_in_the_main_footer() {
// Discoverability: the WALLETS & SETTINGS menu has to be findable from the
// help overlay *and* from the main footer, or F9 is a secret.
let help = help_overlay_text();
assert!(help.contains("F9"), "F9 missing from help:\n{help}");
assert!(
help.contains("/settings"),
"/settings missing from help:\n{help}"
);
let hints = main_footer_hints();
assert!(
hints.contains("F9"),
"F9 missing from footer hints: {hints:?}"
);
let fp = "abcdef0123456789deadbeef";
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
for w in [80u16, 120, 200] {
let line = status_footer(w, fp, onion, None, hints);
assert!(line.contains("F9"), "F9 dropped at {w}: {line:?}");
assert!(line.contains("? help"), "? help dropped at {w}: {line:?}");
assert!(line.contains("TOR"), "TOR dropped at {w}: {line:?}");
assert!(
line.chars().count() <= w as usize,
"footer wider than {w}: {line:?}"
);
}
}
#[test]
fn restore_terminal_is_callable_without_panic() {
onionwire::tui::restore_terminal();
}
#[test]
fn tor_phase_projects_every_tor_state() {
assert_eq!(TorPhase::of(TorState::Bootstrapping), TorPhase::Connecting);
assert_eq!(TorPhase::of(TorState::Published), TorPhase::Ready);
assert_eq!(
TorPhase::of(TorState::DegradedReachable),
TorPhase::Degraded
);
assert_eq!(TorPhase::of(TorState::Stopped), TorPhase::Off);
// The phase-free token is still the bare word the chrome tests lock.
assert_eq!(tor_token(None), "TOR");
}
#[test]
fn footer_states_the_tor_phase_with_a_word_not_only_colour() {
let fp = "abcdef0123456789deadbeef";
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
for (phase, word) in [
(TorPhase::Connecting, "connecting"),
(TorPhase::Ready, "ready"),
(TorPhase::Degraded, "degraded"),
(TorPhase::Off, "off"),
] {
assert!(!phase.glyph().is_empty(), "{phase:?} has no glyph");
for w in [80u16, 120, 200] {
let line = status_footer_phased(w, fp, onion, Some(phase), None, main_footer_hints());
assert!(
line.contains(word),
"{w} footer lacks phase word {word:?}: {line:?}"
);
assert!(line.contains("TOR"), "{w} footer lacks TOR: {line:?}");
assert!(line.contains("me:"), "{w} footer lacks me:: {line:?}");
assert!(
line.chars().count() <= w as usize,
"{w} footer wider than frame: {line:?}"
);
}
}
// A fail-closed note still wins over the hints, phase or not.
let crowded = status_footer_phased(
80,
fp,
onion,
Some(TorPhase::Off),
Some("send failed: peer onion down"),
main_footer_hints(),
);
assert!(crowded.contains("send failed"), "{crowded:?}");
assert!(crowded.contains("off"), "{crowded:?}");
assert!(crowded.contains("? help"), "{crowded:?}");
assert!(crowded.chars().count() <= 80, "{crowded:?}");
}
/// The main footer has its own variant: the header already prints the identity,
/// so the footer drops the onion and keeps `TOR` + `me:` + the hints.
#[test]
fn compact_footer_drops_the_onion_and_keeps_the_rest() {
let fp = "abcdef0123456789deadbeef";
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
let full = status_footer_phased(80, fp, onion, Some(TorPhase::Ready), None, main_footer_hints());
let compact = status_footer_compact(80, fp, Some(TorPhase::Ready), None, main_footer_hints());
assert!(full.contains("onion:"), "{full:?}");
assert!(!compact.contains("onion:"), "{compact:?}");
assert!(compact.contains("TOR"), "{compact:?}");
assert!(compact.contains("me:"), "{compact:?}");
assert!(compact.contains("? help"), "{compact:?}");
assert!(compact.chars().count() <= 80, "{compact:?}");
// The cells the onion gave up go to the hints.
assert!(
compact.chars().count() > 0 && compact.contains("F9"),
"compact footer lost the menu key: {compact:?}"
);
}
#[test]
fn pane_footer_hints_keep_the_menu_and_help_on_every_pane() {
let fp = "abcdef0123456789deadbeef";
for pane in [Pane::Roster, Pane::Chat, Pane::Composer] {
let hints = pane_footer_hints(pane);
assert!(hints.contains("F9"), "{pane:?} lost the menu key: {hints:?}");
assert!(hints.contains("? help"), "{pane:?} lost ? help: {hints:?}");
for w in [80u16, 120, 200] {
let line = status_footer_compact(w, fp, Some(TorPhase::Ready), None, hints);
assert!(line.contains("F9"), "{pane:?} at {w} lost F9: {line:?}");
assert!(
line.contains("? help"),
"{pane:?} at {w} lost ? help: {line:?}"
);
assert!(line.chars().count() <= w as usize, "{pane:?} at {w}: {line:?}");
}
}
// The hints actually change with the pane — that is the point of the lever.
assert_ne!(pane_footer_hints(Pane::Roster), pane_footer_hints(Pane::Composer));
assert_ne!(pane_footer_hints(Pane::Chat), pane_footer_hints(Pane::Composer));
}
#[test]
fn share_screen_shows_invite_not_qr_at_80x24() {
use ratatui::Terminal;
use ratatui::backend::TestBackend;
let sk = [7u8; 32];
let onion = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
let payload = qr::encode(&sk, onion, &[1u8; 32]).expect("encode");
assert!(payload.starts_with("onionwire:v1:"));
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).expect("terminal");
terminal.draw(|f| draw_share(f, &payload)).expect("draw");
let buf = terminal.backend().buffer();
let mut screen = String::new();
for y in 0..24u16 {
for x in 0..80u16 {
screen.push_str(buf[(x, y)].symbol());
}
screen.push('\n');
}
assert!(
screen.contains("onionwire:v1:"),
"share screen missing invite prefix:\n{screen}"
);
for ch in ['█', '▀', '▄'] {
assert!(
!screen.contains(ch),
"share screen still has QR block {ch:?}:\n{screen}"
);
}
}