textual 1.0.0-dev

A reactive TUI framework inspired by the Python Textual library
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! P1G-12 + P1G-15 integration tests for the tree-mode render pipeline.
//!
//! These tests exercise the arena-based tree pipeline:
//!   build_widget_tree_from_root -> run_layout_pass / render_tree_to_frame
//!
//! They do NOT use the legacy FrameBuffer::from_renderable path.

use rich_rs::Console;
use textual::compose;
use textual::prelude::*;
use textual::render::FrameBuffer;
use textual::style::{Offset, OffsetValue, Position, Scalar};

// ===========================================================================
// Helpers
// ===========================================================================

/// Render a root widget through the full tree pipeline, returning (tree, frame, lines).
fn tree_render(
    root: &mut dyn Widget,
    w: usize,
    h: usize,
) -> (WidgetTree, FrameBuffer, Vec<String>) {
    let console = Console::new();
    let mut tree = build_widget_tree_from_root(root).expect("tree should have children");
    let frame = render_tree_to_frame(&mut tree, root, &console, w, h);
    let lines = frame.as_plain_lines();
    (tree, frame, lines)
}

/// Returns true if any of the plain-text lines contains `needle`.
fn lines_contain(lines: &[String], needle: &str) -> bool {
    lines.iter().any(|l| l.contains(needle))
}

fn find_text(lines: &[String], needle: &str) -> (usize, usize) {
    let row = lines
        .iter()
        .position(|l| l.contains(needle))
        .unwrap_or_else(|| panic!("missing text '{needle}' in lines: {lines:?}"));
    let col = lines[row]
        .find(needle)
        .unwrap_or_else(|| panic!("text '{needle}' missing from computed row {row}"));
    (row, col)
}

// ===========================================================================
// P1G-12(a): Composed descendants receive non-zero layout rects
// ===========================================================================

/// Container -> Vertical -> Label chain: all visible children get non-zero
/// layout (verified by rendered output appearing in distinct vertical rows).
#[test]
fn p1g12a_container_vertical_labels_render_in_distinct_rows() {
    let mut root = Container::new()
        .with_child(Label::new("alpha"))
        .with_child(Label::new("bravo"))
        .with_child(Label::new("charlie"));

    let (_tree, _frame, lines) = tree_render(&mut root, 30, 10);

    // Each label should render on its own row.
    assert!(
        lines_contain(&lines, "alpha"),
        "alpha should appear in rendered output"
    );
    assert!(
        lines_contain(&lines, "bravo"),
        "bravo should appear in rendered output"
    );
    assert!(
        lines_contain(&lines, "charlie"),
        "charlie should appear in rendered output"
    );

    // Labels should be on different rows (vertical stacking).
    let alpha_row = lines.iter().position(|l| l.contains("alpha"));
    let bravo_row = lines.iter().position(|l| l.contains("bravo"));
    let charlie_row = lines.iter().position(|l| l.contains("charlie"));
    assert!(
        alpha_row < bravo_row && bravo_row < charlie_row,
        "labels must stack vertically: alpha={alpha_row:?} bravo={bravo_row:?} charlie={charlie_row:?}"
    );
}

/// Tree node count must reflect all composed children (root stub + descendants).
#[test]
fn p1g12a_tree_contains_all_composed_descendants() {
    let mut root = Container::new()
        .with_child(Label::new("one"))
        .with_child(Label::new("two"))
        .with_child(Label::new("three"));

    let tree = build_widget_tree_from_root(&mut root).expect("tree should have children");
    let root_id = tree.root().expect("tree must have a root");

    // Root stub + 3 children = at least 4 nodes.
    assert!(
        tree.len() >= 4,
        "tree should have root + 3 children, got {}",
        tree.len()
    );

    // Root's direct children count.
    let children = tree.children(root_id);
    assert_eq!(children.len(), 3, "root should have exactly 3 children");
}

