summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-10-28 22:36:07 +0100
committersalmonik <salmonik@tuta.io>2026-10-28 22:36:07 +0100
commit25f9abeb4c69024f8d9dceb472ba33c03e00daa0 (patch)
treea02a78f04b5afa2e3fbebffb524f1568df8f00d8 /tests
parentv0.2.4: fix command UX, completion wrapping and auth prompt cancellation (diff)
downloadzagan-bet-25f9abeb4c69024f8d9dceb472ba33c03e00daa0.tar.gz
zagan-bet-25f9abeb4c69024f8d9dceb472ba33c03e00daa0.zip
v0.2.5: fix terminal width, ping timing and output formatting
Diffstat (limited to '')
-rw-r--r--tests/client_core.rs89
-rw-r--r--tests/client_ui.rs65
2 files changed, 152 insertions, 2 deletions
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() {
386 assert_eq!(h.screen.prompt, ""); 386 assert_eq!(h.screen.prompt, "");
387 assert_eq!(h.screen.prompt_cursor, 2); 387 assert_eq!(h.screen.prompt_cursor, 2);
388} 388}
389
390#[test]
391fn info_shows_the_host_and_the_server_fields() {
392 let mut h = Harness::new();
393 h.core.set_host_info(
394 Some("127.0.0.1:11525".to_string()),
395 Some("deadbeef".to_string()),
396 );
397
398 h.line("info");
399 assert!(h
400 .screen
401 .lines
402 .iter()
403 .any(|line| line.contains("Connected to: 127.0.0.1:11525")));
404 assert!(h
405 .screen
406 .lines
407 .iter()
408 .any(|line| line.contains("Fingerprint: deadbeef")));
409
410 h.reply("INFO alice 42 1234");
411
412 assert!(h.screen.lines.iter().any(|line| line == "User: alice"));
413 assert!(h.screen.lines.iter().any(|line| line == "Account: 42"));
414 assert!(h
415 .screen
416 .lines
417 .iter()
418 .any(|line| line == "Balance: 1234 PLN"));
419}
420
421#[test]
422fn info_without_a_connection_does_not_send() {
423 let mut h = Harness::new();
424 h.core.set_connected(false);
425
426 h.line("info");
427
428 assert!(h
429 .screen
430 .lines
431 .iter()
432 .any(|line| line.contains("Not connected to any host")));
433 assert!(h.sent.is_empty());
434}
435
436#[test]
437fn ping_prints_pong() {
438 let mut h = Harness::new();
439 h.line("ping");
440 assert_eq!(h.sent.last().map(String::as_str), Some("PING"));
441
442 h.reply("PONG");
443
444 assert!(h.screen.lines.iter().any(|line| line == "Pong"));
445}
446
447#[test]
448fn send_prints_the_echo() {
449 let mut h = Harness::new();
450 h.line("send hello");
451 h.reply("hello");
452
453 assert!(h
454 .screen
455 .lines
456 .iter()
457 .any(|line| line == "Echo: hello"));
458}
459
460#[test]
461fn disconnect_clears_connection_state() {
462 let mut h = Harness::new();
463 h.line("disconnect");
464
465 assert!(h
466 .screen
467 .lines
468 .iter()
469 .any(|line| line == "Disconnected"));
470
471 h.line("play slots");
472 assert!(h
473 .screen
474 .lines
475 .iter()
476 .any(|line| line.contains("Not connected")));
477}
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 @@
1use zagan_bet::client::ui::{ 1use zagan_bet::client::ui::{
2 command_names, completion_lines, help_lines, input_cursor_column, lines_to_move_up, 2 command_names, completion_lines, display_width, first_contact_lines, format_pong,
3 parse_bet_arg, record_history, 3 help_lines, host_lines, input_cursor_column, lines_to_move_up, parse_bet_arg, record_history,
4}; 4};
5use std::time::Duration;
5 6
6#[test] 7#[test]
7fn help_has_no_source_indentation() { 8fn help_has_no_source_indentation() {
@@ -129,3 +130,63 @@ fn completions_wrap_within_the_requested_width() {
129 ); 130 );
130 } 131 }
131} 132}
133
134#[test]
135fn display_width_counts_terminal_columns_not_bytes() {
136 assert_eq!(display_width("abc"), 3);
137 assert_eq!(display_width("ąćęłńóśźż"), 9);
138 assert_eq!(display_width("中"), 2);
139 assert_eq!(display_width("🎉"), 2);
140 assert_eq!(display_width("e\u{0301}"), 1);
141}
142
143#[test]
144fn cursor_column_uses_display_width() {
145 assert_eq!(input_cursor_column(2, display_width("ąć")), 4);
146 assert_eq!(input_cursor_column(2, display_width("中")), 4);
147}
148
149#[test]
150fn first_contact_message_stays_within_80_columns() {
151 let fingerprint = "a4d6f80784154c2cb739a1df61ab3e9038ed731ee2e05c7611457ac741b3834c";
152 let lines = first_contact_lines("127.0.0.1:11525", fingerprint);
153
154 assert!(lines.iter().any(|line| line.contains("127.0.0.1:11525")));
155 assert!(lines.iter().any(|line| line.contains(fingerprint)));
156 for line in lines {
157 assert!(
158 line.len() <= 80,
159 "first-contact line is too wide ({}): {line:?}",
160 line.len()
161 );
162 }
163}
164
165#[test]
166fn pong_is_formatted_to_one_decimal() {
167 assert_eq!(format_pong(Duration::from_micros(144)), "Pong (0.1ms)");
168 assert_eq!(format_pong(Duration::from_millis(41)), "Pong (41.0ms)");
169}
170
171#[test]
172fn host_lines_fit_an_80_column_terminal() {
173 let fingerprint = "a4d6f80784154c2cb739a1df61ab3e9038ed731ee2e05c7611457ac741b3834c";
174 let lines = host_lines(&[
175 (
176 "127.0.0.1:11525".to_string(),
177 Some(fingerprint.to_string()),
178 ),
179 ("unknown.example:443".to_string(), None),
180 ]);
181
182 assert!(lines.iter().any(|line| line.contains("127.0.0.1:11525")));
183 assert!(lines.iter().any(|line| line.contains(fingerprint)));
184 assert!(lines.iter().any(|line| line.contains("no fingerprint")));
185 for line in lines {
186 assert!(
187 line.len() <= 80,
188 "host line is too wide ({}): {line:?}",
189 line.len()
190 );
191 }
192}