nandi/jolt-nativepublic Fork 0
a78520161e25e26b1bb67e2553889eeaaf58d5ab
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

tests.rs · 590 lines · 19.5 KBRust Blame HistoryRaw
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 17d ago1//! The backend end to end, with no terminal.
2//!
3//! Every one of these mounts real nodes through the same calls the ABI makes,
4//! paints a real frame, and asserts on the lines that came out — which is the
5//! whole reason painting goes through a grid. Nothing here needs a TTY, a
6//! display or raw mode, so it all runs in CI.
7
8use crate::screen::attr;
9use crate::tree::Value;
10use crate::ui::Ui;
11
12fn ui() -> Ui {
13 Ui::new(24, 8)
14}
15
16/// Mount `tag` under `parent` with the string props given.
17fn node(ui: &mut Ui, parent: u32, tag: &str, props: &[(&str, &str)]) -> u32 {
18 let id = ui.tree.new_node(tag);
19 for (key, value) in props {
20 ui.tree.set(id, key, Value::Str((*value).to_owned()));
21 }
22 ui.tree.append(parent, id);
23 id
24}
25
26fn events(ui: &mut Ui) -> Vec<(u32, String, String, f64)> {
27 let mut out = Vec::new();
28 while ui.tree.poll() {
29 let event = ui.tree.current().unwrap();
30 out.push((
31 event.node,
32 event.name.to_owned(),
33 event.text.clone(),
34 event.num,
35 ));
36 }
37 out
38}
39
40#[test]
41fn a_column_paints_its_children_down_the_page() {
42 let mut ui = ui();
43 let root = ui.tree.root();
44 node(&mut ui, root, "label", &[("label", "first")]);
45 node(&mut ui, root, "label", &[("label", "second")]);
46 ui.frame();
47 assert_eq!(ui.screen.line(0), "first");
48 assert_eq!(ui.screen.line(1), "second");
49}
50
51#[test]
52fn a_row_paints_its_children_across_with_its_spacing_between_them() {
53 let mut ui = ui();
54 let root = ui.tree.root();
55 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
56 ui.tree.set(row, "spacing", Value::Num(2.0));
57 node(&mut ui, row, "label", &[("label", "aa")]);
58 node(&mut ui, row, "label", &[("label", "bb")]);
59 ui.frame();
60 assert_eq!(ui.screen.line(0), "aa bb");
61}
62
63#[test]
64fn a_label_wraps_to_the_width_it_was_given() {
65 let mut ui = Ui::new(10, 4);
66 let root = ui.tree.root();
67 node(&mut ui, root, "label", &[("label", "one two three four")]);
68 ui.frame();
69 assert_eq!(ui.screen.line(0), "one two");
70 assert_eq!(ui.screen.line(1), "three four");
71}
72
73#[test]
74fn a_frame_draws_a_border_with_its_label_in_the_top_edge() {
75 let mut ui = Ui::new(12, 4);
76 let root = ui.tree.root();
77 let frame = node(&mut ui, root, "frame", &[("label", "Task")]);
78 node(&mut ui, frame, "label", &[("label", "hi")]);
79 ui.frame();
80 assert_eq!(ui.screen.line(0), "┌ Task ────┐");
81 assert_eq!(ui.screen.line(1), "│hi │");
82 assert_eq!(ui.screen.line(2), "└──────────┘");
83}
84
85#[test]
86fn the_first_focusable_widget_takes_focus_and_tab_walks_the_ring() {
87 let mut ui = ui();
88 let root = ui.tree.root();
89 let first = node(&mut ui, root, "button", &[("label", "one")]);
90 let second = node(&mut ui, root, "button", &[("label", "two")]);
91 ui.frame();
92 assert_eq!(ui.focus(), first);
93 ui.key("tab");
94 assert_eq!(ui.focus(), second);
95 ui.key("tab");
96 assert_eq!(ui.focus(), first, "the ring wraps");
97 ui.key("shift+tab");
98 assert_eq!(ui.focus(), second);
99}
100
101#[test]
102fn autofocus_beats_paint_order() {
103 let mut ui = ui();
104 let root = ui.tree.root();
105 node(&mut ui, root, "button", &[("label", "one")]);
106 let wanted = node(&mut ui, root, "entry", &[]);
107 ui.tree.set(wanted, "autofocus", Value::Bool(true));
108 ui.frame();
109 assert_eq!(ui.focus(), wanted);
110}
111
112#[test]
113fn a_focused_button_is_drawn_in_reverse_and_activates_on_enter() {
114 let mut ui = ui();
115 let root = ui.tree.root();
116 let button = node(&mut ui, root, "button", &[("label", "go")]);
117 ui.frame();
118 assert_eq!(ui.screen.line(0), "[ go ]");
119 assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::REVERSE));
120 ui.key("enter");
121 assert_eq!(
122 events(&mut ui),
123 vec![(button, "click".into(), String::new(), 0.0)]
124 );
125}
126
127#[test]
128fn a_checkbutton_writes_its_new_state_back_as_well_as_reporting_it() {
129 let mut ui = ui();
130 let root = ui.tree.root();
131 let check = node(&mut ui, root, "checkbutton", &[("label", "live")]);
132 ui.frame();
133 assert_eq!(ui.screen.line(0), "[ ] live");
134 ui.key("space");
135 assert_eq!(
136 events(&mut ui),
137 vec![(check, "toggled".into(), String::new(), 1.0)]
138 );
139 // A caller that ignores the event still has a working control.
140 assert_eq!(ui.tree.props(check).bool("active", false), true);
141 ui.frame();
142 assert_eq!(ui.screen.line(0), "[x] live");
143}
144
145#[test]
146fn typing_in_an_entry_edits_its_text_and_reports_every_change() {
147 let mut ui = ui();
148 let root = ui.tree.root();
149 let entry = node(&mut ui, root, "entry", &[("placeholder", "name")]);
150 ui.frame();
151 assert_eq!(
152 ui.screen.line(0),
153 "name",
154 "the placeholder shows until it is typed in"
155 );
156 for key in ["h", "i", "space", "there"] {
157 for name in [key] {
158 if name.chars().count() > 1 && name != "space" {
159 for ch in name.chars() {
160 ui.key(&ch.to_string());
161 }
162 } else {
163 ui.key(name);
164 }
165 }
166 }
167 assert_eq!(ui.tree.props(entry).str("text"), "hi there");
168 let changes = events(&mut ui);
169 assert_eq!(changes.len(), 8);
170 assert_eq!(changes.last().unwrap().2, "hi there");
171 ui.frame();
172 assert_eq!(ui.screen.line(0), "hi there");
173}
174
175#[test]
176fn readline_keys_edit_where_the_caret_is() {
177 let mut ui = ui();
178 let root = ui.tree.root();
179 let entry = node(&mut ui, root, "entry", &[("text", "one two three")]);
180 ui.frame();
181 ui.key("ctrl+w");
182 assert_eq!(ui.tree.props(entry).str("text"), "one two ");
183 ui.key("ctrl+a");
184 ui.key("delete");
185 assert_eq!(ui.tree.props(entry).str("text"), "ne two ");
186 ui.key("ctrl+e");
187 ui.key("backspace");
188 assert_eq!(ui.tree.props(entry).str("text"), "ne two");
189 ui.key("ctrl+u");
190 assert_eq!(ui.tree.props(entry).str("text"), "");
191}
192
193#[test]
194fn enter_in_an_entry_activates_rather_than_typing() {
195 let mut ui = ui();
196 let root = ui.tree.root();
197 let entry = node(&mut ui, root, "entry", &[("text", "search")]);
198 ui.frame();
199 ui.key("enter");
200 assert_eq!(
201 events(&mut ui),
202 vec![(entry, "activate".into(), "search".into(), 0.0)]
203 );
204 assert_eq!(ui.tree.props(entry).str("text"), "search");
205}
206
207#[test]
208fn a_listbox_moves_its_cursor_with_the_arrows_and_with_j_and_k() {
209 let mut ui = ui();
210 let root = ui.tree.root();
211 let list = node(&mut ui, root, "listbox", &[]);
212 for name in ["alpha", "beta", "gamma"] {
213 node(&mut ui, list, "label", &[("label", name)]);
214 }
215 ui.frame();
216 assert_eq!(ui.screen.line(0), "› alpha");
217 ui.key("j");
218 assert_eq!(
219 events(&mut ui),
220 vec![(list, "select".into(), "beta".into(), 1.0)]
221 );
222 ui.key("G");
223 assert_eq!(ui.tree.props(list).num("selected", -1.0), 2.0);
224 ui.key("enter");
225 assert_eq!(
226 events(&mut ui),
227 vec![
228 (list, "select".into(), "gamma".into(), 2.0),
229 (list, "activate".into(), "gamma".into(), 2.0)
230 ]
231 );
232 ui.frame();
233 assert_eq!(ui.screen.line(2), "› gamma");
234}
235
236#[test]
237fn a_key_nothing_wanted_reaches_the_caller_as_an_event() {
238 let mut ui = ui();
239 let root = ui.tree.root();
240 let button = node(&mut ui, root, "button", &[("label", "go")]);
241 ui.frame();
242 assert!(!ui.key("f5"));
243 assert_eq!(
244 events(&mut ui),
245 vec![(button, "key".into(), "f5".into(), 0.0)]
246 );
247}
248
249#[test]
250fn ctrl_c_asks_the_loop_to_stop() {
251 let mut ui = ui();
252 assert!(!ui.should_close());
253 ui.key("ctrl+c");
254 assert!(ui.should_close());
255}
256
257#[test]
258fn an_insensitive_subtree_is_dimmed_and_out_of_the_focus_ring() {
259 let mut ui = ui();
260 let root = ui.tree.root();
261 let column = node(&mut ui, root, "vbox", &[]);
262 ui.tree.set(column, "sensitive", Value::Bool(false));
263 node(&mut ui, column, "button", &[("label", "go")]);
264 let live = node(&mut ui, root, "button", &[("label", "live")]);
265 ui.frame();
266 assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::DIM));
267 assert_eq!(ui.focus(), live);
268}
269
270#[test]
271fn a_click_focuses_and_activates_what_is_under_it() {
272 let mut ui = ui();
273 let root = ui.tree.root();
274 node(&mut ui, root, "button", &[("label", "one")]);
275 let second = node(&mut ui, root, "button", &[("label", "two")]);
276 ui.frame();
277 assert!(ui.click(2, 1));
278 assert_eq!(ui.focus(), second);
279 assert_eq!(
280 events(&mut ui),
281 vec![(second, "click".into(), String::new(), 0.0)]
282 );
283 assert!(!ui.click(20, 7), "a click on nothing is not an event");
284}
285
286#[test]
287fn a_scroll_shows_a_window_of_its_content_and_will_not_go_past_the_end() {
288 let mut ui = Ui::new(10, 3);
289 let root = ui.tree.root();
290 let scroll = node(&mut ui, root, "scroll", &[]);
291 for i in 0..6 {
292 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
293 }
294 ui.frame();
295 assert_eq!(ui.screen.lines(), vec!["row 0", "row 1", "row 2"]);
296 ui.wheel(1, 1, 2);
297 ui.frame();
298 assert_eq!(ui.screen.lines(), vec!["row 2", "row 3", "row 4"]);
299 ui.wheel(1, 1, 40);
300 ui.frame();
301 assert_eq!(ui.screen.lines(), vec!["row 3", "row 4", "row 5"]);
302 // The clamped viewport is written back, so the next scroll up starts from
303 // where the reader actually is.
304 assert_eq!(ui.tree.props(scroll).num("offset", -1.0), 3.0);
305}
306
Scroll the list under the pointer, and from where it actually is a785201 nandi 16d ago307#[test]
308fn the_wheel_scrolls_the_list_under_the_pointer_not_the_first_one_painted() {
309 // frq's wide layout: the chats list down the left, the conversation beside
310 // it, and both of them scrolls. A wheel over the conversation used to move
311 // the sidebar — every scroll answered to a pointer anywhere on screen, so
312 // the first one the walk reached took the lot.
313 let mut ui = Ui::new(20, 3);
314 let root = ui.tree.root();
315 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
316 let sidebar = node(&mut ui, row, "scroll", &[]);
317 ui.tree.set(sidebar, "width-request", Value::Num(10.0));
318 for i in 0..6 {
319 node(&mut ui, sidebar, "label", &[("label", &format!("chat {i}"))]);
320 }
321 let backlog = node(&mut ui, row, "scroll", &[]);
322 ui.tree.set(backlog, "width-request", Value::Num(10.0));
323 for i in 0..6 {
324 node(&mut ui, backlog, "label", &[("label", &format!("line {i}"))]);
325 }
326 ui.frame();
327 assert_eq!(ui.screen.line(0), "chat 0 line 0");
328
329 ui.wheel(15, 1, 2);
330 ui.frame();
331 assert_eq!(
332 ui.screen.line(0),
333 "chat 0 line 2",
334 "the wheel was over the backlog, and only the backlog moved"
335 );
336
337 ui.wheel(2, 1, 1);
338 ui.frame();
339 assert_eq!(
340 ui.screen.line(0),
341 "chat 1 line 2",
342 "and over the sidebar it is the sidebar that moves"
343 );
344}
345
346#[test]
347fn page_keys_move_the_backlog_while_the_entry_keeps_the_focus() {
348 let mut ui = Ui::new(10, 4);
349 let root = ui.tree.root();
350 let scroll = node(&mut ui, root, "scroll", &[]);
351 for i in 0..9 {
352 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
353 }
354 // What a reader is typing into, under the list they are reading.
355 let entry = node(&mut ui, root, "entry", &[("text", "hi")]);
356 ui.frame();
357 assert_eq!(ui.screen.line(0), "row 0");
358
359 assert!(ui.key("page-down"), "the page was taken, not passed on");
360 ui.frame();
361 // A page is the viewport less the line that says where you were.
362 assert_eq!(ui.screen.line(0), "row 2");
363 assert_eq!(ui.focus(), entry, "and the entry still has the keyboard");
364
365 ui.key("page-up");
366 ui.frame();
367 assert_eq!(ui.screen.line(0), "row 0");
368 assert!(events(&mut ui).iter().all(|(_, name, _, _)| name != "key"));
369}
370
371#[test]
372fn a_wheel_between_a_re_render_and_a_frame_moves_from_where_the_list_was() {
373 // A render clears a node's props and writes them again; a wheel or a page
374 // key that landed in that gap used to read an offset of zero and answer
375 // with the top of the buffer.
376 let mut ui = Ui::new(10, 3);
377 let root = ui.tree.root();
378 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
379 for i in 0..9 {
380 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
381 }
382 ui.frame();
383 ui.wheel(1, 1, 4);
384 ui.frame();
385 assert_eq!(ui.screen.line(0), "row 4");
386
387 // The caller re-renders the list: props off, props on, no frame between.
388 ui.tree.clear_props(scroll);
389 ui.tree
390 .set(scroll, "scroll-key", Value::Str("backlog".into()));
391 ui.wheel(1, 1, 1);
392 ui.frame();
393 assert_eq!(ui.screen.line(0), "row 5");
394}
395
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 17d ago396#[test]
397fn an_overlay_floats_in_the_middle_over_whatever_was_under_it() {
398 let mut ui = Ui::new(14, 5);
399 let root = ui.tree.root();
400 node(&mut ui, root, "label", &[("label", "beneath")]);
401 let overlay = node(&mut ui, root, "overlay", &[("label", "Sure?")]);
402 node(&mut ui, overlay, "label", &[("label", "yes")]);
403 ui.frame();
404 assert_eq!(ui.screen.line(1), " ┌ Sure? ┐");
405 assert_eq!(ui.screen.line(2), " │yes │");
406 ui.key("esc");
407 assert_eq!(
408 events(&mut ui),
409 vec![(overlay, "close".into(), String::new(), 0.0)]
410 );
411}
412
413#[test]
414fn a_progress_bar_fills_the_share_of_its_width_it_was_given() {
415 let mut ui = Ui::new(10, 2);
416 let root = ui.tree.root();
417 let bar = node(&mut ui, root, "progress", &[]);
418 ui.tree.set(bar, "value", Value::Num(0.5));
419 ui.frame();
420 assert_eq!(ui.screen.line(0), "█████░░░░░");
421}
422
423#[test]
424fn colours_and_attributes_are_inherited_by_a_subtree() {
425 let mut ui = ui();
426 let root = ui.tree.root();
427 let column = node(&mut ui, root, "vbox", &[("color", "red")]);
428 ui.tree.set(column, "bold", Value::Bool(true));
429 node(&mut ui, column, "label", &[("label", "hi")]);
430 ui.frame();
431 let cell = ui.screen.cell(0, 0).unwrap();
432 assert_eq!(cell.style.fg, crate::screen::Color::Indexed(1));
433 assert!(cell.style.has(attr::BOLD));
434}
435
436#[test]
437fn an_unknown_tag_still_shows_its_children() {
438 let mut ui = ui();
439 let root = ui.tree.root();
440 let odd = node(&mut ui, root, "sparkline", &[]);
441 node(&mut ui, odd, "label", &[("label", "inside")]);
442 ui.frame();
443 assert_eq!(ui.screen.line(0), "inside");
444}
445
446#[test]
447fn focus_survives_a_repaint_and_lands_somewhere_when_its_widget_is_unmounted() {
448 let mut ui = ui();
449 let root = ui.tree.root();
450 let first = node(&mut ui, root, "button", &[("label", "one")]);
451 let second = node(&mut ui, root, "button", &[("label", "two")]);
452 ui.frame();
453 ui.key("tab");
454 assert_eq!(ui.focus(), second);
455 ui.frame();
456 assert_eq!(ui.focus(), second, "a repaint does not move the focus");
457 ui.tree.remove(root, second);
458 ui.frame();
459 assert_eq!(ui.focus(), first);
460}
Share a column's rows out at the width it has, not at its own height 5acc801 nandi 17d ago461
462#[test]
463fn an_unknown_leaf_paints_its_own_text() {
464 // A tag this backend does not know paints as a box so its children still
465 // show — but a leaf has none, and a `:status` badge or a `:link` in the
466 // middle of a message would otherwise be a hole in the sentence.
467 let mut ui = ui();
468 let root = ui.tree.root();
469 node(&mut ui, root, "status", &[("label", "joined")]);
470 node(&mut ui, root, "label", &[("label", "after")]);
471 ui.frame();
472 assert_eq!(ui.screen.line(0), "joined");
473 assert_eq!(ui.screen.line(1), "after");
474}
475
476#[test]
477fn a_nested_column_paints_every_child_and_not_only_the_first() {
478 // Its rows were shared out by measuring each child at the *rows* it had
479 // rather than the columns, so a label wrapped to a paragraph, the overrun
480 // came off the end, and everything after the first child was handed
481 // nothing. Two levels down is where it showed: the top box is as wide as
482 // the screen and as tall, so the two numbers were close enough to hide it.
483 let mut ui = ui();
484 let root = ui.tree.root();
485 let col = node(&mut ui, root, "vbox", &[("orientation", "vertical")]);
486 node(&mut ui, col, "label", &[("label", "the first line")]);
487 node(&mut ui, col, "button", &[("label", "Open")]);
488 ui.frame();
489 assert_eq!(ui.screen.line(0), "the first line");
490 assert_eq!(ui.screen.line(1), "[ Open ]");
491}
Let a backlog keep its place, and leave room for what is under it 68910bd nandi 17d ago492
493#[test]
494fn an_unknown_leaf_that_names_a_picture_paints_nothing() {
495 // An `:avatar`'s label is the nick behind the face — words for something
496 // that cannot be drawn here, and in frq already on the row beside it.
497 let mut ui = ui();
498 let root = ui.tree.root();
499 node(
500 &mut ui,
501 root,
502 "avatar",
503 &[("label", "nandi.uk"), ("src", "/tmp/a.png")],
504 );
505 node(&mut ui, root, "label", &[("label", "nandi.uk")]);
506 ui.frame();
507 assert_eq!(ui.screen.line(0), "nandi.uk");
508 assert_eq!(ui.screen.line(1), "");
509}
510
511#[test]
512fn a_sticky_viewport_opens_at_the_bottom_and_stays_there() {
513 // A backlog taller than its viewport, in a scroll that follows its own
514 // bottom: the newest line is what a chat client opens on, and a line
515 // arriving must not drag the screen out from under a reader who scrolled
516 // up to read history.
517 let mut ui = Ui::new(20, 3);
518 let root = ui.tree.root();
519 let scroll = node(
520 &mut ui,
521 root,
522 "scroll",
523 &[("scroll-key", "backlog"), ("stick-to-bottom", "true")],
524 );
525 for n in 1..=6 {
526 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
527 }
528 ui.frame();
529 assert_eq!(ui.screen.line(2), "line 6");
530
531 // Scrolled up, and it stays where it was put across a re-render — the
532 // position lives under the key, not in a prop the next render clears.
533 ui.wheel(0, 0, -3);
534 ui.frame();
535 assert_eq!(ui.screen.line(0), "line 1");
536 ui.tree.clear_props(scroll);
537 ui.tree.set(scroll, "scroll-key", Value::Str("backlog".into()));
538 ui.tree
539 .set(scroll, "stick-to-bottom", Value::Bool(true));
540 ui.frame();
541 assert_eq!(ui.screen.line(0), "line 1");
542
543 // Back down to the bottom, and it follows again.
544 ui.wheel(0, 0, 9);
545 ui.frame();
546 node(&mut ui, scroll, "label", &[("label", "line 7")]);
547 ui.frame();
548 assert_eq!(ui.screen.line(2), "line 7");
549}
550
551#[test]
552fn a_column_that_asks_for_a_width_gets_it_and_no_more() {
553 // Two panes in a row, the first with a width of its own. Its content is
554 // one long line, so measured naturally it is wider than the screen and the
555 // pane beside it is left nothing — which is the split view painting a
556 // sidebar and a ten-cell column of wrapped fragments.
557 let mut ui = Ui::new(40, 2);
558 let root = ui.tree.root();
559 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
560 let side = node(&mut ui, row, "vbox", &[]);
561 // A number, as the ABI sends one: a width read as a string is no width.
562 ui.tree.set(side, "width-request", Value::Num(10.0));
563 node(
564 &mut ui,
565 side,
566 "label",
567 &[("label", "a preview far longer than ten cells")],
568 );
569 let main = node(&mut ui, row, "vbox", &[]);
570 node(&mut ui, main, "label", &[("label", "the conversation")]);
571 ui.frame();
572 assert_eq!(ui.screen.line(0), "a preview the conversation");
573}
574
575#[test]
576fn a_backlog_taller_than_the_screen_leaves_the_compose_bar_its_row() {
577 // The shape of frq's chat screen: a viewport holding more than fits, and
578 // under it the things you act with. A column that cannot shrink hands the
579 // scroll every row it asks for and paints the entry off the bottom edge.
580 let mut ui = Ui::new(20, 4);
581 let root = ui.tree.root();
582 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
583 for n in 1..=10 {
584 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
585 }
586 node(&mut ui, root, "separator", &[]);
587 node(&mut ui, root, "entry", &[("placeholder", "Message")]);
588 ui.frame();
589 assert_eq!(ui.screen.line(3), "Message");
590}