/// Nested containers: Container -> Vertical -> [Label, Label].
/// Tree depth should be ≥ 3 and all labels should render.
#[test]
fn p1g12a_nested_container_vertical_labels_render() {
    let mut root = Container::new().with_child(
        Vertical::new()
            .with_child(Label::new("inner_one"))
            .with_child(Label::new("inner_two")),
    );

    let (tree, _frame, lines) = tree_render(&mut root, 40, 10);

    assert!(
        lines_contain(&lines, "inner_one"),
        "inner_one should render"
    );
    assert!(
        lines_contain(&lines, "inner_two"),
        "inner_two should render"
    );

    // Walk tree depth: root -> Container-child(Vertical) -> Labels
    let root_id = tree.root().unwrap();
    let depth_nodes = tree.walk_depth_first(root_id);
    assert!(
        depth_nodes.len() >= 4,
        "tree should have ≥4 nodes (root stub, Vertical, 2 Labels), got {}",
        depth_nodes.len()
    );
}

// ===========================================================================
// P1G-12(b): Clip + scroll offsets preserve child visibility/targetability
// ===========================================================================

/// ScrollView with many children: only the first few visible children
/// should appear in the rendered output within the viewport.
#[test]
fn p1g12b_scroll_view_clips_to_viewport() {
    let inner = Container::new()
        .with_child(Label::new("line_A"))
        .with_child(Label::new("line_B"))
        .with_child(Label::new("line_C"))
        .with_child(Label::new("line_D"))
        .with_child(Label::new("line_E"))
        .with_child(Label::new("line_F"))
        .with_child(Label::new("line_G"))
        .with_child(Label::new("line_H"));

    // Viewport is only 4 rows tall — not all 8 labels can fit.
    let mut root = ScrollView::new(inner).height(4);
    let (_tree, _frame, lines) = tree_render(&mut root, 30, 4);

    // At scroll offset 0, early lines (A-D) should be visible.
    let early_visible = ["line_A", "line_B", "line_C", "line_D"]
        .iter()
        .filter(|label| lines_contain(&lines, label))
        .count();
    // Late lines (G, H) must be clipped.
    let late_visible = ["line_G", "line_H"]
        .iter()
        .filter(|label| lines_contain(&lines, label))
        .count();

    assert!(
        early_visible >= 1,
        "at least some early labels (A-D) should be visible, saw {early_visible}"
    );
    assert_eq!(
        late_visible, 0,
        "late labels (G, H) must be clipped by viewport, saw {late_visible}"
    );

    // Total visible must be bounded by viewport height (4).
    let total_visible = [
        "line_A", "line_B", "line_C", "line_D", "line_E", "line_F", "line_G", "line_H",
    ]
    .iter()
    .filter(|label| lines_contain(&lines, label))
    .count();
    assert!(
        total_visible <= 4,
        "visible count should not exceed viewport height (4), saw {total_visible}"
    );
}

/// VerticalScroll with many children: not all can fit in the viewport.
/// The viewport constrains how many children are rendered.
#[test]
fn p1g12b_vertical_scroll_clips_excess_children() {
    let mut root = VerticalScroll::new()
        .with_child(Static::new("row_first"))
        .with_child(Static::new("row_second"))
        .with_child(Static::new("row_third"))
        .with_child(Static::new("row_fourth"))
        .with_child(Static::new("row_fifth"))
        .with_child(Static::new("row_sixth"))
        .with_child(Static::new("row_seventh"))
        .with_child(Static::new("row_eighth"))
        .height(3);

    let (_tree, _frame, lines) = tree_render(&mut root, 30, 3);

    let all_labels = [
        "row_first",
        "row_second",
        "row_third",
        "row_fourth",
        "row_fifth",
        "row_sixth",
        "row_seventh",
        "row_eighth",
    ];
    let total_visible = all_labels
        .iter()
        .filter(|l| lines_contain(&lines, l))
        .count();

    // With only 3 rows of viewport, not all 8 can be visible.
    assert!(
        total_visible < 8,
        "VerticalScroll clipping: not all 8 rows should be visible in 3 rows, saw {total_visible}"
    );
    // At least some should render.
    assert!(
        total_visible >= 1,
        "at least some rows should be visible; lines={lines:?}"
    );
    // Visible count bounded by viewport height (3 rows).
    assert!(
        total_visible <= 3,
        "visible count should not exceed viewport height (3), saw {total_visible}"
    );
}

