1
use super::*;
2

            
3
// ── canned wire strings ────────────────────────────────────────────────────
4
// These mirror the format produced by rpc/src/natives/report.rs.
5

            
6
2
fn balance_wire_quoted(id: u64) -> String {
7
2
    let body = concat!(
8
        "(:balance-report :periods ((:label nil :roots ((",
9
        ":account-id \"00000000-0000-0000-0000-000000000000\" ",
10
        ":account-name \"Assets\" :account-path \"Assets\" :depth 0 ",
11
        ":account-type \"asset\" ",
12
        ":amounts ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount 1000)) ",
13
        ":own-amounts ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount 1000)) ",
14
        ":children ())))))"
15
    );
16
2
    let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
17
2
    format!("(:id {id} :value \"{escaped}\")")
18
2
}
19

            
20
1
fn activity_wire_quoted(id: u64) -> String {
21
1
    let body = concat!(
22
        "(:activity-report :meta nil :periods ((:label \"2026-Q1\" :groups ((",
23
        ":label \"Income\" :flip-sign t :roots ((",
24
        ":account-id \"00000000-0000-0000-0000-000000000000\" ",
25
        ":account-name \"Salary\" :account-path \"Income:Salary\" :depth 0 ",
26
        ":account-type nil ",
27
        ":amounts ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount -1000)) ",
28
        ":own-amounts ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount -1000)) ",
29
        ":children ())) :total ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount -1000)))) ",
30
        ":net ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount -1000)))))"
31
    );
32
1
    let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
33
1
    format!("(:id {id} :value \"{escaped}\")")
34
1
}
35

            
36
1
fn breakdown_wire_quoted(id: u64) -> String {
37
1
    let body = concat!(
38
        "(:category-breakdown :meta nil :tag-name \"category\" :periods ((:label nil :rows ((",
39
        ":tag-value \"Food\" :uncategorized nil ",
40
        ":amounts ((:commodity-id \"00000000-0000-0000-0000-000000000000\" :symbol \"USD\" :amount 200))",
41
        ")))))"
42
    );
43
1
    let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
44
1
    format!("(:id {id} :value \"{escaped}\")")
45
1
}
46

            
47
1
fn error_wire(id: u64) -> String {
48
1
    format!("(:id {id} :error (:code server-error :message \"something went wrong\"))")
49
1
}
50

            
51
// ── on_reply tests ─────────────────────────────────────────────────────────
52

            
53
#[test]
54
1
fn on_reply_balance_produces_loaded_table() {
55
1
    let mut tab = ReportsTab::new();
56
1
    tab.set_loading(1, ReportKind::Balance, &FormValues::default());
57
1
    tab.on_reply(
58
1
        &balance_wire_quoted(1),
59
1
        ReportKind::Balance,
60
1
        FormValues::default(),
61
1
    );
62
1
    assert!(
63
1
        matches!(tab.state, Fetch::Loaded(_)),
64
        "expected Loaded, got {:?}",
65
        tab.state
66
    );
67
1
    if let Fetch::Loaded(lines) = &tab.state {
68
1
        assert_eq!(lines, &["  Assets  1000.00 USD"]);
69
    }
70
1
}
71

            
72
#[test]
73
1
fn on_reply_activity_produces_loaded_table() {
74
1
    let mut tab = ReportsTab::new();
75
1
    tab.set_loading(2, ReportKind::Activity, &FormValues::default());
76
1
    tab.on_reply(
77
1
        &activity_wire_quoted(2),
78
1
        ReportKind::Activity,
79
1
        FormValues::default(),
80
1
    );
81
1
    assert!(
82
1
        matches!(tab.state, Fetch::Loaded(_)),
83
        "expected Loaded, got {:?}",
84
        tab.state
85
    );
86
1
    if let Fetch::Loaded(lines) = &tab.state {
87
1
        assert_eq!(
88
1
            lines,
89
1
            &[
90
1
                "2026-Q1",
91
1
                "Income",
92
1
                "  Salary  -1000.00 USD",
93
1
                "  Total   -1000.00 USD",
94
1
                "  Net     -1000.00 USD"
95
1
            ]
96
        );
97
    }
98
1
}
99

            
100
#[test]
101
1
fn on_reply_breakdown_produces_loaded_table() {
102
1
    let mut tab = ReportsTab::new();
103
1
    tab.set_loading(3, ReportKind::Breakdown, &FormValues::default());
104
1
    tab.on_reply(
105
1
        &breakdown_wire_quoted(3),
106
1
        ReportKind::Breakdown,
107
1
        FormValues::default(),
108
1
    );
109
1
    assert!(
110
1
        matches!(tab.state, Fetch::Loaded(_)),
111
        "expected Loaded, got {:?}",
112
        tab.state
113
    );
114
1
    if let Fetch::Loaded(lines) = &tab.state {
115
1
        assert_eq!(lines, &["  Food  200.00 USD"]);
116
    }
117
1
}
118

            
119
#[test]
120
1
fn on_reply_uses_routed_kind_not_current_selection() {
121
    // The tab's selection has since moved to Activity, but a balance reply
122
    // routed with ReportKind::Balance must still parse as a balance chart.
123
1
    let mut tab = ReportsTab::new();
124
1
    tab.set_loading(9, ReportKind::Activity, &FormValues::default());
125
1
    tab.on_reply(
126
1
        &balance_wire_quoted(9),
127
1
        ReportKind::Balance,
128
1
        FormValues::default(),
129
1
    );
130
1
    if let Fetch::Loaded(lines) = &tab.state {
131
1
        assert_eq!(
132
1
            lines,
133
1
            &["  Assets  1000.00 USD"],
134
            "must parse with the routed kind"
135
        );
136
    } else {
137
        panic!("expected Loaded, got {:?}", tab.state);
138
    }
139
1
}
140

            
141
#[test]
142
1
fn on_reply_error_wire_produces_error_state() {
143
1
    let mut tab = ReportsTab::new();
144
1
    tab.set_loading(4, ReportKind::Balance, &FormValues::default());
145
1
    tab.on_reply(&error_wire(4), ReportKind::Balance, FormValues::default());
146
1
    assert!(
147
1
        matches!(tab.state, Fetch::Error(_)),
148
        "expected Error, got {:?}",
149
        tab.state
150
    );
151
1
    if let Fetch::Error(msg) = &tab.state {
152
1
        assert!(msg.contains("something went wrong"), "message: {msg}");
153
    }
154
1
}
155

            
156
#[test]
157
1
fn set_loading_records_kind_and_chart() {
158
1
    let mut tab = ReportsTab::new();
159
1
    tab.set_loading(7, ReportKind::Activity, &FormValues::default());
160
1
    assert_eq!(tab.kind, ReportKind::Activity);
161
1
    assert!(matches!(tab.state, Fetch::Loading { id: 7 }));
162
1
}
163

            
164
#[test]
165
1
fn report_kind_labels_are_non_empty() {
166
1
    for kind in [
167
1
        ReportKind::Balance,
168
1
        ReportKind::Activity,
169
1
        ReportKind::Breakdown,
170
1
    ] {
171
3
        assert!(!kind.label().is_empty());
172
    }
173
1
}