htui 0.2.0

A terminal HTML/CSS renderer with an embedded JavaScript engine — build interactive TUIs from HTML, CSS and JS
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
use crossterm::style::Color;
use htui::{
    html::{css_parse, html_parse},
    layout::{Rect, layout::TermMap},
    tdom::node::{Node, NodeArena, NodeId, NodeType},
    tui::graph_api::TermLayout,
};

fn setup(html: &str) -> (NodeId, NodeArena) {
    let mut html_parser = html_parse::Parser::new(html);
    let (root_id, mut arena, embedded) = html_parser.parse();
    if !embedded.trim().is_empty() {
        let mut css_parser = css_parse::Parser::new(&embedded);
        let rules = css_parser.parse();
        css_parse::css_into_node(&mut arena, root_id, &rules);
    }
    (root_id, arena)
}

/// Find the first node carrying `id="target"`.
fn find_id(arena: &NodeArena, target: &str) -> Option<NodeId> {
    fn walk(arena: &NodeArena, id: NodeId, target: &str) -> Option<NodeId> {
        let node = &arena[id];
        if node.attributes.get("id").is_some_and(|v| v == target) {
            return Some(id);
        }
        node.children.iter().find_map(|&c| walk(arena, c, target))
    }
    let root = arena
        .iter()
        .find_map(|(id, n)| (n.node_type == NodeType::Document).then_some(id));
    root.and_then(|r| walk(arena, r, target))
}

fn term_map() -> TermMap {
    TermMap {
        columns: 80,
        rows: 24,
    }
}

#[test]
fn fresh_canvas_max_scroll_requires_the_paint_pass() {
    // `rendered_rows`, the basis for `max_scroll`, is established by the
    // paint pass. These assertions pin the fresh canvas to zero before
    // painting and to the measured content height afterward.
    let mut html = String::from("<div>");
    for i in 0..60 {
        html.push_str(&format!("<p>line {i}</p>"));
    }
    html.push_str("</div>");
    let (root_id, arena) = setup(&html);
    let map = term_map();

    // Before the paint pass a brand-new canvas reports no content height.
    let mut pre = TermLayout::new(&map, 1.0);
    pre.layout_only(&arena, root_id, &map);
    assert_eq!(
        pre.max_scroll(),
        0,
        "pre-paint max_scroll on a fresh canvas must be 0"
    );

    // After the full draw the content height is known and scrollable.
    let mut post = TermLayout::new(&map, 1.0);
    post.draw(&arena, root_id, &map);
    assert!(
        post.max_scroll() > 0,
        "post-paint max_scroll must reflect the document height"
    );
}

#[test]
fn scroll_bound_never_exceeds_the_canvas_buffer() {
    // The canvas grows to the full content height (no screen cap), so a
    // normally-painted document always has `rendered_rows <= canvas rows`.
    // Two invariants protect the incremental diff renderer (which slices
    // `layout[scroll_start..]`): the advertised scroll bound never points
    // past the buffer end, and rendering at an out-of-range position is a
    // safe no-op instead of an underflow panic.
    let mut html = String::from("<div>");
    for _ in 0..120 {
        html.push_str("<div>r</div>");
    }
    html.push_str("</div>");
    let (root_id, arena) = setup(&html);
    let map = TermMap {
        columns: 10,
        rows: 4,
    };
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let canvas_rows = tl.layout.len() / map.columns as usize;
    // The advertised scroll bound stays inside the buffer.
    assert!(
        tl.max_scroll() + map.rows as u32 <= canvas_rows as u32,
        "max_scroll must not point past the canvas end"
    );

    // Clamping a wild scroll position must land inside the buffer.
    tl.scroll_y = u32::MAX;
    tl.clamp_scroll();
    assert!(
        (tl.scroll_y as usize).saturating_mul(map.columns as usize) <= tl.layout.len(),
        "clamped scroll must fit in the canvas"
    );

    // An out-of-range position must render as a safe no-op.
    tl.scroll_y = 1000;
    tl.render_diff_to_bytes();
    tl.render_to_bytes();

    // Simulate a stale oversized content-height report (`rendered_rows` is a
    // monotonic high-water mark; a re-layout after a terminal grow can leave
    // it above the canvas for one frame): the guards must still keep
    // rendering safe.
    tl.rendered_rows = (canvas_rows + 100) as u32;
    tl.scroll_y = 1000;
    tl.render_diff_to_bytes();
    tl.render_to_bytes();
}

