1
use super::*;
2

            
3
5
fn config_wire(id: i64, config_val: &str) -> String {
4
5
    format!(r#"(:id {id} :value "(:config-value \"{config_val}\")")"#)
5
5
}
6

            
7
1
fn error_wire(id: i64, code: &str, msg: &str) -> String {
8
1
    format!(r#"(:id {id} :error (:code {code} :message "{msg}"))"#)
9
1
}
10

            
11
#[test]
12
1
fn on_reply_loaded_maps_to_loaded_value() {
13
1
    let mut tab = ConfigTab::new();
14
1
    tab.set_loading("locale", 1);
15
1
    tab.on_reply("locale", &config_wire(1, "en_US"));
16
1
    let (_, cell) = tab.entries.iter().find(|(k, _)| k == "locale").unwrap();
17
1
    assert!(matches!(cell, ConfigCell::Loaded(v) if v == "en_US"));
18
1
}
19

            
20
#[test]
21
1
fn on_reply_nil_value_maps_to_unset() {
22
    // An unset key returns a successful `(:config-value nil)`, not an error.
23
1
    let mut tab = ConfigTab::new();
24
1
    tab.set_loading("locale", 2);
25
1
    tab.on_reply("locale", r#"(:id 2 :value "(:config-value nil)")"#);
26
1
    let (_, cell) = tab.entries.iter().find(|(k, _)| k == "locale").unwrap();
27
1
    assert!(matches!(cell, ConfigCell::Unset), "got {cell:?}");
28
1
}
29

            
30
#[test]
31
1
fn on_reply_server_error_maps_to_error() {
32
    // A genuine server failure must surface as Error, NOT be hidden as unset.
33
1
    let mut tab = ConfigTab::new();
34
1
    tab.set_loading("locale", 2);
35
1
    tab.on_reply("locale", &error_wire(2, "db", "connection refused"));
36
1
    let (_, cell) = tab.entries.iter().find(|(k, _)| k == "locale").unwrap();
37
1
    assert!(matches!(cell, ConfigCell::Error(_)), "got {cell:?}");
38
1
}
39

            
40
#[test]
41
1
fn on_reply_only_affects_keyed_entry() {
42
1
    let mut tab = ConfigTab::new();
43
1
    tab.set_loading("locale", 3);
44
1
    tab.set_loading("userregistrytimeout", 4);
45
1
    tab.on_reply("locale", &config_wire(3, "fr_FR"));
46
1
    let (_, timeout_cell) = tab
47
1
        .entries
48
1
        .iter()
49
2
        .find(|(k, _)| k == "userregistrytimeout")
50
1
        .unwrap();
51
1
    assert!(matches!(timeout_cell, ConfigCell::Loading { id: 4 }));
52
1
}
53

            
54
#[test]
55
1
fn build_set_config_form_basic() {
56
1
    let form = build_set_config_form("locale", "en_US");
57
1
    assert_eq!(form, r#"(set-config "locale" "en_US")"#);
58
1
}
59

            
60
#[test]
61
1
fn build_set_config_form_escapes_quotes() {
62
1
    let form = build_set_config_form("key", r#"val"with"quotes"#);
63
1
    assert!(form.contains(r#"\"with\""#));
64
1
}
65

            
66
#[test]
67
1
fn selected_key_returns_first_by_default() {
68
1
    let tab = ConfigTab::new();
69
1
    assert_eq!(tab.selected_key(), Some("locale"));
70
1
}
71

            
72
#[test]
73
1
fn select_next_and_prev_stay_in_bounds() {
74
1
    let mut tab = ConfigTab::new();
75
1
    tab.select_prev();
76
1
    assert_eq!(tab.selected, 0);
77
100
    for _ in 0..100 {
78
100
        tab.select_next();
79
100
    }
80
1
    assert_eq!(tab.selected, KNOWN_CONFIG_KEYS.len() - 1);
81
1
}
82

            
83
#[test]
84
1
fn reset_clears_all_entries_to_unset() {
85
1
    let mut tab = ConfigTab::new();
86
1
    tab.on_reply("locale", &config_wire(5, "de_DE"));
87
1
    tab.reset();
88
1
    assert!(
89
1
        tab.entries
90
1
            .iter()
91
2
            .all(|(_, c)| matches!(c, ConfigCell::Unset))
92
    );
93
1
}
94

            
95
#[test]
96
1
fn is_idle_true_only_when_all_unset() {
97
1
    let mut tab = ConfigTab::new();
98
1
    assert!(tab.is_idle());
99
1
    tab.set_loading("locale", 1);
100
1
    assert!(!tab.is_idle());
101
1
}
102

            
103
#[test]
104
1
fn selected_value_returns_loaded_string() {
105
1
    let mut tab = ConfigTab::new();
106
1
    tab.on_reply("locale", &config_wire(1, "ja_JP"));
107
1
    assert_eq!(tab.selected_value(), "ja_JP");
108
1
}
109

            
110
#[test]
111
1
fn selected_value_empty_when_unset() {
112
1
    let tab = ConfigTab::new();
113
1
    assert_eq!(tab.selected_value(), "");
114
1
}
115

            
116
#[test]
117
1
fn any_loading_tracks_in_flight_requests() {
118
1
    let mut tab = ConfigTab::new();
119
1
    assert!(!tab.any_loading());
120
1
    tab.set_loading("locale", 7);
121
1
    assert!(tab.any_loading());
122
1
    tab.on_reply("locale", &config_wire(7, "en"));
123
1
    assert!(!tab.any_loading());
124
1
}