| 1 | use std::time::Duration; |
| 2 | |
| 3 | use codewhale_ratatui::{ |
| 4 | CountBar, InstrumentSurface, KeyHint, MotionMode, Ombre, Paint, Role, Segmented, |
| 5 | SegmentedState, TranscriptScrollFacts, TranscriptViewport, WaterPalette, |
| 6 | ocean::OceanRamp, |
| 7 | testing::{Profile, render, text}, |
| 8 | }; |
| 9 | use ratatui::{buffer::Buffer, layout::Rect, style::Modifier, text::Line}; |
| 10 | |
| 11 | #[test] |
| 12 | fn long_action_hints_wrap_without_losing_the_label() { |
| 13 | let theme = Profile::DarkTrue.theme(); |
| 14 | let surface = InstrumentSurface::new("Actions") |
| 15 | .actions(vec![KeyHint::new("Enter", "Open selected session")]); |
| 16 | let area = Rect::new(0, 0, 18, 12); |
| 17 | let layout = surface.areas(area, &theme); |
| 18 | assert!(layout.footer.height > 1); |
| 19 | let buf = render(18, 12, |area, buf| surface.paint(area, buf, &theme)); |
| 20 | let shown = text(&buf).split_whitespace().collect::<Vec<_>>().join(" "); |
| 21 | assert!( |
| 22 | shown.replace(' ', "").contains("Openselectedsession"), |
| 23 | "{shown}" |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn progress_uses_fractional_cells_only_when_unicode_is_available() { |
| 29 | let bar = CountBar::new(1, 7); |
| 30 | let unicode = render(60, 1, |area, buf| { |
| 31 | bar.paint(area, buf, &Profile::DarkTrue.theme()) |
| 32 | }); |
| 33 | let shown = text(&unicode); |
| 34 | assert!(shown.chars().any(|c| "▏▎▍▌▋▊▉".contains(c)), "{shown}"); |
| 35 | let ascii = render(60, 1, |area, buf| { |
| 36 | bar.paint(area, buf, &Profile::Ascii.theme()) |
| 37 | }); |
| 38 | assert!(text(&ascii).is_ascii()); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn selected_segment_covers_brackets_and_narrow_focus_is_visible() { |
| 43 | let theme = Profile::DarkTrue.theme(); |
| 44 | let control = Segmented::new(["First", "Second"], SegmentedState::new(0)).focused(true); |
| 45 | let wide = render(30, 1, |area, buf| control.paint(area, buf, &theme)); |
| 46 | for x in 0..7 { |
| 47 | assert_eq!(wide[(x, 0)].bg, theme.bg(Role::Selected).bg.unwrap()); |
| 48 | } |
| 49 | let narrow = render(4, 1, |area, buf| control.paint(area, buf, &theme)); |
| 50 | assert!(narrow[(0, 0)].modifier.contains(Modifier::UNDERLINED)); |
| 51 | assert_eq!(narrow[(0, 0)].fg, theme.fg(Role::Accent).fg.unwrap()); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn fitting_transcript_has_no_scrollbar_and_final_position_reaches_bottom() { |
| 56 | let rows = vec![Line::from("row"); 5]; |
| 57 | let theme = Profile::DarkTrue.theme(); |
| 58 | let mut viewport = TranscriptViewport::new(&rows); |
| 59 | viewport.scrollbar = Some(TranscriptScrollFacts { |
| 60 | top: 0, |
| 61 | visible: 5, |
| 62 | total: 5, |
| 63 | }); |
| 64 | let fits = render(20, 5, |area, buf| viewport.paint(area, buf, &theme)); |
| 65 | assert!((0..5).all(|y| fits[(19, y)].symbol() == " ")); |
| 66 | viewport.scrollbar = Some(TranscriptScrollFacts { |
| 67 | top: 0, |
| 68 | visible: 5, |
| 69 | total: 6, |
| 70 | }); |
| 71 | let start = render(20, 5, |area, buf| viewport.paint(area, buf, &theme)); |
| 72 | assert_eq!(start[(19, 0)].symbol(), "┃"); |
| 73 | assert_eq!(start[(19, 4)].symbol(), "│"); |
| 74 | viewport.scrollbar.as_mut().unwrap().top = 1; |
| 75 | let end = render(20, 5, |area, buf| viewport.paint(area, buf, &theme)); |
| 76 | assert_eq!(end[(19, 0)].symbol(), "│"); |
| 77 | assert_eq!(end[(19, 4)].symbol(), "┃"); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn ocean_supports_measured_rgb_themes_and_preserves_fallbacks() { |
| 82 | for profile in [Profile::DarkTrue, Profile::DarkGraphite, Profile::LightTrue] { |
| 83 | assert!( |
| 84 | OceanRamp::for_theme(&profile.theme()).is_some(), |
| 85 | "{profile:?}" |
| 86 | ); |
| 87 | } |
| 88 | for profile in [ |
| 89 | Profile::Dark256, |
| 90 | Profile::Light256, |
| 91 | Profile::Ansi16, |
| 92 | Profile::UnknownGround, |
| 93 | Profile::NoColor, |
| 94 | Profile::Ascii, |
| 95 | ] { |
| 96 | assert!( |
| 97 | OceanRamp::for_theme(&profile.theme()).is_none(), |
| 98 | "{profile:?}" |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn water_drift_keeps_reduced_motion_static_and_repeats_the_caller_clock() { |
| 105 | let theme = Profile::DarkTrue.theme(); |
| 106 | let area = Rect::new(0, 0, 40, 10); |
| 107 | let mut source = Buffer::empty(area); |
| 108 | source.set_style(area, theme.bg(Role::Background)); |
| 109 | let pass = Ombre::new(WaterPalette::Lagoon); |
| 110 | let mut still = source.clone(); |
| 111 | pass.apply(area, &mut still, &theme); |
| 112 | for mode in [MotionMode::Reduced, MotionMode::Still] { |
| 113 | let mut frame = source.clone(); |
| 114 | pass.apply_at(area, &mut frame, &theme, Duration::from_secs(6), mode); |
| 115 | assert_eq!(frame, still); |
| 116 | } |
| 117 | let mut moving = source.clone(); |
| 118 | pass.apply_at( |
| 119 | area, |
| 120 | &mut moving, |
| 121 | &theme, |
| 122 | Duration::from_secs(6), |
| 123 | MotionMode::Full, |
| 124 | ); |
| 125 | assert_ne!(moving, still); |
| 126 | let mut repeated = source; |
| 127 | pass.apply_at( |
| 128 | area, |
| 129 | &mut repeated, |
| 130 | &theme, |
| 131 | Duration::from_secs(30), |
| 132 | MotionMode::Full, |
| 133 | ); |
| 134 | assert_eq!(moving, repeated); |
| 135 | assert_eq!(moving[(0, 0)], still[(0, 0)]); |
| 136 | assert_eq!(moving[(39, 9)], still[(39, 9)]); |
| 137 | } |
| 138 |