| 1 | //! Acceptance of the mounted composer. |
| 2 | use super::*; |
| 3 | use codewhale_runtime::config::Config; |
| 4 | use std::path::PathBuf; |
| 5 | |
| 6 | fn app() -> App { |
| 7 | let mut app = App::new( |
| 8 | crate::tui::test_support::test_tui_options(PathBuf::from(".")), |
| 9 | &Config::default(), |
| 10 | ); |
| 11 | app.launch.visible = false; |
| 12 | app.composer.vim_enabled = false; |
| 13 | app.ui_locale = Locale::En; |
| 14 | app.theme_id = palette::ThemeId::Underwater; |
| 15 | app.ui_theme = palette::UNDERWATER_UI_THEME; |
| 16 | app |
| 17 | } |
| 18 | #[test] |
| 19 | fn mounted_kit_menu_hitboxes_match_plan_and_stay_inside_the_composer() { |
| 20 | for &locale in Locale::shipped() { |
| 21 | for case in 0..8 { |
| 22 | let mut app = app(); |
| 23 | app.ui_locale = locale; |
| 24 | app.input = match case { |
| 25 | 0 => "", |
| 26 | 1 => "hello", |
| 27 | 2 => "a CJK 鲸鱼 draft with cafe\u{0301} and a long https://example.com/path", |
| 28 | 3 => "1\u{fe0f}\u{20e3} 👩\u{200d}💻\n\nlast row", |
| 29 | 4 => "select these words across rows", |
| 30 | 5 => "/test", |
| 31 | 6 => "@builder", |
| 32 | _ => "", |
| 33 | } |
| 34 | .into(); |
| 35 | app.cursor_position = app.input.chars().count(); |
| 36 | if case == 4 { |
| 37 | app.selection_anchor = Some(2); |
| 38 | } |
| 39 | if case == 7 { |
| 40 | app.start_history_search(); |
| 41 | } |
| 42 | let slash = if case == 5 { |
| 43 | vec![ |
| 44 | SlashMenuEntry { |
| 45 | name: "/test".into(), |
| 46 | description: "Run checks for this project".into(), |
| 47 | is_skill: false, |
| 48 | alias_hint: Some("jiancha".into()), |
| 49 | }, |
| 50 | SlashMenuEntry { |
| 51 | name: "/review".into(), |
| 52 | description: "Review every changed source file".into(), |
| 53 | is_skill: true, |
| 54 | alias_hint: None, |
| 55 | }, |
| 56 | ] |
| 57 | } else { |
| 58 | Vec::new() |
| 59 | }; |
| 60 | let mentions = if case == 6 { |
| 61 | vec!["builder".into(), "reviewer 鲸鱼".into()] |
| 62 | } else { |
| 63 | Vec::new() |
| 64 | }; |
| 65 | for density in [ |
| 66 | ComposerDensity::Compact, |
| 67 | ComposerDensity::Comfortable, |
| 68 | ComposerDensity::Spacious, |
| 69 | ] { |
| 70 | app.composer_density = density; |
| 71 | for enclosed in [false, true] { |
| 72 | app.composer_border = enclosed; |
| 73 | for width in [20, 40, 80, 120] { |
| 74 | for height in [3, 5, 8, 12] { |
| 75 | let area = Rect::new(7, 5, width, height); |
| 76 | let canvas = Rect::new(2, 3, width + 12, height + 8); |
| 77 | let mut actual = Buffer::empty(canvas); |
| 78 | for cell in &mut actual.content { |
| 79 | cell.set_symbol("~") |
| 80 | .set_style(Style::default().bg(Color::Rgb(11, 23, 37))); |
| 81 | } |
| 82 | let widget = ComposerWidget::new(&app, 12, &slash, &mentions); |
| 83 | widget.render(area, &mut actual); |
| 84 | let plan = widget.plan(area); |
| 85 | let boxes = app.viewport.last_slash_menu_hitboxes.borrow().clone(); |
| 86 | assert_eq!(boxes, plan.menu_rects); |
| 87 | for (_, rect) in boxes { |
| 88 | assert_eq!(rect.intersection(plan.geometry.inner), rect); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | #[test] |
| 98 | fn mounted_home_composer_keeps_native_input_inside_its_tonal_lip() { |
| 99 | use codewhale_ratatui::color::ColorDepth::{Ansi16, Ansi256, Monochrome, TrueColor}; |
| 100 | use palette::ThemeId::{Shoreline, Underwater}; |
| 101 | for (width, height, theme, depth, ascii, draft, tonal) in [ |
| 102 | (106, 39, Underwater, TrueColor, false, "", true), |
| 103 | (106, 39, Underwater, TrueColor, false, "draft", false), |
| 104 | (120, 35, Underwater, Ansi256, false, "", false), |
| 105 | (64, 20, Underwater, Ansi16, false, "", false), |
| 106 | (64, 20, Underwater, TrueColor, true, "", false), |
| 107 | (40, 12, Underwater, Monochrome, false, "", false), |
| 108 | (80, 24, Shoreline, TrueColor, false, "", false), |
| 109 | ] { |
| 110 | let area = Rect::new(0, height - 6, width, 4); |
| 111 | let mut app = app(); |
| 112 | app.theme_id = theme; |
| 113 | app.ui_theme = theme.ui_theme(); |
| 114 | app.launch.visible = draft.is_empty(); |
| 115 | app.composer_border = true; |
| 116 | app.input = draft.into(); |
| 117 | app.cursor_position = draft.len(); |
| 118 | app.viewport.ocean_caps = Some(codewhale_ratatui::Caps { |
| 119 | depth, |
| 120 | ascii, |
| 121 | ..test_native_ocean_caps() |
| 122 | }); |
| 123 | let widget = ComposerWidget::new(&app, 5, &[], &[]); |
| 124 | let mut actual = Buffer::empty(area); |
| 125 | let plan = widget.render_plan(area, &mut actual); |
| 126 | let mut native = Buffer::empty(area); |
| 127 | plan.paint(&mut native); |
| 128 | let inner = plan.geometry.inner; |
| 129 | for y in inner.top()..inner.bottom() { |
| 130 | for x in inner.left()..inner.right() { |
| 131 | assert_eq!(actual[(x, y)], native[(x, y)]); |
| 132 | } |
| 133 | } |
| 134 | let left = area.left(); |
| 135 | let top = area.top(); |
| 136 | let right = area.right() - 1; |
| 137 | let bottom = area.bottom() - 1; |
| 138 | if tonal { |
| 139 | assert_eq!(actual[(left + 1, top)].symbol(), "▄"); |
| 140 | assert_ne!(actual[(left + 1, top)].fg, actual[(right - 1, top)].fg); |
| 141 | assert_eq!(actual[(left + 1, bottom)].symbol(), "▀"); |
| 142 | for y in top + 1..bottom { |
| 143 | assert_eq!(actual[(left, y)].symbol(), " "); |
| 144 | assert_eq!(actual[(right, y)].symbol(), " "); |
| 145 | } |
| 146 | assert!(active_composer_submit_rect(&app, area).is_none()); |
| 147 | } else { |
| 148 | let waterline = theme == Underwater && !ascii && matches!(depth, TrueColor | Ansi256); |
| 149 | for x in [left + 1, right - 1] { |
| 150 | assert_eq!(actual[(x, top)].symbol(), native[(x, top)].symbol()); |
| 151 | assert_eq!(actual[(x, top)].fg != native[(x, top)].fg, waterline); |
| 152 | } |
| 153 | for cell in [(left, bottom), (right, bottom), (left, top + 1)] { |
| 154 | assert_eq!(actual[cell], native[cell]); |
| 155 | } |
| 156 | if !draft.is_empty() { |
| 157 | let submit = active_composer_submit_rect(&app, area).unwrap(); |
| 158 | for x in submit.left()..submit.right() { |
| 159 | assert_eq!(actual[(x, submit.y)], native[(x, submit.y)]); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn mounted_kit_live_custom_theme_slots_remain_exact() { |
| 168 | let mut app = app(); |
| 169 | app.input = "select words".into(); |
| 170 | app.cursor_position = 6; |
| 171 | app.selection_anchor = Some(0); |
| 172 | app.composer_border = true; |
| 173 | app.ui_theme.composer_bg = Color::Rgb(9, 21, 33); |
| 174 | app.ui_theme.accent_primary = Color::Rgb(41, 52, 63); |
| 175 | app.ui_theme.selection_bg = Color::Rgb(71, 82, 93); |
| 176 | app.ui_theme.info = Color::Rgb(101, 112, 123); |
| 177 | let widget = ComposerWidget::new(&app, 9, &[], &[]); |
| 178 | let area = Rect::new(7, 5, 80, 7); |
| 179 | let mut buf = Buffer::empty(area); |
| 180 | widget.render(area, &mut buf); |
| 181 | let plan = widget.plan(area); |
| 182 | assert_eq!(buf[(area.x, area.y)].fg, app.ui_theme.border); |
| 183 | assert_eq!( |
| 184 | buf[(plan.geometry.prompt_x.unwrap(), plan.cursor.unwrap().y)].fg, |
| 185 | app.ui_theme.accent_primary |
| 186 | ); |
| 187 | assert_eq!( |
| 188 | buf[(plan.geometry.text.x, plan.cursor.unwrap().y)].bg, |
| 189 | app.ui_theme.selection_bg |
| 190 | ); |
| 191 | let submit = plan.geometry.submit.unwrap(); |
| 192 | assert_eq!( |
| 193 | buf[(submit.x, submit.y)].fg, |
| 194 | palette::chrome_style(&app.ui_theme, palette::ChromeInk::Info) |
| 195 | .fg |
| 196 | .unwrap() |
| 197 | ); |
| 198 | for y in area.y..area.bottom() { |
| 199 | for x in area.x..area.right() { |
| 200 | assert_eq!( |
| 201 | buf[(x, y)].bg, |
| 202 | if x >= plan.geometry.text.x |
| 203 | && x < plan.cursor.unwrap().x |
| 204 | && y == plan.cursor.unwrap().y |
| 205 | { |
| 206 | app.ui_theme.selection_bg |
| 207 | } else { |
| 208 | app.ui_theme.composer_bg |
| 209 | } |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | #[test] |
| 215 | fn mounted_kit_shared_centered_padding_drives_source_pointer_projection() { |
| 216 | let mut app = app(); |
| 217 | app.composer_border = true; |
| 218 | app.input = "hello".into(); |
| 219 | app.cursor_position = 2; |
| 220 | let widget = ComposerWidget::new(&app, 9, &[], &[]); |
| 221 | let plan = widget.plan(Rect::new(7, 5, 40, 7)); |
| 222 | assert_eq!(plan.top_padding, 2); |
| 223 | let cursor = plan.cursor.unwrap(); |
| 224 | let index = codewhale_ratatui::native_composer_source_at( |
| 225 | &app.input, |
| 226 | usize::from(plan.geometry.text.width), |
| 227 | usize::from(cursor.x - plan.geometry.text.x), |
| 228 | usize::from(cursor.y - plan.geometry.text.y), |
| 229 | plan.scroll_offset, |
| 230 | plan.top_padding, |
| 231 | ); |
| 232 | assert_eq!( |
| 233 | index, app.cursor_position, |
| 234 | "viewport must retain the painter's centered padding, not bottom padding" |
| 235 | ); |
| 236 | app.input = "a\t\u{202e}中b".into(); |
| 237 | app.cursor_position = 4; |
| 238 | let plan = ComposerWidget::new(&app, 9, &[], &[]).plan(Rect::new(7, 5, 40, 7)); |
| 239 | let cursor = plan.cursor.unwrap(); |
| 240 | assert_eq!( |
| 241 | codewhale_ratatui::native_composer_source_at( |
| 242 | &app.input, |
| 243 | usize::from(plan.geometry.text.width), |
| 244 | usize::from(cursor.x - plan.geometry.text.x), |
| 245 | usize::from(cursor.y - plan.geometry.text.y), |
| 246 | plan.scroll_offset, |
| 247 | plan.top_padding |
| 248 | ), |
| 249 | 4 |
| 250 | ); |
| 251 | assert_eq!(app.input, "a\t\u{202e}中b"); |
| 252 | } |
| 253 | #[test] |
| 254 | fn mounted_kit_resize_to_empty_withdraws_slash_targets() { |
| 255 | let mut app = app(); |
| 256 | app.input = "/test".into(); |
| 257 | app.cursor_position = 5; |
| 258 | let slash = vec![SlashMenuEntry { |
| 259 | name: "/test".into(), |
| 260 | description: "test project".into(), |
| 261 | is_skill: false, |
| 262 | alias_hint: None, |
| 263 | }]; |
| 264 | let widget = ComposerWidget::new(&app, 9, &slash, &[]); |
| 265 | let area = Rect::new(7, 5, 80, 9); |
| 266 | let mut buf = Buffer::empty(area); |
| 267 | widget.render(area, &mut buf); |
| 268 | assert!(!app.viewport.last_slash_menu_hitboxes.borrow().is_empty()); |
| 269 | for zero in [Rect::new(7, 5, 0, 9), Rect::new(7, 5, 80, 0)] { |
| 270 | widget.render(zero, &mut buf); |
| 271 | assert!(app.viewport.last_slash_menu_hitboxes.borrow().is_empty()); |
| 272 | assert!(widget.cursor_pos(zero).is_none()); |
| 273 | assert!(active_composer_submit_rect(&app, zero).is_none()); |
| 274 | } |
| 275 | } |
| 276 |