1
use super::*;
2

            
3
188
fn theme() -> Theme {
4
188
    Theme::preset("dn").expect("the dn preset parses")
5
188
}
6

            
7
174
fn state(active: bool, zoomed: bool, number: Option<u8>) -> FrameState {
8
174
    let mode = match active {
9
84
        true => FrameMode::Active,
10
90
        false => FrameMode::Passive,
11
    };
12
174
    FrameState {
13
174
        mode,
14
174
        caps: Caps::ALL,
15
174
        zoomed,
16
174
        number,
17
174
    }
18
174
}
19

            
20
184
fn drawn(title: &str, state: FrameState, area: Rect) -> Buffer {
21
184
    let mut buf = Buffer::empty(area);
22
184
    Chrome::new(&theme(), title, state).render(area, &mut buf);
23
184
    buf
24
184
}
25

            
26
31
fn row(buf: &Buffer, y: u16) -> String {
27
31
    (0..buf.area.width)
28
781
        .map(|x| buf[(x, y)].symbol().to_string())
29
        .collect()
30
31
}
31

            
32
14
fn text_at(buf: &Buffer, area: Rect) -> String {
33
14
    area.positions()
34
41
        .map(|position| buf[position].symbol().to_string())
35
        .collect()
36
14
}
37

            
38
const ACTIVE: FrameState = FrameState {
39
    mode: FrameMode::Active,
40
    caps: Caps::ALL,
41
    zoomed: false,
42
    number: None,
43
};
44

            
45
#[test]
46
1
fn an_active_window_has_the_icons_number_and_title() {
47
1
    let area = Rect::new(0, 0, 30, 4);
48
1
    let buf = drawn("Accounts", state(true, false, Some(1)), area);
49
1
    assert_eq!(row(&buf, 0), "╔═[■]═════ Accounts ═══1═[↑]═╗");
50
1
    assert_eq!(row(&buf, 1), format!("║{}║", " ".repeat(28)));
51
1
    assert_eq!(row(&buf, 3), format!("╚{}─┘", "═".repeat(27)));
52
1
}
53

            
54
#[test]
55
1
fn the_title_starts_at_tvs_column() {
56
1
    let area = Rect::new(0, 0, 80, 3);
57
1
    assert_eq!(title_area(area, "Accounts"), Some(Rect::new(36, 0, 8, 1)));
58
1
}
59

            
60
#[test]
61
1
fn the_unzoom_icon_shows_while_zoomed() {
62
1
    let area = Rect::new(0, 0, 20, 3);
63
1
    let buf = drawn("A", state(true, true, None), area);
64
1
    let zoom = zoom_area(area, state(true, true, None));
65
1
    assert_eq!(text_at(&buf, zoom.expect("fits")), "[↕]");
66
1
}
67

            
68
#[test]
69
1
fn a_passive_window_is_single_with_number_and_title_only() {
70
1
    let area = Rect::new(0, 0, 30, 4);
71
1
    let buf = drawn("Accounts", state(false, false, Some(2)), area);
72
1
    assert_eq!(row(&buf, 0), "┌───────── Accounts ───2─────┐");
73
1
    assert_eq!(row(&buf, 3), format!("└{}┘", "─".repeat(28)));
74
1
    let passive = theme().style(Role::PanelFramePassive);
75
1
    assert_eq!(buf[(0, 0)].style().fg, passive.fg);
76
1
    assert_eq!(buf[(11, 0)].style().fg, passive.fg);
77
1
}
78

            
79
#[test]
80
1
fn a_dragging_window_is_single_in_the_icon_colour_and_keeps_its_icons() {
81
1
    let area = Rect::new(0, 0, 30, 4);
82
1
    let dragging = FrameState {
83
1
        mode: FrameMode::Dragging,
84
1
        number: Some(1),
85
1
        ..FrameState::default()
86
1
    };
87
1
    let buf = drawn("Accounts", dragging, area);
88
1
    assert_eq!(row(&buf, 0), "┌─[■]───── Accounts ───1─[↑]─┐");
89
1
    assert_eq!(row(&buf, 3), format!("└{}─┘", "─".repeat(27)));
90
1
    let icon = theme().style(Role::FrameIcon);
91
1
    for (x, y) in [
92
1
        (0, 0),
93
1
        (3, 0),
94
1
        (11, 0),
95
1
        (23, 0),
96
1
        (26, 0),
97
1
        (28, 3),
98
1
        (29, 3),
99
1
        (0, 2),
100
1
    ] {
101
8
        let style = buf[(x, y)].style();
102
8
        assert_eq!((style.fg, style.bg), (icon.fg, icon.bg), "{x},{y}");
103
    }
104
1
    assert!(close_area(area, dragging).is_some());
105
1
    assert!(zoom_area(area, dragging).is_some());
106
1
    assert!(grow_area(area, dragging).is_some());
107
1
}
108

            
109
#[test]
110
1
fn the_active_roles_colour_frame_title_and_icons() {
111
1
    let theme = theme();
112
1
    let area = Rect::new(0, 0, 30, 4);
113
1
    let buf = drawn("Accounts", state(true, false, Some(1)), area);
114
1
    let frame = theme.style(Role::PanelFrame);
115
1
    let title = theme.style(Role::PanelTitle);
116
1
    let icon = theme.style(Role::FrameIcon);
117
1
    assert_eq!(buf[(0, 0)].style().fg, frame.fg);
118
1
    assert_eq!(buf[(2, 0)].style().fg, frame.fg);
119
1
    assert_eq!(buf[(3, 0)].style().fg, icon.fg);
120
1
    assert_eq!(buf[(3, 0)].style().bg, icon.bg);
121
1
    assert_eq!(buf[(23, 0)].style().fg, frame.fg);
122
1
    assert_eq!(buf[(25, 0)].style().fg, frame.fg);
123
1
    assert_eq!(buf[(26, 0)].style().fg, icon.fg);
124
1
    assert_eq!(buf[(28, 3)].style().fg, icon.fg);
125
1
    assert_eq!(buf[(29, 3)].style().fg, icon.fg);
126
1
    assert_eq!(buf[(1, 1)].style().bg, theme.style(Role::Panel).bg);
127
1
    for x in 10..=19 {
128
10
        assert_eq!(buf[(x, 0)].style().fg, title.fg, "x={x}");
129
10
        assert_eq!(buf[(x, 0)].style().bg, title.bg, "x={x}");
130
    }
131
1
}
132

            
133
#[test]
134
1
fn the_blanks_around_the_title_take_the_title_colours_in_both_presets() {
135
1
    for preset in ["dn", "turbo"] {
136
2
        let theme = Theme::preset(preset).expect("the preset parses");
137
2
        let mut buf = Buffer::empty(Rect::new(0, 0, 30, 3));
138
2
        Chrome::new(&theme, "Accounts", ACTIVE).render(buf.area, &mut buf);
139
2
        let title = theme.style(Role::PanelTitle);
140
2
        for x in [10, 19] {
141
4
            assert_eq!(buf[(x, 0)].symbol(), " ", "{preset} x={x}");
142
4
            assert_eq!(buf[(x, 0)].style().bg, title.bg, "{preset} x={x}");
143
4
            assert_eq!(buf[(x, 0)].style().fg, title.fg, "{preset} x={x}");
144
        }
145
    }
146
1
}
147

            
148
#[test]
149
1
fn a_long_title_is_cut_to_the_width_less_ten() {
150
1
    let area = Rect::new(0, 0, 20, 3);
151
1
    let buf = drawn("A very long title", state(false, false, None), area);
152
1
    assert_eq!(row(&buf, 0), "┌─── A very lon ───┐");
153
1
    let cut = title_area(area, "A very long title");
154
1
    assert_eq!(cut, Some(Rect::new(5, 0, 10, 1)));
155
1
}
156

            
157
#[test]
158
1
fn a_full_width_title_keeps_its_last_character_beside_the_zoom_icon() {
159
1
    let area = Rect::new(0, 0, 30, 3);
160
1
    let buf = drawn(
161
1
        "A title that is much longer",
162
1
        state(true, false, Some(1)),
163
1
        area,
164
1
    );
165
1
    assert_eq!(row(&buf, 0), "╔═[■]A title that is much[↑]═╗");
166
1
}
167

            
168
12
fn assert_areas_match_drawing(area: Rect, state: FrameState) {
169
12
    let buf = drawn("Window", state, area);
170
36
    let cells = |found: Option<Rect>| found.map(|rect| text_at(&buf, rect));
171
12
    let active = state.mode == FrameMode::Active && area.width >= 12 && area.height >= 2;
172
12
    assert_eq!(cells(close_area(area, state)), active.then(|| "[■]".into()));
173
12
    let zoom = if state.zoomed { "[↕]" } else { "[↑]" };
174
12
    assert_eq!(cells(zoom_area(area, state)), active.then(|| zoom.into()));
175
12
    assert_eq!(cells(grow_area(area, state)), active.then(|| "─┘".into()));
176
12
    if !active && area.height > 0 {
177
8
        assert!(!row(&buf, area.y).contains('['), "{state:?} {area:?}");
178
4
    }
179
12
}
180

            
181
#[test]
182
1
fn the_hit_areas_equal_the_drawn_cells() {
183
1
    let full = Rect::new(3, 2, 40, 6);
184
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 50, 10));
185
1
    let theme = theme();
