1
//! A dialog or action belongs to the window it started from, and says so when that is gone.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::route::RouteCtx;
8
use crate::tabs::fetch::Fetch;
9
use crate::tabs::tags::TagsTab;
10
use crate::view::ViewId;
11
use crate::window::WindowId;
12
use forms::FormValues;
13

            
14
4
fn targets(app: &App, matching: fn(&RouteCtx) -> bool) -> Vec<Option<WindowId>> {
15
4
    let routes = app.pending_routes.values();
16
4
    routes
17
7
        .filter(|route| matching(&route.ctx))
18
3
        .map(|route| route.target)
19
        .collect()
20
4
}
21

            
22
1
fn is_tag_edit(ctx: &RouteCtx) -> bool {
23
1
    matches!(ctx, RouteCtx::TagEdit)
24
1
}
25

            
26
6
fn is_tag_create(ctx: &RouteCtx) -> bool {
27
6
    matches!(ctx, RouteCtx::TagCreate)
28
6
}
29

            
30
#[tokio::test]
31
1
async fn a_dialog_routes_to_its_window_after_another_one_is_raised() {
32
1
    let mut app = crate::test_support::app();
33
1
    app.attach_echo();
34
1
    let first = app.window_of(ViewId::Tags);
35
1
    let second = app.open_extra(ViewId::Tags);
36
1
    app.raise_window(first);
37
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
38
1
    app.raise_window(second);
39
1
    assert!(app.submit_tag_create(&FormValues::default()).is_some());
40
1
    assert_eq!(targets(&app, is_tag_create), [Some(first)]);
41
1
}
42

            
43
#[tokio::test]
44
1
async fn a_dialog_of_a_closed_window_says_so_and_routes_nowhere() {
45
1
    let mut app = crate::test_support::app();
46
1
    app.attach_echo();
47
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
48
1
    app.close_window(app.window_of(ViewId::Tags));
49
1
    assert!(app.submit_tag_create(&FormValues::default()).is_none());
50
1
    assert_eq!(app.status, "the Tags window was closed");
51
1
    assert!(targets(&app, is_tag_create).is_empty());
52
1
}
53

            
54
#[tokio::test]
55
1
async fn a_dialog_from_the_console_raises_the_window_of_its_kind() {
56
1
    let mut app = crate::test_support::app();
57
1
    app.attach_echo();
58
1
    app.focus_console();
59
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
60
1
    assert!(app.front_is(ViewId::Tags));
61
1
    assert!(!app.console_input_focused());
62
1
    assert!(app.submit_tag_create(&FormValues::default()).is_some());
63
1
    let tags = app.window_of(ViewId::Tags);
64
1
    assert_eq!(targets(&app, is_tag_create), [Some(tags)]);
65
1
}
66

            
67
#[tokio::test]
68
1
async fn a_filter_reply_raises_its_window_and_takes_the_console_prompt_focus() {
69
1
    let mut app = crate::test_support::app();
70
1
    app.attach_echo();
71
1
    app.filter_transactions(FormValues::default());
72
1
    let request = *app
73
1
        .pending_routes
74
1
        .iter()
75
1
        .find(|(_, route)| matches!(route.ctx, RouteCtx::TransactionFilter { .. }))
76
        .map(|(id, _)| id)
77
1
        .expect("a filter request");
78
1
    app.focus_console();
79
1
    assert!(app.console_input_focused());
80
1
    let wire = format!("(:id {request} :value (:ROWS () :HAS-MORE nil :TOTAL 0))");
81
1
    app.route_reply(request, &wire);
82
1
    assert!(app.front_is(ViewId::Transactions));
83
1
    assert!(!app.console_input_focused());
84
1
}
85

            
86
#[tokio::test]
87
1
async fn refreshing_a_kind_fetches_every_window_of_it() {
88
1
    let mut app = crate::test_support::app();
89
1
    app.attach_echo();
90
1
    let first = app.window_of(ViewId::Tags);
91
1
    let second = app.open_extra(ViewId::Tags);
92
1
    app.refresh_windows(ViewId::Tags);
93
1
    for window in [first, second] {
94
2
        let tags = app.win::<TagsTab>(window);
95
2
        assert!(tags.is_some_and(TagsTab::any_loading), "{window:?}");
96
1
    }
97
1
}
98

            
99
type Action = fn(&mut App, WindowId);
100

            
101
const MISSING_WINDOW_ACTIONS: [(ViewId, &str, Action); 9] = [
102
1
    (ViewId::Reports, "chart", |app, _| app.toggle_report_chart()),
103
1
    (ViewId::Reports, "csv", |app, _| app.request_report_csv()),
104
1
    (ViewId::Reports, "edit report", |app, _| {
105
1
        app.edit_last_report();
106
1
    }),
107
1
    (ViewId::Transactions, "detail", |app, _| {
108
1
        app.open_transaction_detail_selected();
109
1
    }),
110
1
    (ViewId::Transactions, "delete marked", |app, _| {
111
1
        app.open_delete_marked();
112
1
    }),
113
1
    (ViewId::Transactions, "page", |app, _| {
114
1
        app.page_transactions(true);
115
1
    }),
116
1
    (ViewId::Transactions, "edit", |app, _| {
117
1
        app.open_transaction_edit_async("a");
118
1
    }),
119
1
    (ViewId::Transactions, "filter", |app, window| {
120
1
        app.submit_transaction_filter(window, &FormValues::default(), String::new());
121
1
    }),
122
1
    (ViewId::Reports, "run", |app, window| {
123
1
        app.submit_report(
124
1
            window,
125
1
            crate::tabs::reports::ReportKind::Balance,
126
1
            &FormValues::default(),
127
1
        );
128
1
    }),
129
];
130

            
131
#[tokio::test]
132
1
async fn an_action_on_a_missing_window_says_which_window_is_missing() {
133
1
    for (kind, name, action) in MISSING_WINDOW_ACTIONS {
134
9
        let mut app = crate::test_support::app();
135
9
        app.attach_echo();
136
9
        let window = app.window_of(kind);
137
9
        app.close_window(window);
138
9
        app.set_status("");
139
9
        action(&mut app, window);
140
9
        assert_eq!(app.status, format!("no {} window", kind.label()), "{name}");
141
1
    }
142
1
}
143

            
144
const OLDER_TAG: &str = "aaaaaaaa-0000-0000-0000-000000000001";
145
const NEWER_TAG: &str = "bbbbbbbb-0000-0000-0000-000000000002";
146

            
147
2
fn load_tags(app: &mut App, window: WindowId, tag: &str) {
148
2
    app.fetch_tags(window);
149
2
    let Some(Fetch::Loading { id }) = app.win::<TagsTab>(window).map(|tab| tab.state.clone())
150
    else {
151
        panic!("the tags window is not loading");
152
    };
153
2
    let row = format!(r#"(:tag :id "{tag}" :name "kind" :value "fiat" :description "")"#);
154
2
    app.route_reply(id, &format!("(:id {id} :value ({row}))"));
155
2
}
156

            
157
#[tokio::test]
158
1
async fn a_tag_edit_reads_the_selection_of_the_front_window() {
159
1
    let mut app = crate::test_support::app();
160
1
    let sent = app.attach_echo();
161
1
    let older = app.window_of(ViewId::Tags);
162
1
    let newer = app.open_extra(ViewId::Tags);
163
1
    load_tags(&mut app, older, OLDER_TAG);
164
1
    load_tags(&mut app, newer, NEWER_TAG);
165
1
    app.raise_window(older);
166
1
    apply(&mut app, Intent::EditRow);
167
1
    tokio::task::yield_now().await;
168
1
    app.drain_eval();
169
1
    let echoed = sent.frames().join("\n");
170
1
    assert!(echoed.contains(&format!(r#"(load-form "tag-edit" "{OLDER_TAG}")"#)));
171
1
    assert!(!echoed.contains(NEWER_TAG));
172
1
    assert_eq!(targets(&app, is_tag_edit), [Some(older)]);
173
1
}