1
use cli_core::render::schema;
2

            
3
use super::*;
4

            
5
2
fn wire_value(id: u64, val: &str) -> String {
6
2
    format!("(:id {id} :value {val})")
7
2
}
8

            
9
1
fn wire_string(id: u64, s: &str) -> String {
10
1
    format!("(:id {id} :value \"{s}\")")
11
1
}
12

            
13
1
fn wire_error(id: u64, code: &str, msg: &str) -> String {
14
1
    format!("(:id {id} :error (:code {code} :message \"{msg}\"))")
15
1
}
16

            
17
7
fn list_row(cells: Vec<&str>) -> ListRow {
18
7
    ListRow {
19
7
        id: None,
20
7
        cells: cells.into_iter().map(String::from).collect(),
21
7
    }
22
7
}
23

            
24
#[test]
25
1
fn on_reply_scalar_number_becomes_single_row() {
26
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
27
1
    tab.on_reply(&wire_value(1, "42"));
28
1
    assert!(matches!(tab.state, Fetch::Loaded(ref rows) if !rows.is_empty()));
29
1
}
30

            
31
#[test]
32
1
fn on_reply_nil_list_is_loaded_and_empty() {
33
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
34
1
    tab.on_reply(&wire_value(1, "nil"));
35
1
    assert!(matches!(tab.state, Fetch::Loaded(ref rows) if rows.is_empty()));
36
1
}
37

            
38
#[test]
39
1
fn on_reply_server_error_becomes_error_state() {
40
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
41
1
    tab.on_reply(&wire_error(1, "db", "connection failed"));
42
1
    assert!(matches!(tab.state, Fetch::Error(ref s) if s.contains("db")));
43
1
}
44

            
45
#[test]
46
1
fn on_reply_string_that_is_not_a_list_is_single_row() {
47
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
48
1
    tab.on_reply(&wire_string(1, "just-a-uuid"));
49
1
    assert!(!matches!(tab.state, Fetch::Idle));
50
1
}
51

            
52
#[test]
53
1
fn select_next_advances_when_loaded() {
54
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
55
1
    tab.state = Fetch::Loaded(vec![
56
1
        list_row(vec!["a"]),
57
1
        list_row(vec!["b"]),
58
1
        list_row(vec!["c"]),
59
1
    ]);
60
1
    assert_eq!(tab.selected, 0);
61
1
    tab.select_next();
62
1
    assert_eq!(tab.selected, 1);
63
1
    tab.select_next();
64
1
    assert_eq!(tab.selected, 2);
65
1
    tab.select_next();
66
1
    assert_eq!(tab.selected, 2, "must not advance past last row");
67
1
}
68

            
69
#[test]
70
1
fn select_prev_saturates_at_zero() {
71
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
72
1
    tab.selected = 0;
73
1
    tab.select_prev();
74
1
    assert_eq!(tab.selected, 0);
75
1
}
76

            
77
#[test]
78
1
fn reset_returns_to_idle() {
79
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
80
1
    tab.state = Fetch::Loaded(vec![list_row(vec!["x"])]);
81
1
    tab.selected = 3;
82
1
    tab.reset();
83
1
    assert_eq!(tab.state, Fetch::Idle);
84
1
    assert_eq!(tab.selected, 0);
85
1
    assert_eq!(tab.window.top(), 0);
86
1
}
87

            
88
#[test]
89
1
fn set_loading_id_updates_state() {
90
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
91
1
    tab.set_loading_id(42);
92
1
    assert_eq!(tab.state, Fetch::Loading { id: 42 });
93
1
}
94

            
95
// --- L4: structured ListRow tests ---
96

            
97
// Real list-accounts replies are typed records with a leading `:account`
98
// tag (exactly as `render_entity` emits), so `:id` sits after the tag.
99
const TYPED_ACCOUNTS: &str = r#"((:account :id "uuid-1" :name "Checking" :parent "") (:account :id "uuid-2" :name "Savings" :parent ""))"#;
100

            
101
/// Wire reply carrying the typed-record list as the `:value` payload.
102
2
fn wire_accounts_list() -> String {
103
2
    format!("(:id 1 :value {TYPED_ACCOUNTS})")
104
2
}
105

            
106
#[test]
107
1
fn on_reply_list_accounts_rows_carry_ids() {
108
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
109
1
    tab.on_reply(&wire_accounts_list());
110
1
    let Fetch::Loaded(ref rows) = tab.state else {
111
        panic!("expected Loaded state");
112
    };
113
1
    assert_eq!(rows.len(), 2);
114
1
    assert_eq!(rows[0].id.as_deref(), Some("uuid-1"));
115
1
    assert_eq!(rows[1].id.as_deref(), Some("uuid-2"));
116
1
}
117

            
118
#[test]
119
1
fn on_reply_list_accounts_projects_schema_cells() {
120
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
121
1
    tab.on_reply(&wire_accounts_list());
122
1
    let Fetch::Loaded(ref rows) = tab.state else {
123
        panic!("expected Loaded state");
124
    };
125
1
    assert_eq!(rows.len(), 2);
126
    // Name filled, Type/Parent blank (absent from these records)
127
1
    assert_eq!(rows[0].cells, vec!["Checking", "", ""]);
128
1
    assert_eq!(rows[1].cells, vec!["Savings", "", ""]);
129
1
}
130

            
131
#[test]
132
1
fn selected_id_returns_focused_row_id() {
133
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
134
1
    tab.state = Fetch::Loaded(vec![
135
1
        ListRow {
136
1
            id: Some("id-A".into()),
137
1
            cells: vec!["A".into()],
138
1
        },
139
1
        ListRow {
140
1
            id: Some("id-B".into()),
141
1
            cells: vec!["B".into()],
142
1
        },
143
1
    ]);
144
1
    tab.selected = 0;
145
1
    assert_eq!(tab.selected_id(), Some("id-A"));
146
1
    tab.selected = 1;
147
1
    assert_eq!(tab.selected_id(), Some("id-B"));
148
1
}
149

            
150
#[test]
151
1
fn selected_id_none_when_not_loaded() {
152
1
    let tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
153
1
    assert_eq!(tab.selected_id(), None);
154
1
}
155

            
156
#[test]
157
1
fn selected_id_none_when_row_has_no_id() {
158
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
159
1
    tab.state = Fetch::Loaded(vec![ListRow {
160
1
        id: None,
161
1
        cells: vec!["x".into()],
162
1
    }]);
163
1
    assert_eq!(tab.selected_id(), None);
164
1
}
165

            
166
#[test]
167
1
fn element_without_id_field_has_none_id_cells_still_rendered() {
168
1
    let list_str = r#"((:account :name "NoId" :parent ""))"#;
169
1
    let wire = format!(r#"(:id 1 :value "{}")"#, list_str.replace('"', "\\\""));
170
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
171
1
    tab.on_reply(&wire);
172
1
    let Fetch::Loaded(ref rows) = tab.state else {
173
        panic!("expected Loaded state");
174
    };
175
1
    assert_eq!(rows.len(), 1);
176
1
    assert_eq!(rows[0].id, None);
177
1
    assert!(!rows[0].cells.is_empty());
178
1
}
179

            
180
#[test]
181
1
fn select_next_prev_move_selected_id() {
182
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
183
1
    tab.state = Fetch::Loaded(vec![
184
1
        ListRow {
185
1
            id: Some("id-0".into()),
186
1
            cells: vec!["zero".into()],
187
1
        },
188
1
        ListRow {
189
1
            id: Some("id-1".into()),
190
1
            cells: vec!["one".into()],
191
1
        },
192
1
        ListRow {
193
1
            id: Some("id-2".into()),
194
1
            cells: vec!["two".into()],
195
1
        },
196
1
    ]);
197
1
    assert_eq!(tab.selected_id(), Some("id-0"));
198
1
    tab.select_next();
199
1
    assert_eq!(tab.selected_id(), Some("id-1"));
200
1
    tab.select_next();
201
1
    assert_eq!(tab.selected_id(), Some("id-2"));
202
1
    tab.select_prev();
203
1
    assert_eq!(tab.selected_id(), Some("id-1"));
204
1
}
205

            
206
6
fn accounts_wire(ids: &[&str]) -> String {
207
6
    let rows: Vec<String> = ids
208
6
        .iter()
209
17
        .map(|id| format!(r#"(:account :id "{id}" :name "{id}" :parent "")"#))
210
        .collect();
211
6
    format!("(:id 1 :value ({}))", rows.join(" "))
212
6
}
213

            
214
3
fn refreshed_at(index: usize, reply: &[&str]) -> ListTab {
215
3
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
216
3
    tab.on_reply(&accounts_wire(&["a", "b", "c"]));
217
3
    tab.selected = index;
218
3
    tab.refresh();
219
3
    assert_eq!(tab.state, Fetch::Idle);
220
3
    tab.on_reply(&accounts_wire(reply));
221
3
    tab
222
3
}
223

            
224
#[test]
225
1
fn refresh_keeps_the_focused_row_by_id() {
226
1
    let tab = refreshed_at(2, &["a", "b", "bb", "c"]);
227
1
    assert_eq!(tab.selected_id(), Some("c"));
228
1
    assert_eq!(tab.selected, 3);
229
1
}
230

            
231
#[test]
232
1
fn refresh_falls_back_to_the_same_index_when_the_row_is_gone() {
233
1
    let tab = refreshed_at(1, &["a", "c", "d"]);
234
1
    assert_eq!(tab.selected, 1);
235
1
}
236

            
237
#[test]
238
1
fn refresh_clamps_the_fallback_index_to_the_last_row() {
239
1
    let tab = refreshed_at(2, &["a"]);
240
1
    assert_eq!(tab.selected, 0);
241
1
}
242

            
243
#[test]
244
1
fn marked_cell_adds_the_gutter_to_the_first_cell_only_once_a_row_is_marked() {
245
1
    assert_eq!(marked_cell("x", true, true, false), "* x");
246
1
    assert_eq!(marked_cell("x", true, false, false), "  x");
247
1
    assert_eq!(marked_cell("x", true, false, true), "x");
248
1
    assert_eq!(marked_cell("x", false, true, false), "x");
249
1
}
250

            
251
#[test]
252
1
fn select_first_and_last_jump_to_the_ends() {
253
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
254
1
    tab.state = Fetch::Loaded(vec![
255
1
        list_row(vec!["a"]),
256
1
        list_row(vec!["b"]),
257
1
        list_row(vec!["c"]),
258
1
    ]);
259
1
    tab.select_last();
260
1
    assert_eq!(tab.selected, 2);
261
1
    tab.select_first();
262
1
    assert_eq!(tab.selected, 0);
263
1
}
264

            
265
#[test]
266
1
fn select_last_on_an_unloaded_list_stays_put() {
267
1
    let mut tab = ListTab::new("(list-accounts)", &schema::ACCOUNTS);
268
1
    tab.select_last();
269
1
    assert_eq!(tab.selected, 0);
270
1
}
271

            
272
#[test]
273
1
fn a_paged_tab_shows_the_transaction_headers() {
274
1
    assert_eq!(ListTab::paged().headers(), ["Date", "Note", "Amount"]);
275
1
}