From 25f9abeb4c69024f8d9dceb472ba33c03e00daa0 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 28 Oct 2026 22:36:07 +0100 Subject: v0.2.5: fix terminal width, ping timing and output formatting --- tests/client_core.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/client_ui.rs | 65 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 152 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/client_core.rs b/tests/client_core.rs index 79b3dd8..c298ab7 100644 --- a/tests/client_core.rs +++ b/tests/client_core.rs @@ -386,3 +386,92 @@ fn prompt_cursor_tracks_the_input() { assert_eq!(h.screen.prompt, ""); assert_eq!(h.screen.prompt_cursor, 2); } + +#[test] +fn info_shows_the_host_and_the_server_fields() { + let mut h = Harness::new(); + h.core.set_host_info( + Some("127.0.0.1:11525".to_string()), + Some("deadbeef".to_string()), + ); + + h.line("info"); + assert!(h + .screen + .lines + .iter() + .any(|line| line.contains("Connected to: 127.0.0.1:11525"))); + assert!(h + .screen + .lines + .iter() + .any(|line| line.contains("Fingerprint: deadbeef"))); + + h.reply("INFO alice 42 1234"); + + assert!(h.screen.lines.iter().any(|line| line == "User: alice")); + assert!(h.screen.lines.iter().any(|line| line == "Account: 42")); + assert!(h + .screen + .lines + .iter() + .any(|line| line == "Balance: 1234 PLN")); +} + +#[test] +fn info_without_a_connection_does_not_send() { + let mut h = Harness::new(); + h.core.set_connected(false); + + h.line("info"); + + assert!(h + .screen + .lines + .iter() + .any(|line| line.contains("Not connected to any host"))); + assert!(h.sent.is_empty()); +} + +#[test] +fn ping_prints_pong() { + let mut h = Harness::new(); + h.line("ping"); + assert_eq!(h.sent.last().map(String::as_str), Some("PING")); + + h.reply("PONG"); + + assert!(h.screen.lines.iter().any(|line| line == "Pong")); +} + +#[test] +fn send_prints_the_echo() { + let mut h = Harness::new(); + h.line("send hello"); + h.reply("hello"); + + assert!(h + .screen + .lines + .iter() + .any(|line| line == "Echo: hello")); +} + +#[test] +fn disconnect_clears_connection_state() { + let mut h = Harness::new(); + h.line("disconnect"); + + assert!(h + .screen + .lines + .iter() + .any(|line| line == "Disconnected")); + + h.line("play slots"); + assert!(h + .screen + .lines + .iter() + .any(|line| line.contains("Not connected"))); +} diff --git a/tests/client_ui.rs b/tests/client_ui.rs index 78faed0..a22fbe7 100644 --- a/tests/client_ui.rs +++ b/tests/client_ui.rs @@ -1,7 +1,8 @@ use zagan_bet::client::ui::{ - command_names, completion_lines, help_lines, input_cursor_column, lines_to_move_up, - parse_bet_arg, record_history, + command_names, completion_lines, display_width, first_contact_lines, format_pong, + help_lines, host_lines, input_cursor_column, lines_to_move_up, parse_bet_arg, record_history, }; +use std::time::Duration; #[test] fn help_has_no_source_indentation() { @@ -129,3 +130,63 @@ fn completions_wrap_within_the_requested_width() { ); } } + +#[test] +fn display_width_counts_terminal_columns_not_bytes() { + assert_eq!(display_width("abc"), 3); + assert_eq!(display_width("ąćęłńóśźż"), 9); + assert_eq!(display_width("中"), 2); + assert_eq!(display_width("🎉"), 2); + assert_eq!(display_width("e\u{0301}"), 1); +} + +#[test] +fn cursor_column_uses_display_width() { + assert_eq!(input_cursor_column(2, display_width("ąć")), 4); + assert_eq!(input_cursor_column(2, display_width("中")), 4); +} + +#[test] +fn first_contact_message_stays_within_80_columns() { + let fingerprint = "a4d6f80784154c2cb739a1df61ab3e9038ed731ee2e05c7611457ac741b3834c"; + let lines = first_contact_lines("127.0.0.1:11525", fingerprint); + + assert!(lines.iter().any(|line| line.contains("127.0.0.1:11525"))); + assert!(lines.iter().any(|line| line.contains(fingerprint))); + for line in lines { + assert!( + line.len() <= 80, + "first-contact line is too wide ({}): {line:?}", + line.len() + ); + } +} + +#[test] +fn pong_is_formatted_to_one_decimal() { + assert_eq!(format_pong(Duration::from_micros(144)), "Pong (0.1ms)"); + assert_eq!(format_pong(Duration::from_millis(41)), "Pong (41.0ms)"); +} + +#[test] +fn host_lines_fit_an_80_column_terminal() { + let fingerprint = "a4d6f80784154c2cb739a1df61ab3e9038ed731ee2e05c7611457ac741b3834c"; + let lines = host_lines(&[ + ( + "127.0.0.1:11525".to_string(), + Some(fingerprint.to_string()), + ), + ("unknown.example:443".to_string(), None), + ]); + + assert!(lines.iter().any(|line| line.contains("127.0.0.1:11525"))); + assert!(lines.iter().any(|line| line.contains(fingerprint))); + assert!(lines.iter().any(|line| line.contains("no fingerprint"))); + for line in lines { + assert!( + line.len() <= 80, + "host line is too wide ({}): {line:?}", + line.len() + ); + } +} -- cgit v1.2.3