#[test]
fn stale_scroll_batch_row_never_underflows() {
    // Both render paths must treat stale scroll values — including canvas
    // boundaries and 16/32-bit extremes — as safe no-ops.
    let mut html = String::from("<div>");
    for i in 0..40 {
        html.push_str(&format!("<p>line {i}</p>"));
    }
    html.push_str("</div>");
    let (root_id, arena) = setup(&html);
    let map = TermMap {
        columns: 10,
        rows: 4,
    };
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let canvas_rows = (tl.layout.len() / map.columns as usize) as u32;
    let mut stale = vec![0u32, 1, canvas_rows, canvas_rows + 1, canvas_rows + 50];
    // Exercise both sides of the 16-bit boundary.
    stale.extend([u16::MAX as u32, u16::MAX as u32 + 1, u16::MAX as u32 + 100]);
    stale.push(u32::MAX);

    for &scroll in &stale {
        tl.scroll_y = scroll;
        tl.render_to_bytes();
        tl.render_diff_to_bytes();
    }

    // Sanity: a fresh in-range scroll still renders content after the sweep.
    tl.scroll_y = 0;
    assert!(
        !tl.render_to_bytes().is_empty(),
        "in-range scroll must render after stale-scroll sweeps"
    );
}

#[test]
fn unchanged_frame_emits_zero_bytes() {
    let (root_id, arena) = setup("<div>hello world</div>");
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let first = tl.render_diff_to_bytes();
    assert!(!first.is_empty(), "first frame must emit content");
    let first_text = String::from_utf8_lossy(&first);
    assert!(
        first_text.contains("hello") && first_text.contains("world"),
        "first frame must contain the document text"
    );

    let second = tl.render_diff_to_bytes();
    assert!(second.is_empty(), "unchanged frame must emit nothing");
}

#[test]
fn changed_content_emits_smaller_diff_than_full_frame() {
    let (root_id, arena) = setup("<div>first</div>");
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let full = tl.render_to_bytes();
    assert!(String::from_utf8_lossy(&full).contains("first"));
    let first_diff = tl.render_diff_to_bytes();
    assert!(!first_diff.is_empty());
    assert!(
        first_diff.len() < full.len(),
        "first diff must be smaller than the whole-viewport frame"
    );

    // A new document with different text at the same position.
    let (root2, arena2) = setup("<div>second</div>");
    tl.draw(&arena2, root2, &map);
    let diff = tl.render_diff_to_bytes();
    assert!(!diff.is_empty(), "changed frame must emit something");
    assert!(
        diff.len() < full.len(),
        "diff must stay below full-frame size"
    );
    assert!(
        String::from_utf8_lossy(&diff).contains("second"),
        "diff must carry the new text"
    );
}

#[test]
fn scrolling_emits_diff_and_stable_views_emit_nothing() {
    let mut html = String::from("<div>");
    for i in 0..60 {
        html.push_str(&format!("<p>line {i}</p>"));
    }
    html.push_str("</div>");
    let (root_id, arena) = setup(&html);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    assert!(
        !tl.render_diff_to_bytes().is_empty(),
        "first frame must emit content"
    );
    assert!(
        tl.render_diff_to_bytes().is_empty(),
        "stable view must emit nothing"
    );

    tl.scroll_y = 1;
    tl.clamp_scroll();
    assert!(
        tl.max_scroll() >= 1,
        "tall document must be scrollable for this test"
    );
    let scrolled = tl.render_diff_to_bytes();
    assert!(!scrolled.is_empty(), "scrolled frame must emit a diff");
    assert!(
        tl.render_diff_to_bytes().is_empty(),
        "stable scrolled view must emit nothing"
    );
}