// ===========================================================================
// P1G-12(c): Hit-test resolves interactive leaf nodes
// ===========================================================================

/// Button nested in containers renders its text in the expected position.
#[test]
fn p1g12c_button_in_nested_containers_renders_text() {
    let mut root = Container::new().with_child(Vertical::new().with_child(Button::new("Click Me")));

    let (_tree, _frame, lines) = tree_render(&mut root, 40, 10);

    assert!(
        lines_contain(&lines, "Click Me"),
        "Button text 'Click Me' should appear in rendered output; lines={lines:?}"
    );
}

/// Button text must appear within the first few rows (positioned correctly)
/// so that a hit-test at the button's rendered row would resolve to it.
#[test]
fn p1g12c_button_position_is_in_expected_region() {
    let mut root = Container::new()
        .with_child(Static::new("header_text"))
        .with_child(Button::new("Action"));

    let (_tree, _frame, lines) = tree_render(&mut root, 40, 10);

    let header_row = lines.iter().position(|l| l.contains("header_text"));
    let button_row = lines.iter().position(|l| l.contains("Action"));
    assert!(header_row.is_some(), "header should render");
    assert!(button_row.is_some(), "button should render");
    assert!(
        button_row > header_row,
        "button should be below header: header={header_row:?} button={button_row:?}"
    );
}

/// Verify button text occupies a specific column range in the frame, proving
/// a hit-test at those coordinates would resolve to the interactive button.
#[test]
fn p1g12c_button_occupies_expected_column_range() {
    let mut root = Container::new().with_child(Button::new("HitTarget"));

    let (_tree, frame, lines) = tree_render(&mut root, 40, 5);

    // Find the row containing the button text.
    let btn_row = lines
        .iter()
        .position(|l| l.contains("HitTarget"))
        .expect("Button text must appear in output");

    // Find the column where the button text starts.
    let btn_col = lines[btn_row]
        .find("HitTarget")
        .expect("Button text must be findable in its row");

    // The button text occupies columns [btn_col..btn_col+9].
    // Verify these cells are non-empty in the frame buffer.
    for x in btn_col..btn_col + "HitTarget".len() {
        let cell = frame.get(x, btn_row);
        assert!(
            !cell.text.is_empty(),
            "frame cell at ({x}, {btn_row}) should be non-empty for hit-test targeting"
        );
    }
}

// ===========================================================================
// P1G-15(a): Wrapper render/on_event no longer depends on drained local
//            children in tree mode
// ===========================================================================

/// Container with children: after tree extraction, rendering still works
/// (children appear at tree positions, not via parent's render).
#[test]
fn p1g15a_container_tree_mode_renders_children_correctly() {
    let mut root = Container::new()
        .with_child(Label::new("child_alpha"))
        .with_child(Label::new("child_beta"));

    // Building the tree calls take_composed_children internally.
    let (tree, _frame, lines) = tree_render(&mut root, 40, 8);

    // Children should still render via the tree pipeline.
    assert!(
        lines_contain(&lines, "child_alpha"),
        "child_alpha must appear (tree-mode rendering)"
    );
    assert!(
        lines_contain(&lines, "child_beta"),
        "child_beta must appear (tree-mode rendering)"
    );

    // Verify tree was actually built (children extracted).
    let root_id = tree.root().unwrap();
    assert!(
        !tree.children(root_id).is_empty(),
        "tree root should have children after extraction"
    );
}

