1
//! The Config tab lists every key from `list-config`; a stale reply is dropped.
2

            
3
use super::App;
4
use crate::tabs::fetch::Fetch;
5

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

            
14
3
fn loading_id(app: &App) -> anyhow::Result<i64> {
15
3
    match app.config().state {
16
3
        Fetch::Loading { id } => Ok(id),
17
        ref other => anyhow::bail!("not loading: {other:?}"),
18
    }
19
3
}
20

            
21
#[tokio::test]
22
1
async fn the_reply_populates_the_tab_with_all_keys() -> anyhow::Result<()> {
23
1
    let mut app = crate::test_support::app();
24
1
    app.attach_echo();
25

            
26
1
    app.fetch_config(app.window_of(crate::view::ViewId::Config));
27
1
    let id = loading_id(&app)?;
28
1
    app.route_reply(id, &list_wire(id, &[("alpha", "1"), ("theme", "dark")]));
29

            
30
1
    let shown: Vec<_> = app
31
1
        .config()
32
1
        .rows()
33
1
        .iter()
34
2
        .map(|(k, v)| (k.as_str(), v.as_str()))
35
        .collect();
36
1
    assert_eq!(shown, [("alpha", "1"), ("theme", "dark")]);
37
2
    Ok(())
38
1
}
39

            
40
#[tokio::test]
41
1
async fn a_stale_reply_is_dropped() -> anyhow::Result<()> {
42
1
    let mut app = crate::test_support::app();
43
1
    app.attach_echo();
44

            
45
1
    app.fetch_config(app.window_of(crate::view::ViewId::Config));
46
1
    let stale = loading_id(&app)?;
47
1
    app.fetch_config(app.window_of(crate::view::ViewId::Config));
48
1
    let current = loading_id(&app)?;
49
1
    app.route_reply(stale, &list_wire(stale, &[("old", "x")]));
50
1
    assert_eq!(app.config().state, Fetch::Loading { id: current });
51

            
52
1
    app.route_reply(current, &list_wire(current, &[("new", "y")]));
53
1
    assert!(app.config().rows().iter().any(|(k, _)| k == "new"));
54
2
    Ok(())
55
1
}