/// Find the layout box for a node, searching the whole forest.
fn find_box(
    boxes: &[htui::layout::LayoutBox],
    node_id: NodeId,
) -> Option<&htui::layout::LayoutBox> {
    for b in boxes {
        if b.node_id == node_id {
            return Some(b);
        }
        if let Some(found) = find_box(&b.children, node_id) {
            return Some(found);
        }
    }
    None
}

#[test]
fn region_repaint_emits_only_the_changed_text() {
    let (root_id, mut arena) = setup("<div><p id=\"a\">hello</p><p id=\"b\">world</p></div>");
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);
    let first = tl.render_diff_to_bytes();
    assert!(
        String::from_utf8_lossy(&first).contains("world"),
        "first frame must contain both paragraphs"
    );

    // Simulate `textContent` on `<p id=a>`: rewrite its single text child to
    // a same-width string so the layout (box rects) stays identical.
    let a_id = find_id(&arena, "a").expect("paragraph a");
    let text_child = arena[a_id].children[0];
    if let Some(t) = arena[text_child].data.as_mut() {
        *t = "helpx".to_string();
    }

    // The incremental pipeline the render thread runs: re-layout, then
    // repaint only the changed box's rect.
    let boxes = tl.layout_only(&arena, root_id, &map);
    let rect = find_box(&boxes, a_id).expect("a's layout box").rect;
    tl.repaint_region(&arena, &boxes, rect);

    let diff = tl.render_diff_to_bytes();
    let text = String::from_utf8_lossy(&diff);
    assert!(
        text.contains("helpx"),
        "region repaint must show the new text"
    );
    assert!(
        !text.contains("world"),
        "unchanged sibling must not be re-emitted by a region repaint"
    );
}

#[test]
fn region_repaint_preserves_ancestor_background() {
    let html = concat!(
        "<style>div { background-color: cyan }</style>",
        "<div><p id=\"a\">one</p><p id=\"b\">two</p></div>"
    );
    let (root_id, mut arena) = setup(html);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);
    let first = tl.render_diff_to_bytes();
    assert!(!first.is_empty());

    let a_id = find_id(&arena, "a").expect("paragraph a");
    let text_child = arena[a_id].children[0];
    if let Some(t) = arena[text_child].data.as_mut() {
        *t = "uno".to_string();
    }

    let boxes = tl.layout_only(&arena, root_id, &map);
    let rect = find_box(&boxes, a_id).expect("a's layout box").rect;
    tl.repaint_region(&arena, &boxes, rect);
    let diff = tl.render_diff_to_bytes();
    assert!(
        String::from_utf8_lossy(&diff).contains("uno"),
        "region repaint must show the new text"
    );

    // The `<p>` itself has no background; its cells must inherit the
    // ancestor `<div>`'s fill through the repaint's parent-bg chain.
    let cols = tl.columns as usize;
    let text_cell = &tl.layout[rect.y.saturating_mul(cols).saturating_add(rect.x)];
    assert_eq!(
        text_cell.bcolor,
        Some(Color::Rgb {
            r: 0,
            g: 255,
            b: 255
        }),
        "region repaint must keep the ancestor background on text cells"
    );
}

#[test]
fn region_repaint_clears_stale_cells_of_a_shorter_value() {
    // A fixed-width box whose value shrinks: the trailing cells must be
    // blanked (they were cleared with the region), not keep stale glyphs.
    let html = concat!(
        "<style>p { width: 20ch; background-color: yellow }</style>",
        "<p id=\"a\">longvalue</p>"
    );
    let (root_id, mut arena) = setup(html);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);
    let first = tl.render_diff_to_bytes();
    assert!(String::from_utf8_lossy(&first).contains("longvalue"));

    let a_id = find_id(&arena, "a").expect("paragraph a");
    let text_child = arena[a_id].children[0];
    if let Some(t) = arena[text_child].data.as_mut() {
        *t = "hi".to_string();
    }

    let boxes = tl.layout_only(&arena, root_id, &map);
    let rect = find_box(&boxes, a_id).expect("a's layout box").rect;
    tl.repaint_region(&arena, &boxes, rect);
    let diff = tl.render_diff_to_bytes();
    let text = String::from_utf8_lossy(&diff);
    assert!(text.contains("hi"), "region repaint must show the new text");
    assert!(
        !text.contains("longvalue"),
        "stale cells of the previous value must be cleared"
    );
}