/// Frame wrapper: child renders correctly through the tree pipeline.
#[test]
fn p1g15a_frame_tree_mode_renders_child() {
    let mut root = Frame::new(Label::new("framed_content")).border(true);

    let (_tree, _frame, lines) = tree_render(&mut root, 40, 8);

    assert!(
        lines_contain(&lines, "framed_content"),
        "Frame's child should render in tree mode"
    );
}

/// Panel wrapper: child renders correctly through the tree pipeline.
#[test]
fn p1g15a_panel_tree_mode_renders_child() {
    let mut root = Panel::new(Label::new("panel_content")).border(true);

    let (_tree, _frame, lines) = tree_render(&mut root, 40, 8);

    assert!(
        lines_contain(&lines, "panel_content"),
        "Panel's child should render in tree mode"
    );
}

// ===========================================================================
// P1G-15(b): Alias wrappers preserve structural composition
// ===========================================================================

/// Vertical alias: children stack vertically (each on distinct row).
#[test]
fn p1g15b_vertical_alias_stacks_children_vertically() {
    let mut root = Vertical::new()
        .with_child(Label::new("vert_1"))
        .with_child(Label::new("vert_2"))
        .with_child(Label::new("vert_3"));

    let (tree, _frame, lines) = tree_render(&mut root, 30, 10);

    // All three labels should render.
    assert!(lines_contain(&lines, "vert_1"));
    assert!(lines_contain(&lines, "vert_2"));
    assert!(lines_contain(&lines, "vert_3"));

    // Verify vertical order.
    let r1 = lines.iter().position(|l| l.contains("vert_1")).unwrap();
    let r2 = lines.iter().position(|l| l.contains("vert_2")).unwrap();
    let r3 = lines.iter().position(|l| l.contains("vert_3")).unwrap();
    assert!(
        r1 < r2 && r2 < r3,
        "Vertical must stack top-to-bottom: {r1}<{r2}<{r3}"
    );

    // Tree structure should have 3 direct children under root.
    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        3,
        "Vertical should have 3 children in tree"
    );
}

/// Horizontal alias: children laid out side-by-side. Both must render and
/// the tree preserves the correct number of children.
#[test]
fn p1g15b_horizontal_alias_preserves_structure_and_renders() {
    let mut root = Horizontal::new()
        .with_child(Label::new("left_col"))
        .with_child(Label::new("right_col"));

    let (tree, _frame, lines) = tree_render(&mut root, 60, 5);

    // Both labels should appear in rendered output.
    assert!(
        lines_contain(&lines, "left_col"),
        "left_col should render in Horizontal"
    );
    assert!(
        lines_contain(&lines, "right_col"),
        "right_col should render in Horizontal"
    );

    // Tree structure: root should have exactly 2 children.
    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        2,
        "Horizontal should have 2 children in tree"
    );
}

/// VerticalScroll alias preserves tree structure.
#[test]
fn p1g15b_vertical_scroll_alias_preserves_structure() {
    let mut root = VerticalScroll::new()
        .with_child(Label::new("scroll_child_1"))
        .with_child(Label::new("scroll_child_2"))
        .height(6);

    let (tree, _frame, lines) = tree_render(&mut root, 30, 6);

    assert!(lines_contain(&lines, "scroll_child_1"));
    assert!(lines_contain(&lines, "scroll_child_2"));

    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        5,
        "VerticalScroll should expose body + tree-mode scrollbar children in tree"
    );
}

/// HorizontalScroll alias preserves tree structure.
#[test]
fn p1g15b_horizontal_scroll_alias_preserves_structure() {
    let mut root = HorizontalScroll::new()
        .with_child(Label::new("hscroll_1"))
        .with_child(Label::new("hscroll_2"))
        .height(4);

    let (tree, _frame, lines) = tree_render(&mut root, 60, 4);

    assert!(lines_contain(&lines, "hscroll_1"));
    assert!(lines_contain(&lines, "hscroll_2"));

    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        5,
        "HorizontalScroll should expose body + tree-mode scrollbar children in tree"
    );
}

