abstracttui 0.4.1

A reactive, compositor-grade terminal UI engine: fine-grained signals, layered rendering with damage tracking, images (kitty/iTerm2/sixel/mosaic), software-rasterized 3D (GLB), themes and animation.
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! TextArea unit tests (split from textarea.rs for the file budget;
//! `#[path]`-included as its `tests` module). Model cases drive the
//! pure functions; widget cases mount a real tree and dispatch real
//! events (itest_util). The history cases port the reference console's
//! `arrow_nav_action` decision table (backlog 0120 §4).
use super::model::{self, Caret, EditOutcome, History, RowMap, SubmitPolicy};
use super::*;
use crate::base::{Point, Size};
use crate::theme::default_theme;
use crate::ui::{Key, Mods, MouseKind, UiEvent, UiTree};
use crate::widgets::itest_util::{key, key_mod, mount_widget, mouse, render, settle, type_str};

// ------------------------------------------------------------------ model

#[test]
fn rowmap_tiles_every_byte_exactly_once() {
    let cases = [
        "",
        "hello",
        "hello world next",
        "a\n\nb",
        "ab\n",
        "aa      b",
        "abcdefgh",
        "城市化进程加快",
        "x👍🏽y\nsecond line with words",
        "tab\there",
        "👨\u{200D}👩\u{200D}👧\u{200D}👦 family",
    ];
    for text in cases {
        for width in [1, 3, 4, 11, 80] {
            let map = RowMap::build(text, width);
            assert!(!map.rows.is_empty(), "{text:?}@{width}: at least one row");
            let mut cursor = 0usize;
            for row in &map.rows {
                assert_eq!(row.start, cursor, "{text:?}@{width}: rows must tile");
                assert!(row.start <= row.text_end && row.text_end <= row.end);
                cursor = row.end;
            }
            assert_eq!(cursor, text.len(), "{text:?}@{width}: full coverage");
        }
    }
}

#[test]
fn rowmap_soft_wrap_matches_editor_expectations() {
    // Word break: the space hangs on the first row, "next" starts clean.
    let map = RowMap::build("hello world next", 11);
    let rows: Vec<_> = map
        .rows
        .iter()
        .map(|r| &"hello world next"[r.start..r.text_end])
        .collect();
    assert_eq!(rows, vec!["hello world ", "next"]);
    // Hard newlines split; empty lines get a row; trailing newline
    // leaves an empty caret row.
    let map = RowMap::build("a\n\nb", 10);
    assert_eq!(map.len(), 3);
    let map = RowMap::build("ab\n", 10);
    assert_eq!(map.len(), 2);
    assert_eq!(map.rows[1].start, 3);
    // CJK wraps by columns, never half a glyph.
    let map = RowMap::build("城市化", 5);
    let rows: Vec<_> = map
        .rows
        .iter()
        .map(|r| &"城市化"[r.start..r.text_end])
        .collect();
    assert_eq!(rows, vec!["城市", "化"]);
    // A ZWJ family never splits internally, even under a hard break —
    // the overflowing row counts as FULL, so the caret's phantom row
    // follows it (an exactly-full last row has no margin cell).
    let family = "👨\u{200D}👩\u{200D}👧\u{200D}👦";
    let map = RowMap::build(family, 1);
    assert_eq!(
        &family[map.rows[0].start..map.rows[0].text_end],
        family,
        "cluster stays whole"
    );
    assert_eq!(map.len(), 2, "content row + the caret's phantom row");
    // The phantom row appears exactly when the last row is full.
    assert_eq!(RowMap::build("abc", 4).len(), 1, "spare cell: no phantom");
    assert_eq!(RowMap::build("abcd", 4).len(), 2, "full row: phantom");
}

fn apply(text: &mut String, c: &mut Caret, key: Key, mods: Mods, w: i32) -> EditOutcome {
    model::apply_key(text, c, key, mods, w, SubmitPolicy::EnterSubmits)
}

#[test]
fn vertical_moves_keep_goal_column_over_wide_clusters() {
    let mut text = String::from("abcdef\nxy\nlmnopq");
    let mut c = Caret {
        byte: 6, // end of "abcdef" (col 6)
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Down, Mods::NONE, 20);
    assert_eq!(c.byte, 9, "short row clamps to its end (col 2)");
    apply(&mut text, &mut c, Key::Down, Mods::NONE, 20);
    let map = RowMap::build(&text, 20);
    assert_eq!(
        map.col_of(&text, c.byte, 2),
        6,
        "goal column 6 restored past the short row"
    );
    // Wide cluster: the caret never lands inside the emoji. Row 0 cols:
    // a=0, b=1, 👍🏽=2..4, c=4 — a goal of 3 is mid-emoji and snaps to
    // the cluster start; a goal of 4 is the boundary after it.
    let mut text = String::from("ab👍🏽cd\nxyz");
    let mut c = Caret {
        byte: text.len(), // end of "xyz": col 3
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Up, Mods::NONE, 20); // goal col 3
    assert_eq!(c.byte, 2, "mid-emoji goal snapped to the cluster start");
    let mut text = String::from("ab👍🏽cd\nxyzw");
    let mut c = Caret {
        byte: text.len(), // end of "xyzw": col 4
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Up, Mods::NONE, 20); // goal col 4
    assert_eq!(c.byte, 10, "boundary after the emoji is its own stop");
}

#[test]
fn home_end_are_per_visual_row_and_ctrl_spans_the_document() {
    // Width 13: rows are "hello world " (12 cols — one spare) / "next".
    let mut text = String::from("hello world next");
    let mut c = Caret {
        byte: 14, // inside "next" on row 1
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Home, Mods::NONE, 13);
    assert_eq!(c.byte, 12, "Home goes to the visual row start");
    assert!(!c.sticky);
    let mut c2 = Caret {
        byte: 2,
        ..Caret::origin()
    };
    apply(&mut text, &mut c2, Key::End, Mods::NONE, 13);
    assert_eq!(c2.byte, 12, "End of the soft row is the boundary byte");
    assert!(c2.sticky, "…with end affinity (the row has a spare cell)");
    let map = RowMap::build(&text, 13);
    assert_eq!(
        map.visual(&text, c2.byte, c2.sticky).0,
        0,
        "renders on row 0"
    );
    assert_eq!(
        map.visual(&text, c2.byte, false).0,
        1,
        "without affinity the same byte is row 1"
    );
    // On a FULL soft row (width 11 makes row 0 exactly "hello world "
    // with no spare cell) End declines the affinity: the boundary byte
    // renders at the next row's start instead of stomping the margin.
    let mut c3 = Caret {
        byte: 2,
        ..Caret::origin()
    };
    apply(&mut text, &mut c3, Key::End, Mods::NONE, 11);
    assert_eq!(c3.byte, 12);
    assert!(!c3.sticky, "full rows have no margin cell");
    apply(&mut text, &mut c2, Key::Home, Mods::CTRL, 13);
    assert_eq!(c2.byte, 0);
    apply(&mut text, &mut c2, Key::End, Mods::CTRL, 13);
    assert_eq!(c2.byte, text.len());
}

#[test]
fn history_edges_follow_the_arrow_nav_decision_table() {
    // Empty buffer: straight to history.
    let mut text = String::new();
    let mut c = Caret::origin();
    assert_eq!(
        apply(&mut text, &mut c, Key::Up, Mods::NONE, 10),
        EditOutcome::HistoryBack
    );
    assert_eq!(
        apply(&mut text, &mut c, Key::Down, Mods::NONE, 10),
        EditOutcome::HistoryForward
    );
    // Multi-row buffer: Up walks rows first…
    let mut text = String::from("ab\ncd");
    let mut c = Caret {
        byte: 4,
        ..Caret::origin()
    };
    assert_eq!(
        apply(&mut text, &mut c, Key::Up, Mods::NONE, 10),
        EditOutcome::Handled { edited: false }
    );
    // …then jumps to the text start…
    assert_eq!(
        apply(&mut text, &mut c, Key::Up, Mods::NONE, 10),
        EditOutcome::Handled { edited: false }
    );
    assert_eq!(c.byte, 0);
    // …and only the edge recalls history.
    assert_eq!(
        apply(&mut text, &mut c, Key::Up, Mods::NONE, 10),
        EditOutcome::HistoryBack
    );
    // Down mirrors: rows, then end, then history.
    assert_eq!(
        apply(&mut text, &mut c, Key::Down, Mods::NONE, 10),
        EditOutcome::Handled { edited: false }
    );
    assert_eq!(
        apply(&mut text, &mut c, Key::Down, Mods::NONE, 10),
        EditOutcome::Handled { edited: false }
    );
    assert_eq!(c.byte, text.len());
    assert_eq!(
        apply(&mut text, &mut c, Key::Down, Mods::NONE, 10),
        EditOutcome::HistoryForward
    );
    // Shift+arrow at the edge extends the selection, never recalls.
    let mut c = Caret::origin();
    assert_eq!(
        apply(&mut text, &mut c, Key::Down, Mods::SHIFT, 10),
        EditOutcome::Handled { edited: false }
    );
}

#[test]
fn history_store_preserves_the_draft_across_a_round_trip() {
    let mut h = History::new(8);
    h.push("one");
    h.push("two");
    h.push("two"); // consecutive duplicate: skipped
    assert_eq!(h.len(), 2);
    assert_eq!(h.back("draft in progress").as_deref(), Some("two"));
    assert_eq!(h.back("ignored").as_deref(), Some("one"));
    assert_eq!(h.back("ignored"), None, "at the oldest: stays put");
    assert_eq!(h.forward().as_deref(), Some("two"));
    assert_eq!(
        h.forward().as_deref(),
        Some("draft in progress"),
        "past the newest: the draft returns"
    );
    assert_eq!(h.forward(), None, "not navigating anymore");
    // Cap drops oldest-first.
    let mut h = History::new(2);
    h.push("a");
    h.push("b");
    h.push("c");
    assert_eq!(h.len(), 2);
    assert_eq!(h.back("").as_deref(), Some("c"));
    assert_eq!(h.back("").as_deref(), Some("b"));
    assert_eq!(h.back(""), None, "'a' dropped by the cap");
}

#[test]
fn cluster_atomic_edits_over_zwj_and_combining_marks() {
    // Backspace removes a whole ZWJ family.
    let mut text = String::from("x👨\u{200D}👩\u{200D}👧\u{200D}👦");
    let mut c = Caret {
        byte: text.len(),
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Backspace, Mods::NONE, 20);
    assert_eq!(text, "x");
    assert_eq!(c.byte, 1);
    // Inserting a combining mark merges clusters; the caret snaps past
    // the merged whole (never mid-cluster).
    let mut text = String::from("ab");
    let mut c = Caret {
        byte: 1,
        ..Caret::origin()
    };
    apply(&mut text, &mut c, Key::Char('\u{0301}'), Mods::NONE, 20);
    assert_eq!(text, "a\u{0301}b");
    assert_eq!(c.byte, 3, "caret after the merged cluster");
    // Delete removes one whole cluster forward.
    let mut text = String::from("👍🏽y");
    let mut c = Caret::origin();
    apply(&mut text, &mut c, Key::Delete, Mods::NONE, 20);
    assert_eq!(text, "y");
}

#[test]
fn word_jumps_cross_line_boundaries() {
    let mut text = String::from("one two\nthree");
    let mut c = Caret::origin();
    apply(&mut text, &mut c, Key::Right, Mods::ALT, 20);
    assert_eq!(c.byte, 3);
    apply(&mut text, &mut c, Key::Right, Mods::ALT, 20);
    assert_eq!(c.byte, 7);
    apply(&mut text, &mut c, Key::Right, Mods::ALT, 20);
    assert_eq!(c.byte, 13, "jump crosses the newline");
    apply(&mut text, &mut c, Key::Left, Mods::ALT, 20);
    assert_eq!(c.byte, 8, "back to the start of 'three'");
}

#[test]
fn selection_spans_rows_and_replaces_on_type() {
    let mut text = String::from("ab\ncd");
    let mut c = Caret::origin();
    apply(&mut text, &mut c, Key::Down, Mods::SHIFT, 10);
    assert_eq!(c.anchor, Some(0));
    assert_eq!(c.byte, 3, "extended to row 1 col 0");
    apply(&mut text, &mut c, Key::Char('Z'), Mods::NONE, 10);
    assert_eq!(text, "Zcd", "selection (incl. newline) replaced");
    assert_eq!(c.anchor, None);
}

#[test]
fn scroll_window_follows_the_caret() {
    let mut c = Caret::origin();
    model::adjust_top(&mut c, 5, 10, 3);
    assert_eq!(c.top, 3, "caret row 5 visible in a 3-row window");
    model::adjust_top(&mut c, 0, 10, 3);
    assert_eq!(c.top, 0, "moving up pulls the window up");
    model::adjust_top(&mut c, 9, 4, 3);
    assert_eq!(c.top, 1, "top clamps to total - visible");
}

// ----------------------------------------------------------------- widget

fn composer(
    size: Size,
) -> (
    crate::reactive::RootScope,
    UiTree,
    TextAreaState,
    std::rc::Rc<std::cell::RefCell<Vec<String>>>,
) {
    let t = &default_theme().tokens;
    let submitted: std::rc::Rc<std::cell::RefCell<Vec<String>>> = Default::default();
    let s2 = submitted.clone();
    let holder: std::rc::Rc<std::cell::RefCell<Option<TextAreaState>>> = Default::default();
    let h2 = holder.clone();
    let (root, mut tree) = mount_widget(size, move |cx| {
        let state = TextAreaState::new(cx);
        *h2.borrow_mut() = Some(state.clone());
        let st = state.clone();
        TextArea::new()
            .state(&state)
            .placeholder("say something")
            .rows(1, 3)
            .on_submit(move |v| {
                s2.borrow_mut().push(v.to_string());
                st.push_history(v);
                st.clear();
            })
            .element(cx, t)
            .build()
    });
    key(&mut tree, Key::Tab); // focus
    let state = holder.borrow().clone().expect("state");
    (root, tree, state, submitted)
}

#[test]
fn typing_renders_inside_the_frame_and_wraps() {
    let size = Size::new(12, 4);
    let (_root, mut tree, state, _) = composer(size);
    type_str(&mut tree, "hello world next");
    assert_eq!(state.text(), "hello world next");
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert_eq!(canvas.cell(Point::new(1, 0)).unwrap().0, 'h');
    assert!(canvas.row_text(0).contains("hello"));
    assert!(
        canvas.row_text(1).contains("next"),
        "soft wrap: {:?}",
        canvas.row_text(1)
    );
    // Focused frame wears border_focus on every widget row.
    let theme = default_theme();
    assert_eq!(
        canvas.cell(Point::new(0, 0)).unwrap().1,
        theme.tokens.border_focus
    );
    assert_eq!(
        canvas.cell(Point::new(0, 1)).unwrap().1,
        theme.tokens.border_focus
    );
}

#[test]
fn grows_to_cap_then_scrolls_internally() {
    let size = Size::new(12, 6);
    let t = &default_theme().tokens;
    let (root, mut tree) = mount_widget(size, move |cx| {
        let state = TextAreaState::new(cx);
        crate::ui::Element::new()
            .style(crate::layout::Style::column())
            .child(
                TextArea::new()
                    .state(&state)
                    .rows(1, 3)
                    .element(cx, t)
                    .build(),
            )
            .child(crate::ui::text("MARK"))
            .build()
    });
    key(&mut tree, Key::Tab);
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert!(canvas.row_text(1).contains("MARK"), "1 row when empty");
    type_str(&mut tree, "aaaa bbbb cccc"); // wraps to 2 rows at width 10
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert!(canvas.row_text(2).contains("MARK"), "grew to 2 rows");
    type_str(&mut tree, " dddd eeee ffff gggg");
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert!(
        canvas.row_text(3).contains("MARK"),
        "capped at 3 rows: {:?}",
        (0..6).map(|y| canvas.row_text(y)).collect::<Vec<_>>()
    );
    assert!(
        !canvas.row_text(0).contains("aaaa"),
        "scrolled: the first row left the window"
    );
    drop(root);
}

#[test]
fn enter_submits_and_alt_shift_enter_insert_newlines() {
    let size = Size::new(20, 5);
    let (_root, mut tree, state, submitted) = composer(size);
    type_str(&mut tree, "hello");
    key_mod(&mut tree, Key::Enter, Mods::ALT);
    type_str(&mut tree, "world");
    assert_eq!(state.text(), "hello\nworld", "Alt+Enter inserts");
    key_mod(&mut tree, Key::Enter, Mods::SHIFT);
    type_str(&mut tree, "!");
    assert_eq!(state.text(), "hello\nworld\n!", "kitty Shift+Enter inserts");
    key(&mut tree, Key::Enter);
    assert_eq!(*submitted.borrow(), vec!["hello\nworld\n!".to_string()]);
    assert_eq!(state.text(), "", "the submit handler cleared the buffer");
}

#[test]
fn enter_inserts_policy_never_submits() {
    let t = &default_theme().tokens;
    let submitted: std::rc::Rc<std::cell::RefCell<Vec<String>>> = Default::default();
    let s2 = submitted.clone();
    let holder: std::rc::Rc<std::cell::RefCell<Option<TextAreaState>>> = Default::default();
    let h2 = holder.clone();
    let (_root, mut tree) = mount_widget(Size::new(20, 5), move |cx| {
        let state = TextAreaState::new(cx);
        *h2.borrow_mut() = Some(state.clone());
        TextArea::new()
            .state(&state)
            .submit_policy(SubmitPolicy::EnterInserts)
            .on_submit(move |v| s2.borrow_mut().push(v.to_string()))
            .element(cx, t)
            .build()
    });
    key(&mut tree, Key::Tab);
    type_str(&mut tree, "a");
    key(&mut tree, Key::Enter);
    type_str(&mut tree, "b");
    let state = holder.borrow().clone().unwrap();
    assert_eq!(state.text(), "a\nb");
    assert!(submitted.borrow().is_empty());
}

#[test]
fn block_paste_inserts_whole_and_never_submits() {
    let size = Size::new(20, 5);
    let (_root, mut tree, state, submitted) = composer(size);
    tree.dispatch(&UiEvent::Paste("line one\r\nline two\rline three".into()));
    assert_eq!(
        state.text(),
        "line one\nline two\nline three",
        "newlines kept, endings normalized"
    );
    assert!(submitted.borrow().is_empty(), "paste is never a submit");
    // Caret at the end, scroll window shows the tail.
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert!(canvas.row_text(2).contains("line three"));
}

#[test]
fn history_recall_replaces_buffer_and_draft_survives() {
    let size = Size::new(24, 5);
    let (_root, mut tree, state, _) = composer(size);
    type_str(&mut tree, "first");
    key(&mut tree, Key::Enter);
    type_str(&mut tree, "second");
    key(&mut tree, Key::Enter);
    assert_eq!(state.history_len(), 2);
    type_str(&mut tree, "draft");
    // One-row buffer, caret at the end: first Up jumps to the start…
    key(&mut tree, Key::Up);
    assert_eq!(state.text(), "draft");
    assert_eq!(state.caret_byte(), 0);
    // …the edge Up recalls the newest entry.
    key(&mut tree, Key::Up);
    assert_eq!(state.text(), "second");
    assert_eq!(state.caret_byte(), 6, "recalled entries open at their end");
    key(&mut tree, Key::Up); // to start of "second"
    key(&mut tree, Key::Up); // older
    assert_eq!(state.text(), "first");
    // Down mirrors: end first, then newer, then the draft returns.
    key(&mut tree, Key::Down); // (already at end after recall)
    assert_eq!(state.text(), "second");
    key(&mut tree, Key::Down);
    assert_eq!(state.text(), "draft", "the in-progress draft survived");
}

#[test]
fn caret_cell_tracks_typing_and_clears_on_blur() {
    let size = Size::new(20, 4);
    let (_root, mut tree, state, _) = composer(size);
    let first = state.caret_cell().get_untracked().expect("focused: anchor");
    type_str(&mut tree, "ab");
    let after = state.caret_cell().get_untracked().expect("still focused");
    assert_eq!(after.y, first.y);
    assert_eq!(after.x, first.x + 2, "anchor follows the caret");
    tree.set_focus(None);
    assert_eq!(
        state.caret_cell().get_untracked(),
        None,
        "blur clears the anchor (owner-driven dismissal keys off this)"
    );
}

#[test]
fn autofocus_at_mount_anchors_to_the_solved_rect_not_the_origin() {
    // field-agora (agora-tui's composer): a TextArea focused by the very
    // turn that MOUNTS it published its anchor from `Rect::ZERO` — the
    // solver had not run when `FocusIn` arrived — so `caret_cell` read
    // (1, 0) and the owner's completion panel opened at the top-left of
    // the screen, 27 rows from the composer. Autofocus is exactly the
    // case where nothing corrects it: the character that opened the
    // panel was consumed ABOVE the widget, so no consumed key ever
    // republishes, and a resize dismisses the panel rather than moving
    // it. The solved rect is the second publish source that closes it.
    let t = &default_theme().tokens;
    let size = Size::new(20, 8);
    let holder: std::rc::Rc<std::cell::RefCell<Option<TextAreaState>>> = Default::default();
    let h2 = holder.clone();
    let (_root, mut tree) = mount_widget(size, move |cx| {
        let state = TextAreaState::new(cx);
        *h2.borrow_mut() = Some(state.clone());
        // Five rows of chrome above, so a ZERO-rect anchor and a solved
        // one cannot be confused: the composer's first row is y=5.
        Element::new()
            .style(LayoutStyle::column().width(Dimension::Percent(1.0)))
            .child(Element::new().style(LayoutStyle::line(5)).build())
            .child(
                TextArea::new()
                    .state(&state)
                    .rows(1, 3)
                    .element(cx, t)
                    .autofocus()
                    .build(),
            )
            .build()
    });
    // No key is ever dispatched — that is the whole point of the case.
    settle(&mut tree, size);
    let state = holder.borrow().clone().expect("state");
    let cell = state
        .caret_cell()
        .get_untracked()
        .expect("autofocused: an anchor is published");
    assert_eq!(
        cell,
        Point::new(1, 5),
        "anchor sits on the composer's solved row, not the screen origin"
    );
}

#[test]
fn placeholder_disabled_and_a11y() {
    let t = &default_theme().tokens;
    let theme = default_theme();
    let size = Size::new(20, 3);
    // Placeholder shows only unfocused + empty (TextInput parity).
    let (_root, mut tree) = mount_widget(size, move |cx| {
        TextArea::new().placeholder("say it").element(cx, t).build()
    });
    let canvas = render(&mut tree, size);
    assert!(canvas.row_text(0).contains("say it"));
    assert_eq!(
        canvas.cell(Point::new(1, 0)).unwrap().1,
        theme.tokens.text_faint
    );
    key(&mut tree, Key::Tab);
    let canvas = render(&mut tree, size);
    assert!(!canvas.row_text(0).contains("say it"));
    // a11y: role + live value.
    type_str(&mut tree, "hi");
    let snapshot = tree.accessibility_tree();
    let entry = snapshot.find(crate::ui::Role::TextArea).expect("role");
    assert_eq!(entry.value.as_deref(), Some("hi"));
    assert_eq!(entry.label, "say it");
    // Disabled: not focusable, faint ink, inert.
    let t2 = &default_theme().tokens;
    let holder: std::rc::Rc<std::cell::RefCell<Option<TextAreaState>>> = Default::default();
    let h2 = holder.clone();
    let (_root2, mut tree2) = mount_widget(size, move |cx| {
        let state = TextAreaState::new(cx);
        state.set_text("frozen");
        *h2.borrow_mut() = Some(state.clone());
        TextArea::new()
            .state(&state)
            .disabled(true)
            .element(cx, t2)
            .build()
    });
    key(&mut tree2, Key::Tab);
    assert_eq!(tree2.focused(), None, "disabled is out of the focus order");
    type_str(&mut tree2, "x");
    let state = holder.borrow().clone().unwrap();
    assert_eq!(state.text(), "frozen", "disabled ignores keys");
    crate::reactive::flush_effects();
    tree2.layout();
    let canvas = render(&mut tree2, size);
    assert_eq!(
        canvas.cell(Point::new(1, 0)).unwrap().1,
        theme.tokens.text_faint,
        "disabled ink is text_faint"
    );
}

/// Backlog first-app/0291: an `.autofocus()`ed composer is focused from
/// boot, so the classic empty-AND-unfocused rule never paints its
/// placeholder. The opt-in paints the hint beside the caret: caret
/// block at column 0 of the text area (visible — "where am I typing"
/// beats one hint word), hint one cell past it in `text_faint`, and
/// the first typed character removes the hint.
#[test]
fn placeholder_while_focused_paints_beside_the_caret() {
    let t = &default_theme().tokens;
    let theme = default_theme();
    let size = Size::new(20, 3);
    let (_root, mut tree) = mount_widget(size, move |cx| {
        TextArea::new()
            .placeholder("say it")
            .placeholder_while_focused(true)
            .element(cx, t)
            .build()
    });
    key(&mut tree, Key::Tab); // focus — the autofocused-composer state
    let canvas = render(&mut tree, size);
    // The caret block stays visible at the text area's first cell.
    let caret = canvas.cell(Point::new(1, 0)).unwrap();
    assert_eq!(caret.0, ' ', "caret cell is a styled blank");
    assert_eq!(caret.2, theme.tokens.cursor, "caret bg is the cursor token");
    // The hint starts ONE CELL PAST the caret, in placeholder ink.
    assert_eq!(canvas.cell(Point::new(2, 0)).unwrap().0, 's');
    assert_eq!(
        canvas.cell(Point::new(2, 0)).unwrap().1,
        theme.tokens.text_faint,
        "focused placeholder keeps the text_faint ink"
    );
    assert!(canvas.row_text(0).contains("say it"));
    // One typed character hides the hint; the glyph takes column 0.
    type_str(&mut tree, "h");
    crate::reactive::flush_effects();
    tree.layout();
    let canvas = render(&mut tree, size);
    assert!(
        !canvas.row_text(0).contains("say it"),
        "typing hides the focused placeholder: {:?}",
        canvas.row_text(0)
    );
    assert_eq!(canvas.cell(Point::new(1, 0)).unwrap().0, 'h');
    assert_eq!(
        canvas.cell(Point::new(2, 0)).unwrap().2,
        theme.tokens.cursor,
        "caret advanced past the typed glyph"
    );
}

#[test]
fn replace_range_snaps_to_cluster_boundaries() {
    let (_root, _tree, state, _) = composer(Size::new(20, 4));
    state.set_text("x👍🏽y");
    // Byte 3 sits inside the emoji cluster (1..9): both ends snap.
    state.replace_range(3..3, "@");
    assert_eq!(
        state.text(),
        "x👍🏽@y",
        "mid-cluster insert snapped to the end"
    );
    assert_eq!(state.caret_byte(), 10);
}

/// Every scrolling surface in the engine answers the wheel; a text
/// area that only answered the keyboard was the odd one out. The
/// wheel moves the WINDOW and leaves the caret where the user types.
#[test]
fn the_wheel_scrolls_the_window_without_moving_the_caret() {
    let size = Size::new(24, 8);
    let t = &default_theme().tokens;
    let holder: std::rc::Rc<std::cell::RefCell<Option<TextAreaState>>> = Default::default();
    let h2 = holder.clone();
    let (_root, mut tree) = mount_widget(size, move |cx| {
        let state = TextAreaState::new(cx);
        state.value().set(
            (1..=40)
                .map(|i| format!("line {i}"))
                .collect::<Vec<_>>()
                .join("\n"),
        );
        *h2.borrow_mut() = Some(state.clone());
        TextArea::new()
            .state(&state)
            .rows(1, 4)
            .element(cx, t)
            .build()
    });
    let state = holder.borrow().clone().unwrap();

    // The first content row names itself, so the window's position is
    // readable off the screen.
    let top = |tree: &mut UiTree| -> i32 {
        let canvas = render(tree, size);
        for y in 0..size.h {
            let row = canvas.row_text(y);
            // Rows carry the widget's border glyphs, so find the label
            // rather than expecting it at the start.
            if let Some(at) = row.find("line ") {
                let rest = &row[at + "line ".len()..];
                if let Ok(n) = rest.split_whitespace().next().unwrap_or("").parse::<i32>() {
                    return n;
                }
            }
        }
        -1
    };

    let first = top(&mut tree);
    assert!(first >= 1, "content is visible: {first}");
    let caret_before = state.caret_byte();

    assert!(
        mouse(&mut tree, MouseKind::ScrollDown, 4, 2),
        "the wheel is consumed while there is room to scroll"
    );
    let after = top(&mut tree);
    assert!(after > first, "the window moved down: {first} -> {after}");
    assert_eq!(
        state.caret_byte(),
        caret_before,
        "scrolling is not editing: the caret stays put"
    );

    mouse(&mut tree, MouseKind::ScrollUp, 4, 2);
    assert!(top(&mut tree) < after, "the wheel scrolled back up");

    // At the top there is nothing left to give: the event is NOT
    // consumed, so an outer scroll pane can take it.
    for _ in 0..40 {
        mouse(&mut tree, MouseKind::ScrollUp, 4, 2);
    }
    assert!(
        !mouse(&mut tree, MouseKind::ScrollUp, 4, 2),
        "a wheel at the top passes through to an outer pane"
    );
}