#[test]
fn block_element_char_width() {
    // U+2588 FULL BLOCK / U+2591 LIGHT SHADE are progress-bar glyphs. Their
    // East Asian Width is "Ambiguous": Unicode treats
    // them as one column, but CJK-configured legacy terminals may render two.
    // The engine must lay them out consistently with the terminal or the bar
    // misaligns (over-wide row, wrapped percentage, shifted digits).
    use htui::tui::graph_api::{char_width, grapheme_width};
    assert_eq!(grapheme_width("█"), char_width('█'));
    assert_eq!(grapheme_width("░"), char_width('░'));
    assert_eq!(grapheme_width("█"), 1, "full block must be single-width");
    assert_eq!(grapheme_width("░"), 1, "light shade must be single-width");
}

#[test]
fn canvas_starts_at_one_viewport_and_grows_on_demand() {
    let map = term_map(); // 80×24
    let viewport = map.columns as usize * map.rows as usize;
    let mut tl = TermLayout::new(&map, 1.0);
    assert_eq!(
        tl.layout.len(),
        viewport,
        "canvas must start at a single viewport, not the full scroll capacity"
    );

    // A short document must not grow the buffer.
    let (root_id, arena) = setup("<div>hi</div>");
    tl.draw(&arena, root_id, &map);
    assert_eq!(
        tl.layout.len(),
        viewport,
        "short doc must keep the 1-viewport canvas"
    );

    // A tall document grows the buffer exactly to its content height.
    let mut html = String::from("<div>");
    for i in 0..60 {
        html.push_str(&format!("<p>line {i}</p>"));
    }
    html.push_str("</div>");
    let (root_id, arena) = setup(&html);
    tl.draw(&arena, root_id, &map);
    let layout_rows = tl.layout.len() / map.columns as usize;
    assert!(
        layout_rows > map.rows as usize,
        "tall doc must grow the canvas beyond the viewport"
    );
    assert_eq!(
        tl.rendered_rows as usize, layout_rows,
        "canvas must grow to exactly the content height (no screen cap)"
    );
}

/// Replace the children of `id="target"` with a single text node (the DOM
/// shape `el.textContent = v` leaves behind).
fn set_text_by_id(arena: &mut NodeArena, target: &str, text: &str) {
    let node = find_id(arena, target).expect("target node");
    let old = std::mem::take(&mut arena[node].children);
    for c in old {
        if let Some(n) = arena.get_mut(c) {
            n.parent = None;
        }
    }
    let text_id = arena.insert(Node::text(text));
    arena[text_id].parent = Some(node);
    arena[node].children.push(text_id);
}