/// ScrollableContainer alias preserves tree structure.
#[test]
fn p1g15b_scrollable_container_alias_preserves_structure() {
    let mut root = ScrollableContainer::new()
        .with_child(Label::new("sc_child_1"))
        .with_child(Label::new("sc_child_2"))
        .height(6);

    let (tree, _frame, lines) = tree_render(&mut root, 30, 6);

    assert!(lines_contain(&lines, "sc_child_1"));
    assert!(lines_contain(&lines, "sc_child_2"));

    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        5,
        "ScrollableContainer should expose body + tree-mode scrollbar children in tree"
    );
}

/// HorizontalGroup alias preserves tree structure and layout.
#[test]
fn p1g15b_horizontal_group_preserves_structure() {
    let mut root = HorizontalGroup::new()
        .with_child(Label::new("hg_left"))
        .with_child(Label::new("hg_right"));

    let (tree, _frame, lines) = tree_render(&mut root, 60, 5);

    assert!(lines_contain(&lines, "hg_left"));
    assert!(lines_contain(&lines, "hg_right"));

    let root_id = tree.root().unwrap();
    assert_eq!(tree.children(root_id).len(), 2);
}

// ===========================================================================
// P1G-15(c): Deep wrapper-chain proof
// ===========================================================================

/// Dock -> ScrollView -> HorizontalGroup -> VerticalScroll -> Button
/// The DEEP chain: verify button text renders, all intermediate wrappers
/// contribute to tree structure, and tree depth matches expected nesting.
#[test]
fn p1g15c_deep_wrapper_chain_dock_scroll_hgroup_vscroll_button() {
    let button = Button::new("DeepBtn");
    let vscroll = VerticalScroll::new().with_child(button).height(8);
    let hgroup = HorizontalGroup::new().with_child(vscroll);
    let scroll_view = ScrollView::new(hgroup).height(8);
    let mut root = Dock::new().push_fill(scroll_view);

    let (tree, _frame, lines) = tree_render(&mut root, 60, 10);

    // Button text must appear in rendered output.
    assert!(
        lines_contain(&lines, "DeepBtn"),
        "Button text 'DeepBtn' must render through deep wrapper chain; lines={lines:?}"
    );

    // Tree depth: root-stub -> Dock-child(ScrollView) -> HorizontalGroup -> VerticalScroll -> Button
    // That's at least 5 levels deep.
    let root_id = tree.root().unwrap();
    let all_nodes = tree.walk_depth_first(root_id);
    assert!(
        all_nodes.len() >= 5,
        "deep chain should produce ≥5 tree nodes, got {}",
        all_nodes.len()
    );

    // Verify tree depth by finding the deepest leaf (most ancestors).
    let max_depth = all_nodes
        .iter()
        .filter(|&&id| tree.children(id).is_empty())
        .map(|&id| tree.ancestors(id).len() + 1) // +1 for the leaf itself
        .max()
        .expect("tree should have at least one leaf node");
    assert!(
        max_depth >= 4,
        "deepest leaf should be at depth ≥4 (got {max_depth})"
    );
}

/// Variant of the deep chain with header + button inside the VerticalScroll.
#[test]
fn p1g15c_deep_chain_with_compose_and_labels() {
    let mut root = Dock::new().push_fill(ScrollView::new(
        HorizontalGroup::new().with_child(
            VerticalScroll::new()
                .with_child(Static::new("Deep Header"))
                .with_child(Button::new("DeepAction"))
                .height(10),
        ),
    ));

    let (tree, _frame, lines) = tree_render(&mut root, 60, 12);

    assert!(
        lines_contain(&lines, "Deep Header"),
        "Static header should render in deep chain"
    );
    assert!(
        lines_contain(&lines, "DeepAction"),
        "Button should render in deep chain"
    );

    // Verify structural depth.
    let root_id = tree.root().unwrap();
    let all_nodes = tree.walk_depth_first(root_id);
    assert!(
        all_nodes.len() >= 6,
        "chain with header+button should have ≥6 nodes, got {}",
        all_nodes.len()
    );
}

