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
//! P2 gate tests: widget-specific CSS property consumption.
//!
//! P2G-30: ScrollView scrollbar CSS properties (color/size/gutter/visibility).
//! P2G-32: Link widget CSS link-styling properties (normal + hover).
//! P2G-36: Per-property transitions (parser + resolution).

use std::time::Duration;

use rich_rs::Console;
use textual::css::set_style_context;
use textual::event::{Event, EventCtx, MouseDownEvent};
use textual::prelude::*;
use textual::render::FrameBuffer;
use textual::style::{PropertyTransition, ScrollbarGutter, ScrollbarVisibility, TransitionTiming};

// ───────────────────────────────────────────────────────────────────────
// P2G-30  Scrollbar CSS
// ───────────────────────────────────────────────────────────────────────

#[test]
fn p2g30_scrollbar_color_parses() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-color: #ff0000; }"#;
    let sheet = StyleSheet::parse(css);
    let rules = sheet.rules();
    assert!(!rules.is_empty(), "should parse scrollbar-color rule");
    let style = &rules[0].style();
    assert_eq!(
        style.scrollbar_color,
        Some(Color::parse("#ff0000").unwrap()),
        "scrollbar_color should be red"
    );
}

#[test]
fn p2g30_scrollbar_background_parses() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-background: #112233; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.scrollbar_background,
        Some(Color::parse("#112233").unwrap())
    );
}

#[test]
fn p2g30_scrollbar_hover_active_colors() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"
        ScrollView {
            scrollbar-color-hover: #aabbcc;
            scrollbar-color-active: #ddeeff;
            scrollbar-background-hover: #001122;
            scrollbar-background-active: #334455;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.scrollbar_color_hover,
        Some(Color::parse("#aabbcc").unwrap())
    );
    assert_eq!(
        style.scrollbar_color_active,
        Some(Color::parse("#ddeeff").unwrap())
    );
    assert_eq!(
        style.scrollbar_background_hover,
        Some(Color::parse("#001122").unwrap())
    );
    assert_eq!(
        style.scrollbar_background_active,
        Some(Color::parse("#334455").unwrap())
    );
}

#[test]
fn p2g30_scrollbar_corner_color() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-corner-color: #abcdef; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.scrollbar_corner_color,
        Some(Color::parse("#abcdef").unwrap())
    );
}

#[test]
fn p2g30_scrollbar_gutter_stable() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-gutter: stable; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.scrollbar_gutter, Some(ScrollbarGutter::Stable));
}

#[test]
fn p2g30_scrollbar_gutter_auto() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-gutter: auto; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.scrollbar_gutter, Some(ScrollbarGutter::Auto));
}

#[test]
fn p2g30_scrollbar_size_shorthand() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-size: 3; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.scrollbar_size, Some(3));
}

#[test]
fn p2g30_scrollbar_size_per_axis() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"
        ScrollView {
            scrollbar-size-horizontal: 2;
            scrollbar-size-vertical: 4;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.scrollbar_size_horizontal, Some(2));
    assert_eq!(style.scrollbar_size_vertical, Some(4));
}

#[test]
fn p2g30_scrollbar_visibility_hidden() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-visibility: hidden; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.scrollbar_visibility,
        Some(ScrollbarVisibility::Hidden)
    );
}

#[test]
fn p2g30_scrollbar_visibility_visible() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-visibility: visible; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.scrollbar_visibility,
        Some(ScrollbarVisibility::Visible)
    );
}

#[test]
fn p2g30_scrollbar_visibility_auto() {
    // NOTE: parse-only — verifies CSS parser, not runtime scrollbar rendering.
    let css = r#"ScrollView { scrollbar-visibility: auto; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.scrollbar_visibility, Some(ScrollbarVisibility::Auto));
}

