1
//! The list windows the app creates at startup and the lookups over them.
2

            
3
use super::*;
4
use crate::tabs::config::ConfigTab;
5
use crate::tabs::list::ListTab;
6
use crate::tabs::nms::ConsoleState;
7
use crate::tabs::reports::ReportsTab;
8
use crate::tabs::scripts::ScriptsTab;
9
use crate::tabs::ssh_keys::SshKeysTab;
10
use crate::tabs::tags::TagsTab;
11
use crate::tabs::templates::TemplatesTab;
12
use crate::window::{View, WindowId};
13

            
14
#[test]
15
1
fn each_list_kind_has_its_window() {
16
1
    let mut app = crate::test_support::app();
17
1
    for kind in [ViewId::Accounts, ViewId::Transactions, ViewId::Commodities] {
18
3
        assert!(
19
3
            app.win_mut::<ListTab>(app.window_of(kind)).is_some(),
20
            "{kind:?}"
21
        );
22
3
        assert_eq!(app.window_view(app.window_of(kind)), Some(kind));
23
    }
24
1
}
25

            
26
#[test]
27
1
fn startup_windows_have_distinct_ids() {
28
1
    let app = crate::test_support::app();
29
1
    let ids: std::collections::HashSet<_> = ViewId::ALL
30
1
        .into_iter()
31
10
        .map(|kind| app.window_of(kind))
32
        .collect();
33
1
    assert_eq!(ids.len(), ViewId::ALL.len());
34
1
}
35

            
36
#[test]
37
1
fn a_front_list_is_the_front_window_of_that_kind_only() {
38
1
    let app = crate::test_support::app();
39
1
    assert!(app.front_list(ViewId::Accounts).is_some());
40
1
    assert!(app.front_list(ViewId::Transactions).is_none());
41
1
    assert!(app.front_list(ViewId::Reports).is_none());
42
1
}
43

            
44
#[test]
45
1
fn each_single_view_has_its_window() {
46
1
    let mut app = crate::test_support::app();
47
1
    assert!(
48
1
        app.win_mut::<ReportsTab>(app.window_of(ViewId::Reports))
49
1
            .is_some()
50
    );
51
1
    assert!(
52
1
        app.win_mut::<ConfigTab>(app.window_of(ViewId::Config))
53
1
            .is_some()
54
    );
55
1
    assert!(
56
1
        app.win_mut::<SshKeysTab>(app.window_of(ViewId::SshKeys))
57
1
            .is_some()
58
    );
59
1
    assert!(
60
1
        app.win_mut::<TagsTab>(app.window_of(ViewId::Tags))
61
1
            .is_some()
62
    );
63
1
    assert!(
64
1
        app.win_mut::<TemplatesTab>(app.window_of(ViewId::Templates))
65
1
            .is_some()
66
    );
67
1
    assert!(
68
1
        app.win_mut::<ScriptsTab>(app.window_of(ViewId::Scripts))
69
1
            .is_some()
70
    );
71
1
    assert!(
72
1
        app.win_mut::<ConsoleState>(app.window_of(ViewId::Console))
73
1
            .is_some()
74
    );
75
1
    for kind in ViewId::ALL {
76
10
        assert_eq!(app.window_view(app.window_of(kind)), Some(kind));
77
    }
78
1
}
79

            
80
#[test]
81
1
fn no_last_report_before_one_ran() {
82
1
    let app = crate::test_support::app();
83
1
    assert!(app.last_report(app.window_of(ViewId::Reports)).is_none());
84
1
}
85

            
86
#[test]
87
1
fn a_reply_window_is_a_list_window_only() {
88
1
    let mut app = crate::test_support::app();
89
1
    assert!(
90
1
        app.win_mut::<ListTab>(app.window_of(ViewId::Config))
91
1
            .is_none()
92
    );
93
1
    assert!(app.win_mut::<ListTab>(WindowId(u32::MAX)).is_none());
94
1
}
95

            
96
#[test]
97
1
fn a_gone_window_has_no_view() {
98
1
    let app = crate::test_support::app();
99
1
    assert_eq!(app.window_view(WindowId(u32::MAX)), None);
100
1
}
101

            
102
#[test]
103
1
fn a_lookup_takes_the_most_recent_window_of_its_kind() {
104
1
    let mut app = crate::test_support::app();
105
1
    let mut newer = ConfigTab::new();
106
1
    newer.follow = Some("newer".into());
107
1
    let id = app.open_new_window(View::Config(newer));
108
1
    assert_eq!(app.config().follow.as_deref(), Some("newer"));
109
1
    assert_eq!(app.window_view(id), Some(ViewId::Config));
110
1
}
111

            
112
#[test]
113
1
fn the_active_view_is_the_window_of_the_active_kind() {
114
1
    let mut app = crate::test_support::app();
115
1
    for kind in ViewId::ALL {
116
10
        app.switch_tab(kind);
117
10
        assert_eq!(app.active_view().map(View::kind), Some(kind));
118
10
        assert_eq!(app.active_view_mut().map(|view| view.kind()), Some(kind));
119
    }
120
1
}
121

            
122
#[test]
123
1
fn the_selected_id_is_read_from_the_front_window_only() {
124
1
    let mut app = crate::test_support::app();
125
1
    assert_eq!(app.selected_id(ViewId::Accounts), None);
126
2
    let rows = || {
127
2
        Fetch::Loaded(vec![ListRow {
128
2
            id: Some("a1".into()),
129
2
            cells: vec!["x".into()],
130
2
        }])
131
2
    };
132
1
    app.accounts_mut().state = rows();
133
1
    app.transactions_mut().state = rows();
134
1
    assert_eq!(app.selected_id(ViewId::Accounts).as_deref(), Some("a1"));
135
1
    assert_eq!(app.selected_id(ViewId::Transactions), None);
136
1
}