186
1
    Chrome::new(&theme, "Window", state(true, false, Some(3))).render(full, &mut buf);
187
1
    let title = title_area(full, "Window").expect("fits");
188
1
    assert_eq!(text_at(&buf, title), "Window");
189
1
    assert_eq!(close_area(full, ACTIVE), Some(Rect::new(5, 2, 3, 1)));
190
1
    assert_eq!(zoom_area(full, ACTIVE), Some(Rect::new(38, 2, 3, 1)));
191
1
    assert_eq!(grow_area(full, ACTIVE), Some(Rect::new(41, 7, 2, 1)));
192
1
    assert_eq!(buf[(36, 2)].symbol(), "3");
193
1
    let zoomed = state(true, true, None);
194
1
    let passive = state(false, false, Some(1));
195
1
    for area in [
196
1
        Rect::new(0, 0, 40, 6),
197
1
        Rect::new(0, 0, 12, 2),
198
1
        Rect::new(0, 0, 11, 3),
199
1
        Rect::new(0, 0, 30, 1),
200
1
    ] {
201
4
        for state in [ACTIVE, zoomed, passive] {
202
12
            assert_areas_match_drawing(area, state);
203
        }
204
    }
205
1
}
206

            
207
#[test]
208
1
fn the_inner_area_is_inside_the_frame() {
209
1
    assert_eq!(inner(Rect::new(3, 2, 40, 6)), Rect::new(4, 3, 38, 4));
210
1
}
211

            
212
#[test]
213
1
fn tiny_windows_drop_icons_then_labels_without_panicking() {
214
1
    let full = state(true, false, Some(1));
215
1
    let narrow = Rect::new(0, 0, 11, 3);
216
1
    let icons_gone = drawn("Title", full, narrow);
217
1
    assert_eq!(row(&icons_gone, 0), "╔═══ T ═══╗");
218
1
    assert!(close_area(narrow, full).is_none());
219
1
    assert!(zoom_area(narrow, full).is_none());
220
1
    assert!(grow_area(narrow, full).is_none());
221
1
    assert!(title_area(narrow, "Title").is_some());
222
1
    assert!(title_area(Rect::new(0, 0, 10, 3), "Title").is_none());
223
1
    assert!(close_area(Rect::new(0, 0, 12, 3), full).is_some());
224
1
    for width in 0..=14 {
225
15
        for height in 0..=4 {
226
75
            for active in [true, false] {
227
150
                let area = Rect::new(0, 0, width, height);
228
150
                let buf = drawn("Title", state(active, true, Some(9)), area);
229
150
                assert_eq!(buf.area, area);
230
            }
231
        }
232
    }
233
1
}
234

            
235
#[test]
236
1
fn a_one_row_window_draws_no_labels() {
237
1
    let area = Rect::new(0, 0, 30, 1);
238
1
    let buf = drawn("Title", state(true, false, Some(1)), area);
239
1
    assert!(!row(&buf, 0).contains("Title"));
240
1
    assert!(close_area(area, ACTIVE).is_none());
241
1
}
242

            
243
#[test]
244
1
fn no_number_draws_none() {
245
1
    let buf = drawn("", state(false, false, None), Rect::new(0, 0, 30, 3));
246
1
    assert_eq!(row(&buf, 0), format!("┌{}┐", "─".repeat(28)));
247
1
}
248

            
249
9
fn tiled(name: &str, tail: &str, width: u16) -> String {
250
9
    let area = Rect::new(0, 0, width, 3);
251
9
    let title = joined_title(name, tail);
252
9
    row(&drawn(&title, state(false, false, None), area), 0)
253
9
}
254

            
255
#[test]
256
1
fn a_cut_name_keeps_the_ordinal_whole() {
257
1
    assert_eq!(tiled("Transactions", " (2)", 20), "┌─── Trans… (2) ───┐");
258
1
}
259

            
260
#[test]
261
1
fn a_cut_name_keeps_the_filter_whole() {
262
1
    let row = tiled("Transactions", " [2026-11-15–]", 31);
263
1
    assert!(row.contains("Transa… [2026-11-15–]"), "{row}");
264
1
}
265

            
266
#[test]
267
1
fn a_tail_wider_than_the_title_keeps_its_head() {
268
1
    let row = tiled("Transactions", " [2026-11-15–]", 20);
269
1
    assert!(row.contains("[2026-11-"), "{row}");
270
1
    assert!(!row.contains('…'), "{row}");
271
1
}
272

            
273
#[test]
274
1
fn a_sixteen_cell_title_in_a_tile() {
275
1
    assert_eq!(
276
1
        tiled("Transactions", " [date>2020]", 26),
277
1
        "┌─── Tra… [date>2020] ───┐"
278
    );
279
1
    assert!(tiled("Transactions", " (2)", 26).contains("Transactions (2)"));
280
1
    assert!(tiled("Transactions", "", 26).contains("Transactions"));
281
1
    assert!(tiled("A very long title of one", "", 26).contains("A very long"));
282
1
}
283

            
284
#[test]
285
1
fn wide_characters_are_counted_by_their_cells() {
286
1
    let row = tiled("口座口座口座", " (2)", 20).replace(' ', "");
287
1
    assert!(row.contains("口座…(2)"), "{row}");
288
1
    let row = tiled("口座口座口座口座", "", 20).replace(' ', "");
289
1
    assert!(row.contains("口座口座口"), "{row}");
290
1
}
291

            
292
#[test]
293
1
fn a_title_that_fits_is_drawn_whole() {
294
1
    let title = joined_title("Accounts", " (2)");
295
1
    assert_eq!(fitted(&title, 12), "Accounts (2)");
296
1
    assert_eq!(fitted(&title, 11), "Accoun… (2)");
297
1
}
298

            
299
#[test]
300
1
fn a_wide_title_that_leaves_a_cell_over_shows_no_frame_line_inside() {
301
1
    let area = Rect::new(0, 0, 20, 3);
302
1
    for (name, tail) in [("A口座口座口座", ""), ("口座口座口座", " (2)")] {
303
2
        let title = joined_title(name, tail);
304
2
        let buf = drawn(&title, state(false, false, None), area);
305
20
        let inside: Vec<&str> = (5..15).map(|x| buf[(x, 0)].symbol()).collect();
306
2
        assert!(!inside.contains(&"─"), "{name}{tail}: {inside:?}");
307
    }
308
1
}
309

            
310
#[test]
311
1
fn a_window_without_the_flag_has_no_icon_and_no_hit_area() {
312
1
    let area = Rect::new(0, 0, 30, 4);
313
1
    let fixed = FrameState {
314
1
        caps: Caps::FIXED,
315
1
        ..ACTIVE
316
1
    };
317
1
    assert_eq!(zoom_area(area, fixed), None);
318
1
    assert_eq!(grow_area(area, fixed), None);
319
1
    assert!(close_area(area, fixed).is_some());
320
1
    let buf = drawn("Viewer", fixed, area);
321
1
    assert!(!row(&buf, 0).contains('↑'));
322
1
    assert!(!row(&buf, 3).contains('┘'));
323
1
    let zoom_only = FrameState {
324
1
        caps: Caps {
325
1
            zoom: true,
326
1
            ..Caps::FIXED
327
1
        },
328
1
        ..ACTIVE
329
1
    };
330
1
    assert!(zoom_area(area, zoom_only).is_some());
331
1
    assert_eq!(grow_area(area, zoom_only), None);
332
1
}