1
//! The cells and the drop-down of a form are recorded where the frame drew them.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::hits::{Hits, Surface, Target};
6
use crate::modal::Modal;
7
use crate::test_support::draw_frame;
8
use crate::widgets::grid::CellId;
9
use crate::widgets::splits::{
10
    COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE,
11
};
12
use crate::widgets::{SelectOption, Widget};
13
use ratatui::buffer::Buffer;
14
use ratatui::layout::Rect;
15

            
16
const SPLITS: usize = 3;
17

            
18
12
fn options(n: usize) -> Vec<SelectOption> {
19
12
    (0..n)
20
        .map(|i| SelectOption {
21
70
            id: format!("id{i}"),
22
70
            label: format!("label{i}"),
23
70
        })
24
        .collect()
25
12
}
26

            
27
/// A transaction form with the splits focused and `n` account options in every select.
28
6
fn transaction(n: usize) -> App {
29
6
    let mut form = Form::transaction_create();
30
6
    form.focus = SPLITS;
31
6
    if let Some(Widget::Splits(splits)) = form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
32
6
        splits.set_account_options(options(n));
33
6
        splits.set_commodity_options(options(n));
34
    }
35
6
    let mut app = crate::test_support::app();
36
6
    app.overlays.push(Modal::Form(form));
37
6
    app
38
6
}
39

            
40
6
fn splits(app: &mut App) -> &mut crate::widgets::SplitsWidget {
41
6
    match app.overlays.top_mut() {
42
6
        Some(Modal::Form(form)) => match form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
43
6
            Some(Widget::Splits(splits)) => splits,
44
            _ => panic!("no splits"),
45
        },
46
        _ => panic!("no form"),
47
    }
48
6
}
49

            
50
4
fn cells(hits: &Hits) -> Vec<(Rect, CellId)> {
51
4
    hits.entries()
52
4
        .iter()
53
146
        .filter_map(|(rect, target)| match target {
54
23
            Target::Cell { field, cell } if *field == SPLITS => Some((*rect, *cell)),
55
123
            _ => None,
56
146
        })
57
        .collect()
58
4
}
59

            
60
5
fn text(buffer: &Buffer, rect: Rect) -> String {
61
5
    (rect.x..rect.right())
62
164
        .map(|x| buffer[(x, rect.y)].symbol())
63
        .collect()
64
5
}
65

            
66
2
fn cols(cells: &[(Rect, CellId)], row: usize) -> Vec<usize> {
67
2
    let of_row = cells
68
2
        .iter()
69
11
        .filter(|(_, cell)| cell.row == row && !cell.tag);
70
11
    of_row.map(|(_, cell)| cell.col).collect()
71
2
}
72

            
73
#[test]
74
1
fn a_splits_row_records_its_cells_left_to_right_and_not_its_labels() {
75
1
    let app = transaction(3);
76
1
    let (buffer, hits) = draw_frame(&app, 160, 30);
77
1
    let found = cells(&hits);
78
1
    assert_eq!(
79
1
        cols(&found, 0),
80
1
        [COL_FROM, COL_TO, COL_FROM_COMM, COL_TO_COMM, COL_VALUE]
81
    );
82
5
    let xs: Vec<u16> = found.iter().map(|(rect, _)| rect.x).collect();
83
4
    assert!(xs.windows(2).all(|pair| pair[0] < pair[1]), "{xs:?}");
84
1
    assert!(text(&buffer, found[0].0).contains("label0"));
85
1
}
86

            
87
#[test]
88
1
fn the_conversion_amount_is_a_cell_while_it_shows() {
89
1
    let mut app = transaction(3);
90
1
    splits(&mut app).col_focus = COL_TO_AMOUNT;
91
1
    let (_, hits) = draw_frame(&app, 160, 30);
92
1
    assert_eq!(cols(&cells(&hits), 0).last(), Some(&COL_TO_AMOUNT));
93
1
}
94

            
95
#[test]
96
1
fn a_shifted_row_records_the_cells_where_the_shift_put_them() {
97
1
    let mut app = transaction(3);
98
1
    splits(&mut app).col_focus = COL_TO_AMOUNT;
99
1
    let (buffer, hits) = draw_frame(&app, 50, 30);
100
1
    let found = cells(&hits);
101
7
    let at = |col| found.iter().find(|(_, cell)| cell.col == col);
102
1
    let (rect, _) = at(COL_TO_AMOUNT).expect("the focused cell is drawn whole");
103
1
    assert!(text(&buffer, *rect).trim().is_empty());
104
1
    assert!(at(COL_FROM).is_some(), "the frozen From stays: {found:?}");
105
1
    assert!(at(COL_TO).is_none(), "{found:?}");
106
1
    assert!(rect.right() <= 50);
107
1
}
108

            
109
#[test]
110
1
fn an_open_tag_list_records_tag_cells_apart_from_the_split_cells() {
111
1
    let mut app = transaction(3);
112
1
    splits(&mut app).add_tag();
113
1
    splits(&mut app).open_tags();
114
1
    let (_, hits) = draw_frame(&app, 160, 30);
115
1
    let found = cells(&hits);
116
6
    assert!(found.iter().any(|(_, cell)| cell.tag && cell.row == 0));
117
1
    assert!(found.iter().any(|(_, cell)| !cell.tag));
118
1
}
119

            
120
#[test]
121
1
fn an_open_select_records_its_options_on_top_of_a_box_that_is_not_a_block() {
122
1
    let mut app = transaction(3);
123
1
    splits(&mut app).open_select();
124
1
    let (buffer, hits) = draw_frame(&app, 160, 30);
125
1
    let entries = hits.entries();
126
1
    let options: Vec<_> = entries
127
1
        .iter()
128
42
        .filter(|(_, target)| matches!(target, Target::Option(_)))
129
        .collect();
130
1
    assert_eq!(options.len(), 3);
131
1
    for (index, (rect, target)) in options.iter().enumerate() {
132
3
        assert_eq!(*target, Target::Option(index));
133
3
        assert!(text(&buffer, *rect).contains(&format!("label{index}")));
134
    }
135
1
    let boxed = entries
136
1
        .iter()
137
30
        .find(|(_, target)| *target == Target::Body(Surface::Options));
138
1
    assert!(boxed.is_some_and(|(rect, _)| rect.contains(options[0].0.as_position())));
139
1
    assert_eq!(
140
1
        hits.hit_at(options[0].0.as_position())
141
            .map(|(_, target, _)| target),
142
1
        Some(Target::Option(0))
143
    );
144
1
}
145

            
146
#[test]
147
1
fn a_long_list_records_the_window_it_shows_and_a_bar() {
148
1
    let mut app = transaction(20);
149
1
    splits(&mut app).open_select();
150
1
    let (_, hits) = draw_frame(&app, 160, 30);
151
1
    let shown: Vec<_> = hits
152
1
        .entries()
153
1
        .iter()
154
51
        .filter_map(|(_, target)| match target {
155
8
            Target::Option(index) => Some(*index),
156
43
            _ => None,
157
51
        })
158
        .collect();
159
1
    assert_eq!(shown, (0..8).collect::<Vec<_>>());
160
1
    let entries = hits.entries();
161
1
    let bar: Vec<_> = entries
162
1
        .iter()
163
51
        .filter(|(_, target)| matches!(target, Target::Scroll(Surface::Options, _)))
164
        .collect();
165
1
    assert!(!bar.is_empty());
166
1
}