| 1 | use super::*; |
| 2 | use crate::tui::color_compat::ColorCompatBackend; |
| 3 | use codewhale_ratatui::ocean::ocean_semantic_surfaces; |
| 4 | use ratatui::{ |
| 5 | style::{Modifier, Style}, |
| 6 | text::{Line, Span}, |
| 7 | }; |
| 8 | |
| 9 | fn caps(depth: codewhale_palette::ColorDepth) -> codewhale_ratatui::Caps { |
| 10 | ColorCompatBackend::new(std::io::sink(), depth, codewhale_palette::PaletteMode::Dark) |
| 11 | .native_ocean_caps() |
| 12 | } |
| 13 | fn column(area: Rect, phase: ShellPhase, elapsed: u128) -> OceanColumn { |
| 14 | OceanColumn::new( |
| 15 | OceanRamp::for_theme(&codewhale_palette::UNDERWATER_UI_THEME).unwrap(), |
| 16 | area, |
| 17 | elapsed, |
| 18 | None, |
| 19 | phase, |
| 20 | true, |
| 21 | 750, |
| 22 | 30, |
| 23 | ) |
| 24 | .with_paint_caps(Some(caps(codewhale_palette::ColorDepth::TrueColor))) |
| 25 | } |
| 26 | fn ordinary(area: Rect) -> Buffer { |
| 27 | let mut buf = Buffer::empty(area); |
| 28 | for cell in &mut buf.content { |
| 29 | cell.set_bg(codewhale_palette::UNDERWATER_UI_THEME.surface_bg) |
| 30 | .set_fg(Color::Rgb(255, 255, 255)); |
| 31 | } |
| 32 | buf |
| 33 | } |
| 34 | #[test] |
| 35 | fn guarded_ocean_backend_depth_is_forwarded_and_explicit_pane_supplies_ground_evidence() { |
| 36 | use codewhale_ratatui::{color::ColorDepth as Depth, detect::Appearance}; |
| 37 | for (host, native) in [ |
| 38 | (codewhale_palette::ColorDepth::TrueColor, Depth::TrueColor), |
| 39 | (codewhale_palette::ColorDepth::Ansi256, Depth::Ansi256), |
| 40 | (codewhale_palette::ColorDepth::Ansi16, Depth::Ansi16), |
| 41 | (codewhale_palette::ColorDepth::Monochrome, Depth::Monochrome), |
| 42 | ] { |
| 43 | let reported = caps(host); |
| 44 | assert_eq!(reported.depth, native); |
| 45 | assert_eq!( |
| 46 | reported.appearance, |
| 47 | Appearance::Unknown, |
| 48 | "OS-dark mode is not measured ground" |
| 49 | ); |
| 50 | let area = Rect::new(7, 5, 12, 4); |
| 51 | let mut buf = ordinary(area); |
| 52 | let before = buf.clone(); |
| 53 | let column = column(area, ShellPhase::Idle, 0).with_paint_caps(Some(reported)); |
| 54 | column.paint_matching_native( |
| 55 | area, |
| 56 | &mut buf, |
| 57 | codewhale_palette::UNDERWATER_UI_THEME.surface_bg, |
| 58 | &codewhale_palette::UNDERWATER_UI_THEME, |
| 59 | &[], |
| 60 | ); |
| 61 | if native == Depth::TrueColor { |
| 62 | assert_ne!(buf, before, "actual dark pane supplies its own evidence"); |
| 63 | } else { |
| 64 | assert_eq!( |
| 65 | buf, before, |
| 66 | "fallback retains the selected flat pane ground" |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | let area = Rect::new(7, 5, 12, 4); |
| 71 | let col = column(area, ShellPhase::Idle, 0); |
| 72 | for ground in [Color::Reset, Color::White, Color::Rgb(250, 250, 250)] { |
| 73 | let mut buf = ordinary(area); |
| 74 | buf.set_style(area, Style::default().bg(ground)); |
| 75 | let before = buf.clone(); |
| 76 | col.paint_matching_native( |
| 77 | area, |
| 78 | &mut buf, |
| 79 | ground, |
| 80 | &codewhale_palette::UNDERWATER_UI_THEME, |
| 81 | &[], |
| 82 | ); |
| 83 | assert_eq!( |
| 84 | buf, before, |
| 85 | "unknown or known-light explicit grounds cannot earn a dark field" |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | #[test] |
| 90 | fn guarded_ocean_semantic_alias_reverse_unknown_ink_and_style_facts_survive_final_shell_pass() { |
| 91 | let theme = codewhale_palette::UNDERWATER_UI_THEME; |
| 92 | let area = Rect::new(7, 5, 20, 4); |
| 93 | let col = column(area, ShellPhase::Idle, 0); |
| 94 | let rows = vec![Line::from(vec![ |
| 95 | Span::raw("plain "), |
| 96 | Span::styled( |
| 97 | "selected", |
| 98 | Style::default() |
| 99 | .bg(theme.surface_bg) |
| 100 | .add_modifier(Modifier::ITALIC), |
| 101 | ), |
| 102 | ])]; |
| 103 | let protected = ocean_semantic_surfaces( |
| 104 | &rows, |
| 105 | area, |
| 106 | codewhale_tui_kit::ui_text::grapheme_display_width, |
| 107 | ); |
| 108 | assert_eq!(protected, vec![Rect::new(13, 5, 8, 1)]); |
| 109 | let mut buf = ordinary(area); |
| 110 | buf[(7, 6)].set_symbol("x").set_fg(Color::Reset); |
| 111 | buf[(8, 6)].set_symbol("x").set_fg(Color::Magenta); |
| 112 | buf[(9, 6)].set_symbol("x").set_fg(col.color_at_y(6)); |
| 113 | buf[(10, 6)].set_bg(theme.selection_bg); |
| 114 | buf[(11, 6)] |
| 115 | .modifier |
| 116 | .insert(Modifier::REVERSED | Modifier::BOLD); |
| 117 | let before = buf.clone(); |
| 118 | col.paint_matching_native(area, &mut buf, theme.surface_bg, &theme, &protected); |
| 119 | for x in [8, 10, 11] { |
| 120 | assert_eq!(buf[(x, 6)], before[(x, 6)]); |
| 121 | } |
| 122 | // The terminal backend lifts water-coloured glyphs to visible body ink; |
| 123 | // the guard projects that same adaptation before admitting the backdrop. |
| 124 | for x in [7, 9] { |
| 125 | assert_eq!(buf[(x, 6)].fg, before[(x, 6)].fg); |
| 126 | assert_eq!(buf[(x, 6)].modifier, before[(x, 6)].modifier); |
| 127 | assert_eq!(buf[(x, 6)].bg, col.color_at_y(6)); |
| 128 | } |
| 129 | for x in 13..21 { |
| 130 | assert_eq!( |
| 131 | buf[(x, 5)], |
| 132 | before[(x, 5)], |
| 133 | "explicit style aliases remain semantic" |
| 134 | ); |
| 135 | } |
| 136 | assert_eq!(buf[(7, 5)].bg, col.color_at_y(5)); |
| 137 | } |
| 138 | #[test] |
| 139 | fn guarded_ocean_uses_backend_projected_custom_ink_and_live_supporting_floor() { |
| 140 | let mut theme = codewhale_palette::UNDERWATER_UI_THEME; |
| 141 | theme.text_body = Color::Rgb(255, 255, 255); |
| 142 | theme.border = Color::Rgb(111, 149, 181); |
| 143 | let area = Rect::new(7, 5, 8, 4); |
| 144 | let col = column(area, ShellPhase::Idle, 0); |
| 145 | let mut buf = ordinary(area); |
| 146 | buf[(7, 5)] |
| 147 | .set_symbol("x") |
| 148 | .set_fg(codewhale_palette::TEXT_BODY); |
| 149 | buf[(8, 5)] |
| 150 | .set_symbol("|") |
| 151 | .set_fg(theme.border) |
| 152 | .modifier |
| 153 | .insert(Modifier::ITALIC); |
| 154 | let before = buf.clone(); |
| 155 | let projected = crate::tui::color_compat::project_ocean_ink( |
| 156 | &buf[(7, 5)], |
| 157 | col.color_at_y(5), |
| 158 | caps(codewhale_palette::ColorDepth::TrueColor).depth, |
| 159 | &theme, |
| 160 | ); |
| 161 | // Underwater retains its body token; custom supporting ink still follows |
| 162 | // the live theme and the backend's contrast projection. |
| 163 | assert_eq!(projected, codewhale_palette::TEXT_BODY); |
| 164 | col.paint_matching_native(area, &mut buf, theme.surface_bg, &theme, &[]); |
| 165 | assert_eq!(buf[(7, 5)].bg, col.color_at_y(5)); |
| 166 | assert_eq!( |
| 167 | buf[(7, 5)].fg, |
| 168 | before[(7, 5)].fg, |
| 169 | "guard never rewrites source ink" |
| 170 | ); |
| 171 | assert_eq!(buf[(8, 5)].modifier, before[(8, 5)].modifier); |
| 172 | assert_eq!(buf[(8, 5)].bg, col.color_at_y(5)); |
| 173 | } |
| 174 | #[test] |
| 175 | fn guarded_ocean_clip_cannot_modify_cells_outside_the_request() { |
| 176 | let area = Rect::new(7, 5, 80, 24); |
| 177 | let col = column(area, ShellPhase::Working, 480); |
| 178 | let mut buf = ordinary(area); |
| 179 | let before = buf.clone(); |
| 180 | let requested = Rect::new(9, 7, 8, 3); |
| 181 | col.paint_matching_native( |
| 182 | requested, |
| 183 | &mut buf, |
| 184 | codewhale_palette::UNDERWATER_UI_THEME.surface_bg, |
| 185 | &codewhale_palette::UNDERWATER_UI_THEME, |
| 186 | &[], |
| 187 | ); |
| 188 | for y in area.y..area.bottom() { |
| 189 | for x in area.x..area.right() { |
| 190 | if !requested.contains((x, y).into()) { |
| 191 | assert_eq!(buf[(x, y)], before[(x, y)]); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn guarded_ocean_volumetric_caustics_shimmer_open_water_and_preserve_text() { |
| 199 | let theme = codewhale_palette::UNDERWATER_UI_THEME; |
| 200 | let area = Rect::new(0, 0, 60, 16); |
| 201 | let mut animated_buf = ordinary(area); |
| 202 | animated_buf[(4, 2)].set_symbol("A").set_fg(theme.text_body); |
| 203 | let mut still_buf = animated_buf.clone(); |
| 204 | let protected = [Rect::new(20, 0, 10, 6)]; |
| 205 | |
| 206 | let animated = column(area, ShellPhase::Working, 3_200); |
| 207 | animated.paint_matching_native( |
| 208 | area, |
| 209 | &mut animated_buf, |
| 210 | theme.surface_bg, |
| 211 | &theme, |
| 212 | &protected, |
| 213 | ); |
| 214 | |
| 215 | let still = OceanColumn::new( |
| 216 | OceanRamp::for_theme(&theme).unwrap(), |
| 217 | area, |
| 218 | 3_200, |
| 219 | None, |
| 220 | ShellPhase::Working, |
| 221 | false, |
| 222 | 0, |
| 223 | 30, |
| 224 | ) |
| 225 | .with_paint_caps(Some(caps(codewhale_palette::ColorDepth::TrueColor))); |
| 226 | still.paint_matching_native(area, &mut still_buf, theme.surface_bg, &theme, &protected); |
| 227 | |
| 228 | let shimmered = (0..8) |
| 229 | .flat_map(|y| (0..60).map(move |x| (x, y))) |
| 230 | .filter(|&(x, y)| animated_buf[(x, y)].bg != animated.color_at_y(y)) |
| 231 | .count(); |
| 232 | assert!(shimmered > 0); |
| 233 | assert_eq!(animated_buf[(4, 2)].bg, animated.color_at_y(2)); |
| 234 | for y in 0..6 { |
| 235 | for x in 20..30 { |
| 236 | assert_eq!(animated_buf[(x, y)].bg, theme.surface_bg); |
| 237 | } |
| 238 | } |
| 239 | assert_eq!(still_buf[(1, 0)].bg, still.color_at_y(0)); |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn launch_light_reaches_shell_bands_at_both_color_depths_and_keeps_foreground() { |
| 244 | let area = Rect::new(7, 5, 102, 40); |
| 245 | let theme = codewhale_palette::UNDERWATER_UI_THEME; |
| 246 | let reserved = Rect::new(45, 20, 24, 3); |
| 247 | let mut source = ordinary(area); |
| 248 | source[(46, 15)].set_char('A').set_fg(theme.text_body); |
| 249 | source[(48, 18)] |
| 250 | .set_char('B') |
| 251 | .modifier |
| 252 | .insert(Modifier::REVERSED); |
| 253 | let dark = codewhale_ratatui::color::relative_luminance; |
| 254 | for depth in [ |
| 255 | codewhale_palette::ColorDepth::TrueColor, |
| 256 | codewhale_palette::ColorDepth::Ansi256, |
| 257 | ] { |
| 258 | let draw = |elapsed, clips: &[Rect]| { |
| 259 | let mut buf = source.clone(); |
| 260 | let column = column(area, ShellPhase::Idle, elapsed) |
| 261 | .with_paint_caps(Some(caps(depth))) |
| 262 | .with_directional_light(true); |
| 263 | for &clip in clips { |
| 264 | column.paint_matching_native(clip, &mut buf, theme.surface_bg, &theme, &[reserved]); |
| 265 | } |
| 266 | buf |
| 267 | }; |
| 268 | let whole = draw(0, &[area]); |
| 269 | let split = [Rect::new(7, 5, 102, 27), Rect::new(7, 32, 102, 13)]; |
| 270 | assert_eq!(whole, draw(9_000, &split)); |
| 271 | assert_eq!(whole[(48, 18)], source[(48, 18)]); |
| 272 | assert_eq!(whole[(52, 22)], source[(52, 22)]); |
| 273 | assert_eq!(whole[(46, 15)].symbol(), "A"); |
| 274 | assert_eq!(whole[(46, 15)].fg, theme.text_body); |
| 275 | |
| 276 | let sun = dark(whole[(86, 6)].bg).unwrap(); |
| 277 | let shade = dark(whole[(12, 6)].bg).unwrap(); |
| 278 | let floor = dark(whole[(86, 43)].bg).unwrap(); |
| 279 | assert!( |
| 280 | sun > 2.0 * shade && shade > floor, |
| 281 | "{depth:?}: {sun}, {shade}, {floor}" |
| 282 | ); |
| 283 | } |
| 284 | } |
| 285 |