#[test]
fn incremental_repaint_matches_full_repaint_after_value_changes() {
    // Golden test for the incremental path: after every simulated refresh
    // `textContent` update cycle, the incrementally repainted
    // canvas must be pixel-identical to a full re-draw. Any stale / missing
    // cell in the region repaint fails here, which would otherwise show up as
    // flickering or "jumping" digits on screen.
    let html = concat!(
        "<div>",
        "<p id=\"now\">now: 14:03:22 · session 00:05</p>",
        "<p id=\"cpu\">n/a</p>",
        "<p id=\"mem\">n/a</p>",
        "<p id=\"proc\">processes: n/a</p>",
        "</div>"
    );
    let (root_id, arena0) = setup(html);
    let map = term_map();
    let mut full = TermLayout::new(&map, 1.0);
    let mut incr = TermLayout::new(&map, 1.0);
    full.draw(&arena0, root_id, &map);
    incr.draw(&arena0, root_id, &map);
    let _ = full.render_diff_to_bytes();
    let _ = incr.render_diff_to_bytes();

    // Text lengths change across cycles (short→long, long→short) so wrapping
    // is exercised as well as in-place rewrites. The `cpu` cycles use block
    // and shade glyphs to prove the incremental path stays pixel-identical
    // for them too.
    let cycles: &[(&str, &str)] = &[
        ("now", "now: 14:03:23 · session 00:05"),
        ("cpu", "[████████░░░░░░░░] 45%"),
        ("mem", "8.1 / 15.9"),
        ("now", "now: 14:03:24 · session 00:06"),
        ("cpu", "[████████████░░░░] 98%"),
        ("proc", "processes: 1234"),
        ("cpu", "[██░░░░░░░░░░░░░░] 12%"),
        ("now", "now: 14:03:25 · session 00:07"),
        ("mem", "7.2 / 15.9"),
    ];
    let mut arena = arena0.clone();
    for &(id, text) in cycles {
        let mut next = arena.clone();
        set_text_by_id(&mut next, id, text);
        arena = next;

        // Full path: reset + layout + paint everything.
        full.draw(&arena, root_id, &map);
        let _ = full.render_diff_to_bytes();

        // Incremental path: layout only, repaint just the touched box rect.
        let boxes = incr.layout_only(&arena, root_id, &map);
        let node = find_id(&arena, id).expect("dirty node");
        let rect = find_box(&boxes, node).expect("dirty box").rect;
        incr.repaint_region(&arena, &boxes, rect);
        let _ = incr.render_diff_to_bytes();

        assert!(
            same_canvas(&full.layout, &incr.layout),
            "incremental canvas diverged from full after '{id}' = '{text}'"
        );
    }
}

/// Compare two canvases cell-by-cell (word / fg / bg / style).
fn same_canvas(a: &[htui::tui::graph_api::Block], b: &[htui::tui::graph_api::Block]) -> bool {
    a.len() == b.len()
        && a.iter().zip(b.iter()).all(|(x, y)| {
            x.word == y.word && x.fcolor == y.fcolor && x.bcolor == y.bcolor && x.style == y.style
        })
}

#[test]
fn viewport_resize_recreates_canvas_and_recovers() {
    // The render thread's resize path replaces the `TermLayout` (its
    // dimensions are fixed at construction) and re-renders from scratch —
    // this mirrors that: draw at one size, allocate a fresh canvas at a
    // different size, and verify the incremental renderer re-emits the full
    // viewport instead of silently keeping stale pixels.
    let (root_id, arena) = setup("<div>hello resize</div>");

    let small = TermMap {
        columns: 40,
        rows: 10,
    };
    let mut tl = TermLayout::new(&small, 1.0);
    tl.draw(&arena, root_id, &small);
    let first_bytes = tl.render_diff_to_bytes();
    let first = String::from_utf8_lossy(&first_bytes);
    assert!(
        first.contains("hello") && first.contains("resize"),
        "first frame must carry the document text"
    );

    // Grow: a fresh canvas has an empty `last_frame` snapshot, so the first
    // diff must clear the screen and emit every content cell again.
    let big = TermMap {
        columns: 100,
        rows: 30,
    };
    let mut grown = TermLayout::new(&big, 1.0);
    grown.draw(&arena, root_id, &big);
    let diff = grown.render_diff_to_bytes();
    assert!(!diff.is_empty(), "resize must re-emit the whole viewport");
    let text = String::from_utf8_lossy(&diff);
    assert!(
        text.contains("hello") && text.contains("resize"),
        "resize frame must carry the document text"
    );
    assert!(
        grown.render_diff_to_bytes().is_empty(),
        "stable view after resize must emit nothing"
    );

    // A stale scroll position beyond the new viewport must clamp, not panic.
    grown.scroll_y = 9999;
    grown.clamp_scroll();
    assert_eq!(grown.scroll_y, grown.max_scroll());
    let _ = grown.render_diff_to_bytes();

    // Shrink: fresh canvas again, still renders.
    let tiny = TermMap {
        columns: 20,
        rows: 5,
    };
    let mut shrunk = TermLayout::new(&tiny, 1.0);
    shrunk.draw(&arena, root_id, &tiny);
    assert!(
        !shrunk.render_diff_to_bytes().is_empty(),
        "shrunken viewport must re-emit content"
    );
}