#[test]
fn p2g30_scroll_view_render_with_css_scrollbar_size() {
    // Verify that ScrollView respects CSS scrollbar-size-vertical for
    // vertical scrollbar width (default is 2, we set 3).
    let css = r#"ScrollView { scrollbar-size-vertical: 3; }"#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut sv = ScrollView::new(Label::new("content"));
    let _ = sv.take_composed_children(); // Enter tree mode.
    sv.set_virtual_content_size(10, 100);

    let console = Console::default();
    let mut opts = console.options().clone();
    opts.size = (30, 10);
    opts.max_width = 30;
    opts.max_height = 10;

    let segments = Widget::render(&sv, &console, &opts);
    let lines = rich_rs::Segment::split_and_crop_lines(segments, 30, None, true, false);
    // The scrollbar should be present (content > viewport).
    assert_eq!(lines.len(), 10);
    // With scrollbar-size-vertical: 3, the content viewport should be 30-3=27 wide
    // and the last 3 cells of each line should be scrollbar chrome.
    // We verify that lines have segments for scrollbar.
    let has_scrollbar = lines.iter().any(|line| line.len() > 1);
    assert!(
        has_scrollbar,
        "scrollbar chrome should be present when content overflows"
    );
}

#[test]
fn p2g30_scroll_view_visibility_hidden_no_scrollbar() {
    let css = r#"ScrollView { scrollbar-visibility: hidden; }"#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut sv = ScrollView::new(Label::new("content"));
    let _ = sv.take_composed_children();
    sv.set_virtual_content_size(10, 100);

    let console = Console::default();
    let mut opts = console.options().clone();
    opts.size = (20, 10);
    opts.max_width = 20;
    opts.max_height = 10;

    let segments = Widget::render(&sv, &console, &opts);
    let lines = rich_rs::Segment::split_and_crop_lines(segments, 20, None, true, false);
    // With visibility: hidden, no scrollbar should appear.
    // All lines should be single segments (space fills, no scrollbar chrome).
    for (i, line) in lines.iter().enumerate() {
        assert_eq!(
            line.len(),
            1,
            "line {} should have 1 segment (no scrollbar chrome) when visibility=hidden",
            i
        );
    }
}

#[test]
fn p2g30_scroll_view_hover_subpart_colors_are_consumed() {
    let css = r#"
        ScrollView {
            scrollbar-size-vertical: 1;
            scrollbar-color: #101010;
            scrollbar-color-hover: #11aa11;
            scrollbar-background: #202020;
            scrollbar-background-hover: #2222aa;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut sv = ScrollView::new(Label::new("content"));
    let _ = sv.take_composed_children();
    sv.set_virtual_content_size(5, 100);

    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 4);
    opts.max_width = 10;
    opts.max_height = 4;

    // Prime viewport/widget dimensions used by on_mouse_move hover hit-tests.
    let _ = Widget::render(&sv, &console, &opts);

    // Hover a track-only cell.
    assert!(
        sv.on_mouse_move(9, 3),
        "hovering scrollbar track should mark changed"
    );
    let _ = Widget::render(&sv, &console, &opts);

    // Hover the thumb cell.
    assert!(
        sv.on_mouse_move(9, 0),
        "hovering scrollbar thumb should mark changed"
    );
    let _ = Widget::render(&sv, &console, &opts);
}

#[test]
fn p2g30_scroll_view_drag_thumb_uses_active_color() {
    let css = r#"
        ScrollView {
            scrollbar-size-vertical: 1;
            scrollbar-color: #101010;
            scrollbar-color-active: #ff5500;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut sv = ScrollView::new(Label::new("content"));
    let _ = sv.take_composed_children();
    sv.set_virtual_content_size(5, 100);

    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 4);
    opts.max_width = 10;
    opts.max_height = 4;

    // Prime viewport/widget dimensions used by mouse-down scrollbar hit-tests.
    let _ = Widget::render(&sv, &console, &opts);

    let mut ctx = EventCtx::default();
    sv.on_event(
        &Event::MouseDown(MouseDownEvent {
            target: NodeId::default(),
            screen_x: 9,
            screen_y: 0,
            x: 9,
            y: 0,
        }),
        &mut ctx,
    );
    assert!(
        ctx.handled(),
        "scrollbar thumb mouse-down should be handled"
    );

    let _ = Widget::render(&sv, &console, &opts);
}