// ===========================================================================
// Additional coverage: layout_pass standalone + tree structure invariants
// ===========================================================================

/// run_layout_pass produces a tree where all nodes can be walked and the
/// tree is structurally valid after layout.
#[test]
fn layout_pass_tree_structure_remains_valid() {
    let mut root = Container::new()
        .with_child(
            Vertical::new()
                .with_child(Label::new("a"))
                .with_child(Label::new("b")),
        )
        .with_child(Label::new("c"));

    let mut tree = build_widget_tree_from_root(&mut root).expect("tree should exist");

    // Install stylesheet context and run layout.
    let sheet = textual::css::default_widget_stylesheet();
    let _guard = textual::css::set_style_context(sheet);
    run_layout_pass(&mut tree, (40, 10));

    // Tree should still be walkable.
    let root_id = tree.root().unwrap();
    let all_nodes = tree.walk_depth_first(root_id);
    assert!(
        all_nodes.len() >= 5,
        "all composed nodes should survive layout pass"
    );

    // Every node should be retrievable.
    for &nid in &all_nodes {
        assert!(
            tree.get(nid).is_some(),
            "node {nid:?} should be accessible after layout"
        );
    }

    // Parent-child relationships should be consistent.
    for &nid in &all_nodes {
        for &child_id in tree.children(nid) {
            assert_eq!(
                tree.parent(child_id),
                Some(nid),
                "child's parent should match"
            );
        }
    }
}

/// After tree build, children are no longer in the original widget's local
/// children vec (take_composed_children drained them). The tree pipeline
/// fully owns them.
#[test]
fn tree_build_drains_container_local_children() {
    let mut root = Container::new()
        .with_child(Label::new("x"))
        .with_child(Label::new("y"));

    // Before tree build, container has 2 children.
    assert_eq!(
        root.children().len(),
        2,
        "container should start with 2 children"
    );

    let tree = build_widget_tree_from_root(&mut root).expect("tree exists");

    // After tree build, container's local children should be drained.
    assert_eq!(
        root.children().len(),
        0,
        "container's local children should be empty after tree extraction"
    );

    // But the tree owns them.
    let root_id = tree.root().unwrap();
    assert_eq!(
        tree.children(root_id).len(),
        2,
        "tree should own the 2 extracted children"
    );
}

/// Multiple alias wrappers in parallel: Vertical + Horizontal side by side
/// in a HorizontalGroup. Both sub-trees should render correctly.
#[test]
fn parallel_alias_wrappers_both_render() {
    let mut root = HorizontalGroup::new()
        .with_child(
            Vertical::new()
                .with_child(Label::new("v_top"))
                .with_child(Label::new("v_bot")),
        )
        .with_child(
            Vertical::new()
                .with_child(Label::new("v2_top"))
                .with_child(Label::new("v2_bot")),
        );

    let (_tree, _frame, lines) = tree_render(&mut root, 60, 10);

    assert!(lines_contain(&lines, "v_top"), "first Vertical top label");
    assert!(
        lines_contain(&lines, "v_bot"),
        "first Vertical bottom label"
    );
    assert!(lines_contain(&lines, "v2_top"), "second Vertical top label");
    assert!(
        lines_contain(&lines, "v2_bot"),
        "second Vertical bottom label"
    );
}

