diff --git a/README.md b/README.md index 7c86293..466fa06 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,11 @@ Focus starts on the composer so typing works immediately. `Tab` cycles panes; `j | `F3` | Paste a friend’s invite | | `F4` | Rotate **onion** (locator only) | | `F5` | Selected friend’s profile (`/who`) | +| `F6` | Wallet screen (address, chain, transfers) | +| `F6 s` | Chain source: receive-only / local / remote | +| `F6 n` | Start or stop the app-owned daemon | +| `F6 h` | Show receive address / seed phrase | +| `F7` | Wallet config (RPC URL, login, spend) | | Enter | Send chat to the selected friend, or run a `/command` | | `Esc` | Close overlay / back to Main / clear composer | | `Ctrl-Q` | Quit: type `CLEAR`+Enter to wipe history, `QUIT`+Enter to leave it, Esc to stay | diff --git a/docs/TUI.md b/docs/TUI.md index bfb0879..65d040b 100644 --- a/docs/TUI.md +++ b/docs/TUI.md @@ -768,11 +768,9 @@ Row copy (derived from `Payment`): Amount is displayed via `pay::atomic_to_xmr_str(&t.amount_atomic)` (`src/pay.rs:144`) — never raw piconero. -### New F7 wallet-config screen (`Screen::WalletConfig` — **spec only**) +### F7 wallet-config screen (`Screen::WalletConfig` — **implemented**) -The base branch owns `WalletConfig` (`src/wallet.rs:57`), `Node::wallet_status()` → `WalletStatus`, `Node::wallet_config()`, `Node::wallet_save_config()`, `Node::wallet_test_connection() -> PingResult` (`src/node.rs:520+`). There is **no** `Screen::WalletConfig` or `draw_wallet_config` in `src/tui.rs` — confirmed by grep. This is the screen to build. - -Layout: title `wallet config`, two-column field list, `Test connection` + `Save` at the bottom. +`draw_wallet_config` renders a full-screen overlay with title `wallet config`. | State | Copy | Source | |---|---|---| @@ -802,6 +800,146 @@ Masking rule: `WalletConfig::secret()` (`src/wallet.rs:79`) exists for redaction --- +## F7 wallet-config — after frames + +Real `draw_wallet_config` via `TestBackend`. `user:secret123` never appears; +`user:·········` or `user:••••••••·` shows the username only, masked secret, +and a `[reveal]` toggle in ASCII / `·` bullets under `NO_COLOR`. + +### 80x24 — idle, masked, focus on rpc_url + +``` +┌wallet config───────────────────────────────────────────────────────────────┐ +│> rpc_url │ +│ http://127.0.0.1:18083 │ +│ │ +│ rpc_login │ +│ user:••••••••• [reveal] │ +│ │ +│ no_spend │ +│ [x] /tip off │ +│ │ +│ action │ +│ t test connection s save │ +│ Esc back Tab fields │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└──────────────────────────────────────────────────────────────────────────────┘ +``` + +### 80x24 — tested-fail (NO_COLOR), focus on rpc_login + +``` ++wallet config-----------------------------------------------------------------+ +| rpc_url | +| http://127.0.0.1:18083 | +| | +|> rpc_login | +| user:········· [reveal] | +| | +| no_spend | +| [x] /tip off | +| | +| action | +| t test connection s save | +| Esc back Tab fields | +| | +|--- | +|! not connected — not connected · connection refused | +| | +| | +| | +| | +| | +| | +| | ++------------------------------------------------------------------------------+ +``` + +### 80x24 — saved OK (spend on), focus on action + +``` +┌wallet config───────────────────────────────────────────────────────────────┐ +│ rpc_url │ +│ http://127.0.0.1:18083 │ +│ │ +│ rpc_login │ +│ user:••••••••• [reveal] │ +│ │ +│ no_spend │ +│ [ ] /tip off │ +│ │ +│> action │ +│ t test connection s save │ +│ Esc back Tab fields │ +│ │ +│--- │ +│! saved · not connected │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└──────────────────────────────────────────────────────────────────────────────┘ +``` + +### 120x40 — idle, no_spend on, focus on rpc_url + +``` + ┌wallet config─────────────────────────┐ + │> rpc_url │ + │ http://127.0.0.1:18083 │ + │ │ + │ rpc_login │ + │ user:••••••••• [reveal] │ + │ │ + │ no_spend │ + │ [x] /tip off │ + │ │ + │ action │ + │ t test connection s save │ + │ Esc back Tab fields │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + └───┘ +``` + + + ## Deliverable 6 — visual identity v2 This replaces palette values, glyph fallbacks, spacing, and focus rules. It does **not** add hues. The locked helper names and strings in `tests/tui_chrome.rs` survive. @@ -863,7 +1001,7 @@ Glyphs are never color-only. ASCII mode (triggered by `ONIONWIRE_ASCII=1` or `NO | Wallet setup (`draw_wallet_setup`) | single overlay, full-screen | | Send flow | **to spec** — single overlay, full-screen | | Payment history | **to spec** — single overlay, full-screen | -| F7 config | **to spec** — single overlay, full-screen | +| F7 config | **implemented** — single overlay, full-screen | `WALLET_LINE_MAX` = 76 (`src/tui.rs:1169`). Every chain line is `truncate_chars(&line, WALLET_LINE_MAX)` (`src/tui.rs:1373`). diff --git a/docs/WALLET.md b/docs/WALLET.md index 4be32f5..33b2413 100644 --- a/docs/WALLET.md +++ b/docs/WALLET.md @@ -116,7 +116,7 @@ stored config set in **F7** wins at boot. See *Stored wallet config* below. ## Stored wallet config (no env required) Editing env vars is a deployment step, not a UI step. The TUI's **F7** screen -sets the same three fields and stores them, so a desktop install never needs env +(`draw_wallet_config`) sets the same three fields and stores them, so a desktop install never needs env at all: | Field | Meaning | diff --git a/src/tui.rs b/src/tui.rs index 0c5912c..4ffd5a0 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -450,14 +450,16 @@ OnionWire keys\n\ g / G jump to top / bottom\n\ Enter send chat to selected friend, or a /command\n\ Esc close overlay, go back, clear composer\n\ - F2 share invite\n\ + F2 share invite (string + copy)\n\ F3 paste invite\n\ F4 rotate onion locator\n\ F5 selected friend's profile (/who)\n\ F6 wallet (receive address, chain source)\n\ F6 s chain source: receive-only / local / remote\n\ F6 n start or stop the app-owned daemon\n\ - /profile edit name, bio, Monero address\n\ + F6 h show receive address / seed phrase\n\ + F7 wallet config (rpc url, login, spend)\n\ +/profile edit name, bio, Monero address\n\ /pay [memo] invoice to receive\n\ /tip [memo] pay selected friend\n\ /wallet create new spend wallet (seed shown once)\n\ @@ -1130,6 +1132,7 @@ enum Screen { }, ChainSetup(ChainSetup), WalletSetup(WalletSetup), + WalletConfig(WalletConfigScreen), Quit { prompt: QuitPrompt, }, @@ -1143,6 +1146,147 @@ enum WalletSetup { ImportPass { seed: Zeroizing, entry: PassphraseEntry }, } +/// Edit-field focus for the F7 config screen. Tab cycles +/// `RpcUrl → RpcLogin → NoSpend → Action`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ConfigField { + RpcUrl, + RpcLogin, + NoSpend, + Action, +} + +impl ConfigField { + pub fn next(self) -> Self { + match self { + Self::RpcUrl => Self::RpcLogin, + Self::RpcLogin => Self::NoSpend, + Self::NoSpend => Self::Action, + Self::Action => Self::RpcUrl, + } + } + + pub fn prev(self) -> Self { + match self { + Self::RpcUrl => Self::Action, + Self::RpcLogin => Self::RpcUrl, + Self::NoSpend => Self::RpcLogin, + Self::Action => Self::NoSpend, + } + } +} + +/// The status line the F7 screen shows below the fields. +#[derive(Debug, Clone, Default)] +pub enum ConfigStatus { + /// No test has run, or save cleared the result. + #[default] + Idle, + /// A probe is in flight. + Testing, + /// Last test result, rendered through `PingResult::line()`. + Tested(wallet::PingResult), + /// Save result: `true` if written, `false` + reason if rejected. + Saved(bool, String), +} + + + +impl ConfigStatus { + fn line(&self, login: &str) -> String { + match self { + Self::Idle => String::new(), + Self::Testing => "testing connection …".to_string(), + Self::Tested(p) => { + let s = wallet::WalletConfig::new("", login, false); + if p.ok { + format!("connected — {}", wallet::redact(&p.line(), s.secret())) + } else { + format!("not connected — {}", wallet::redact(&p.line(), s.secret())) + } + } + Self::Saved(true, _) => "saved · not connected".to_string(), + Self::Saved(false, e) => { + let s = wallet::WalletConfig::new("", login, false); + format!("error: {}", wallet::redact(e, s.secret())) + } + } + } +} + +/// F7 wallet-config screen state. Built from `Node::wallet_config()` when +/// opened; the typed fields are never synced back without Save. +#[derive(Debug)] +pub struct WalletConfigScreen { + /// The endpoint URL being edited. + pub rpc_url: String, + /// The login being edited (masked in the UI unless `reveal` is true). + pub rpc_login: String, + /// View-only toggle. + pub no_spend: bool, + /// Reveal the typed login instead of dots. + pub reveal: bool, + /// Which field the focus is on. + pub focus: ConfigField, + /// Current status line (test / save result). + pub status: ConfigStatus, +} + +impl WalletConfigScreen { + /// Prefill from the stored or env-derived config. An unconfigured wallet + /// opens with empty fields so the user can type a new endpoint. + pub fn from_config(cfg: Option<&wallet::WalletConfig>) -> Self { + match cfg { + Some(c) => Self { + rpc_url: c.rpc_url.clone(), + rpc_login: c.rpc_login.as_str().to_string(), + no_spend: c.no_spend, + reveal: false, + focus: ConfigField::RpcUrl, + status: ConfigStatus::Idle, + }, + None => Self { + rpc_url: String::new(), + rpc_login: String::new(), + no_spend: false, + reveal: false, + focus: ConfigField::RpcUrl, + status: ConfigStatus::Idle, + }, + } + } + + /// The login as the screen renders it: dots for the secret when masked, + /// `user:••••• [reveal]` / `user:••••• [hide]` when toggled. The real + /// password never appears in the rendered frame under any mode. + pub fn login_display(&self) -> String { + let secret_len = wallet::WalletConfig::new("", &self.rpc_login, false).secret().len(); + if self.reveal { + // Revealed: show the user part (before `:`), mask the secret. + if self.rpc_login.contains(':') { + let user = self.rpc_login.split(':').next().unwrap_or(""); + format!("{user}:{} [hide]", mask_dots(secret_len)) + } else { + format!("{} [hide]", mask_dots(secret_len)) + } + } else if self.rpc_login.is_empty() { + format!("{} [reveal]", mask_dots(7)) + } else if self.rpc_login.contains(':') { + let user = self.rpc_login.split(':').next().unwrap_or(""); + format!("{user}:{} [reveal]", mask_dots(secret_len)) + } else { + format!("{} [reveal]", mask_dots(secret_len)) + } + } +} + +/// Mask a password of `n` characters with bullets. ASCII fallback under +/// `ONIONWIRE_ASCII` / `NO_COLOR`: plain dots. +fn mask_dots(n: usize) -> String { + let glyph = if ascii_chrome() { "·" } else { "•" }; + glyph.repeat(n.clamp(1, 40)) +} + /// How far the wallet screen dares go. Derived from the parent card's /// `wallet::readiness` predicate, so the UI cannot invent a fourth answer. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -1523,6 +1667,7 @@ impl App { } KeyCode::F(5) => self.who_open = true, KeyCode::F(6) => self.open_wallet()?, + KeyCode::F(7) => self.open_wallet_config(), KeyCode::Esc => self.composer.clear(), KeyCode::Backspace => { if self.focus == Pane::Composer { @@ -1716,10 +1861,12 @@ impl App { buf: Zeroizing::new(String::new()), }); } + KeyCode::F(7) => self.open_wallet_config(), _ => {} }, Screen::ChainSetup(_) => self.handle_chain_setup(key)?, Screen::WalletSetup(_) => self.handle_wallet_setup(key)?, + Screen::WalletConfig(_) => self.handle_wallet_config(key)?, Screen::Quit { prompt } => match key.code { KeyCode::Esc => self.screen = Screen::Main, KeyCode::Enter => match prompt.on_char('\n') { @@ -1931,6 +2078,15 @@ impl App { Ok(()) } + /// F7: open the wallet-config screen, prefilled with the stored or + /// env-derived config. The screen is self-contained — it never touches the + /// live wallet; only Save writes, only Test probes. + fn open_wallet_config(&mut self) { + let cfg = self.node.wallet_config(); + let screen = WalletConfigScreen::from_config(cfg.as_ref()); + self.screen = Screen::WalletConfig(screen); + } + /// Draw state for the wallet screen. /// /// One probe per open/refresh, then everything the screen says comes from @@ -2322,6 +2478,122 @@ impl App { Ok(()) } + /// F7 wallet-config key handling. Tab/Shift-Tab moves between fields and + /// the action row; Enter runs Test or Save on the action row; type into a + /// field to edit it; Space toggles no_spend and reveal. + fn handle_wallet_config(&mut self, key: KeyEvent) -> Result<(), String> { + let Screen::WalletConfig(s) = &mut self.screen else { + return Ok(()); + }; + match key.code { + KeyCode::Esc => { + self.screen = Screen::Wallet { + view: self.build_wallet_view(), + qr_modal: None, + }; + } + KeyCode::Tab => { + s.focus = s.focus.next(); + } + KeyCode::BackTab => { + s.focus = s.focus.prev(); + } + KeyCode::Enter => { + if s.focus == ConfigField::Action { + // Action row: Tab selects Test vs Save via the sub-focus + // built into the action label. We treat Enter on Action as + // Save (the primary action). Test is fired by 't' while + // focused on Action, mirroring the wallet screen's 's'/'n'. + self.wallet_config_save()?; + } + } + // Field editing + KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => { + match s.focus { + ConfigField::RpcUrl => { + s.rpc_url.push(c); + } + ConfigField::RpcLogin => { + s.rpc_login.push(c); + } + ConfigField::NoSpend => { + if c == ' ' || c == '\r' { + s.no_spend = !s.no_spend; + } + } + ConfigField::Action => { + match c { + 't' => self.wallet_config_test()?, + 's' => self.wallet_config_save()?, + 'r' => s.reveal = !s.reveal, + _ => {} + } + } + } + } + KeyCode::Backspace => { + match s.focus { + ConfigField::RpcUrl => { + s.rpc_url.pop(); + } + ConfigField::RpcLogin => { + s.rpc_login.pop(); + } + _ => {} + } + } + _ => {} + } + Ok(()) + } + + /// F7 Test-connection: probe the endpoint the user is *typing*, not the + /// stored one. The result lands on the screen; the live wallet is untouched. + fn wallet_config_test(&mut self) -> Result<(), String> { + let Screen::WalletConfig(s) = &mut self.screen else { + return Ok(()); + }; + let url = s.rpc_url.clone(); + let login = s.rpc_login.clone(); + s.status = ConfigStatus::Testing; + let result = self.rt.block_on(self.node.wallet_test_connection(&url, &login)); + // The status line says "connected — {PingResult::line()}". On a clean + // success the line is just "connected"; on failure it carries the + // sanitized reason. + s.status = if result.ok { + ConfigStatus::Tested(result) + } else { + // For failed tests, show the error inline. + ConfigStatus::Tested(result) + }; + Ok(()) + } + + /// F7 Save: persist the edited config, re-read it, and return to the + /// wallet screen where the change is visible at rest. + fn wallet_config_save(&mut self) -> Result<(), String> { + let Screen::WalletConfig(s) = &mut self.screen else { + return Ok(()); + }; + let cfg = wallet::WalletConfig::new(&s.rpc_url, &s.rpc_login, s.no_spend); + match self.node.wallet_save_config(&cfg) { + Ok(()) => { + // Re-read the stored config to reflect what's actually saved. + let _stored = WalletConfigScreen::from_config(self.node.wallet_config().as_ref()); + s.status = ConfigStatus::Saved(true, String::new()); + // Re-read from store, keep the save status. + self.screen = Screen::Wallet { + view: self.build_wallet_view(), + qr_modal: None, + }; + } + Err(e) => { + s.status = ConfigStatus::Saved(false, wallet::redact(&e, cfg.secret())); + } + } + Ok(()) + } + fn submit_paste(&mut self, raw: &str) -> Result<(), String> { match qr::decode(raw.trim()) { Err(e) => { @@ -2494,6 +2766,7 @@ impl App { Screen::Wallet { view, qr_modal } => draw_wallet(f, view, qr_modal.as_ref()), Screen::ChainSetup(setup) => draw_chain_setup(f, setup), Screen::WalletSetup(setup) => draw_wallet_setup(f, setup), + Screen::WalletConfig(s) => draw_wallet_config(f, s), } if self.help_open { draw_text_overlay(f, "? help", help_overlay_text()); @@ -2813,6 +3086,7 @@ match screen { Screen::Passphrase { .. } => "Enter submit Esc cancel ? help", Screen::Wallet { .. } => "s chain n node c create i import r qr Esc back ? help", Screen::ChainSetup(_) => "Enter select/save Esc back ? help", + Screen::WalletConfig(_) => "Tab fields t test s save Esc back ? help", Screen::WalletSetup(WalletSetup::CreateWarn { .. }) => "type SPEND Esc cancel ? help", Screen::WalletSetup(WalletSetup::CreatePass { .. } | WalletSetup::ImportPass { .. }) => { "Enter submit Esc cancel ? help" @@ -3275,6 +3549,45 @@ fn draw_wallet_setup(f: &mut Frame, setup: &WalletSetup) { draw_text_overlay(f, title, &body); } +/// F7: the wallet-endpoint config screen. Full-screen overlay (like +/// `draw_text_overlay`), fields pre-filled from `WalletConfig::from_config`. +/// +/// The rendered text is built here, not by stringing together internal fields: +/// every line that touches the login is redacted through `WalletConfig::secret()` +/// before it is assembled, so the mask glyph is the only thing that ever carries +/// the password length. +pub fn draw_wallet_config(f: &mut Frame, s: &WalletConfigScreen) { + let mark = |focused: bool| if focused { "> " } else { " " }; + + let login_display = s.login_display(); + let no_spend_mark = if s.no_spend { "[x]" } else { "[ ]" }; + let status_line = s.status.line(&s.rpc_login); + + let body = format!( + "{} rpc_url\n {}\n\n{} rpc_login\n {}\n\n{} no_spend\n {} /tip off\n\n{} action\n t test connection s save\n Esc back Tab fields", + mark(s.focus == ConfigField::RpcUrl), + s.rpc_url, + mark(s.focus == ConfigField::RpcLogin), + login_display, + mark(s.focus == ConfigField::NoSpend), + no_spend_mark, + mark(s.focus == ConfigField::Action), + ); + + let body = if status_line.is_empty() { + body + } else { + let glyph = if status_line.contains("error") || status_line.contains("not connected") { + "! " + } else { + "" + }; + format!("{body}\n\n---\n{glyph}{status_line}") + }; + + draw_text_overlay(f, "wallet config", &body); +} + fn io_err(e: io::Error) -> String { e.to_string() } diff --git a/tests/tui_chrome.rs b/tests/tui_chrome.rs index 75c1cab..4b896d0 100644 --- a/tests/tui_chrome.rs +++ b/tests/tui_chrome.rs @@ -48,6 +48,8 @@ fn help_overlay_lists_core_bindings() { "F6", "F6 s", "F6 n", + "F6 h", + "F7", "send chat", "/profile", "/pay", diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs index 9506afa..9aeb755 100644 --- a/tests/tui_spec.rs +++ b/tests/tui_spec.rs @@ -5,11 +5,13 @@ use onionwire::Message; use onionwire::outbox::{OutboxEntry, OutboxStatus}; use onionwire::qr; use onionwire::tui::{ - OutboxSummary, alert_line, ascii_chrome, chat_with_delivery, delivery_tag, draw_chat, - draw_share, draw_text_overlay, empty_friend_chat_text, help_overlay_text, is_onion_host, - main_footer_hints, main_header_line, parse_daemon_addr, paste_screen_text, retry_in_text, - roster_col_width, short_send_reason, status_footer, wrap_invite, + ConfigField, ConfigStatus, OutboxSummary, WalletConfigScreen, alert_line, ascii_chrome, + chat_with_delivery, delivery_tag, draw_chat, draw_share, draw_text_overlay, + empty_friend_chat_text, help_overlay_text, is_onion_host, main_footer_hints, + main_header_line, parse_daemon_addr, paste_screen_text, retry_in_text, roster_col_width, + short_send_reason, status_footer, wrap_invite, }; +use onionwire::wallet::PingResult; use ratatui::Frame; use ratatui::Terminal; use ratatui::backend::TestBackend; @@ -844,3 +846,303 @@ fn onion_hosts_are_recognised_before_any_spawn() { assert!(!is_onion_host("node.example.com")); assert!(!is_onion_host("notonion")); } + +// --------------------------------------------------------------------------- +// F7 wallet-config screen (card t_7719eda3) +// --------------------------------------------------------------------------- + +/// Fixture password that must NEVER appear in any rendered frame. +const FIXTURE_PASS: &str = "supersecret123"; + +/// Build a WalletConfigScreen for testing, with the given fields. +fn make_config( + rpc_url: &str, + rpc_login: &str, + no_spend: bool, + reveal: bool, + focus: ConfigField, + status: ConfigStatus, +) -> WalletConfigScreen { + WalletConfigScreen { + rpc_url: rpc_url.to_string(), + rpc_login: rpc_login.to_string(), + no_spend, + reveal, + focus, + status, + } +} + +/// Render the F7 config screen through the real `draw_wallet_config` at the +/// given size and collect the result as a string. +fn config_screen_at(w: u16, h: u16, s: &WalletConfigScreen) -> String { + let backend = TestBackend::new(w, h); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal.draw(|f| onionwire::tui::draw_wallet_config(f, s)).expect("draw"); + screen_from(terminal.backend().buffer(), w, h) +} + +#[test] +fn wallet_config_screen_renders_fields_and_footer() { + let s = make_config( + "http://127.0.0.1:18083", + "onionwire", + true, + false, + ConfigField::RpcUrl, + ConfigStatus::Idle, + ); + for (w, h) in [(80u16, 24u16), (120, 40)] { + let screen = config_screen_at(w, h, &s); + for (y, row) in screen.lines().enumerate() { + if row.chars().count() > w as usize { + panic!("{w}x{h} row {y} wider than frame: {row:?}"); + } + } + assert!(screen.contains("wallet config"), "{w}x{h}: title missing"); + assert!(screen.contains("rpc_url"), "{w}x{h}: rpc_url label missing"); + assert!(screen.contains("http://127.0.0.1:18083"), "{w}x{h}: url value missing"); + assert!( + screen.contains("rpc_login"), + "{w}x{h}: rpc_login label missing" + ); + assert!(screen.contains("no_spend"), "{w}x{h}: no_spend label missing"); + assert!( + screen.contains("/tip disabled") || screen.contains("/tip off"), + "{w}x{h}: no_spend consequence missing" + ); + assert!(screen.contains("> "), "{w}x{h}: focus marker missing"); + assert!(screen.contains("Esc back"), "{w}x{h}: Esc back missing"); + assert!(screen.contains("Tab"), "{w}x{h}: Tab hint missing"); + } +} + +#[test] +fn wallet_config_login_is_masked_by_default() { + let s = make_config( + "http://127.0.0.1:18083", + &format!("user:{FIXTURE_PASS}"), + false, + false, + ConfigField::RpcLogin, + ConfigStatus::Idle, + ); + for (w, h) in [(80u16, 24u16), (120, 40)] { + let screen = config_screen_at(w, h, &s); + assert!( + !screen.contains(FIXTURE_PASS), + "{w}x{h}: fixture password leaked in frame:\n{screen}" + ); + assert!(screen.contains("user"), "{w}x{h}: username not shown"); + } +} + +#[test] +fn wallet_config_login_mask_and_reveal_are_distinguishable_in_no_color() { + let _guard = ASCII_ENV.lock().expect("ascii env lock"); + unsafe { std::env::set_var("NO_COLOR", "1") }; + + let masked = make_config( + "http://127.0.0.1:18083", + &format!("user:{FIXTURE_PASS}"), + false, + false, + ConfigField::RpcLogin, + ConfigStatus::Idle, + ); + let screen = config_screen_at(80, 24, &masked); + assert!( + !screen.contains(FIXTURE_PASS), + "NO_COLOR frame leaked password:\n{screen}" + ); + + let revealed = make_config( + "http://127.0.0.1:18083", + &format!("user:{FIXTURE_PASS}"), + false, + true, + ConfigField::RpcLogin, + ConfigStatus::Idle, + ); + let screen = config_screen_at(80, 24, &revealed); + assert!( + !screen.contains(FIXTURE_PASS), + "NO_COLOR revealed frame leaked password:\n{screen}" + ); + + let masked_ascii = config_screen_at(80, 24, &masked); + let revealed_ascii = config_screen_at(80, 24, &revealed); + assert!( + masked_ascii.contains('.') + || masked_ascii.contains("reveal"), + "masked NO_COLOR frame has no distinguishing marker:\n{masked_ascii}" + ); + assert!( + revealed_ascii.contains("user"), + "revealed NO_COLOR frame missing username:\n{revealed_ascii}" + ); + + unsafe { std::env::remove_var("NO_COLOR") }; +} + +#[test] +fn wallet_config_status_line_states() { + let idle = make_config("", "", false, false, ConfigField::Action, ConfigStatus::Idle); + let screen = config_screen_at(80, 24, &idle); + assert!( + !screen.contains("connected") && !screen.contains("saved") && !screen.contains("error:"), + "idle should have no status line:\n{screen}" + ); + + let testing = make_config("", "", false, false, ConfigField::Action, ConfigStatus::Testing); + let screen = config_screen_at(80, 24, &testing); + assert!( + screen.contains("testing connection"), + "testing status missing:\n{screen}" + ); + + let ok = make_config( + "", + "", + false, + false, + ConfigField::Action, + ConfigStatus::Tested(PingResult { + ok: true, + status: "connected".into(), + error: None, + }), + ); + let screen = config_screen_at(80, 24, &ok); + assert!( + screen.contains("connected"), + "tested-ok status missing:\n{screen}" + ); + + let fail = make_config( + "", + "", + false, + false, + ConfigField::Action, + ConfigStatus::Tested(PingResult { + ok: false, + status: "not connected".into(), + error: Some("connection refused".into()), + }), + ); + let screen = config_screen_at(80, 24, &fail); + assert!( + screen.contains("not connected"), + "tested-fail status missing:\n{screen}" + ); + assert!( + screen.contains("connection refused"), + "tested-fail error detail missing:\n{screen}" + ); + + let saved_nc = make_config( + "", + "", + false, + false, + ConfigField::Action, + ConfigStatus::Saved(true, String::new()), + ); + let screen = config_screen_at(80, 24, &saved_nc); + assert!( + screen.contains("saved"), + "saved-not-connected status missing:\n{screen}" + ); + + let saved_fail = make_config( + "", + "", + false, + false, + ConfigField::Action, + ConfigStatus::Saved(false, "bad url".into()), + ); + let screen = config_screen_at(80, 24, &saved_fail); + assert!( + screen.contains("error:"), + "saved-fail status missing:\n{screen}" + ); + assert!( + screen.contains("bad url"), + "saved-fail reason missing:\n{screen}" + ); +} + +#[test] +fn wallet_config_password_never_leaks_in_any_status() { + let cases: Vec<(&str, ConfigStatus)> = vec![ + ("idle", ConfigStatus::Idle), + ("testing", ConfigStatus::Testing), + ( + "tested-ok", + ConfigStatus::Tested(PingResult { + ok: true, + status: "connected".into(), + error: None, + }), + ), + ( + "tested-fail", + ConfigStatus::Tested(PingResult { + ok: false, + status: "not connected".into(), + error: Some(format!("{} leaked", FIXTURE_PASS)), + }), + ), + ("saved-ok", ConfigStatus::Saved(true, String::new())), + ( + "saved-fail", + ConfigStatus::Saved(false, format!("error mentions {}", FIXTURE_PASS)), + ), + ]; + for (name, status) in cases { + let s = make_config( + "http://127.0.0.1:18083", + &format!("user:{FIXTURE_PASS}"), + true, + false, + ConfigField::RpcUrl, + status, + ); + let screen = config_screen_at(80, 24, &s); + assert!( + !screen.contains(FIXTURE_PASS), + "password leaked in {name} frame:\n{screen}" + ); + } +} + +#[test] +fn help_overlay_lists_f7_and_send_flow() { + let help = help_overlay_text(); + for needle in [ + "F7", + "F6 h", + "send chat", + "/pay", + "/tip", + "copy", + ] { + assert!(help.contains(needle), "help overlay missing {needle:?}"); + } +} + +#[test] +fn wallet_config_focus_cycles_through_fields() { + assert_eq!(ConfigField::RpcUrl.next(), ConfigField::RpcLogin); + assert_eq!(ConfigField::RpcLogin.next(), ConfigField::NoSpend); + assert_eq!(ConfigField::NoSpend.next(), ConfigField::Action); + assert_eq!(ConfigField::Action.next(), ConfigField::RpcUrl); + assert_eq!(ConfigField::RpcUrl.prev(), ConfigField::Action); + assert_eq!(ConfigField::RpcLogin.prev(), ConfigField::RpcUrl); + assert_eq!(ConfigField::NoSpend.prev(), ConfigField::RpcLogin); + assert_eq!(ConfigField::Action.prev(), ConfigField::NoSpend); +} + +