1
//! Reply delivery: each routed reply updates the tab state it targets.
2

            
3
use super::*;
4
use crate::tabs::reports::ReportKind;
5
use forms::FormValues;
6

            
7
6
fn wire_error(id: u64, code: &str, msg: &str) -> String {
8
6
    format!("(:id {id} :error (:code {code} :message \"{msg}\"))")
9
6
}
10

            
11
4
fn wire_value(id: u64, val: &str) -> String {
12
4
    format!("(:id {id} :value {val})")
13
4
}
14

            
15
#[test]
16
1
fn deliver_reply_accounts_updates_accounts_state() {
17
1
    let mut app = crate::test_support::app();
18
1
    let wire = wire_error(1, "db", "connection failed");
19
1
    app.deliver_reply(
20
1
        Route {
21
1
            target: Some(app.window_of(ViewId::Accounts)),
22
1
            ctx: RouteCtx::None,
23
1
        },
24
1
        &wire,
25
1
    );
26
1
    assert!(
27
1
        matches!(app.accounts().state, Fetch::Error(ref s) if s.contains("db")),
28
        "accounts state must reflect the error reply"
29
    );
30
1
}
31

            
32
#[test]
33
1
fn deliver_reply_transactions_updates_transactions_state() {
34
1
    let mut app = crate::test_support::app();
35
1
    let wire = wire_error(1, "tx", "failed");
36
1
    app.deliver_reply(
37
1
        Route {
38
1
            target: Some(app.window_of(ViewId::Transactions)),
39
1
            ctx: RouteCtx::None,
40
1
        },
41
1
        &wire,
42
1
    );
43
1
    assert!(matches!(app.transactions().state, Fetch::Error(_)));
44
1
}
45

            
46
#[test]
47
1
fn deliver_reply_commodities_updates_commodities_state() {
48
1
    let mut app = crate::test_support::app();
49
1
    let wire = wire_error(1, "c", "failed");
50
1
    app.deliver_reply(
51
1
        Route {
52
1
            target: Some(app.window_of(ViewId::Commodities)),
53
1
            ctx: RouteCtx::None,
54
1
        },
55
1
        &wire,
56
1
    );
57
1
    assert!(matches!(app.commodities().state, Fetch::Error(_)));
58
1
}
59

            
60
#[test]
61
1
fn deliver_reply_reports_updates_reports_state() {
62
1
    let mut app = crate::test_support::app();
63
1
    let wire = wire_error(1, "rpt", "failed");
64
1
    app.deliver_reply(
65
1
        Route {
66
1
            target: Some(app.window_of(ViewId::Reports)),
67
1
            ctx: RouteCtx::Reports {
68
1
                kind: ReportKind::Balance,
69
1
                seq: 0,
70
1
                values: FormValues::default(),
71
1
            },
72
1
        },
73
1
        &wire,
74
1
    );
75
1
    assert!(matches!(app.reports().state, Fetch::Error(_)));
76
1
}
77

            
78
#[test]
79
1
fn deliver_reply_console_pushes_to_scrollback() {
80
1
    let mut app = crate::test_support::app();
81
1
    let wire = wire_value(1, "42");
82
1
    app.deliver_reply(
83
1
        Route {
84
1
            target: Some(app.window_of(ViewId::Console)),
85
1
            ctx: RouteCtx::None,
86
1
        },
87
1
        &wire,
88
1
    );
89
1
    assert!(
90
1
        !app.console().scrollback.is_empty(),
91
        "console reply must appear in scrollback"
92
    );
93
1
}
94

            
95
#[test]
96
1
fn orphan_reply_goes_to_the_status_not_the_console() {
97
1
    let mut app = crate::test_support::app();
98
1
    let wire = wire_value(99, "42");
99
    // No route is present for id=99 → orphan branch fires.
100
1
    app.route_reply(99, &wire);
101
1
    assert_eq!(app.status, "[warn] orphan reply id=99: 42");
102
1
    assert!(app.console().scrollback.is_empty());
103
1
}
104

            
105
#[test]
106
1
fn deliver_reply_mutation_success_refreshes_accounts() {
107
1
    let mut app = crate::test_support::app();
108
1
    app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
109
1
        id: None,
110
1
        cells: vec!["existing".into()],
111
1
    }]);
112
1
    let wire = wire_value(1, "\"550e8400-e29b-41d4-a716-446655440001\"");
113
1
    app.deliver_reply(
114
1
        Route {
115
1
            target: Some(app.window_of(ViewId::Accounts)),
116
1
            ctx: RouteCtx::Mutation {
117
1
                refresh: ViewId::Accounts,
118
1
            },
119
1
        },
120
1
        &wire,
121
1
    );
122
1
    assert!(
123
1
        matches!(app.accounts().state, Fetch::Idle),
124
        "accounts must be reset to Idle after successful mutation"
125
    );
126
1
}
127

            
128
#[test]
129
1
fn deliver_reply_mutation_error_sets_status() {
130
1
    let mut app = crate::test_support::app();
131
1
    let wire = wire_error(1, "constraint", "account exists");
132
1
    app.deliver_reply(
133
1
        Route {
134
1
            target: Some(app.window_of(ViewId::Accounts)),
135
1
            ctx: RouteCtx::Mutation {
136
1
                refresh: ViewId::Accounts,
137
1
            },
138
1
        },
139
1
        &wire,
140
1
    );
141
1
    assert!(
142
1
        app.status.contains("mutation failed"),
143
        "status must reflect mutation error, got: {}",
144
        app.status
145
    );
146
1
    assert!(matches!(app.accounts().state, Fetch::Idle));
147
1
}
148

            
149
#[test]
150
1
fn deliver_reply_mutation_success_refreshes_commodities() {
151
1
    let mut app = crate::test_support::app();
152
1
    app.commodities_mut().state = Fetch::Loaded(vec![ListRow {
153
1
        id: None,
154
1
        cells: vec!["USD".into()],
155
1
    }]);
156
1
    let wire = wire_value(1, "\"550e8400-e29b-41d4-a716-446655440001\"");
157
1
    app.deliver_reply(
158
1
        Route {
159
1
            target: Some(app.window_of(ViewId::Commodities)),
160
1
            ctx: RouteCtx::Mutation {
161
1
                refresh: ViewId::Commodities,
162
1
            },
163
1
        },
164
1
        &wire,
165
1
    );
166
1
    assert!(
167
1
        matches!(app.commodities().state, Fetch::Idle),
168
        "commodities must be reset to Idle after successful mutation"
169
    );
170
1
}
171

            
172
#[test]
173
1
fn a_reply_to_a_gone_window_is_dropped() {
174
1
    let mut app = crate::test_support::app();
175
1
    let gone = crate::window::WindowId(ViewId::ALL.len() as u32);
176
1
    app.pending_routes.insert(
177
1
        5,
178
1
        Route {
179
1
            target: Some(gone),
180
1
            ctx: RouteCtx::Mutation {
181
1
                refresh: ViewId::Accounts,
182
1
            },
183
1
        },
184
1
    );
185
1
    app.route_reply(5, &wire_error(5, "db", "late"));
186
1
    assert!(app.pending_routes.is_empty());
187
1
    assert_eq!(app.status, "");
188
1
    assert!(matches!(app.accounts().state, Fetch::Idle));
189
1
}