// ───────────────────────────────────────────────────────────────────────
// P2G-32  Link CSS styling
// ───────────────────────────────────────────────────────────────────────

#[test]
fn p2g32_link_color_parses() {
    // NOTE: parse-only — verifies CSS parser, not runtime link rendering.
    let css = r#"Link { link-color: #ff0000; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.link_color,
        Some(Color::parse("#ff0000").unwrap()),
        "link_color should be parsed"
    );
}

#[test]
fn p2g32_link_background_parses() {
    // NOTE: parse-only — verifies CSS parser, not runtime link rendering.
    let css = r#"Link { link-background: #00ff00; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.link_background,
        Some(Color::parse("#00ff00").unwrap())
    );
}

#[test]
fn p2g32_link_style_parses() {
    // NOTE: parse-only — verifies CSS parser, not runtime link rendering.
    let css = r#"Link { link-style: bold underline; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    let flags = style.link_style.expect("link_style should be set");
    assert!(flags.bold, "bold flag should be set");
    assert!(flags.underline, "underline flag should be set");
    assert!(!flags.italic, "italic flag should not be set");
}

#[test]
fn p2g32_link_hover_variants_parse() {
    // NOTE: parse-only — verifies CSS parser, not runtime link rendering.
    let css = r#"
        Link {
            link-color-hover: #aabb00;
            link-background-hover: #00aabb;
            link-style-hover: italic;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.link_color_hover,
        Some(Color::parse("#aabb00").unwrap())
    );
    assert_eq!(
        style.link_background_hover,
        Some(Color::parse("#00aabb").unwrap())
    );
    let flags = style
        .link_style_hover
        .expect("link_style_hover should be set");
    assert!(flags.italic, "hover italic flag should be set");
}

