summaryrefslogtreecommitdiffstats
path: root/tests/client_ui.rs
blob: f5e3c0e1c94864d663c0ae449b66647284d3adfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use zagan_bet::client::ui::{
    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,
    visual_line_count,
};
use std::time::Duration;

#[test]
fn help_has_no_source_indentation() {
    for line in help_lines() {
        assert!(
            !line.starts_with("        "),
            "help line kept its source indentation: {line:?}"
        );
    }
}

#[test]
fn help_documents_every_command() {
    let help = help_lines().join("\n");
    for command in command_names() {
        assert!(help.contains(command), "help text is missing '{command}'");
    }
}

#[test]
fn completion_knows_every_command() {
    let commands = command_names();
    for expected in [
        "connect",
        "games",
        "play",
        "hosts",
        "send",
        "logout",
        "quit",
    ] {
        assert!(
            commands.contains(&expected),
            "tab completion is missing '{expected}'"
        );
    }
}

#[test]
fn history_records_a_command_once() {
    let mut history = Vec::new();
    record_history(&mut history, "info");
    record_history(&mut history, "info");

    assert_eq!(history, vec!["info"]);
}

#[test]
fn history_keeps_distinct_commands_in_order() {
    let mut history = Vec::new();
    record_history(&mut history, "info");
    record_history(&mut history, "ping");
    record_history(&mut history, "info");

    assert_eq!(history, vec!["info", "ping", "info"]);
}

#[test]
fn blank_commands_are_not_recorded() {
    let mut history = Vec::new();
    record_history(&mut history, "");
    record_history(&mut history, "   ");

    assert!(history.is_empty());
}

#[test]
fn bet_accepts_ten_or_more() {
    assert_eq!(parse_bet_arg("10"), Ok(10));
    assert_eq!(parse_bet_arg("50"), Ok(50));
    assert_eq!(parse_bet_arg(" 250 "), Ok(250));
}

#[test]
fn bet_rejects_less_than_ten() {
    assert!(parse_bet_arg("9").is_err());
    assert!(parse_bet_arg("0").is_err());
}

#[test]
fn bet_rejects_non_numbers() {
    assert!(parse_bet_arg("abc").is_err());
    assert!(parse_bet_arg("-50").is_err());
    assert!(parse_bet_arg("").is_err());
}

#[test]
fn redraw_moves_up_by_line_count_minus_one() {
    assert_eq!(lines_to_move_up(0), 0);
    assert_eq!(lines_to_move_up(1), 0);
    assert_eq!(lines_to_move_up(7), 6);
    assert_eq!(lines_to_move_up(70_000), u16::MAX);
}

#[test]
fn display_width_ignores_ansi_sequences() {
    assert_eq!(display_width("\x1b[31m18\x1b[0m"), 2);
    assert_eq!(display_width("\x1b[1;31m[ 18 ]\x1b[0m"), 6);
    assert_eq!(display_width("plain"), 5);
}

#[test]
fn visual_line_count_ignores_ansi_sequences() {
    let styled = format!("\x1b[31m{}\x1b[0m", "x".repeat(75));
    assert_eq!(visual_line_count(&[styled], 72), 2);
    assert_eq!(
        visual_line_count(&["\x1b[31m18\x1b[0m".to_string()], 80),
        1
    );
}

#[test]
fn visual_line_count_accounts_for_terminal_wrapping() {
    let lines = vec![
        "12345".to_string(),
        "123456".to_string(),
        String::new(),
    ];

    assert_eq!(visual_line_count(&lines, 5), 4); // 1 + 2 + 1
    assert_eq!(visual_line_count(&lines, 6), 3); // 1 + 1 + 1
    assert_eq!(visual_line_count(&lines, 80), 3);
    assert_eq!(visual_line_count(&[], 80), 0);

    let wide = "x".repeat(75);
    assert_eq!(visual_line_count(&[wide.clone()], 75), 1);
    assert_eq!(visual_line_count(&[wide], 72), 2);
}

#[test]
fn input_cursor_is_after_the_removed_character() {
    assert_eq!(input_cursor_column(10, 8), 18);
    assert_eq!(input_cursor_column(10, 0), 10);
    assert_eq!(input_cursor_column(2, 5), 7);
    assert_eq!(input_cursor_column(70_000, 70_000), u16::MAX);
}

#[test]
fn completions_wrap_within_the_requested_width() {
    let commands = command_names();
    let lines = completion_lines(&commands, 40);

    assert_eq!(lines.first().map(String::as_str), Some("Possible completions:"));
    for line in &lines[1..] {
        assert!(
            line.len() <= 40,
            "completion line is too wide ({} > 40): {line:?}",
            line.len()
        );
    }

    let listed: Vec<&str> = lines[1..]
        .iter()
        .flat_map(|line| line.split_whitespace())
        .collect();
    for command in commands {
        assert!(
            listed.contains(&command),
            "completion list is missing '{command}'"
        );
    }
}

#[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()
        );
    }
}