//! Visual audit dumps of the current OnionWire TUI. //! //! The main screen is the **real** render path: `draw_main_chrome` takes a //! `MainView` of plain data, so this test draws exactly what `App::draw_main` //! draws (no stand-in layout constants, no stub widget). Screens that are still //! private (`draw_paste`, `draw_rotate`, …) are labelled RECONSTRUCTED and copy //! the layout constants and copy from `src/tui.rs`. //! //! Print frames: //! cargo test --locked --test tui_frames -- --nocapture dump_audit_frames //! //! Widths are asserted in terminal **cells** via `onionwire::tui::cell_width` //! (`Span::width`), never `chars().count()`. use onionwire::outbox::{OutboxEntry, OutboxStatus}; use onionwire::qr; use onionwire::tui::{ ChatRowStyle, FriendRow, MainView, Pane, TorPhase, backup_screen_text, banner_for_width, cell_width, chat_no_friends_lines, chat_with_delivery, compact_banner, draw_main_chrome, draw_share, draw_text_overlay, fingerprint_mismatch_banner, help_overlay_text, main_footer_hints, onion_glyph, quit_screen_text, restore_screen_text, rotate_screen_text, roster_line, status_footer, who_overlay_text, wipe_screen_text, wordmark_banner, ProfileEditor, WipeKind, }; use onionwire::{FriendProfile, Message}; use ratatui::Frame; use ratatui::Terminal; use ratatui::backend::TestBackend; use ratatui::buffer::Buffer; use ratatui::layout::{Constraint, Layout}; use ratatui::widgets::{Block, Borders, Paragraph, Wrap}; const SIZES: [(u16, u16); 3] = [(80, 24), (120, 40), (200, 60)]; const FP: &str = "abcdef0123456789deadbeefcafebabe"; const ONION: &str = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion"; const LONG_BODY: &str = "this is a very long chat line that should wrap inside the chat pane instead of running off the right edge of an 80-column terminal and colliding with the roster border"; fn fp8(s: &str) -> String { s.chars().take(8).collect() } fn dump_buffer(label: &str, w: u16, h: u16, buf: &Buffer) { println!(); println!("===== {label} {w}x{h} ====="); for y in 0..h { let mut row = String::new(); for x in 0..w { row.push_str(buf[(x, y)].symbol()); } let cells = cell_width(&row); assert!( cells <= w as usize, "{label} row {y} width {cells} cells > {w}: {row:?}" ); println!("{row}"); } } fn draw_to(w: u16, h: u16, label: &str, render: impl FnOnce(&mut Frame)) { let backend = TestBackend::new(w, h); let mut terminal = Terminal::new(backend).expect("terminal"); terminal.draw(render).expect("draw"); dump_buffer(label, w, h, terminal.backend().buffer()); } fn draw_frame(w: u16, h: u16, render: impl FnOnce(&mut Frame)) -> Buffer { let backend = TestBackend::new(w, h); let mut terminal = Terminal::new(backend).expect("terminal"); terminal.draw(render).expect("draw"); terminal.backend().buffer().clone() } fn screen_of(buf: &Buffer, w: u16, h: u16) -> String { let mut screen = String::new(); for y in 0..h { for x in 0..w { screen.push_str(buf[(x, y)].symbol()); } screen.push('\n'); } screen } // --------------------------------------------------------------------------- // Real main-screen states // --------------------------------------------------------------------------- fn friend_rows() -> Vec { vec![ FriendRow { label: "alice".into(), fingerprint: "aaaaaaaaaaaaaaaa".into(), stale: false, pq: true, }, FriendRow { label: "bob-the-stale-peer".into(), fingerprint: "bbbbbbbbbbbbbbbb".into(), stale: true, pq: true, }, ] } #[test] fn a_legacy_friend_row_says_classical_in_the_roster() { let pq = FriendRow { label: "alice".into(), fingerprint: "aaaaaaaaaaaaaaaa".into(), stale: false, pq: true, }; let legacy = FriendRow { pq: false, ..pq.clone() }; let pq_text: String = roster_line(&pq, 40).spans.iter().map(|s| s.content.clone()).collect(); let legacy_text: String = roster_line(&legacy, 40) .spans .iter() .map(|s| s.content.clone()) .collect(); assert!(pq_text.contains("alice"), "{pq_text}"); assert!( !pq_text.contains("classical"), "a v2 friend must not be labelled classical: {pq_text}" ); assert!( legacy_text.contains("classical"), "a v1 friend must be visibly labelled as lacking PQ protection: {legacy_text}" ); } fn out_msg(id: i64, body: &str) -> Message { Message { id, dir: "out".into(), plaintext: body.as_bytes().to_vec(), } } fn in_msg(id: i64, body: &str) -> Message { Message { id, dir: "in".into(), plaintext: body.as_bytes().to_vec(), } } fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { OutboxEntry { id: 1, friend_pk: vec![9u8; 32], message_id, body: body.as_bytes().to_vec(), payload: None, attempts, next_attempt_at: now + next_in, created_at: now, status: OutboxStatus::Queued, last_error: None, } } /// Transcript rows through the real helper: markers included. fn chat_ok() -> Vec { let msgs = [out_msg(1, "ping"), in_msg(2, "pong"), in_msg(3, LONG_BODY)]; chat_with_delivery(&msgs, &[], 1000).0 } /// Transcript with one row still held by the outbox, so the footer carries the /// reachability note instead of a bare `sent`. fn chat_queued() -> (Vec, String) { let now = 1000; let msgs = [out_msg(1, "ping"), in_msg(2, "pong"), out_msg(3, "are you there?")]; let pending = [queued_row(3, "are you there?", 1, 45, now)]; let (lines, summary) = chat_with_delivery(&msgs, &pending, now); (lines, summary.note().unwrap_or_default()) } fn empty_chat(phase: TorPhase) -> Vec { chat_no_friends_lines(Some(phase)) } #[test] fn dump_audit_frames() { let invite = qr::encode(&[7u8; 32], ONION, &[1u8; 32], &[9u8; 1184]).expect("encode"); let friends = friend_rows(); let chat = chat_ok(); let (queued_chat, queued_note) = chat_queued(); let editor = ProfileEditor::new("lance", "tor messenger", ""); let profile_body = { let labels = ["name", "bio", "xmr"]; let values = [editor.display_name(), editor.bio(), editor.xmr_addr()]; let mut body = String::from( "Your profile (friend-visible)\nEnter saves Esc cancels Tab next field\n", ); for (i, (label, value)) in labels.iter().zip(values).enumerate() { let mark = if editor.field() == i { ">" } else { " " }; body.push_str(&format!("\n{mark} {label}: {value}")); } body }; let who = who_overlay_text( FP, ONION, Some(&FriendProfile { display_name: "alice".into(), bio: "hi".into(), xmr_addr: String::new(), updated_at: 0, }), ); println!("invite payload length (chars) = {}", invite.chars().count()); println!("wordmark:\n{}", wordmark_banner()); println!("compact: {}", compact_banner()); println!("onion_glyph:\n{}", onion_glyph()); println!( "fingerprint_mismatch_banner: {}", fingerprint_mismatch_banner() ); for (w, h) in SIZES { draw_to(w, h, "chrome banner+footer (real helpers)", |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]); }); draw_to(w, h, "main: chat + stale roster, focus chat, tor ready", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Chat, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, ); }); draw_to(w, h, "main: focus roster, tor connecting", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Connecting), friends: &friends, selected: 1, focus: Pane::Roster, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("bob-the-stale-peer"), chat_scroll: 0, composer: "", alert: None, note: None, }, ); }); draw_to(w, h, "main: focus composer, promoted identity", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Degraded), friends: &friends, selected: 0, focus: Pane::Composer, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "hello from composer", alert: None, note: None, }, ); }); draw_to(w, h, "main: EMPTY roster, tor connecting", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Connecting), friends: &[], selected: 0, focus: Pane::Roster, chat_lines: &empty_chat(TorPhase::Connecting), chat_style: ChatRowStyle::State, chat_name: None, chat_scroll: 0, composer: "", alert: None, note: None, }, ); }); draw_to(w, h, "main: EMPTY roster, tor off (disconnected)", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: "", phase: Some(TorPhase::Off), friends: &[], selected: 0, focus: Pane::Roster, chat_lines: &empty_chat(TorPhase::Off), chat_style: ChatRowStyle::State, chat_name: None, chat_scroll: 0, composer: "", alert: Some("fingerprint mismatch"), note: None, }, ); }); draw_to(w, h, "main: ERROR alert + fail-closed note", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Degraded), friends: &friends, selected: 0, focus: Pane::Composer, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: Some("send failed: peer onion down"), note: Some("send failed: peer onion down"), }, ); }); draw_to(w, h, "main: outbox queued (peer unreachable)", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Roster, chat_lines: &queued_chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: Some(&queued_note), }, ); }); draw_to(w, h, "draw_share (real)", |f| draw_share(f, &invite)); draw_to(w, h, "main + ? help overlay (real draw_text_overlay)", |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Chat, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, ); draw_text_overlay(f, "? help", help_overlay_text()); }); draw_to(w, h, "main + /who overlay (real draw_text_overlay)", |f| { draw_text_overlay(f, "/who", &who); }); draw_to(w, h, "RECONSTRUCTED profile", |f| { draw_overlay_screen(f, "(o) profile", &profile_body); }); draw_to(w, h, "RECONSTRUCTED paste", |f| { draw_overlay_screen( f, "(o) paste invite", &format!("paste onionwire:v1 payload, Enter to submit\n\n{invite}"), ); }); draw_to(w, h, "RECONSTRUCTED paste error", |f| { draw_overlay_screen(f, "(o) paste invite", "bad invite\n\nnot-a-payload"); }); draw_to(w, h, "RECONSTRUCTED approve", |f| { draw_overlay_screen( f, "(o) approve", &format!( "Unknown key. Add this friend?\n\nfp {}…\nonion {ONION}\n\ny accept n / Esc reject", fp8(FP) ), ); }); draw_to(w, h, "RECONSTRUCTED rotate", |f| { draw_overlay_screen( f, "(o) rotate locator", &format!("{}\n\nROT", rotate_screen_text()), ); }); draw_to(w, h, "RECONSTRUCTED wipe", |f| { draw_overlay_screen( f, "(o) wipe", &format!("{}\n\nWI", wipe_screen_text(WipeKind::Messages)), ); }); draw_to(w, h, "RECONSTRUCTED quit", |f| { draw_overlay_screen(f, "(o) quit", &format!("{}\n\nQUI", quit_screen_text())); }); draw_to(w, h, "RECONSTRUCTED backup", |f| { draw_overlay_screen( f, "(o) backup", &format!("{}\n\nBACK", backup_screen_text()), ); }); draw_to(w, h, "RECONSTRUCTED restore", |f| { draw_overlay_screen(f, "(o) restore", &format!("{}\n\n", restore_screen_text())); }); } } fn draw_overlay_screen(f: &mut Frame, title: &str, body: &str) { f.render_widget( Paragraph::new(body) .wrap(Wrap { trim: false }) .block(Block::default().borders(Borders::ALL).title(title)), f.area(), ); } /// Every real main-screen state must fit every supported size in cells. #[test] fn main_chrome_fits_80x24_120x40_and_200x60() { let friends = friend_rows(); let chat = chat_ok(); let (queued_chat, queued_note) = chat_queued(); let empty_connecting = empty_chat(TorPhase::Connecting); let states: Vec<(&str, MainView<'_>, Option<&str>)> = vec![ ( "chat+stale", MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Chat, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, None, ), ( "empty+connecting", MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Connecting), friends: &[], selected: 0, focus: Pane::Roster, chat_lines: &empty_connecting, chat_style: ChatRowStyle::State, chat_name: None, chat_scroll: 0, composer: "", alert: None, note: None, }, None, ), ( "error+alert", MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Degraded), friends: &friends, selected: 0, focus: Pane::Composer, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: Some("send failed: peer onion down"), note: Some("send failed: peer onion down"), }, None, ), ( "queued outbox", MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Roster, chat_lines: &queued_chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: Some(&queued_note), }, None, ), ]; for (name, view, _) in states { for (w, h) in SIZES { let buf = draw_frame(w, h, |f| draw_main_chrome(f, &view)); let screen = screen_of(&buf, w, h); for (y, row) in screen.lines().enumerate() { assert!( cell_width(row) <= w as usize, "{name} at {w}x{h}: row {y} is {} cells: {row:?}", cell_width(row) ); } } } } /// The frame dump must show the phase, and the phase must not be colour-only. #[test] fn main_frame_states_are_legible_without_colour() { let friends = friend_rows(); let chat = chat_ok(); for (phase, word) in [ (TorPhase::Connecting, "connecting"), (TorPhase::Ready, "ready"), (TorPhase::Degraded, "degraded"), (TorPhase::Off, "off"), ] { let buf = draw_frame(80, 24, |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(phase), friends: &friends, selected: 0, focus: Pane::Chat, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, ) }); let screen = screen_of(&buf, 80, 24); let header = screen.lines().next().unwrap_or_default().to_string(); let footer = screen.lines().last().unwrap_or_default().to_string(); assert!(header.contains(word), "header lacks {word:?}: {header:?}"); assert!(footer.contains(word), "footer lacks {word:?}: {footer:?}"); assert!(header.contains("onionwire"), "{header:?}"); assert!(footer.contains("TOR"), "{footer:?}"); } } /// Focus must be visible with the colour stripped: a marker on the focused /// pane, not just a coloured border. #[test] fn focus_is_a_marker_not_only_colour() { let friends = friend_rows(); let chat = chat_ok(); let marker_of = |focus: Pane, w: u16| -> String { let buf = draw_frame(w, 24, |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, ) }); screen_of(&buf, w, 24) }; for w in [80u16, 120] { let roster_focus = marker_of(Pane::Roster, w); let chat_focus = marker_of(Pane::Chat, w); assert!( roster_focus.contains("> alice"), "roster focus has no row marker at {w}: {roster_focus}" ); assert!( chat_focus.contains("> chat"), "chat focus has no title marker at {w}" ); assert!( !roster_focus.contains("> chat"), "chat title keeps the focus marker while roster is focused" ); assert_eq!( roster_focus.lines().count(), 24, "frame height changed with focus" ); } } /// The empty roster is a state, not a blank box. #[test] fn empty_roster_and_empty_chat_are_designed_states() { let buf = draw_frame(80, 24, |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Connecting), friends: &[], selected: 0, focus: Pane::Roster, chat_lines: &empty_chat(TorPhase::Connecting), chat_style: ChatRowStyle::State, chat_name: None, chat_scroll: 0, composer: "", alert: None, note: None, }, ) }); let screen = screen_of(&buf, 80, 24); assert!(screen.contains("no friends"), "{screen}"); assert!(screen.contains("F3 adds one"), "{screen}"); assert!(screen.contains("F3 paste an invite"), "{screen}"); assert!( screen.contains("tor connecting"), "connecting state is not on screen:\n{screen}" ); } #[test] fn public_copy_fits_80_by_chars_count() { for (name, text) in [ ("wordmark", wordmark_banner()), ("help", help_overlay_text()), ("rotate", rotate_screen_text()), ("quit", quit_screen_text()), ("wipe", wipe_screen_text(WipeKind::Messages)), ("wipe-all", wipe_screen_text(WipeKind::All)), ("backup", backup_screen_text()), ("restore", restore_screen_text()), ] { for line in text.lines() { assert!( line.chars().count() <= 80, "{name} line exceeds 80 by chars().count(): {line:?}" ); } } assert!(compact_banner().chars().count() <= 40); assert!(!onion_glyph().is_empty()); assert_eq!(fingerprint_mismatch_banner(), "fingerprint mismatch"); } #[test] fn footer_drops_full_onion_at_80_keeps_it_at_120() { let at_80 = status_footer(80, FP, ONION, None, main_footer_hints()); let at_120 = status_footer(120, FP, ONION, None, main_footer_hints()); let at_200 = status_footer(200, FP, ONION, None, main_footer_hints()); assert!(at_80.chars().count() <= 80, "{at_80:?}"); assert!(at_120.chars().count() <= 120, "{at_120:?}"); assert!(at_200.chars().count() <= 200, "{at_200:?}"); assert!( !at_80.contains(ONION), "80-col footer should truncate onion: {at_80}" ); assert!( at_120.contains(ONION), "120-col footer should keep full onion: {at_120}" ); assert!(at_80.contains("? help") && at_120.contains("? help")); } /// Spec: never drop `send failed`. Keep `TOR` / `me:` / `? help` at 80 and 120. #[test] fn status_footer_drops_send_failed_when_crowded() { let note = "send failed: peer onion down"; for (w, line) in [ ( 80u16, status_footer(80, FP, ONION, Some(note), main_footer_hints()), ), ( 120, status_footer(120, FP, ONION, Some(note), main_footer_hints()), ), ( 200, status_footer(200, FP, ONION, Some(note), main_footer_hints()), ), ] { assert!( line.chars().count() <= w as usize, "{w} footer wider than frame: {} {line:?}", line.chars().count() ); assert!( line.contains("send failed"), "{w} dropped fail-closed note: {line:?}" ); assert!(line.contains("TOR"), "{w} missing TOR: {line:?}"); assert!(line.contains("me:"), "{w} missing me:: {line:?}"); assert!(line.contains("? help"), "{w} missing ? help: {line:?}"); } } #[test] fn share_invite_wraps_at_three_sizes() { let invite = qr::encode(&[7u8; 32], ONION, &[1u8; 32], &[9u8; 1184]).expect("encode"); for (w, h) in SIZES { let backend = TestBackend::new(w, h); let mut terminal = Terminal::new(backend).expect("terminal"); terminal.draw(|f| draw_share(f, &invite)).expect("draw"); let buf = terminal.backend().buffer(); let mut screen = String::new(); for y in 0..h { for x in 0..w { screen.push_str(buf[(x, y)].symbol()); } screen.push('\n'); } assert!(screen.contains("onionwire:v2:"), "{w}x{h}\n{screen}"); assert!(screen.contains("(o) invite"), "{w}x{h}\n{screen}"); } } /// The help overlay is cleared underneath and never wider than the frame. #[test] fn help_overlay_keeps_a_margin_and_shows_the_tail() { for (w, h) in SIZES { let buf = draw_frame(w, h, |f| { f.render_widget(Paragraph::new("underlying frame junk"), f.area()); draw_text_overlay(f, "? help", help_overlay_text()); }); let screen = screen_of(&buf, w, h); assert!( !screen.contains("underlying frame junk"), "{w}x{h} overlay punched through:\n{screen}" ); assert!( screen.contains("Esc closes this overlay"), "{w}x{h} help tail clipped" ); // A one-cell margin on each side: no border glyph in column 0. for line in screen.lines() { let first = line.chars().next().unwrap_or(' '); assert!( first != '┌' && first != '│', "{w}x{h} overlay is full-bleed: {line:?}" ); } } } /// Documented defect in `tests/tui_chrome.rs`: `chars().count()` is not cells. /// Do not add `unicode-width` on this card. #[test] fn chars_count_is_not_terminal_cells() { let cjk = "你好"; assert_eq!(cjk.chars().count(), 2, "CJK under-counts vs 4 wide cells"); let combining = "e\u{0301}"; assert_eq!( combining.chars().count(), 2, "combining mark over-counts vs 1 cell" ); let emoji = "🧅"; assert_eq!(emoji.chars().count(), 1, "emoji under-counts vs 2 cells"); let ellipsis = "…"; assert_eq!(ellipsis.chars().count(), 1); } /// Sanity: the real main renderer is the one under test here. #[test] fn main_chrome_draws_the_same_frame_as_the_app_layout() { let friends = friend_rows(); let chat = chat_ok(); let buf = draw_frame(80, 24, |f| { draw_main_chrome( f, &MainView { me_fp: FP, me_onion: ONION, phase: Some(TorPhase::Ready), friends: &friends, selected: 0, focus: Pane::Chat, chat_lines: &chat, chat_style: ChatRowStyle::Text, chat_name: Some("alice"), chat_scroll: 0, composer: "", alert: None, note: None, }, ) }); let screen = screen_of(&buf, 80, 24); assert!(screen.contains("roster 2 1 stale"), "{screen}"); assert!(screen.contains("> chat alice"), "{screen}"); assert!(screen.contains("message"), "composer placeholder: {screen}"); }