fix(tui): invite payload gets a chrome-free region so mouse-copy is exact
Some checks failed
android-ci / android (pull_request) Has been cancelled
ci / test (pull_request) Has been cancelled
ci / sdk (pull_request) Has been cancelled
ci / fuzz (pull_request) Has been cancelled

Mouse-selecting the invite copied the pane border: draw_share rendered the
payload inside themed_block, which is Borders::ALL, so every wrapped row sat
between a leading and a trailing glyph. OnionWire enables no mouse capture,
so the terminal's own drag-select is the copy path and it picks up chrome.

The payload now has its own region — flush at column 0, full frame width,
no block, no title, no gutter on any row it occupies — with the block
(title, fp, onion, copy hint) above it. Measured in cells, not chars.

Also add C: OSC 52 hand the payload to the terminal clipboard (same
sequence the F9 menu uses), with the key named on the screen and the mouse
selection named as the fallback for terminals that block the sequence.

Regression tests reconstruct the payload out of the rendered frame at
80x24, 120x40, 200x60 and 60x20 (they fail on base: the leading border
column) and assert no payload row carries a border glyph.
This commit is contained in:
agent 2026-09-18 23:54:07 -04:00
commit c3116eb19e
No known key found for this signature in database
3 changed files with 308 additions and 13 deletions

View file

@ -7,6 +7,7 @@ use onionwire::tui::{
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() {
@ -338,7 +339,177 @@ fn share_screen_shows_invite_not_qr_at_80x24() {
for ch in ['█', '▀', '▄'] {
assert!(
!screen.contains(ch),
"share screen still has QR block {ch:?}:\n{screen}"
"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::<Vec<_>>()
.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],
)
.expect("encode");
for (w, h) in [(80u16, 24u16), (120, 40), (200, 60), (60, 20)] {
let buf = share_frame(w, h, &payload);
let rows: Vec<String> = (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();
assert_eq!(
rebuilt,
payload,
"mouse-selecting the invite at {w}x{h} does not rebuild the payload:\n\
got {rebuilt:?}\nwant {payload:?}\nframe:\n{}",
frame_text(&buf, w, h)
);
}
}
#[test]
fn share_payload_rows_carry_no_border_glyph() {
let payload = qr::encode(
&[7u8; 32],
"abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion",
&[1u8; 32],
)
.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],
)
.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<u8> {
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
}