1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
use super::*;
use zellij_utils::data::{PaletteColor, Style};
fn style_with_distinct_selected_background() -> Style {
let mut style = Style::default();
style.colors.text_selected.background = PaletteColor::EightBit(200);
style.colors.text_selected.base = PaletteColor::EightBit(201);
style.colors.text_selected.emphasis_1 = PaletteColor::EightBit(202);
style.colors.text_selected.emphasis_3 = PaletteColor::EightBit(203);
style
}
fn sample_shortcuts() -> GuestModalShortcuts {
GuestModalShortcuts {
zoom: vec!["Ctrl g".to_string(), "o".to_string(), "f".to_string()],
ascend: vec!["Ctrl o".to_string()],
descend: vec!["Ctrl o".to_string(), "d".to_string()],
}
}
fn render_lines(chunks: &[CharacterChunk]) -> Vec<String> {
let mut lines: Vec<String> = Vec::new();
for chunk in chunks {
let text: String = chunk
.terminal_characters
.iter()
.map(|c| c.character)
.collect();
lines.push(text);
}
lines
}
fn rendered_text(chunks: &[CharacterChunk]) -> String {
chunks
.iter()
.flat_map(|chunk| chunk.terminal_characters.iter().map(|c| c.character))
.collect()
}
#[test]
fn renders_all_expected_content() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let chunks = guest_modal_chunks(80, 30, 0, 0, &style, "my-session", 1, &shortcuts);
assert_eq!(chunks.len(), 30);
let rendered = rendered_text(&chunks);
assert!(rendered.contains("Nested Zellij session detected: my-session"));
assert!(rendered.contains("What would you like to do?"));
assert!(rendered.contains("Zoom in and control this session"));
assert!(rendered.contains("Control this session on focus"));
assert!(rendered.contains("<↓↑> select"));
assert!(rendered.contains("<Enter> confirm"));
assert!(rendered.contains("<Esc> dismiss"));
}
#[test]
fn renders_resolved_keybindings_as_joined_tokens() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let chunks = guest_modal_chunks(80, 30, 0, 0, &style, "s", 0, &shortcuts);
let rendered = rendered_text(&chunks);
assert!(rendered.contains("<Ctrl g> + <o> + <f>"));
assert!(rendered.contains("<Ctrl o>"));
}
#[test]
fn renders_unbound_when_no_keys() {
let style = Style::default();
let shortcuts = GuestModalShortcuts::default();
let chunks = guest_modal_chunks(80, 30, 0, 0, &style, "s", 0, &shortcuts);
let rendered = rendered_text(&chunks);
assert!(rendered.contains("<unbound>"));
}
#[test]
fn selection_marker_moves_with_selected_index() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let first = render_lines(&guest_modal_chunks(
80, 30, 0, 0, &style, "s", 0, &shortcuts,
));
assert!(first
.iter()
.any(|line| line.trim_start().starts_with("> 1.")));
assert!(first.iter().any(|line| line.trim_start().starts_with("2.")));
assert!(!first
.iter()
.any(|line| line.trim_start().starts_with("> 2.")));
let second = render_lines(&guest_modal_chunks(
80, 30, 0, 0, &style, "s", 1, &shortcuts,
));
assert!(second
.iter()
.any(|line| line.trim_start().starts_with("> 2.")));
assert!(!second
.iter()
.any(|line| line.trim_start().starts_with("> 1.")));
}
fn number_column(line: &str, number: &str) -> usize {
line.find(number).expect("option number present")
}
#[test]
fn options_are_left_justified_within_the_centered_block() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let chunks = guest_modal_chunks(80, 30, 0, 0, &style, "s", 0, &shortcuts);
let lines = render_lines(&chunks);
let selected_line = lines
.iter()
.find(|line| line.trim_start().starts_with("> 1."))
.expect("selected option line present");
let selected_number_column = number_column(selected_line, "1.");
assert!(
selected_number_column > 2,
"block must be centered with a left pad before the marker"
);
for (number, needle) in [("2.", "2.")] {
let line = lines
.iter()
.find(|line| line.trim_start().starts_with(needle))
.expect("unselected option line present");
assert_eq!(
number_column(line, number),
selected_number_column,
"the option number column is stable across selection"
);
}
}
#[test]
fn small_columns_render_the_fallback() {
let style = Style::default();
let shortcuts = GuestModalShortcuts::default();
let chunks = guest_modal_chunks(18, 20, 0, 0, &style, "s", 0, &shortcuts);
assert_eq!(chunks.len(), 20);
let rendered = rendered_text(&chunks);
assert!(rendered.contains("Nested"));
assert!(!rendered.contains("What would you like to do?"));
}
#[test]
fn small_rows_render_the_fallback() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let chunks = guest_modal_chunks(80, 5, 0, 0, &style, "s", 0, &shortcuts);
assert_eq!(chunks.len(), 5);
let rendered = rendered_text(&chunks);
assert!(rendered.contains("Nested Zellij session"));
assert!(!rendered.contains("What would you like to do?"));
}
#[test]
fn tiny_geometry_does_not_panic() {
let style = Style::default();
let shortcuts = GuestModalShortcuts::default();
let chunks = guest_modal_chunks(3, 2, 0, 0, &style, "session", 0, &shortcuts);
assert_eq!(chunks.len(), 2);
let empty = guest_modal_chunks(0, 0, 0, 0, &style, "session", 0, &shortcuts);
assert!(empty.is_empty());
}
#[test]
fn every_rendered_row_spans_the_full_width() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let columns = 80;
let chunks = guest_modal_chunks(columns, 30, 0, 0, &style, "s", 0, &shortcuts);
for chunk in &chunks {
let width: usize = chunk
.terminal_characters
.iter()
.map(|c| c.character.width().unwrap_or(0).max(1))
.sum();
assert_eq!(width, columns);
}
}
#[test]
fn hit_test_round_trips_the_options() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let rows = 40;
let columns = 80;
let mut hit_rows: Vec<usize> = Vec::new();
for row in 0..rows {
if let Some(option) =
guest_modal_option_at_content_row(rows, columns, row, &style, "session", 0, &shortcuts)
{
assert_eq!(hit_rows.len(), option);
hit_rows.push(row);
}
}
assert_eq!(hit_rows.len(), 2);
}
#[test]
fn hit_test_returns_none_off_option_rows_and_in_fallback() {
let style = Style::default();
let shortcuts = sample_shortcuts();
assert_eq!(
guest_modal_option_at_content_row(40, 10, 0, &style, "session", 0, &shortcuts),
None
);
assert_eq!(
guest_modal_option_at_content_row(4, 10, 2, &style, "session", 0, &shortcuts),
None
);
assert_eq!(
guest_modal_option_at_content_row(40, 80, 0, &style, "session", 0, &shortcuts),
None
);
}
#[test]
fn hit_test_matches_rendered_marker_rows() {
let style = Style::default();
let shortcuts = sample_shortcuts();
let rows = 40;
let columns = 80;
let selected = 1;
let lines = render_lines(&guest_modal_chunks(
columns, rows, 0, 0, &style, "session", selected, &shortcuts,
));
for (row, line) in lines.iter().enumerate() {
let hit = guest_modal_option_at_content_row(
rows, columns, row, &style, "session", selected, &shortcuts,
);
let trimmed = line.trim_start();
let is_option_line =
trimmed.starts_with("> ") || trimmed.starts_with("1.") || trimmed.starts_with("2.");
assert_eq!(hit.is_some(), is_option_line, "row {} mismatch", row);
}
}
#[test]
fn selected_rows_carry_the_selected_background_rectangle() {
let style = style_with_distinct_selected_background();
let shortcuts = sample_shortcuts();
let selected = 0;
let columns = 80;
let rows = 40;
let styles = guest_modal_styles(&style);
let layout = guest_modal_layout(columns, rows, &styles, "session", selected, &shortcuts);
let selected_option_offset = layout.option_rows[selected];
let chunks = guest_modal_chunks(columns, rows, 0, 0, &style, "session", selected, &shortcuts);
let selected_row_index = layout.start_row + selected_option_offset;
let selected_chunk = &chunks[selected_row_index];
let selected_background = styles.selected_fill.background;
let base_background = styles.fill.background;
assert_ne!(
selected_background, base_background,
"the test style must give the selected background a distinct value"
);
let block_start = layout.left_pad;
let block_end = layout.left_pad + layout.block_width;
let mut block_cells = 0;
for (column, character) in selected_chunk.terminal_characters.iter().enumerate() {
if column >= block_start && column < block_end {
assert_eq!(
character.styles.background, selected_background,
"block cell {} on the selected row must carry the selected background",
column
);
block_cells += 1;
} else {
assert_eq!(
character.styles.background, base_background,
"outer padding cell {} must keep the base background",
column
);
}
}
assert_eq!(
block_cells, layout.block_width,
"the selected rectangle spans the whole block width"
);
}
#[test]
fn unselected_rows_do_not_use_the_selected_background() {
let style = style_with_distinct_selected_background();
let shortcuts = sample_shortcuts();
let selected = 0;
let columns = 80;
let rows = 40;
let styles = guest_modal_styles(&style);
let layout = guest_modal_layout(columns, rows, &styles, "session", selected, &shortcuts);
let other_option_offset = layout.option_rows[1];
let selected_background = styles.selected_fill.background;
let chunks = guest_modal_chunks(columns, rows, 0, 0, &style, "session", selected, &shortcuts);
let other_row = &chunks[layout.start_row + other_option_offset];
for character in &other_row.terminal_characters {
assert_ne!(
character.styles.background, selected_background,
"unselected option row must not carry the selected background"
);
}
}
#[test]
fn keycode_words_join_with_plus_separators() {
let style = Style::default();
let styles = guest_modal_styles(&style);
let keys = vec!["Ctrl g".to_string(), "o".to_string()];
let words = guest_modal_keycode_words(&keys, styles.fill.clone(), styles.keycode.clone());
let joined: String = words.iter().map(|(text, _)| text.as_str()).collect();
assert_eq!(joined, "<Ctrl g>+<o>");
assert_eq!(words[0].1, styles.keycode);
assert_eq!(words[1].1, styles.fill);
assert_eq!(words[2].1, styles.keycode);
}
#[test]
fn keycode_words_render_unbound_when_empty() {
let style = Style::default();
let styles = guest_modal_styles(&style);
let words = guest_modal_keycode_words(&[], styles.fill.clone(), styles.keycode.clone());
assert_eq!(words.len(), 1);
assert_eq!(words[0].0, "<unbound>");
assert_eq!(words[0].1, styles.fill);
}
#[test]
fn wrap_words_respects_width_and_indent() {
let style = Style::default();
let styles = guest_modal_styles(&style);
let words = guest_modal_text_words("one two three four five", styles.fill.clone());
let indent = 4;
let width = 14;
let lines = guest_modal_wrap_words(&words, indent, styles.fill.clone(), width);
assert!(lines.len() > 1, "text wider than the width must wrap");
for line in &lines {
let indent_segment = &line[0];
assert_eq!(indent_segment.0, " ".repeat(indent));
let rendered_width: usize = line.iter().map(|(text, _)| text.width()).sum();
assert!(
rendered_width <= width,
"wrapped line width {} exceeds {}",
rendered_width,
width
);
}
}
#[test]
fn center_pad_centers_and_clamps() {
assert_eq!(center_modal_pad(10, 30), 10);
assert_eq!(center_modal_pad(30, 30), 0);
assert_eq!(center_modal_pad(40, 30), 0);
}
|