1
//! Two windows of one kind: a reply reaches the window that asked and no other.
2

            
3
use super::*;
4
use crate::tabs::list::ListTab;
5
use crate::tabs::reports::{ReportKind, ReportsTab};
6
use crate::tabs::ssh_keys::SshKeysTab;
7
use crate::window::{View, WindowId};
8
use forms::FormValues;
9

            
10
4
fn error_wire(id: i64) -> String {
11
4
    format!(r#"(:id {id} :error (:code db :message "boom"))"#)
12
4
}
13

            
14
4
fn list_state(app: &mut App, window: WindowId) -> Option<Fetch<Vec<ListRow>>> {
15
4
    app.win_mut::<ListTab>(window)
16
4
        .map(|list| list.state.clone())
17
4
}
18

            
19
6
fn keys_state(app: &mut App, window: WindowId) -> Option<Fetch<()>> {
20
6
    app.win_mut::<SshKeysTab>(window)
21
6
        .map(|keys| keys.state.clone())
22
6
}
23

            
24
5
fn loading_id<T>(state: Option<Fetch<T>>) -> anyhow::Result<i64> {
25
5
    match state {
26
5
        Some(Fetch::Loading { id }) => Ok(id),
27
        _ => anyhow::bail!("not loading"),
28
    }
29
5
}
30

            
31
1
fn second_transactions(app: &mut App) -> WindowId {
32
1
    app.open_new_window(View::List(ViewId::Transactions, ListTab::paged()))
33
1
}
34

            
35
#[tokio::test]
36
1
async fn a_page_reply_reaches_only_its_transactions_window() -> anyhow::Result<()> {
37
1
    let mut app = crate::test_support::app();
38
1
    app.attach_echo();
39
1
    let first = app.window_of(ViewId::Transactions);
40
1
    let second = second_transactions(&mut app);
41
1
    app.ensure_list_loaded(first);
42
1
    app.ensure_list_loaded(second);
43
1
    let id = loading_id(list_state(&mut app, first))?;
44
1
    let other = loading_id(list_state(&mut app, second))?;
45

            
46
1
    app.route_reply(id, &error_wire(id));
47

            
48
1
    assert!(matches!(list_state(&mut app, first), Some(Fetch::Error(_))));
49
1
    assert_eq!(
50
1
        list_state(&mut app, second),
51
1
        Some(Fetch::Loading { id: other })
52
    );
53
2
    Ok(())
54
1
}
55

            
56
#[test]
57
1
fn a_report_reply_reaches_only_its_reports_window() {
58
1
    let mut app = crate::test_support::app();
59
1
    let first = app.window_of(ViewId::Reports);
60
1
    let second = app.open_new_window(View::Reports(ReportsTab::new()));
61
1
    let route = Route {
62
1
        target: Some(first),
63
1
        ctx: RouteCtx::Reports {
64
1
            kind: ReportKind::Balance,
65
1
            seq: 0,
66
1
            values: FormValues::default(),
67
1
        },
68
1
    };
69

            
70
1
    app.deliver_reply(route, &error_wire(1));
71

            
72
1
    assert!(matches!(
73
1
        app.win_mut::<ReportsTab>(first)
74
1
            .map(|tab| tab.state.clone()),
75
        Some(Fetch::Error(_))
76
    ));
77
1
    assert_eq!(
78
1
        app.win_mut::<ReportsTab>(second)
79
1
            .map(|tab| tab.state.clone()),
80
1
        Some(Fetch::Idle)
81
    );
82
1
}
83

            
84
#[tokio::test]
85
1
async fn a_list_reply_reaches_only_its_ssh_keys_window() -> anyhow::Result<()> {
86
1
    let mut app = crate::test_support::app();
87
1
    app.attach_echo();
88
1
    let first = app.window_of(ViewId::SshKeys);
89
1
    let second = app.open_new_window(View::SshKeys(SshKeysTab::new()));
90
1
    app.fetch_ssh_keys(first);
91
1
    app.fetch_ssh_keys(second);
92
1
    let id = loading_id(keys_state(&mut app, first))?;
93
1
    let other = loading_id(keys_state(&mut app, second))?;
94

            
95
1
    app.route_reply(id, &error_wire(id));
96

            
97
1
    assert!(matches!(keys_state(&mut app, first), Some(Fetch::Error(_))));
98
1
    assert_eq!(
99
1
        keys_state(&mut app, second),
100
1
        Some(Fetch::Loading { id: other })
101
    );
102
2
    Ok(())
103
1
}
104

            
105
#[tokio::test]
106
1
async fn a_reply_to_a_closed_window_is_dropped() -> anyhow::Result<()> {
107
1
    let mut app = crate::test_support::app();
108
1
    app.attach_echo();
109
1
    let first = app.window_of(ViewId::SshKeys);
110
1
    let second = app.open_new_window(View::SshKeys(SshKeysTab::new()));
111
1
    app.fetch_ssh_keys(first);
112
1
    let waiting = loading_id(keys_state(&mut app, first))?;
113
1
    let id = 900;
114
1
    app.pending_routes.insert(
115
1
        id,
116
1
        Route {
117
1
            target: Some(second),
118
1
            ctx: RouteCtx::SshKeyRemove {
119
1
                fingerprint: "fp".into(),
120
1
            },
121
1
        },
122
1
    );
123
1
    app.close_window(second);
124

            
125
1
    app.route_reply(id, &error_wire(id));
126

            
127
1
    assert_eq!(
128
1
        keys_state(&mut app, first),
129
1
        Some(Fetch::Loading { id: waiting })
130
    );
131
1
    assert!(!app.pending_routes.contains_key(&id));
132
1
    assert!(app.status.is_empty());
133
2
    Ok(())
134
1
}