1
use super::*;
2
use crate::tabs::fetch::Fetch;
3

            
4
8
fn list_wire(id: i64, entries: &[(&str, &str)]) -> String {
5
8
    let plists: String = entries
6
8
        .iter()
7
13
        .map(|(k, v)| format!(r#"(:KEY "{k}" :VALUE "{v}")"#))
8
        .collect();
9
8
    let value = if plists.is_empty() {
10
1
        "nil".to_string()
11
    } else {
12
7
        format!("({plists})")
13
    };
14
8
    format!("(:id {id} :value {value})")
15
8
}
16

            
17
1
fn error_wire(id: i64, code: &str, msg: &str) -> String {
18
1
    format!(r#"(:id {id} :error (:code {code} :message "{msg}"))"#)
19
1
}
20

            
21
1
fn loaded(tab: &ConfigTab) -> Vec<(&str, &str)> {
22
1
    tab.rows()
23
1
        .iter()
24
2
        .map(|(k, v)| (k.as_str(), v.as_str()))
25
        .collect()
26
1
}
27

            
28
#[test]
29
1
fn on_reply_lists_every_key_with_its_value() {
30
1
    let mut tab = ConfigTab::new();
31
1
    tab.set_loading(1);
32
1
    tab.on_reply(&list_wire(1, &[("alpha", "1"), ("zeta", "")]));
33
1
    assert_eq!(loaded(&tab), [("alpha", "1"), ("zeta", "")]);
34
1
}
35

            
36
#[test]
37
1
fn on_reply_nil_shows_no_rows() {
38
1
    let mut tab = ConfigTab::new();
39
1
    tab.on_reply(&list_wire(1, &[]));
40
1
    assert_eq!(tab.state, Fetch::Loaded(()));
41
1
    assert!(tab.rows().is_empty());
42
1
}
43

            
44
#[test]
45
1
fn on_reply_server_error_maps_to_error() {
46
1
    let mut tab = ConfigTab::new();
47
1
    tab.on_reply(&error_wire(2, "db", "connection refused"));
48
1
    assert!(matches!(&tab.state, Fetch::Error(m) if m.contains("connection refused")));
49
1
}
50

            
51
#[test]
52
1
fn on_reply_malformed_entry_maps_to_error() {
53
1
    let mut tab = ConfigTab::new();
54
1
    tab.on_reply(r#"(:id 3 :value ((:KEY "a")))"#);
55
1
    assert!(matches!(&tab.state, Fetch::Error(m) if m.contains("malformed")));
56
1
    tab.on_reply(r#"(:id 3 :value "text")"#);
57
1
    assert!(matches!(&tab.state, Fetch::Error(m) if m.contains("unexpected")));
58
1
}
59

            
60
#[test]
61
1
fn select_next_and_prev_stay_in_bounds() {
62
1
    let mut tab = ConfigTab::new();
63
1
    tab.on_reply(&list_wire(1, &[("a", "1"), ("b", "2")]));
64
1
    tab.select_prev();
65
1
    assert_eq!(tab.selected, 0);
66
1
    for _ in 0..100 {
67
100
        tab.select_next();
68
    }
69
1
    assert_eq!(tab.selected, 1);
70
1
}
71

            
72
#[test]
73
1
fn a_reload_keeps_the_selection_on_its_key() {
74
1
    let mut tab = ConfigTab::new();
75
1
    tab.on_reply(&list_wire(1, &[("b", "2"), ("c", "3")]));
76
1
    tab.select_next();
77
1
    assert_eq!(tab.selected_key(), Some("c"));
78
1
    tab.on_reply(&list_wire(2, &[("a", "1"), ("b", "2"), ("c", "3")]));
79
1
    assert_eq!(tab.selected_key(), Some("c"));
80
1
}
81

            
82
#[test]
83
1
fn reset_returns_to_idle_and_keeps_the_rows() {
84
1
    let mut tab = ConfigTab::new();
85
1
    tab.on_reply(&list_wire(1, &[("a", "1")]));
86
1
    tab.reset();
87
1
    assert!(tab.is_idle());
88
1
    assert_eq!(tab.rows().len(), 1);
89
1
}
90

            
91
#[test]
92
1
fn selected_edit_follows_the_selected_row() {
93
1
    let mut tab = ConfigTab::new();
94
1
    assert_eq!(tab.selected_edit(), None);
95
1
    tab.on_reply(&list_wire(1, &[("a", "1"), ("b", "2")]));
96
1
    assert_eq!(
97
1
        tab.selected_edit(),
98
1
        Some(("a".to_string(), "1".to_string()))
99
    );
100
1
    tab.select_next();
101
1
    assert_eq!(
102
1
        tab.selected_edit(),
103
1
        Some(("b".to_string(), "2".to_string()))
104
    );
105
1
}
106

            
107
#[test]
108
1
fn any_loading_tracks_the_in_flight_request() {
109
1
    let mut tab = ConfigTab::new();
110
1
    assert!(!tab.any_loading());
111
1
    tab.set_loading(7);
112
1
    assert!(tab.any_loading());
113
1
    tab.on_reply(&list_wire(7, &[("a", "1")]));
114
1
    assert!(!tab.any_loading());
115
1
}