/// Regression guard for the buttons_advanced-like wrapper chain.
/// Each vertical scroll column must keep its children stacked vertically.
/// If wrappers collapse into a horizontal flow, this should fail.
#[test]
fn p1g15_buttons_advanced_chain_preserves_vertical_grouping() {
    let mut root =
        Dock::new().push_fill(ScrollView::new(Horizontal::new().with_compose(compose![
            VerticalScroll::new().with_compose(compose![
                Static::new("COL1"),
                Button::new("C1_A"),
                Button::new("C1_B"),
            ]),
            VerticalScroll::new().with_compose(compose![
                Static::new("COL2"),
                Button::new("C2_A"),
                Button::new("C2_B"),
            ]),
        ])));

    let (_tree, _frame, lines) = tree_render(&mut root, 80, 20);
    let (r_c1a, c_c1a) = find_text(&lines, "C1_A");
    let (r_c1b, _c_c1b) = find_text(&lines, "C1_B");
    let (r_c2a, c_c2a) = find_text(&lines, "C2_A");
    let (r_c2b, _c_c2b) = find_text(&lines, "C2_B");

    // Vertical grouping per column.
    assert!(
        r_c1b > r_c1a,
        "column 1 buttons must stack vertically: C1_A@({r_c1a},{c_c1a}) C1_B@({r_c1b},_)"
    );
    assert!(
        r_c2b > r_c2a,
        "column 2 buttons must stack vertically: C2_A@({r_c2a},{c_c2a}) C2_B@({r_c2b},_)"
    );

    // First-row buttons should be in different columns (roughly same row, different x).
    assert!(
        c_c2a > c_c1a + 4,
        "columns must be horizontally separated: C1_A@({r_c1a},{c_c1a}) C2_A@({r_c2a},{c_c2a})"
    );
}

/// Regression guard: bordered footer wrappers in tree mode must keep child text
/// on the interior content row (not on the border rows).
#[test]
fn p1g15_footer_wrapper_keeps_text_inside_border_content_row() {
    let footer = Styled::new(
        Static::new("Events: demo"),
        Style::new()
            .line_pad(1)
            .bg(Color::parse("#303a43").unwrap())
            .border_top(Color::parse("#44cc44").unwrap())
            .border_right(Color::parse("#44cc44").unwrap())
            .border_bottom(Color::parse("#44cc44").unwrap())
            .border_left(Color::parse("#44cc44").unwrap()),
    );
    let mut root = Dock::new()
        .push_fill(Static::new("body"))
        .push_bottom(Some(3), footer);

    let (_tree, _frame, lines) = tree_render(&mut root, 80, 24);
    let (events_row, _events_col) = find_text(&lines, "Events: demo");

    assert!(
        events_row > 0 && events_row + 1 < lines.len(),
        "events row should have both top and bottom neighbors; row={events_row} lines={lines:?}"
    );
    assert!(
        !lines[events_row - 1].contains("Events: demo"),
        "top border row must not contain footer text; line={}",
        lines[events_row - 1]
    );
    assert!(
        !lines[events_row + 1].contains("Events: demo"),
        "bottom border row must not contain footer text; line={}",
        lines[events_row + 1]
    );
    assert!(
        lines[events_row].contains('│'),
        "interior row should contain side border glyph(s); line={}",
        lines[events_row]
    );
}

/// Layout regression guard: combinator width rules must apply during layout
/// (not only render-time style resolution).
#[test]
fn p1g15_layout_honors_horizontal_child_combinator_width() {
    let mut root = Horizontal::new()
        .with_child(VerticalScroll::new().with_child(Label::new("A")))
        .with_child(VerticalScroll::new().with_child(Label::new("B")))
        .with_child(VerticalScroll::new().with_child(Label::new("C")))
        .with_child(VerticalScroll::new().with_child(Label::new("D")));

    let mut tree = build_widget_tree_from_root(&mut root).expect("tree should have children");
    let root_id = tree.root().expect("tree should have root");
    let children = tree.children(root_id).to_vec();

    let sheet = StyleSheet::parse(
        r#"
Horizontal {
    layout: horizontal;
}
Horizontal > VerticalScroll {
    width: 24;
}
"#,
    );
    let _guard = textual::css::set_style_context(sheet);
    run_layout_pass(&mut tree, (80, 24));

    let widths: Vec<u16> = children
        .into_iter()
        .map(|id| {
            let (layout, _content) =
                textual::layout::inspect_node_rects(&tree, id).expect("child rects should exist");
            layout.2.saturating_sub(layout.0)
        })
        .collect();
    assert_eq!(widths, vec![24, 24, 24, 24]);
}

