1
use super::grid_tests::{drawn, find, options, role_of, row_text};
2
use super::*;
3
use crate::widgets::FocusedSubWidget;
4
use crate::widgets::SplitsWidget;
5
use crate::widgets::splits::{
6
    COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE,
7
};
8
use ratatui::buffer::Buffer;
9
use ratatui::layout::Position;
10

            
11
const SPLITS: usize = 3;
12
const BANK: &str = "Assets:Bank:Checking";
13
const FOOD: &str = "Expenses:Food:Groceries";
14
const SIZES: [(u16, u16); 2] = [(100, 30), (80, 24)];
15
const COLUMNS: [usize; 6] = [
16
    COL_FROM,
17
    COL_TO,
18
    COL_FROM_COMM,
19
    COL_TO_COMM,
20
    COL_VALUE,
21
    COL_TO_AMOUNT,
22
];
23

            
24
40
fn form_of(loaded: bool, edit: impl FnOnce(&mut SplitsWidget)) -> Form {
25
40
    let mut form = Form::transaction_create();
26
40
    form.focus = SPLITS;
27
40
    let Widget::Splits(sw) = &mut form.fields[SPLITS].widget else {
28
        panic!("field {SPLITS} is the splits");
29
    };
30
40
    sw.add_row();
31
40
    if loaded {
32
38
        sw.set_account_options(options(&[BANK, FOOD, "Assets:Cash"]));
33
38
        sw.set_commodity_options(options(&["USD", "Hungarian Forint (HUF)"]));
34
38
        for (row, (from, to)) in [(BANK, FOOD), (FOOD, BANK)].into_iter().enumerate() {
35
76
            sw.row_focus = row;
36
76
            sw.col_focus = COL_FROM;
37
76
            sw.edit_select(|select| select.select_id(&from.to_lowercase()));
38
76
            sw.col_focus = COL_TO;
39
76
            sw.edit_select(|select| select.select_id(&to.to_lowercase()));
40
        }
41
2
    }
42
40
    sw.row_focus = 0;
43
40
    sw.col_focus = COL_VALUE;
44
40
    edit(sw);
45
40
    form
46
40
}
47

            
48
38
fn two_splits(edit: impl FnOnce(&mut SplitsWidget)) -> Form {
49
38
    form_of(true, edit)
50
38
}
51

            
52
61
fn line_of(buf: &Buffer, row: usize) -> u16 {
53
61
    find(buf, &format!("{}.", row + 1))[0].y
54
61
}
55

            
56
/// The text of `line` with its runs of blanks folded to one.
57
22
fn folded(line: &str) -> String {
58
22
    line.split_whitespace().collect::<Vec<_>>().join(" ")
59
22
}
60

            
61
22
fn split_line(buf: &Buffer, row: usize) -> String {
62
22
    folded(&row_text(buf, line_of(buf, row)))
63
22
}
64

            
65
18
fn marks(buf: &Buffer, y: u16) -> Vec<u16> {
66
18
    (0..buf.area.width)
67
1620
        .filter(|&x| buf[(x, y)].symbol() == "▼")
68
        .collect()
69
18
}
70

            
71
/// Where the columns of split line `row` sit: its select marks.
72
16
fn shape(buf: &Buffer, row: usize) -> Vec<u16> {
73
16
    marks(buf, line_of(buf, row))
74
16
}
75

            
76
3
fn type_amount(sw: &mut SplitsWidget, text: &str) {
77
3
    for c in text.chars() {
78
37
        if let Some(FocusedSubWidget::Amount(amount)) = sw.focused_subwidget_mut() {
79
37
            amount.insert_char(c);
80
        }
81
    }
82
3
}
83

            
84
#[test]
85
1
fn the_100_column_dump_shows_the_row_number_the_tag_count_and_from() {
86
1
    let (buf, _) = drawn(two_splits(|_| {}), (100, 30));
87
30
    let dump: Vec<String> = (0..buf.area.height).map(|y| row_text(&buf, y)).collect();
88
1
    assert!(
89
1
        split_line(&buf, 0).contains("1. #0 Assets:Bank:Checking"),
90
        "{dump:#?}"
91
    );
92
1
    assert!(
93
1
        split_line(&buf, 1).contains("2. #0 Expenses:Food:Groceries"),
94
        "{dump:#?}"
95
    );
96
1
}
97

            
98
#[test]
99
1
fn the_80_column_dump_shows_the_row_number_the_tag_count_and_from() {
100
1
    let (buf, _) = drawn(two_splits(|_| {}), (80, 24));
101
24
    let dump: Vec<String> = (0..buf.area.height).map(|y| row_text(&buf, y)).collect();
102
1
    assert!(split_line(&buf, 0).contains("1. #0 …"), "{dump:#?}");
103
1
    assert!(split_line(&buf, 1).contains("2. #0 …"), "{dump:#?}");
104
1
}
105

            
106
#[test]
107
1
fn both_commodity_columns_are_equally_wide() {
108
1
    let (buf, _) = drawn(two_splits(|_| {}), (100, 30));
109
1
    let at = marks(&buf, line_of(&buf, 0));
110
1
    assert_eq!(at.len(), 4, "{at:?}");
111
1
    assert_eq!(at[3] - at[2], at[2] - at[1], "{at:?}");
112
1
    assert_eq!(at[3] - at[2], 10, "{at:?}");
113
1
}
114

            
115
#[test]
116
1
fn the_rows_share_every_column() {
117
1
    for size in SIZES {
118
2
        let (buf, _) = drawn(two_splits(|_| {}), size);
119
2
        assert_eq!(shape(&buf, 0), shape(&buf, 1), "{size:?}");
120
    }
121
1
}
122

            
123
#[test]
124
1
fn widths_do_not_change_when_the_options_arrive() {
125
1
    for size in SIZES {
126
2
        let (before, _) = drawn(form_of(false, |_| {}), size);
127
2
        let (after, _) = drawn(two_splits(|_| {}), size);
128
2
        assert_eq!(shape(&before, 0), shape(&after, 0), "{size:?}");
129
    }
130
1
}
131

            
132
#[test]
133
1
fn widths_do_not_change_while_typing() {
134
1
    for size in SIZES {
135
2
        let (idle, _) = drawn(two_splits(|_| {}), size);
136
2
        let (typed, _) = drawn(two_splits(|sw| type_amount(sw, "123456789012345678")), size);
137
2
        assert_eq!(shape(&idle, 0), shape(&typed, 0), "{size:?}");
138
2
        let (filtered, _) = drawn(
139
2
            two_splits(|sw| {
140
2
                sw.col_focus = COL_FROM;
141
2
                sw.open_select();
142
2
                sw.edit_select(|select| {
143
2
                    "Expenses:Food:Gro"
144
2
                        .chars()
145
34
                        .for_each(|c| select.filter_push(c))
146
2
                });
147
2
            }),
148
2
            size,
149
        );
150
2
        assert_eq!(shape(&idle, 0), shape(&filtered, 0), "{size:?}");
151
    }
152
1
}
153

            
154
#[test]
155
1
fn the_conversion_slot_appears_only_when_needed() {
156
3
    let arrows = |buf: &Buffer| row_text(buf, line_of(buf, 0)).matches('→').count();
157
1
    let (idle, _) = drawn(two_splits(|_| {}), (100, 30));
158
1
    let (shown, _) = drawn(two_splits(|sw| sw.col_focus = COL_TO_AMOUNT), (100, 30));
159
1
    assert_eq!(arrows(&idle), 1);
160
1
    assert_eq!(arrows(&shown), 2);
161
1
    let (filled, _) = drawn(
162
1
        two_splits(|sw| {
163
1
            sw.col_focus = COL_TO_AMOUNT;
164
1
            type_amount(sw, "9");
165
1
            sw.col_focus = COL_VALUE;
166
1
        }),
167
1
        (100, 30),
168
    );
169
1
    assert_eq!(arrows(&filled), 2);
170
1
}
171

            
172
#[test]
173
1
fn the_tag_count_is_visible_with_long_names_and_counts_blank_rows() {
174
1
    for size in SIZES {
175
2
        let (buf, _) = drawn(two_splits(|_| {}), size);
176
2
        assert!(split_line(&buf, 0).contains("1. #0 "), "{size:?}");
177
2
        let (tagged, _) = drawn(
178
2
            two_splits(|sw| {
179
2
                sw.open_tags();
180
2
                sw.add_tag();
181
2
            }),
182
2
            size,
183
        );
184
2
        assert!(split_line(&tagged, 0).contains("1. #1 "), "{size:?}");
185
2
        assert!(split_line(&tagged, 1).contains("2. #0 "), "{size:?}");
186
    }
187
1
}
188

            
189
#[test]
190
1
fn the_row_number_and_from_stay_visible_on_every_focus() {
191
1
    for size in SIZES {
192
2
        for col in COLUMNS {
193
12
            let (buf, _) = drawn(two_splits(|sw| sw.col_focus = col), size);
194
12
            let y = line_of(&buf, 0);
195
12
            let text = split_line(&buf, 0);
196
12
            let from = text.split_once("1. #0 ").map_or("", |(_, rest)| rest);
197
12
            let shown = from.chars().next().is_some_and(|c| c != '▼' && c != '→');
198
12
            assert!(shown, "{size:?} col {col}: {text}");
199
12
            let focused = (0..buf.area.width)
200
682
                .any(|x| role_of(&buf, Position::new(x, y)) == Some(tvui::Role::InputFocused));
201
12
            assert!(
202
12
                focused,
203
                "{size:?} col {col}: the focused cell is shown\n{text}"
204
            );
205
        }
206
    }
207
1
}
208

            
209
5
fn inner(buf: &Buffer, row: usize) -> String {
210
5
    let text = row_text(buf, line_of(buf, row));
211
5
    let cells: Vec<&str> = text.split('║').collect();
212
5
    cells
213
5
        .get(1)
214
5
        .map_or(String::new(), |inner| inner.trim_end().to_string())
215
5
}
216

            
217
#[test]
218
1
fn the_100_column_split_lines_are_pinned() {
219
1
    let (buf, _) = drawn(two_splits(|_| {}), (100, 30));
220
1
    assert_eq!(
221
1
        inner(&buf, 0),
222
1
        "    1. #0   Assets:Bank:Checking     ▼ →  Expenses:Food:Groceries ▼  (none) ▼  same   ▼"
223
    );
224
1
    assert_eq!(
225
1
        inner(&buf, 1),
226
1
        "    2. #0   Expenses:Food:Groceries  ▼ →  Assets:Bank:Checking    ▼  (none) ▼  same   ▼"
227
    );
228
1
}
229

            
230
#[test]
231
1
fn the_80_column_split_lines_are_pinned() {
232
1
    let (buf, _) = drawn(two_splits(|_| {}), (80, 24));
233
1
    assert_eq!(
234
1
        inner(&buf, 0),
235
1
        "    1. #0   …Bank:Checking ▼ →  …od:Groceries ▼  (none) ▼  same   ▼"
236
    );
237
1
}
238

            
239
#[test]
240
1
fn the_leg_of_a_tag_row_scrolls_into_view_under_the_split() {
241
1
    let form = two_splits(|sw| {
242
1
        sw.open_tags();
243
1
        sw.add_tag();
244
1
        sw.focus_tag(0, "description");
245
1
        if let Some(tags) = sw.tags_mut() {
246
1
            tags.set_focused_text("a long description of the tag, wide enough to push the leg out");
247
        }
248
1
        sw.focus_tag(0, "side");
249
1
    });
250
1
    let (buf, _) = drawn(form, (80, 24));
251
1
    let focused = |y| {
252
1
        (0..buf.area.width)
253
60
            .any(|x| role_of(&buf, Position::new(x, y)) == Some(tvui::Role::InputFocused))
254
1
    };
255
1
    let y = line_of(&buf, 0) + 2;
256
1
    assert!(focused(y), "{}", row_text(&buf, y));
257
1
}
258

            
259
const FORINT: &str = "hungarian forint (huf)";
260

            
261
3
fn converting(sw: &mut SplitsWidget) {
262
3
    sw.col_focus = COL_FROM_COMM;
263
3
    sw.edit_select(|select| select.select_id("usd"));
264
3
    sw.col_focus = COL_TO_COMM;
265
3
    sw.edit_select(|select| select.select_id(FORINT));
266
3
    sw.col_focus = COL_VALUE;
267
3
}
268

            
269
#[test]
270
1
fn the_100_column_line_with_the_conversion_shown_is_pinned() {
271
1
    let (buf, _) = drawn(two_splits(converting), (100, 30));
272
1
    assert_eq!(
273
1
        inner(&buf, 0),
274
1
        "    1. #0   …ts:Bank:Checking ▼ →  …s:Food:Groceries ▼  USD    ▼  Hungar►▼            →"
275
    );
276
1
}
277

            
278
#[test]
279
1
fn the_80_column_line_with_the_conversion_shown_is_pinned() {
280
1
    let (buf, _) = drawn(two_splits(converting), (80, 24));
281
1
    assert_eq!(
282
1
        inner(&buf, 0),
283
1
        "    1. #0   …Checking ▼ →  …roceries ▼  USD    ▼  Hungar►▼            →"
284
    );
285
1
}
286

            
287
#[test]
288
1
fn a_to_account_cut_by_the_shift_is_clipped_not_hidden() {
289
1
    let (buf, _) = drawn(
290
1
        two_splits(|sw| {
291
1
            converting(sw);
292
1
            sw.col_focus = COL_TO_AMOUNT;
293
1
        }),
294
1
        (80, 24),
295
    );
296
1
    let y = line_of(&buf, 0);
297
1
    let text = row_text(&buf, y);
298
1
    assert_eq!(marks(&buf, y).len(), 4, "{text}");
299
1
    assert!(text.contains("ceries"), "{text}");
300
1
}