/// The page-selection highlight must preserve background-driven widgets.
///
/// Background-driven widgets can be made from adjacent glyph-free divs with
/// different fills. Selection must blend an existing RGB background with the
/// selection colour
/// so the two halves stay distinguishable (tinted, browser-like translucent
/// `::selection`).
#[test]
fn selection_highlight_preserves_progress_bar_colors() {
    let html = r#"
      <style>
        .prog { display: flex; }
        .fill { background-color: #bb9af7; }
        .rest { background-color: #2f334d; }
      </style>
      <div class="prog">
        <div class="fill" style="flex-grow: 30;"></div>
        <div class="rest" style="flex-grow: 70;"></div>
      </div>
    "#;
    let (root_id, arena) = setup(html);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let fill = Color::Rgb {
        r: 0xbb,
        g: 0x9a,
        b: 0xf7,
    };
    let rest = Color::Rgb {
        r: 0x2f,
        g: 0x33,
        b: 0x4d,
    };
    let fill_idx: Vec<usize> = tl
        .layout
        .iter()
        .enumerate()
        .filter(|(_, c)| c.bcolor == Some(fill))
        .map(|(i, _)| i)
        .collect();
    let rest_idx: Vec<usize> = tl
        .layout
        .iter()
        .enumerate()
        .filter(|(_, c)| c.bcolor == Some(rest))
        .map(|(i, _)| i)
        .collect();
    assert!(!fill_idx.is_empty(), "fill half must render");
    assert!(!rest_idx.is_empty(), "rest half must render");
    let cols = tl.columns as usize;
    assert_eq!(
        fill_idx[0] / cols,
        rest_idx[0] / cols,
        "fill and rest share one bar row"
    );

    // Right-drag style block selection spanning the whole bar row.
    let row = (fill_idx[0] / cols) as u32;
    let xs = (fill_idx[0] % cols) as u32;
    let xe = (rest_idx[rest_idx.len() - 1] % cols + 1) as u32;
    tl.apply_text_selection(
        &[(row, xs, xe)],
        Color::Rgb {
            r: 0x3b,
            g: 0x42,
            b: 0x61,
        },
    );

    // Blended halves stay distinct — the bar boundary survives the highlight.
    let blended_fill = Color::Rgb {
        r: ((0xbb + 0x3b) / 2) as u8,
        g: ((0x9a + 0x42) / 2) as u8,
        b: ((0xf7 + 0x61) / 2) as u8,
    };
    let blended_rest = Color::Rgb {
        r: ((0x2f + 0x3b) / 2) as u8,
        g: ((0x33 + 0x42) / 2) as u8,
        b: ((0x4d + 0x61) / 2) as u8,
    };
    assert!(
        fill_idx
            .iter()
            .all(|&i| tl.layout[i].bcolor == Some(blended_fill)),
        "fill cells must keep a purple-tinted background under the highlight"
    );
    assert!(
        rest_idx
            .iter()
            .all(|&i| tl.layout[i].bcolor == Some(blended_rest)),
        "rest cells must keep a dark background under the highlight"
    );
    assert_ne!(blended_fill, blended_rest, "bar boundary must stay visible");
}

/// Cells without a background must keep the flat selection colour and their
/// original foreground — the standard terminal highlight stays untouched.
#[test]
fn selection_highlight_keeps_plain_highlight_on_default_bg() {
    let (root_id, arena) = setup(r#"<div>plain text</div>"#);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);

    let cols = tl.columns as usize;
    let row = (0..tl.layout.len())
        .find(|&i| !tl.layout[i].word.is_empty() && tl.layout[i].word != " ")
        .map(|i| i / cols)
        .expect("text row");
    let fg = tl.layout[row * cols].fcolor;
    let sel = Color::Rgb {
        r: 0x3b,
        g: 0x42,
        b: 0x61,
    };
    tl.apply_text_selection(&[(row as u32, 0, 20)], sel);

    for x in 0..20 {
        let cell = &tl.layout[row * cols + x];
        if !cell.word.is_empty() {
            assert_eq!(cell.bcolor, Some(sel), "glyph cell takes the selection bg");
            assert_eq!(cell.fcolor, fg, "glyph keeps its foreground");
        }
    }
}

/// A selection re-applied after a repaint must not darken.
///
/// The render thread repaints the selection bounds before re-applying the
/// highlight (see `RenderState::layout_and_paint` / `repaint_dynamic`), so
/// every selected cell is blended exactly once from its fresh paint colour.
/// Blending a cell that already carries the highlight would darken it a step
/// per repaint — a timer / hover repaint turns that into the bright / dark
/// flicker.
#[test]
fn selection_reapply_after_full_repaint_keeps_single_blend() {
    let html = r#"
      <style>
        .prog { display: flex; height: 1ch; }
        .fill { background-color: #bb9af7; }
        .rest { background-color: #2f334d; }
      </style>
      <div class="prog">
        <div class="fill" style="flex-grow: 30;"></div>
        <div class="rest" style="flex-grow: 70;"></div>
      </div>
    "#;
    let (root_id, arena) = setup(html);
    let map = term_map();
    let mut tl = TermLayout::new(&map, 1.0);
    tl.draw(&arena, root_id, &map);
    let boxes = tl.layout_only(&arena, root_id, &map);

    let fill = Color::Rgb {
        r: 0xbb,
        g: 0x9a,
        b: 0xf7,
    };
    let rest = Color::Rgb {
        r: 0x2f,
        g: 0x33,
        b: 0x4d,
    };
    let fill_idx: Vec<usize> = tl
        .layout
        .iter()
        .enumerate()
        .filter(|(_, c)| c.bcolor == Some(fill))
        .map(|(i, _)| i)
        .collect();
    let rest_idx: Vec<usize> = tl
        .layout
        .iter()
        .enumerate()
        .filter(|(_, c)| c.bcolor == Some(rest))
        .map(|(i, _)| i)
        .collect();
    assert!(!fill_idx.is_empty(), "fill half must render");
    assert!(!rest_idx.is_empty(), "rest half must render");
    let cols = tl.columns as usize;
    let row = (fill_idx[0] / cols) as u32;
    let xs = (fill_idx[0] % cols) as u32;
    let xe = (rest_idx[rest_idx.len() - 1] % cols + 1) as u32;

    let sel = Color::Rgb {
        r: 0x3b,
        g: 0x42,
        b: 0x61,
    };
    let band = [(row, xs, xe)];
    tl.apply_text_selection(&band, sel);

    let blended_fill = Color::Rgb {
        r: 123,
        g: 110,
        b: 172,
    };
    let blended_rest = Color::Rgb {
        r: 53,
        g: 58,
        b: 87,
    };
    assert!(
        fill_idx
            .iter()
            .all(|&i| tl.layout[i].bcolor == Some(blended_fill)),
        "fill cells must show the single-blend colour after the first apply"
    );

    // Render-thread sequence when an animated widget repaints while the
    // selection is active: a repaint covering the selection bounds restores
    // the base colours, then the highlight is re-applied — one blend, same
    // colour as before (no progressive darkening).
    let sel_rect = Rect::new(xs as usize, row as usize, (xe - xs) as usize, 1);
    tl.repaint_region(&arena, &boxes, sel_rect);
    tl.apply_text_selection(&band, sel);
    assert!(
        fill_idx
            .iter()
            .all(|&i| tl.layout[i].bcolor == Some(blended_fill)),
        "re-applied highlight must keep the single-blend colour (no darkening)"
    );
    assert!(
        rest_idx
            .iter()
            .all(|&i| tl.layout[i].bcolor == Some(blended_rest)),
        "rest cells must keep the single-blend colour under the re-applied highlight"
    );
}