#[test]
fn p2g32_link_background_initial_clears_property() {
    let css = r#"
        Link {
            link-background: #00ff00;
            link-background: initial;
            link-background-hover: #00aabb;
            link-background-hover: initial;
            link-style: bold;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(
        style.link_background, None,
        "link-background: initial should clear value"
    );
    assert_eq!(
        style.link_background_hover, None,
        "link-background-hover: initial should clear value"
    );
}

#[test]
fn p2g32_color_initial_clears_fg_bg_and_link_colors() {
    let css = r#"
        Link {
            color: #010203;
            color: initial;
            background: #040506;
            background: initial;
            link-color: #070809;
            link-color: initial;
            link-color-hover: #0a0b0c;
            link-color-hover: initial;
            link-style: underline;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    assert_eq!(style.fg, None, "color: initial should clear fg");
    assert_eq!(style.fg_auto, None, "color: initial should clear fg_auto");
    assert_eq!(style.bg, None, "background: initial should clear bg");
    assert_eq!(
        style.link_color, None,
        "link-color: initial should clear value"
    );
    assert_eq!(
        style.link_color_hover, None,
        "link-color-hover: initial should clear value"
    );
}

#[test]
fn dce12_color_auto_percent_parses_to_fg_auto() {
    let css = r#"Label { color: auto 90%; }"#;
    let sheet = StyleSheet::parse(css);
    let style = sheet.rules()[0].style();
    assert_eq!(style.fg, None);
    assert_eq!(style.fg_auto.map(|auto| auto.alpha_percent), Some(90));
}

#[test]
fn dce12_fg_auto_percent_parses_to_fg_auto() {
    let css = r#"Label { fg: auto 50%; }"#;
    let sheet = StyleSheet::parse(css);
    let style = sheet.rules()[0].style();
    assert_eq!(style.fg, None);
    assert_eq!(style.fg_auto.map(|auto| auto.alpha_percent), Some(50));
}

#[test]
fn dce11_tint_applies_to_rendered_foreground_and_background() {
    let css = r#"
Label {
    color: #ff0000;
    background: #00ff00;
    tint: #000000 100%;
}
"#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let console = Console::new();
    let mut options = console.options().clone();
    options.size = (1, 1);
    options.max_width = 1;
    options.max_height = 1;

    let label = Label::new("x");
    let renderable = WidgetRenderable::new(&label);
    let buf = FrameBuffer::from_renderable(&console, &options, &renderable, None);
    let cell = buf.get(0, 0);
    let style = cell.style.expect("style should be present");

    assert_eq!(style.color, Some(Color::rgb(0, 0, 0).to_simple_opaque()));
    assert_eq!(style.bgcolor, Some(Color::rgb(0, 0, 0).to_simple_opaque()));
}

#[test]
fn p2g32_link_render_applies_css_color() {
    let css = r#"Link { link-color: #ff0000; }"#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let link = Link::new("hello").with_url("https://example.com");
    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 1);
    opts.max_width = 10;
    opts.max_height = 1;

    let segments = Widget::render(&link, &console, &opts);
    let first = segments
        .iter()
        .find(|s| s.control.is_none())
        .expect("text segment");
    let style = first.style.expect("style should be set");
    let red = Color::parse("#ff0000").unwrap();
    assert_eq!(
        style.color,
        Some(red.to_simple_opaque()),
        "link should render with CSS link-color"
    );
}

#[test]
fn p2g32_link_hover_applies_hover_css() {
    let css = r#"
        Link {
            link-color: #aaaaaa;
            link-color-hover: #ff0000;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut link = Link::new("hello").with_url("https://example.com");
    link.set_hovered(true);

    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 1);
    opts.max_width = 10;
    opts.max_height = 1;

    let segments = Widget::render(&link, &console, &opts);
    let first = segments
        .iter()
        .find(|s| s.control.is_none())
        .expect("text segment");
    let style = first.style.expect("style should be set");
    let red = Color::parse("#ff0000").unwrap();
    assert_eq!(
        style.color,
        Some(red.to_simple_opaque()),
        "hovered link should use link-color-hover"
    );
}

#[test]
fn p2g32_link_normal_does_not_use_hover_css() {
    let css = r#"
        Link {
            link-color: #aaaaaa;
            link-color-hover: #ff0000;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let link = Link::new("hello").with_url("https://example.com");
    // NOT hovered.
    assert!(!link.is_hovered());

    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 1);
    opts.max_width = 10;
    opts.max_height = 1;

    let segments = Widget::render(&link, &console, &opts);
    let first = segments
        .iter()
        .find(|s| s.control.is_none())
        .expect("text segment");
    let style = first.style.expect("style should be set");
    let grey = Color::parse("#aaaaaa").unwrap();
    assert_eq!(
        style.color,
        Some(grey.to_simple_opaque()),
        "non-hovered link should use normal link-color, not hover"
    );
}

// ───────────────────────────────────────────────────────────────────────
// P2G-36  Per-property transitions
// ───────────────────────────────────────────────────────────────────────

#[test]
fn p2g36_transition_shorthand_parses_single_property() {
    // NOTE: parse-only — verifies CSS parser, not runtime transition behavior.
    let css = r#"ScrollView { transition: offset_y 500ms linear; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    let transitions = style
        .transitions
        .as_ref()
        .expect("transitions should be set");
    assert_eq!(transitions.len(), 1);
    assert_eq!(transitions[0].property, "offset_y");
    assert_eq!(transitions[0].duration, Duration::from_millis(500));
    assert_eq!(transitions[0].timing, TransitionTiming::Linear);
    assert_eq!(transitions[0].delay, Duration::ZERO);
}

#[test]
fn p2g36_transition_shorthand_parses_multi_property() {
    // NOTE: parse-only — verifies CSS parser, not runtime transition behavior.
    let css = r#"
        ScrollView {
            transition: opacity 300ms linear 100ms, background 200ms;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    let transitions = style
        .transitions
        .as_ref()
        .expect("transitions should be set");
    assert_eq!(
        transitions.len(),
        2,
        "should parse 2 per-property transitions"
    );

    // First: opacity 300ms linear 100ms
    assert_eq!(transitions[0].property, "opacity");
    assert_eq!(transitions[0].duration, Duration::from_millis(300));
    assert_eq!(transitions[0].timing, TransitionTiming::Linear);
    assert_eq!(transitions[0].delay, Duration::from_millis(100));

    // Second: background 200ms (defaults: linear timing, 0 delay)
    assert_eq!(transitions[1].property, "background");
    assert_eq!(transitions[1].duration, Duration::from_millis(200));
    assert_eq!(transitions[1].delay, Duration::ZERO);
}

#[test]
fn p2g36_transition_also_sets_generic_fields_from_first_item() {
    // NOTE: parse-only — verifies CSS parser, not runtime transition behavior.
    let css = r#"ScrollView { transition: offset_y 400ms in_out_cubic 50ms; }"#;
    let sheet = StyleSheet::parse(css);
    let style = &sheet.rules()[0].style();
    // Generic transition fields should match the first item.
    assert_eq!(style.transition_duration, Some(Duration::from_millis(400)));
    assert_eq!(style.transition_delay, Some(Duration::from_millis(50)));
    assert_eq!(style.transition_timing, Some(TransitionTiming::InOutCubic));
}

#[test]
fn p2g36_resolve_transition_for_specific_property() {
    let style = Style::new()
        .transition_duration(Duration::from_millis(1000))
        .transition_timing(TransitionTiming::OutCubic);
    let mut style = style;
    style.transitions = Some(vec![
        PropertyTransition {
            property: "opacity".to_string(),
            duration: Duration::from_millis(200),
            timing: TransitionTiming::Linear,
            delay: Duration::ZERO,
        },
        PropertyTransition {
            property: "background".to_string(),
            duration: Duration::from_millis(500),
            timing: TransitionTiming::InOutCubic,
            delay: Duration::from_millis(100),
        },
    ]);

    // "opacity" should use per-property transition.
    let (dur, del, ease) =
        textual::runtime::resolve_transition_for_property(&style, "opacity").unwrap();
    assert_eq!(dur, Duration::from_millis(200));
    assert_eq!(del, Duration::ZERO);
    assert_eq!(ease, AnimationEase::Linear);

    // "background" should use per-property transition.
    let (dur, del, ease) =
        textual::runtime::resolve_transition_for_property(&style, "background").unwrap();
    assert_eq!(dur, Duration::from_millis(500));
    assert_eq!(del, Duration::from_millis(100));
    assert_eq!(ease, AnimationEase::InOutCubic);

    // "unknown" should fall back to generic transition.
    let (dur, _del, ease) =
        textual::runtime::resolve_transition_for_property(&style, "unknown").unwrap();
    assert_eq!(dur, Duration::from_millis(1000));
    assert_eq!(ease, AnimationEase::OutCubic);
}

#[test]
fn p2g36_resolve_transition_all_wildcard() {
    let mut style = Style::new();
    style.transitions = Some(vec![PropertyTransition {
        property: "all".to_string(),
        duration: Duration::from_millis(300),
        timing: TransitionTiming::Linear,
        delay: Duration::from_millis(50),
    }]);

    // "all" matches any property name.
    let (dur, del, ease) =
        textual::runtime::resolve_transition_for_property(&style, "anything").unwrap();
    assert_eq!(dur, Duration::from_millis(300));
    assert_eq!(del, Duration::from_millis(50));
    assert_eq!(ease, AnimationEase::Linear);
}

#[test]
fn p2g36_resolve_transition_zero_duration_returns_none() {
    let mut style = Style::new();
    style.transitions = Some(vec![PropertyTransition {
        property: "opacity".to_string(),
        duration: Duration::ZERO,
        timing: TransitionTiming::Linear,
        delay: Duration::ZERO,
    }]);

    assert!(
        textual::runtime::resolve_transition_for_property(&style, "opacity").is_none(),
        "zero-duration transition should return None"
    );
}

#[test]
fn p2g36_resolve_transition_no_transitions_no_generic() {
    let style = Style::new();
    assert!(
        textual::runtime::resolve_transition_for_property(&style, "anything").is_none(),
        "empty style should return None"
    );
}

// ───────────────────────────────────────────────────────────────────────
// DCE-08 / DCE-09  text-style negation + token refs
// ───────────────────────────────────────────────────────────────────────

#[test]
fn dce08_text_style_not_examples_parse() {
    let css = r#"
        Label.a { text-style: not reverse; }
        Label.b { text-style: bold not underline; }
        Label.c { text-style: bold italic not dim; }
    "#;
    let sheet = StyleSheet::parse(css);
    let rules = sheet.rules();
    assert_eq!(rules.len(), 3);

    let style_a = &rules[0].style();
    assert_eq!(style_a.reverse, Some(false));

    let style_b = &rules[1].style();
    assert_eq!(style_b.bold, Some(true));
    assert_eq!(style_b.underline, Some(false));

    let style_c = &rules[2].style();
    assert_eq!(style_c.bold, Some(true));
    assert_eq!(style_c.italic, Some(true));
    assert_eq!(style_c.dim, Some(false));
}

#[test]
fn dce09_text_style_token_refs_parse() {
    let css = r#"
        Label.a { text-style: $button-focus-text-style; }
        Label.b { text-style: $block-cursor-text-style; }
        Label.c { text-style: $block-cursor-blurred-text-style; }
        Label.d { text-style: $input-cursor-text-style; }
    "#;
    let sheet = StyleSheet::parse(css);
    let rules = sheet.rules();
    assert_eq!(rules.len(), 2);

    let style_a = &rules[0].style();
    assert_eq!(style_a.bold, Some(true));
    assert_eq!(style_a.reverse, Some(true));

    let style_b = &rules[1].style();
    assert_eq!(style_b.bold, Some(true));
}

// ───────────────────────────────────────────────────────────────────────
// P2-32 behavioral: disabled link ignores hover styling
// ───────────────────────────────────────────────────────────────────────

#[test]
fn p2_32_disabled_link_ignores_hover_style() {
    // A disabled link that is also hovered should use normal link-color,
    // NOT link-color-hover. This matches Python Textual behavior.
    let css = r#"
        Link {
            link-color: #aaaaaa;
            link-color-hover: #ff0000;
        }
    "#;
    let sheet = StyleSheet::parse(css);
    let _guard = set_style_context(sheet);

    let mut link = Link::new("hello")
        .with_url("https://example.com")
        .with_disabled(true);
    link.set_hovered(true);

    let console = Console::new();
    let mut opts = console.options().clone();
    opts.size = (10, 1);
    opts.max_width = 10;
    opts.max_height = 1;

    let segments = Widget::render(&link, &console, &opts);
    let first = segments
        .iter()
        .find(|s| s.control.is_none())
        .expect("text segment");
    let style = first.style.expect("style should be set");
    let grey = Color::parse("#aaaaaa").unwrap();
    let red = Color::parse("#ff0000").unwrap();
    assert_eq!(
        style.color,
        Some(grey.to_simple_opaque()),
        "disabled+hovered link should use normal link-color, not hover"
    );
    assert_ne!(
        style.color,
        Some(red.to_simple_opaque()),
        "disabled link should NOT use link-color-hover"
    );
}