/// Overlay regression guard: a visible modal layer must not erase base content
/// outside the modal rectangle in tree mode.
#[test]
fn p1g15_overlay_modal_preserves_underlay_outside_modal_bounds() {
    let base_bg = Color::parse("#224466").unwrap().to_simple_opaque();
    let base = Styled::new(
        Static::new("BASE_MARKER"),
        Style::new()
            .width(Scalar::Percent(100.0))
            .height(Scalar::Percent(100.0))
            .bg(Color::parse("#224466").unwrap()),
    );

    let mut modal_layer = Container::new().with_child(Static::new("MODAL_CARD"));
    {
        let style = &mut modal_layer.styles_mut().expect("modal styles").style;
        style.position = Some(Position::Absolute);
        style.offset = Some(Offset {
            x: OffsetValue::Cells(2),
            y: OffsetValue::Cells(1),
        });
        style.width = Some(Scalar::Cells(14));
        style.height = Some(Scalar::Cells(3));
    }

    let mut root = Dock::new()
        .push_fill(Overlay::new(base, modal_layer).visible(true))
        .push_bottom(Some(1), Static::new("FOOTER_SENTINEL"));

    let (_tree, frame, lines) = tree_render(&mut root, 50, 10);

    assert!(
        lines_contain(&lines, "MODAL_CARD"),
        "modal content should be visible; lines={lines:?}"
    );
    assert!(
        lines_contain(&lines, "BASE_MARKER"),
        "base marker should remain visible when modal is shown; lines={lines:?}"
    );
    assert!(
        lines_contain(&lines, "FOOTER_SENTINEL"),
        "footer sibling should still render; lines={lines:?}"
    );
    // Outside modal rect (x >= 20, y >= 4) must keep base background.
    assert_eq!(
        frame.get(20, 4).style.and_then(|s| s.bgcolor),
        Some(base_bg),
        "transparent modal wrapper must not wipe base surface outside modal bounds"
    );
}

/// Layout regression guard: toggling overlay visibility must not change sibling
/// geometry (footer row stays stable between hidden and visible states).
#[test]
fn p1g15_overlay_visibility_toggle_does_not_shift_footer_layout() {
    let build_root = |overlay_visible: bool| {
        let base = Container::new().with_compose(compose![
            Static::new("BASE_LINE_1"),
            Static::new("BASE_LINE_2"),
            Static::new("BASE_LINE_3"),
        ]);

        let mut modal_layer = Container::new().with_child(Static::new("MODAL_CARD"));
        {
            let style = &mut modal_layer.styles_mut().expect("modal styles").style;
            style.position = Some(Position::Absolute);
            style.offset = Some(Offset {
                x: OffsetValue::Cells(1),
                y: OffsetValue::Cells(1),
            });
            style.width = Some(Scalar::Cells(12));
            style.height = Some(Scalar::Cells(2));
        }

        Dock::new()
            .push_fill(Overlay::new(base, modal_layer).visible(overlay_visible))
            .push_bottom(Some(1), Static::new("FOOTER_SENTINEL"))
    };

    let mut hidden_root = build_root(false);
    let (_tree_hidden, _frame_hidden, hidden_lines) = tree_render(&mut hidden_root, 50, 10);
    let (hidden_footer_row, hidden_footer_col) = find_text(&hidden_lines, "FOOTER_SENTINEL");

    let mut visible_root = build_root(true);
    let (_tree_visible, _frame_visible, visible_lines) = tree_render(&mut visible_root, 50, 10);
    let (visible_footer_row, visible_footer_col) = find_text(&visible_lines, "FOOTER_SENTINEL");

    assert_eq!(
        hidden_footer_row, visible_footer_row,
        "overlay visibility toggle must not move footer row"
    );
    assert_eq!(
        hidden_footer_col, visible_footer_col,
        "overlay visibility toggle must not move footer column"
    );
}