1
use super::*;
2

            
3
#[tokio::test]
4
1
async fn submit_echoes_the_form_in_the_console_and_sends_its_frame() {
5
1
    let mut app = crate::test_support::app();
6
1
    let sent = app.attach_echo();
7
1
    let console = app.window_of(ViewId::Console);
8
1
    app.submit_console_form(console, "(x)".to_string());
9
1
    tokio::task::yield_now().await;
10
1
    assert!(app.console().scrollback.iter().any(|l| l == "> (x)"));
11
1
    assert_eq!(sent.frames(), ["(:id 1 :stream 1 :form (x))"]);
12
1
}
13

            
14
#[tokio::test]
15
1
async fn a_frame_nobody_awaits_goes_to_the_status() {
16
1
    let mut app = crate::test_support::app();
17
1
    let (eval, replies) = ConsoleEval::scripted();
18
1
    app.console_eval = Some(eval);
19
1
    assert!(replies.try_send("((((".to_string()).is_ok());
20
1
    app.drain_eval();
21
1
    assert_eq!(app.status, "((((");
22
1
}
23

            
24
#[tokio::test]
25
1
async fn each_reply_settles_its_own_route_whatever_the_order() {
26
1
    let mut app = crate::test_support::app();
27
1
    let (eval, replies) = ConsoleEval::scripted();
28
1
    app.console_eval = Some(eval);
29
1
    let console = app.window_of(ViewId::Console);
30
1
    app.submit_console_form(console, "(a)".to_string());
31
1
    app.submit_console_form(console, "(b)".to_string());
32
1
    assert_eq!(app.pending_routes.len(), 2);
33
1
    assert!(replies.try_send("(:id 2 :value 2)".to_string()).is_ok());
34
1
    app.drain_eval();
35
1
    assert_eq!(app.pending_routes.keys().copied().collect::<Vec<_>>(), [1]);
36
1
    assert!(replies.try_send("(:id 1 :value 1)".to_string()).is_ok());
37
1
    app.drain_eval();
38
1
    assert!(app.pending_routes.is_empty());
39
1
}
40

            
41
#[test]
42
1
fn submit_without_eval_reports_not_connected_in_status() {
43
1
    let mut app = crate::test_support::app();
44
1
    let console = app.window_of(ViewId::Console);
45
1
    app.submit_console_form(console, "(x)".to_string());
46
1
    assert!(app.console().scrollback.iter().any(|l| l == "> (x)"));
47
1
    assert_eq!(app.status, "console not connected");
48
1
}
49

            
50
#[test]
51
1
fn drain_without_eval_is_noop() {
52
1
    let mut app = crate::test_support::app();
53
1
    app.drain_eval();
54
1
    assert!(app.console().scrollback.is_empty());
55
1
}
56

            
57
#[tokio::test]
58
1
async fn submit_without_console_ids_reports_it_in_status() {
59
1
    let mut app = crate::test_support::app();
60
1
    app.attach_echo();
61
1
    app.break_console();
62
1
    let console = app.window_of(ViewId::Console);
63
1
    app.submit_console_form(console, "(x)".to_string());
64
1
    assert!(app.console().scrollback.iter().any(|l| l == "> (x)"));
65
1
    assert_eq!(app.status, crate::tabs::nms_eval::UNAVAILABLE);
66
1
}
67

            
68
#[test]
69
1
fn a_multi_line_notice_shows_its_first_line_and_a_log_hint() {
70
1
    let mut app = crate::test_support::app();
71
1
    app.notify(vec!["first".to_string(), "second".to_string()]);
72
1
    assert_eq!(app.status, "first … (see log)");
73
1
}
74

            
75
#[test]
76
1
fn an_empty_notice_leaves_the_status_alone() {
77
1
    let mut app = crate::test_support::app();
78
1
    app.set_status("kept");
79
1
    app.notify(Vec::new());
80
1
    assert_eq!(app.status, "kept");
81
1
}
82

            
83
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
84
1
async fn a_reader_error_reply_clears_the_console_route() {
85
1
    let mut app = crate::test_support::app();
86
1
    app.attach_console(
87
1
        ConsoleEval::connect(
88
1
            &tokio::runtime::Handle::current(),
89
1
            rpc::ScriptCtx::new(sqlx::types::Uuid::nil()),
90
1
            rpc::Session::new,
91
1
        )
92
1
        .expect("session"),
93
1
    );
94
1
    app.pending_routes.clear();
95
1
    let console = app.window_of(ViewId::Console);
96
1
    app.submit_console_form(console, "(+ 1".to_string());
97
1
    assert_eq!(app.pending_routes.len(), 1);
98
1
    for _ in 0..200 {
99
1
        app.drain_eval();
100
1
        if app.pending_routes.is_empty() {
101
1
            break;
102
        }
103
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
104
    }
105
1
    assert!(app.pending_routes.is_empty());
106
1
    app.submit_console_form(console, "(+ 2 2)".to_string());
107
1
    for _ in 0..200 {
108
8
        app.drain_eval();
109
8
        if app.pending_routes.is_empty() {
110
1
            break;
111
7
        }
112
7
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
113
1
    }
114
1
    assert!(
115
4
        app.console().scrollback.iter().any(|l| l == "4"),
116
1
        "{:?}",
117
1
        app.console().scrollback
118
1
    );
119
1
}
120

            
121
#[test]
122
1
fn a_console_reply_lands_in_the_submitting_console_only() {
123
1
    let mut app = crate::test_support::app();
124
1
    let first = app.window_of(ViewId::Console);
125
1
    let second = app.open_new_window(View::Console(ConsoleState::new()));
126
    let route = |target| Route {
127
2
        target: Some(target),
128
2
        ctx: RouteCtx::None,
129
2
    };
130
1
    app.pending_routes.insert(7, route(first));
131
1
    app.pending_routes.insert(8, route(second));
132
1
    app.route_reply(8, "(:id 8 :value 42)");
133
1
    assert!(
134
1
        app.win::<ConsoleState>(first)
135
1
            .is_some_and(|c| c.scrollback.is_empty())
136
    );
137
1
    assert!(
138
1
        app.win::<ConsoleState>(second)
139
1
            .is_some_and(|c| !c.scrollback.is_empty())
140
    );
141
1
    app.route_reply(7, "(:id 7 :value 43)");
142
1
    assert!(
143
1
        app.win::<ConsoleState>(first)
144
1
            .is_some_and(|c| !c.scrollback.is_empty())
145
    );
146
1
}
147

            
148
#[test]
149
1
fn submitting_from_the_older_console_echoes_there_not_in_the_recent_one() {
150
1
    let mut app = crate::test_support::app();
151
1
    let first = app.window_of(ViewId::Console);
152
1
    let second = app.open_new_window(View::Console(ConsoleState::new()));
153
1
    app.submit_console_form(first, "(x)".to_string());
154
1
    assert!(
155
1
        app.win::<ConsoleState>(first)
156
1
            .is_some_and(|c| c.scrollback.iter().any(|l| l == "> (x)"))
157
    );
158
1
    assert!(
159
1
        app.win::<ConsoleState>(second)
160
1
            .is_some_and(|c| c.scrollback.is_empty())
161
    );
162
1
}
163

            
164
#[test]
165
1
fn a_reply_for_a_closed_console_is_dropped() {
166
1
    let mut app = crate::test_support::app();
167
1
    let second = app.open_new_window(View::Console(ConsoleState::new()));
168
1
    app.pending_routes.insert(
169
1
        9,
170
1
        Route {
171
1
            target: Some(second),
172
1
            ctx: RouteCtx::None,
173
1
        },
174
1
    );
175
1
    app.close_window(second);
176
1
    app.route_reply(9, "(:id 9 :value 1)");
177
1
    assert!(app.console().scrollback.is_empty());
178
1
    assert!(app.status.is_empty());
179
1
}
180

            
181
#[test]
182
1
fn switching_away_from_console_clears_focus() {
183
1
    let mut app = crate::test_support::app();
184
1
    app.focus_console();
185
1
    app.switch_tab(Tab::Accounts);
186
1
    assert!(
187
1
        !app.console_input_focused(),
188
        "leaving console must drop its input focus"
189
    );
190
1
    app.focus_console();
191
1
    app.next_window();
192
1
    assert!(
193
1
        !app.console_input_focused(),
194
        "next_window off console must drop focus"
195
    );
196
1
}
197

            
198
#[test]
199
1
fn refresh_tab_goes_idle_and_keeps_the_selection() {
200
1
    let mut app = crate::test_support::app();
201
1
    app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
202
1
        id: None,
203
1
        cells: vec!["foo".into()],
204
1
    }]);
205
1
    app.accounts_mut().selected = 2;
206
1
    app.refresh_windows(Tab::Accounts);
207
1
    assert_eq!(app.accounts().state, Fetch::Idle);
208
1
    assert_eq!(app.accounts().selected, 2);
209
1
}
210

            
211
#[test]
212
1
fn switch_tab_to_reports_does_not_trigger_fetch() {
213
1
    let app = crate::test_support::app();
214
1
    assert!(matches!(app.accounts().state, Fetch::Idle));
215
1
    assert!(matches!(app.transactions().state, Fetch::Idle));
216
1
    assert!(matches!(app.commodities().state, Fetch::Idle));
217
1
}
218

            
219
#[test]
220
1
fn refresh_tab_while_loading_is_noop() {
221
1
    let mut app = crate::test_support::app();
222
1
    app.accounts_mut().state = Fetch::Loading { id: 7 };
223
1
    app.refresh_windows(Tab::Accounts);
224
    // A fetch is in flight — refresh must not reset it or submit again.
225
1
    assert!(matches!(app.accounts().state, Fetch::Loading { id: 7 }));
226
1
}