Merge branch 'main' into onionwire/t_716400d7-onionwire-tui-the-f6-wallet-screen-clips
This commit is contained in:
commit
d93e9c3276
2 changed files with 123 additions and 25 deletions
112
src/tui.rs
112
src/tui.rs
|
|
@ -56,8 +56,17 @@ fn ellipsis() -> &'static str {
|
||||||
/// Cells, not chars: the message can carry a peer-supplied transport error, and
|
/// Cells, not chars: the message can carry a peer-supplied transport error, and
|
||||||
/// a CJK/emoji byte there must not push the alert into the next pane.
|
/// a CJK/emoji byte there must not push the alert into the next pane.
|
||||||
pub fn alert_line(msg: &str, width: u16) -> String {
|
pub fn alert_line(msg: &str, width: u16) -> String {
|
||||||
|
alert_line_for_mode(color_mode(), msg, width)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`alert_line`] with the colour mode supplied by the caller.
|
||||||
|
///
|
||||||
|
/// Tests pin the `! ` ornament of the no-colour path through this entry point
|
||||||
|
/// rather than flipping `NO_COLOR`, which is process-global and mutated
|
||||||
|
/// concurrently by other tests in the same binary.
|
||||||
|
pub fn alert_line_for_mode(mode: ColorMode, msg: &str, width: u16) -> String {
|
||||||
let mut text = msg.to_string();
|
let mut text = msg.to_string();
|
||||||
if color_mode() == ColorMode::None && !text.starts_with("! ") {
|
if mode == ColorMode::None && !text.starts_with("! ") {
|
||||||
text = format!("! {text}");
|
text = format!("! {text}");
|
||||||
}
|
}
|
||||||
fit_cells(&text, width as usize)
|
fit_cells(&text, width as usize)
|
||||||
|
|
@ -1380,14 +1389,16 @@ fn function_key_for(screen: &Screen, code: KeyCode) -> Option<FunctionKey> {
|
||||||
impl App {
|
impl App {
|
||||||
fn new(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<Self, String> {
|
fn new(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<Self, String> {
|
||||||
let friends = node.list_friends()?;
|
let friends = node.list_friends()?;
|
||||||
// First run is the create flow, not an empty list to pick from; an
|
// A fresh install has no wallet to pick and nothing to warn about yet,
|
||||||
// install that already has wallets starts on the list, so a restart
|
// so it starts on the main view. The spend-authority warning is not
|
||||||
// lands on the wallets it remembers. A catalogue that cannot be read is
|
// weakened by this: every path where a user commits to creating or
|
||||||
// the screen's own error state (the create row still works there).
|
// restoring a wallet (`/wallet create`, F6 `c`, the list's create row)
|
||||||
|
// still routes into `WalletSetup::CreateWarn`. An install that already
|
||||||
|
// has wallets starts on the list, so a restart lands on the wallets it
|
||||||
|
// remembers. A catalogue that cannot be read is the screen's own error
|
||||||
|
// state (the create row still works there).
|
||||||
let screen = match node.list_wallets() {
|
let screen = match node.list_wallets() {
|
||||||
Ok(rows) if rows.is_empty() => Screen::WalletSetup(WalletSetup::CreateWarn {
|
Ok(rows) if rows.is_empty() => Screen::Main,
|
||||||
typed: String::new(),
|
|
||||||
}),
|
|
||||||
Ok(rows) => Screen::WalletSelect {
|
Ok(rows) => Screen::WalletSelect {
|
||||||
state: WalletSelectState::from_rows(wallet_rows(rows)),
|
state: WalletSelectState::from_rows(wallet_rows(rows)),
|
||||||
},
|
},
|
||||||
|
|
@ -5689,18 +5700,90 @@ mod wallet_store_tests {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Nothing remembered means there is no list to choose from: the first run
|
fn key(code: KeyCode) -> KeyEvent {
|
||||||
/// goes straight to the existing creation flow.
|
KeyEvent::new(code, KeyModifiers::NONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The frame the real `App::draw` path puts on a terminal of this size.
|
||||||
|
fn frame_on(app: &App, width: u16, height: u16) -> String {
|
||||||
|
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height))
|
||||||
|
.expect("test terminal");
|
||||||
|
term.draw(|f| app.draw(f)).expect("draw");
|
||||||
|
let buf = term.backend().buffer().clone();
|
||||||
|
let mut drawn = String::new();
|
||||||
|
for y in 0..buf.area.height {
|
||||||
|
for x in 0..buf.area.width {
|
||||||
|
drawn.push_str(buf[(x, y)].symbol());
|
||||||
|
}
|
||||||
|
drawn.push('\n');
|
||||||
|
}
|
||||||
|
drawn
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The first frame on the standard 80x24 terminal.
|
||||||
|
fn first_frame(app: &App) -> String {
|
||||||
|
frame_on(app, 80, 24)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whitespace-collapsed copy of some drawn text, so a sentence the renderer
|
||||||
|
/// wrapped across lines still reads as the one string that produced it.
|
||||||
|
fn flattened(text: &str) -> String {
|
||||||
|
text.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Nothing remembered means nothing to warn about: a first run opens on the
|
||||||
|
/// main view, not on the create flow's spend-authority warning. Asserting
|
||||||
|
/// the screen alone would pass with the warning merely drawn behind Main, so
|
||||||
|
/// the second half reads the first frame the app actually renders.
|
||||||
#[test]
|
#[test]
|
||||||
fn an_install_with_no_wallet_starts_on_the_create_flow() {
|
fn a_fresh_install_starts_on_main_not_the_spend_warning() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let (app, _rt) = app(&dir.path().join("home"), &[]);
|
let (app, _rt) = app(&dir.path().join("home"), &[]);
|
||||||
|
assert!(
|
||||||
|
matches!(app.screen, Screen::Main),
|
||||||
|
"a first run must start on the main view"
|
||||||
|
);
|
||||||
|
|
||||||
|
let frame = first_frame(&app);
|
||||||
|
assert!(
|
||||||
|
!flattened(&frame).contains(&flattened(wallet::SPEND_AUTHORITY_WARNING)),
|
||||||
|
"the spend-authority warning must not be on the first frame:\n{frame}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Moving the entry point did not weaken the warning: `/wallet create` on a
|
||||||
|
/// fresh install still lands on `CreateWarn`, and the frame really carries
|
||||||
|
/// the spend-authority copy.
|
||||||
|
#[test]
|
||||||
|
fn the_create_flow_is_still_reachable_and_still_warns() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let (mut app, _rt) = app(&dir.path().join("home"), &[]);
|
||||||
|
assert!(matches!(app.screen, Screen::Main));
|
||||||
|
|
||||||
|
for c in "/wallet create".chars() {
|
||||||
|
app.handle_key(key(KeyCode::Char(c))).expect("typed key");
|
||||||
|
}
|
||||||
|
app.handle_key(key(KeyCode::Enter)).expect("enter");
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
matches!(
|
matches!(
|
||||||
app.screen,
|
app.screen,
|
||||||
Screen::WalletSetup(WalletSetup::CreateWarn { .. })
|
Screen::WalletSetup(WalletSetup::CreateWarn { .. })
|
||||||
),
|
),
|
||||||
"a first run must start on the create flow"
|
"the create command must still warn before anything is created"
|
||||||
|
);
|
||||||
|
// The overlay is taller than its body and scrolls to its tail, so the
|
||||||
|
// 80x24 frame carries only the warning's closing words; the whole
|
||||||
|
// sentence needs a frame wide enough to hold it on one row.
|
||||||
|
let narrow = first_frame(&app);
|
||||||
|
assert!(
|
||||||
|
flattened(&narrow).contains("trust."),
|
||||||
|
"the create flow must warn on the standard frame:\n{narrow}"
|
||||||
|
);
|
||||||
|
let wide = frame_on(&app, 200, 60);
|
||||||
|
assert!(
|
||||||
|
flattened(&wide).contains(&flattened(wallet::SPEND_AUTHORITY_WARNING)),
|
||||||
|
"the spend-authority warning must be drawn on the create flow:\n{wide}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -5793,7 +5876,10 @@ mod wallet_store_tests {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let home = dir.path().join("home");
|
let home = dir.path().join("home");
|
||||||
let (mut app, _rt) = app(&home, &[]);
|
let (mut app, _rt) = app(&home, &[]);
|
||||||
assert!(matches!(app.screen, Screen::WalletSetup(_)));
|
assert!(
|
||||||
|
matches!(app.screen, Screen::Main),
|
||||||
|
"a fresh install starts on the main view"
|
||||||
|
);
|
||||||
assert!(
|
assert!(
|
||||||
app.node.list_wallets().expect("list wallets").is_empty(),
|
app.node.list_wallets().expect("list wallets").is_empty(),
|
||||||
"nothing to list yet"
|
"nothing to list yet"
|
||||||
|
|
|
||||||
|
|
@ -833,7 +833,7 @@ async fn renders_a_queued_row_from_a_real_outbox_send() {
|
||||||
// Colour token set (card t_49b7c2b1)
|
// Colour token set (card t_49b7c2b1)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
use onionwire::tui::{ColorMode, tokens_for};
|
use onionwire::tui::{ColorMode, alert_line_for_mode, tokens_for};
|
||||||
use ratatui::style::{Color, Modifier};
|
use ratatui::style::{Color, Modifier};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -910,17 +910,24 @@ fn tokens_for_is_deterministic_across_calls() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn alert_line_prefixes_when_no_color() {
|
fn alert_line_prefixes_when_no_color() {
|
||||||
// NO_COLOR → ColorMode::None → alert_line prepends "! "
|
// ColorMode::None (NO_COLOR) → alert_line prepends "! ". The mode is
|
||||||
let _guard = std::env::var_os("NO_COLOR");
|
// injected rather than read from the process env: other tests in this
|
||||||
unsafe { std::env::set_var("NO_COLOR", "1") };
|
// binary flip NO_COLOR concurrently, which made the env version flaky.
|
||||||
let line = alert_line("peer down", 80);
|
let plain = alert_line_for_mode(ColorMode::None, "peer down", 80);
|
||||||
assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}");
|
assert!(
|
||||||
unsafe {
|
plain.starts_with("! "),
|
||||||
match _guard {
|
"expected '! ' prefix in None mode: {plain:?}"
|
||||||
Some(v) => std::env::set_var("NO_COLOR", v),
|
);
|
||||||
None => std::env::remove_var("NO_COLOR"),
|
// The ornament belongs to the None path only; RGB keeps the message verbatim.
|
||||||
}
|
assert_eq!(
|
||||||
};
|
alert_line_for_mode(ColorMode::Rgb, "peer down", 80),
|
||||||
|
"peer down"
|
||||||
|
);
|
||||||
|
// An alert that already carries the ornament is not double-prefixed.
|
||||||
|
assert_eq!(
|
||||||
|
alert_line_for_mode(ColorMode::None, "! peer down", 80),
|
||||||
|
"! peer down"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -974,6 +981,11 @@ fn render_menu(w: u16, h: u16, st: &WalletMenuState) -> String {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn wallets_settings_menu_frame_shows_the_shipped_footer() {
|
fn wallets_settings_menu_frame_shows_the_shipped_footer() {
|
||||||
|
// Both sides of the assertion read the process environment through
|
||||||
|
// `ascii_chrome()` / `color_mode()` (glyphs and alert ornaments), so this
|
||||||
|
// test must serialise with the other env-mutating tests in this binary.
|
||||||
|
let _guard = ASCII_ENV.lock().expect("ascii env lock");
|
||||||
|
force_unicode_mode();
|
||||||
// The app renders this screen's one status line with `status_footer_phased`
|
// The app renders this screen's one status line with `status_footer_phased`
|
||||||
// (the chrome redesign put the Tor phase in it) and the hint from
|
// (the chrome redesign put the Tor phase in it) and the hint from
|
||||||
// `footer_hints`; the fixture has to do the same or every menu frame in the
|
// `footer_hints`; the fixture has to do the same or every menu frame in the
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue