//! 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, }; use ratatui::buffer::Buffer; #[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", "F7", "F8", "p", "send chat", "/profile", "/pay", "/tip", "/wallet create", "/wallet import", "/file", "/backup", "/restore", "/wipe-all", "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 wallet_and_node_keys_are_listed_and_the_menu_key_is_not() { // F6 is the wallet and F7 is the node. There is no second menu, so F9 and // `/settings` must not be printed — a key that no longer exists is worse // than a missing one. let help = help_overlay_text(); assert!(help.contains("F6"), "F6 missing from help:\n{help}"); assert!(help.contains("F7"), "F7 missing from help:\n{help}"); assert!( !help.contains("F9"), "F9 is still a printed key:\n{help}" ); assert!( !help.contains("/settings"), "/settings is still a printed command:\n{help}" ); let hints = main_footer_hints(); assert!(hints.contains("F6"), "F6 missing from footer hints: {hints:?}"); assert!(hints.contains("F7"), "F7 missing from footer hints: {hints:?}"); assert!(!hints.contains("F9"), "F9 still in 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("F6"), "F6 dropped at {w}: {line:?}"); assert!(line.contains("F7"), "F7 dropped at {w}: {line:?}"); assert!(!line.contains("F9"), "F9 still rendered at {w}: {line:?}"); assert!(line.contains("? help"), "? help dropped at {w}: {line:?}"); assert!(line.contains("TOR"), "TOR dropped at {w}: {line:?}"); assert!( onionwire::tui::cell_width(&line) <= 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!( onionwire::tui::cell_width(&compact) <= 80, "{compact:?}" ); // The cells the onion gave up go to the wallet and node keys. assert!(compact.contains("F6"), "compact footer lost F6: {compact:?}"); assert!(compact.contains("F7"), "compact footer lost F7: {compact:?}"); assert!(!compact.contains("F9"), "compact footer still prints F9: {compact:?}"); } #[test] fn pane_footer_hints_name_the_wallet_and_the_node_not_a_menu() { let fp = "abcdef0123456789deadbeef"; for pane in [Pane::Roster, Pane::Chat, Pane::Composer] { let hints = pane_footer_hints(pane); assert!(hints.contains("F6"), "{pane:?} lost the wallet key: {hints:?}"); assert!(hints.contains("F7"), "{pane:?} lost the node key: {hints:?}"); assert!(!hints.contains("F9"), "{pane:?} still prints 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("F6"), "{pane:?} at {w} lost F6: {line:?}"); assert!(line.contains("F7"), "{pane:?} at {w} lost F7: {line:?}"); assert!(!line.contains("F9"), "{pane:?} at {w} still prints F9: {line:?}"); assert!( line.contains("? help"), "{pane:?} at {w} lost ? help: {line:?}" ); assert!( onionwire::tui::cell_width(&line) <= 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], &[9u8; 1184]).expect("encode"); assert!(payload.starts_with("onionwire:v2:")); 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:v2:"), "share screen missing invite prefix:\n{screen}" ); for ch in ['█', '▀', '▄'] { assert!( !screen.contains(ch), "share screen still has QR block {ch:?}:\\n{screen}" ); } } // --------------------------------------------------------------------------- // Mouse-copy: the invite must be selectable without chrome (card t_af8b509e) // // OnionWire never enables mouse capture (no `EnableMouseCapture` anywhere in // `src/`), so the terminal's own drag-select is what copies an invite. A drag // copies exactly the cells it crosses — including the `│` columns of any pane // that spans the payload rows. The payload therefore has to live in a region // no chrome crosses, and these tests reconstruct it out of the rendered frame // the same way the terminal hands it to the clipboard. // --------------------------------------------------------------------------- /// Every glyph a border can be drawn with, unicode chrome and `ASCII_BORDERS`. const BORDER_GLYPHS: [char; 6] = ['│', '|', '┃', '+', '-', '┄']; fn share_frame(width: u16, height: u16, payload: &str) -> Buffer { use ratatui::Terminal; use ratatui::backend::TestBackend; let backend = TestBackend::new(width, height); let mut terminal = Terminal::new(backend).expect("terminal"); terminal.draw(|f| draw_share(f, payload)).expect("draw"); terminal.backend().buffer().clone() } fn frame_row(buf: &Buffer, width: u16, y: u16) -> String { (0..width).map(|x| buf[(x, y)].symbol()).collect() } fn frame_text(buf: &Buffer, width: u16, height: u16) -> String { (0..height) .map(|y| frame_row(buf, width, y)) .collect::>() .join("\n") } /// True when the row, read from column 0, is payload: the invite's own alphabet /// and nothing else. A row that starts with a border cell, a space, or any /// character the payload cannot contain is not a payload row — it is payload /// with chrome in front of it, and a drag would copy that chrome. fn row_is_payload(row: &str) -> bool { let trimmed = row.trim_end(); !trimmed.is_empty() && trimmed .chars() .all(|c| c.is_ascii_alphanumeric() || matches!(c, ':' | '=' | '.')) } /// The same row classification, but tolerant of a gutter: used to find the rows /// that *carry* payload even when something else is drawn on them. fn row_carries_payload(row: &str) -> bool { row_is_payload(row.trim_matches([' ', '│', '|', '┃', '+', '-'])) } #[test] fn share_payload_rebuilds_from_the_frame_at_80x24_and_120x40() { let payload = qr::encode( &[7u8; 32], "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion", &[1u8; 32], &[9u8; 1184], ) .expect("encode"); for (w, h) in [(80u16, 24u16), (120, 40), (200, 60), (60, 20)] { let buf = share_frame(w, h, &payload); let rows: Vec = (0..h) .map(|y| frame_row(&buf, w, y)) .filter(|row| row_is_payload(row)) .collect(); assert!( !rows.is_empty(), "no chrome-free payload row at column 0 on the {w}x{h} share screen; \ a mouse drag across the invite would copy the pane borders:\n{}", frame_text(&buf, w, h) ); // Trailing cells are the frame's pad, not the payload; nothing else is // removed — no gutter is allowed to be there in the first place. let rebuilt: String = rows.iter().map(|r| r.trim_end()).collect(); // A v2 invite is ~1.9 KB. A short terminal cannot show all of it, so // the frame must either carry the whole payload in order or a faithful // prefix of it — never a reordered, interleaved or chrome-contaminated // one. `C`/OSC 52 copies the payload itself. assert!( payload.starts_with(&rebuilt) && !rebuilt.is_empty(), "mouse-selecting the invite at {w}x{h} does not rebuild a payload prefix:\n\ got {rebuilt:?}\nwant {payload:?}\nframe:\n{}", frame_text(&buf, w, h) ); if rebuilt.len() == payload.len() { assert_eq!(rebuilt, payload, "full payload rebuild differs"); } } } #[test] fn share_payload_rows_carry_no_border_glyph() { let payload = qr::encode( &[7u8; 32], "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion", &[1u8; 32], &[9u8; 1184], ) .expect("encode"); for (w, h) in [(80u16, 24u16), (120, 40), (200, 60), (60, 20)] { let buf = share_frame(w, h, &payload); let mut carrying = 0; for y in 0..h { let row = frame_row(&buf, w, y); if !row_carries_payload(&row) { continue; } carrying += 1; for glyph in BORDER_GLYPHS { assert!( !row.contains(glyph), "row {y} carries invite payload AND the border glyph {glyph:?} \ at {w}x{h}: {row:?}\nframe:\n{}", frame_text(&buf, w, h) ); } } assert!( carrying > 0, "no payload rows found at {w}x{h}:\n{}", frame_text(&buf, w, h) ); } } /// (b) the explicit affordance: `C` hands the payload to the terminal's own /// clipboard (OSC 52) and the screen names that key, so the escape sequence has /// to carry the invite byte for byte and nothing else. #[test] fn share_copy_key_sequence_carries_the_payload_exactly() { use onionwire::tui::osc52_copy_sequence; let payload = qr::encode( &[7u8; 32], "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion", &[1u8; 32], &[9u8; 1184], ) .expect("encode"); let seq = osc52_copy_sequence(&payload); assert!(seq.starts_with("\u{1b}]52;c;"), "{seq:?}"); assert!(seq.ends_with('\u{7}'), "{seq:?}"); let b64 = &seq["\u{1b}]52;c;".len()..seq.len() - 1]; assert_eq!(b64_decode(b64), payload.as_bytes()); for (w, h) in [(80u16, 24u16), (120, 40), (200, 60)] { let buf = share_frame(w, h, &payload); let screen = frame_text(&buf, w, h); assert!( screen.contains("press C") && screen.contains("copy"), "the {w}x{h} share screen does not name the copy key:\n{screen}" ); } } /// Minimal RFC 4648 decoder, so the copy path is checked end to end rather than /// against a hardcoded string. fn b64_decode(text: &str) -> Vec { const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let mut out = Vec::new(); let mut acc: u32 = 0; let mut bits = 0; for ch in text.chars().filter(|c| *c != '=') { let v = ALPHABET.find(ch).expect("base64 alphabet") as u32; acc = (acc << 6) | v; bits += 6; if bits >= 8 { bits -= 8; out.push((acc >> bits) as u8); } } out }