tui: wallet selection screen (F6 -> w) with create as the last row
Screen::WalletSelect: a table of saved wallets — name, identity prefix, locator prefix, creation day — the selected row's full identity and locator under it, and a `+ create new wallet` row as the last item. j/k or arrows move, Enter takes the choice, Esc back. Empty, failed and in-flight reads each get their own copy; a wallet with a NULL label, no published locator or no usable timestamp reads `(unlabelled)` / `-` / `unknown` rather than blank. Presentation only. Rows are plain display data (WalletRow) built by the caller; the render path never reads the store, and the selection is by row index because two records may share an identity key. Loading a chosen record into the session is the wallet-store integration step, so the F6 `w` entry opens the screen on its unavailable state until that accessor lands, and the create row works today either way. Evidence: tests/tui_select.rs (16 tests) drives the real draw path through TestBackend at 80x24, 120x40 and 200x60, pins the dumped frame's footer to status_footer_phased(..., wallet_select_footer_hint()), asserts exactly one focus marker, a cell-width line budget from 20 to 200 columns, the window disclosure with the create row always visible, hostile labels (ESC/BEL/C1/ bidi/newline/tab) and the crate-local UTC date formatter.
This commit is contained in:
parent
3769bd9a42
commit
99a4ab640e
3 changed files with 1095 additions and 2 deletions
475
tests/tui_select.rs
Normal file
475
tests/tui_select.rs
Normal file
|
|
@ -0,0 +1,475 @@
|
|||
//! Wallet selection screen (`F6` → `w`).
|
||||
//!
|
||||
//! The screen lists the persisted wallets and, last, the `create new wallet`
|
||||
//! row. Assertions measure in terminal **cells** (`cell_width`, which goes
|
||||
//! through `Span::width`) and accept either ellipsis form because
|
||||
//! `ascii_chrome()` follows `NO_COLOR` / `ONIONWIRE_ASCII` and the TUI test
|
||||
//! binaries run in parallel.
|
||||
|
||||
use onionwire::tui::{
|
||||
TorPhase, WalletChoice, WalletRow, WalletSelectState, cell_width, draw_wallet_select,
|
||||
format_day_utc, status_footer_phased, wallet_select_footer_hint, wallet_select_lines,
|
||||
wallet_select_text,
|
||||
};
|
||||
use ratatui::Terminal;
|
||||
use ratatui::backend::TestBackend;
|
||||
|
||||
const FP: &str = "abcdef0123456789deadbeefcafebabe";
|
||||
const ONION: &str = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion";
|
||||
const ID_A: &str = "ab12cd34ef5678901234567890abcdef1234567890abcdef1234567890abcd";
|
||||
const ID_B: &str = "ff00aa11bb22cc33dd44ee55ff6677889900aabbccddeeff0011223344556677";
|
||||
const ONION_A: &str = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx";
|
||||
const ONION_B: &str = "zyxwvutsrqponmlkjihgfedcba765432zyxwvutsrqponmlkjihgfedcba";
|
||||
|
||||
fn row(name: &str, id: &str, onion: &str, created_at: i64) -> WalletRow {
|
||||
WalletRow {
|
||||
name: name.into(),
|
||||
identity_pubkey: id.into(),
|
||||
onion_locator: onion.into(),
|
||||
created_at,
|
||||
}
|
||||
}
|
||||
|
||||
/// Two saved wallets plus the create row: the shape the screen opens on.
|
||||
fn two_wallets() -> WalletSelectState {
|
||||
WalletSelectState::from_rows(vec![
|
||||
row("lance", ID_A, ONION_A, 1_789_963_705),
|
||||
row("cold", ID_B, ONION_B, 1_756_320_000),
|
||||
])
|
||||
}
|
||||
|
||||
/// The row count is the wallet count **plus the create row**, which is always
|
||||
/// the last item: Enter on it starts the creation flow, not a wallet load.
|
||||
#[test]
|
||||
fn selection_walks_every_row_and_ends_on_create() {
|
||||
let mut st = two_wallets();
|
||||
assert_eq!(
|
||||
st.choice(),
|
||||
WalletChoice::Open(0),
|
||||
"opens on the first wallet"
|
||||
);
|
||||
|
||||
st.select_next();
|
||||
assert_eq!(st.choice(), WalletChoice::Open(1));
|
||||
st.select_next();
|
||||
assert_eq!(
|
||||
st.choice(),
|
||||
WalletChoice::Create,
|
||||
"the create row is the last item in the list"
|
||||
);
|
||||
|
||||
// Past the end is a no-op, not a panic: the bottom of the list is Create.
|
||||
st.select_next();
|
||||
assert_eq!(st.choice(), WalletChoice::Create);
|
||||
|
||||
st.select_prev();
|
||||
assert_eq!(st.choice(), WalletChoice::Open(1));
|
||||
st.select_prev();
|
||||
st.select_prev();
|
||||
assert_eq!(
|
||||
st.choice(),
|
||||
WalletChoice::Open(0),
|
||||
"top of the list is sticky"
|
||||
);
|
||||
}
|
||||
|
||||
/// With no saved wallets the screen still has exactly one item — Create — so
|
||||
/// Enter can never be a dead key on a first run.
|
||||
#[test]
|
||||
fn empty_state_still_offers_create() {
|
||||
let st = WalletSelectState::default();
|
||||
assert_eq!(st.choice(), WalletChoice::Create);
|
||||
assert!(st.selected_row().is_none());
|
||||
}
|
||||
|
||||
/// A read that failed is not an empty list: the state says so.
|
||||
#[test]
|
||||
fn error_state_is_not_an_empty_list() {
|
||||
let st = WalletSelectState::error("store lock poisoned".into());
|
||||
assert!(st.error.is_some());
|
||||
assert_eq!(st.choice(), WalletChoice::Create);
|
||||
}
|
||||
|
||||
/// The card's four fields, plus the create row, on the body at the 80-column
|
||||
/// floor: name, identity prefix, locator prefix, creation date.
|
||||
#[test]
|
||||
fn wallet_select_lists_every_required_field() {
|
||||
let body = wallet_select_text(78, 21, &two_wallets());
|
||||
for needle in [
|
||||
"name", // column header
|
||||
"identity", // column header
|
||||
"locator", // column header
|
||||
"since", // column header
|
||||
"lance",
|
||||
"cold", // names
|
||||
"ab12cd34", // identity prefix
|
||||
"ff00aa11", // identity prefix of the second wallet
|
||||
"abcdefgh", // locator prefix
|
||||
"zyxwvuts", // locator prefix of the second wallet
|
||||
"2026-09-21", // creation date
|
||||
"2025-08-27", // creation date of the second wallet
|
||||
"create new wallet", // the always-last row
|
||||
] {
|
||||
assert!(body.contains(needle), "missing {needle:?}:\n{body}");
|
||||
}
|
||||
// A prefix is a prefix: the row prints an identifying start, not the whole
|
||||
// key (the full value belongs to the detail strip under the table).
|
||||
let lance_row = body
|
||||
.lines()
|
||||
.find(|l| l.contains("lance"))
|
||||
.expect("lance row");
|
||||
assert!(
|
||||
!lance_row.contains(ID_A),
|
||||
"a row printed the whole identity: {lance_row:?}"
|
||||
);
|
||||
// Truncation is disclosed with the chrome's ellipsis (… or ...).
|
||||
assert!(
|
||||
body.contains('…') || body.contains("..."),
|
||||
"no truncation marker on a prefix:\n{body}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The create row is last, and the marker is on the selection — one marker
|
||||
/// only, whichever row is focused.
|
||||
#[test]
|
||||
fn wallet_select_marks_exactly_one_row_and_puts_create_last() {
|
||||
let mut st = two_wallets();
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
let marked: Vec<&str> = body.lines().filter(|l| l.starts_with('>')).collect();
|
||||
assert_eq!(marked.len(), 1, "exactly one row is focused:\n{body}");
|
||||
assert!(marked[0].contains("lance"), "{:?}", marked[0]);
|
||||
assert!(
|
||||
body.lines().last().unwrap_or_default().contains("create"),
|
||||
"the create row is not last:\n{body}"
|
||||
);
|
||||
|
||||
st.select_next();
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
let marked: Vec<&str> = body.lines().filter(|l| l.starts_with('>')).collect();
|
||||
assert_eq!(marked.len(), 1, "exactly one row is focused:\n{body}");
|
||||
assert!(marked[0].contains("cold"), "{:?}", marked[0]);
|
||||
|
||||
// The bottom of the list is the create row and it takes the marker too.
|
||||
st.select_next();
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
let marked: Vec<&str> = body.lines().filter(|l| l.starts_with('>')).collect();
|
||||
assert_eq!(marked.len(), 1, "exactly one row is focused:\n{body}");
|
||||
assert!(
|
||||
marked[0].contains("create new wallet"),
|
||||
"the marker never reached the create row: {:?}",
|
||||
marked[0]
|
||||
);
|
||||
}
|
||||
|
||||
/// A wallet with no label still identifies itself: the primary cell is a
|
||||
/// placeholder, the row keeps its identity, and an unknown creation time is a
|
||||
/// word, not a blank or a zero.
|
||||
#[test]
|
||||
fn wallet_select_names_an_unlabelled_wallet_and_an_unknown_date() {
|
||||
let st = WalletSelectState::from_rows(vec![row("", ID_B, ONION_B, 0)]);
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
assert!(
|
||||
body.contains("(unlabelled)"),
|
||||
"a NULL label left the primary cell empty:\n{body}"
|
||||
);
|
||||
assert!(
|
||||
body.contains("ff00aa11"),
|
||||
"an unlabelled wallet lost its identity:\n{body}"
|
||||
);
|
||||
assert!(
|
||||
body.contains("unknown"),
|
||||
"an unknown creation date is blank:\n{body}"
|
||||
);
|
||||
assert!(
|
||||
!body.contains("1970-01-01"),
|
||||
"epoch rendered as a date:\n{body}"
|
||||
);
|
||||
}
|
||||
|
||||
/// A wallet whose onion service has not been published yet has no locator. That
|
||||
/// is a state, not an error: the cell says so and the row stays readable.
|
||||
#[test]
|
||||
fn wallet_select_renders_an_unpublished_locator_as_a_placeholder() {
|
||||
let st = WalletSelectState::from_rows(vec![row("fresh", ID_A, "", 1_789_963_705)]);
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
let row_line = body
|
||||
.lines()
|
||||
.find(|l| l.contains("fresh"))
|
||||
.expect("wallet row");
|
||||
assert!(
|
||||
row_line.contains("2026-09-21"),
|
||||
"an empty locator pushed the row out of shape: {row_line:?}"
|
||||
);
|
||||
assert!(
|
||||
row_line.contains(" - "),
|
||||
"an unpublished locator has no placeholder: {row_line:?}"
|
||||
);
|
||||
assert!(
|
||||
!body.to_lowercase().contains("error") && !body.to_lowercase().contains("failed"),
|
||||
"an unpublished locator read as a failure:\n{body}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Empty, failed and in-flight reads are three different states, and none of
|
||||
/// them is a blank pane. All three still offer the create row.
|
||||
#[test]
|
||||
fn wallet_select_words_the_empty_error_and_reading_states() {
|
||||
let empty = wallet_select_text(78, 21, &WalletSelectState::default());
|
||||
assert!(empty.contains("no saved wallets"), "\n{empty}");
|
||||
assert!(empty.contains("create new wallet"), "\n{empty}");
|
||||
|
||||
let failed = wallet_select_text(
|
||||
78,
|
||||
21,
|
||||
&WalletSelectState::error("store lock poisoned".into()),
|
||||
);
|
||||
assert!(failed.contains("could not read"), "\n{failed}");
|
||||
assert!(failed.contains("store lock poisoned"), "\n{failed}");
|
||||
assert!(failed.contains("create new wallet"), "\n{failed}");
|
||||
// A failed read must not read as "you have no wallets".
|
||||
assert!(!failed.contains("no saved wallets"), "\n{failed}");
|
||||
|
||||
let reading = wallet_select_text(78, 21, &WalletSelectState::reading());
|
||||
assert!(reading.contains("reading"), "\n{reading}");
|
||||
assert!(reading.contains("create new wallet"), "\n{reading}");
|
||||
}
|
||||
|
||||
/// The whole point of a prefix is that the row cannot outgrow the pane: a row
|
||||
/// with a name wide enough to swallow the table still truncates in cells.
|
||||
#[test]
|
||||
fn wallet_select_truncates_a_wide_name_in_cells() {
|
||||
let st = WalletSelectState::from_rows(vec![row(
|
||||
"私のウォレットはとても長い名前を持っています",
|
||||
ID_A,
|
||||
ONION_A,
|
||||
1_789_963_705,
|
||||
)]);
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
assert!(
|
||||
body.contains("私の"),
|
||||
"a CJK name was dropped entirely:\n{body}"
|
||||
);
|
||||
assert!(
|
||||
body.contains('…') || body.contains("..."),
|
||||
"a long name was clipped without an ellipsis:\n{body}"
|
||||
);
|
||||
for line in body.lines() {
|
||||
assert!(
|
||||
cell_width(line) <= 78,
|
||||
"line wider than the pane: {} cells: {line:?}",
|
||||
cell_width(line)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Every body line fits the pane it was laid out for, from well under the
|
||||
/// 80-column floor to 200 — trailing columns drop, nothing wraps.
|
||||
#[test]
|
||||
fn wallet_select_columns_drop_before_they_wrap() {
|
||||
for w in 20u16..=200 {
|
||||
for state in [
|
||||
two_wallets(),
|
||||
WalletSelectState::default(),
|
||||
WalletSelectState::error("store lock poisoned".into()),
|
||||
WalletSelectState::reading(),
|
||||
] {
|
||||
for line in wallet_select_lines(w, 21, &state) {
|
||||
let width: usize = line
|
||||
.spans
|
||||
.iter()
|
||||
.map(|s| cell_width(s.content.as_ref()))
|
||||
.sum();
|
||||
assert!(width <= w as usize, "width {w}: {width} cells: {line:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A long list windows and says how many rows are out of view; the create row
|
||||
/// is never the row that falls off.
|
||||
#[test]
|
||||
fn wallet_select_windows_a_long_list_and_keeps_create_visible() {
|
||||
let rows: Vec<WalletRow> = (0..40)
|
||||
.map(|i| row(&format!("w{i:02}"), ID_A, ONION_A, 1_789_963_705))
|
||||
.collect();
|
||||
let mut st = WalletSelectState::from_rows(rows);
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
assert!(body.contains("below"), "window not disclosed:\n{body}");
|
||||
assert!(
|
||||
body.contains("w00"),
|
||||
"the first row is not on screen:\n{body}"
|
||||
);
|
||||
assert!(!body.contains("w39"), "\n{body}");
|
||||
assert!(
|
||||
body.lines().last().unwrap_or_default().contains("create"),
|
||||
"the create row was pushed off the pane:\n{body}"
|
||||
);
|
||||
|
||||
for _ in 0..39 {
|
||||
st.select_next();
|
||||
}
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
assert!(
|
||||
body.contains("w39"),
|
||||
"the last row never came into view:\n{body}"
|
||||
);
|
||||
assert!(body.contains("above"), "window not disclosed:\n{body}");
|
||||
assert!(
|
||||
body.lines().last().unwrap_or_default().contains("create"),
|
||||
"the create row was pushed off the pane:\n{body}"
|
||||
);
|
||||
assert_eq!(st.choice(), WalletChoice::Open(39));
|
||||
}
|
||||
|
||||
/// The screen is not a sink for peer bytes: a label is untrusted display data.
|
||||
#[test]
|
||||
fn wallet_select_hostile_label_cannot_reach_the_terminal() {
|
||||
let hostile = "safe\u{1b}[31mred\u{7}\u{202e}\u{2066}\u{9b}\nrow\ttab";
|
||||
let st = WalletSelectState::from_rows(vec![row(hostile, ID_A, ONION_A, 1_789_963_705)]);
|
||||
for (w, h) in [(80u16, 24u16), (120, 40)] {
|
||||
let screen = render(w, h, &st);
|
||||
for (what, ch) in [
|
||||
("ESC", '\u{1b}'),
|
||||
("BEL", '\u{7}'),
|
||||
("C1", '\u{9b}'),
|
||||
("bidi", '\u{202e}'),
|
||||
("bidi isolate", '\u{2066}'),
|
||||
] {
|
||||
assert!(!screen.contains(ch), "{w}x{h}: {what} reached the frame");
|
||||
}
|
||||
assert!(screen.contains("safe"), "{w}x{h}:\n{screen}");
|
||||
}
|
||||
let body = wallet_select_text(78, 21, &st);
|
||||
assert!(body.contains("safe[31mred"), "\n{body}");
|
||||
assert_eq!(
|
||||
body.lines().filter(|l| l.contains("safe[31mred")).count(),
|
||||
1,
|
||||
"the hostile label split or duplicated a row:\n{body}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Days from unix seconds, UTC, without a date crate — including a leap day and
|
||||
/// the epoch, which reads as "unknown" rather than 1970-01-01.
|
||||
#[test]
|
||||
fn format_day_utc_covers_the_calendar_edges() {
|
||||
for (secs, want) in [
|
||||
(0i64, "unknown"),
|
||||
(-1, "unknown"),
|
||||
(1, "1970-01-01"),
|
||||
(86_400, "1970-01-02"),
|
||||
(951_782_400, "2000-02-29"),
|
||||
(1_700_000_000, "2023-11-14"),
|
||||
(1_789_963_705, "2026-09-21"),
|
||||
] {
|
||||
assert_eq!(format_day_utc(secs), want, "{secs}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The app renders this screen's one status line with `status_footer_phased`
|
||||
/// and `footer_hints`; the fixture does the same, so a dumped frame can never
|
||||
/// show a footer the app does not draw.
|
||||
fn render(w: u16, h: u16, st: &WalletSelectState) -> String {
|
||||
let backend = TestBackend::new(w, h);
|
||||
let mut terminal = Terminal::new(backend).expect("terminal");
|
||||
let footer = status_footer_phased(
|
||||
w,
|
||||
FP,
|
||||
ONION,
|
||||
Some(TorPhase::Ready),
|
||||
None,
|
||||
wallet_select_footer_hint(),
|
||||
);
|
||||
terminal
|
||||
.draw(|f| draw_wallet_select(f, st, &footer))
|
||||
.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');
|
||||
}
|
||||
screen
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_select_frame_shows_the_shipped_footer() {
|
||||
let shipped = status_footer_phased(
|
||||
80,
|
||||
FP,
|
||||
ONION,
|
||||
Some(TorPhase::Ready),
|
||||
None,
|
||||
wallet_select_footer_hint(),
|
||||
);
|
||||
let frame = render(80, 24, &two_wallets());
|
||||
let last = frame.lines().nth(23).unwrap_or_default();
|
||||
assert_eq!(
|
||||
last.trim_end(),
|
||||
shipped.trim_end(),
|
||||
"the dumped frame's footer is not the one the app renders"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_select_frames_fit_at_the_three_sizes() {
|
||||
for (w, h) in [(80u16, 24u16), (120, 40), (200, 60)] {
|
||||
let screen = render(w, h, &two_wallets());
|
||||
for needle in [
|
||||
"(o) wallets",
|
||||
"2 saved",
|
||||
"lance",
|
||||
"2026-09-21",
|
||||
"create new wallet",
|
||||
"? help",
|
||||
] {
|
||||
assert!(
|
||||
screen.contains(needle),
|
||||
"{w}x{h} missing {needle:?}:\n{screen}"
|
||||
);
|
||||
}
|
||||
for line in screen.lines() {
|
||||
assert!(
|
||||
cell_width(line) <= w as usize,
|
||||
"line wider than {w} at {w}x{h}: {line:?}"
|
||||
);
|
||||
}
|
||||
let lines = wallet_select_lines(w - 2, h - 3, &two_wallets());
|
||||
assert!(
|
||||
lines.len() <= (h - 3) as usize,
|
||||
"body needs {} rows, {w}x{h} shows {}",
|
||||
lines.len(),
|
||||
h - 3
|
||||
);
|
||||
}
|
||||
// 200x60 fits the whole identity and locator in the detail strip.
|
||||
let wide = render(200, 60, &two_wallets());
|
||||
assert!(
|
||||
wide.contains(ID_A),
|
||||
"200x60 truncated the identity:\n{wide}"
|
||||
);
|
||||
assert!(
|
||||
wide.contains(ONION_A),
|
||||
"200x60 truncated the locator:\n{wide}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wallet_select_frame_dumps() {
|
||||
for (w, h) in [(80u16, 24u16), (120, 40), (200, 60)] {
|
||||
println!("===== WALLETS {w}x{h} =====");
|
||||
print!("{}", render(w, h, &two_wallets()));
|
||||
println!("===== WALLETS (empty) {w}x{h} =====");
|
||||
print!("{}", render(w, h, &WalletSelectState::default()));
|
||||
println!("===== WALLETS (failed read) {w}x{h} =====");
|
||||
print!(
|
||||
"{}",
|
||||
render(
|
||||
w,
|
||||
h,
|
||||
&WalletSelectState::error("store lock poisoned".into())
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue