| 1 | //! Host contracts around the shared native posture renderer. |
| 2 | |
| 3 | use codewhale_palette::{ChromeInk, UI_THEME, UiTheme}; |
| 4 | use ratatui::{ |
| 5 | buffer::Buffer, |
| 6 | layout::Rect, |
| 7 | style::{Color, Modifier, Style}, |
| 8 | }; |
| 9 | |
| 10 | use super::{TidelineFooter, render_tideline_footer}; |
| 11 | |
| 12 | fn custom_theme() -> UiTheme { |
| 13 | UiTheme { |
| 14 | status_working: Color::Rgb(1, 2, 3), |
| 15 | permission_ask: Color::Rgb(2, 3, 4), |
| 16 | permission_auto_review: Color::Rgb(3, 4, 5), |
| 17 | permission_full_access: Color::Rgb(4, 5, 6), |
| 18 | accent_action: Color::Rgb(5, 6, 7), |
| 19 | warning: Color::Rgb(6, 7, 8), |
| 20 | accent_primary: Color::Rgb(7, 8, 9), |
| 21 | text_soft: Color::Rgb(8, 9, 10), |
| 22 | text_muted: Color::Rgb(9, 10, 11), |
| 23 | text_hint: Color::Rgb(10, 11, 12), |
| 24 | text_dim: Color::Rgb(11, 12, 13), |
| 25 | error_fg: Color::Rgb(12, 13, 14), |
| 26 | ..UI_THEME |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn posture_retains_every_host_ink_including_the_pinned_right_fact() { |
| 32 | let theme = custom_theme(); |
| 33 | for ink in [ |
| 34 | ChromeInk::Outcome, |
| 35 | ChromeInk::PermissionAsk, |
| 36 | ChromeInk::PermissionAutoReview, |
| 37 | ChromeInk::PermissionFullAccess, |
| 38 | ChromeInk::Waiting, |
| 39 | ChromeInk::Attention, |
| 40 | ChromeInk::Active, |
| 41 | ChromeInk::PolicyAct, |
| 42 | ChromeInk::PolicyPlan, |
| 43 | ChromeInk::PolicyOperate, |
| 44 | ChromeInk::Identity, |
| 45 | ChromeInk::Info, |
| 46 | ChromeInk::MetadataValue, |
| 47 | ChromeInk::Metadata, |
| 48 | ChromeInk::MetadataHint, |
| 49 | ChromeInk::MetadataDim, |
| 50 | ChromeInk::Failure, |
| 51 | ] { |
| 52 | let counts = [("C (Ctrl+])".into(), ink)]; |
| 53 | let footer = TidelineFooter::new(&theme, ("P", ink)) |
| 54 | .permission_key(Some("K")) |
| 55 | .turn_clock(Some(("W", ink))) |
| 56 | .counts(&counts) |
| 57 | .session_clock(Some(("S", ink))) |
| 58 | .hint(Some(("H", ink))) |
| 59 | .right(Some(("R", ink))); |
| 60 | let area = Rect::new(3, 2, 100, 2); |
| 61 | let mut buf = Buffer::empty(Rect::new(0, 0, 110, 5)); |
| 62 | buf.set_style( |
| 63 | buf.area, |
| 64 | Style::default() |
| 65 | .fg(crate::tui::infoline::source_theme(false) |
| 66 | .color(codewhale_ratatui::Role::Accent) |
| 67 | .unwrap()) |
| 68 | .bg(Color::Rgb(30, 40, 50)) |
| 69 | .add_modifier(Modifier::ITALIC), |
| 70 | ); |
| 71 | let before = buf.clone(); |
| 72 | let targets = render_tideline_footer(area, &mut buf, &footer); |
| 73 | let column = |symbol: &str| { |
| 74 | (area.left()..area.right()) |
| 75 | .find(|x| buf[(*x, area.y)].symbol() == symbol) |
| 76 | .unwrap_or_else(|| panic!("{ink:?}: {symbol} did not paint")) |
| 77 | }; |
| 78 | let expected = match ink { |
| 79 | ChromeInk::MetadataValue => theme.text_body, |
| 80 | ChromeInk::Metadata => theme.text_soft, |
| 81 | ChromeInk::MetadataHint => theme.text_muted, |
| 82 | _ => ink.color(&theme), |
| 83 | }; |
| 84 | for symbol in ["P", "W", "C", "S", "H", "R"] { |
| 85 | let cell = &buf[(column(symbol), area.y)]; |
| 86 | assert_eq!( |
| 87 | cell.fg, |
| 88 | if symbol == "P" { |
| 89 | theme.text_body |
| 90 | } else { |
| 91 | expected |
| 92 | }, |
| 93 | "{ink:?}: {symbol}" |
| 94 | ); |
| 95 | assert!(!cell.modifier.contains(Modifier::REVERSED)); |
| 96 | assert_eq!(cell.bg, Color::Rgb(30, 40, 50)); |
| 97 | assert!(cell.modifier.contains(Modifier::ITALIC)); |
| 98 | assert_eq!( |
| 99 | cell.modifier.contains(Modifier::BOLD), |
| 100 | matches!(symbol, "P" | "W") |
| 101 | ); |
| 102 | } |
| 103 | assert_eq!(buf[(column("●"), area.y)].fg, expected); |
| 104 | assert_eq!(buf[(column("K"), area.y)].fg, theme.text_muted); |
| 105 | assert_eq!(targets, [(0, Rect::new(column("C"), area.y, 10, 1))]); |
| 106 | assert_eq!(buf[(column("C") + 2, area.y)].fg, theme.text_muted); |
| 107 | assert_eq!(buf[(column("W") - 1, area.y)].fg, theme.text_dim); |
| 108 | for position in [(3, 2), (102, 2), (60, 2), (3, 3), (109, 4)] { |
| 109 | assert_eq!(buf[position], before[position], "{ink:?}: {position:?}"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn posture_counts_keep_cjk_combining_and_ascii_pointer_geometry_at_every_width() { |
| 116 | let counts = [ |
| 117 | ("模型e\u{301}↓\0 (Ctrl+])".into(), ChromeInk::Active), |
| 118 | ("作業 2".into(), ChromeInk::Waiting), |
| 119 | ]; |
| 120 | for ascii in [false, true] { |
| 121 | for width in 0..=45 { |
| 122 | let area = Rect::new(3, 2, width, 2); |
| 123 | let mut buf = Buffer::empty(Rect::new(0, 0, 55, 5)); |
| 124 | buf.set_style(buf.area, Style::default().bg(Color::Rgb(30, 40, 50))); |
| 125 | let before = buf.clone(); |
| 126 | let footer = TidelineFooter::new(&UI_THEME, ("許可\u{202e}", ChromeInk::PermissionAsk)) |
| 127 | .counts(&counts) |
| 128 | .ascii_safe(ascii); |
| 129 | let targets = render_tideline_footer(area, &mut buf, &footer); |
| 130 | assert_eq!(buf[(2, 2)], before[(2, 2)]); |
| 131 | assert_eq!(buf[(area.right(), 2)], before[(area.right(), 2)]); |
| 132 | assert_eq!(buf[(3, 3)], before[(3, 3)], "one reserved row"); |
| 133 | if width >= 34 { |
| 134 | assert_eq!( |
| 135 | targets, |
| 136 | [(0, Rect::new(13, 2, 15, 1)), (1, Rect::new(30, 2, 6, 1))] |
| 137 | ); |
| 138 | for (x, symbol) in [ |
| 139 | (13, "模"), |
| 140 | (15, "型"), |
| 141 | (17, "e\u{301}"), |
| 142 | (20, "("), |
| 143 | (30, "作"), |
| 144 | (32, "業"), |
| 145 | (35, "2"), |
| 146 | ] { |
| 147 | assert_eq!(buf[(x, 2)].symbol(), symbol, "{width}: {symbol}"); |
| 148 | } |
| 149 | assert_eq!(buf[(18, 2)].symbol(), if ascii { "v" } else { "↓" }); |
| 150 | } else { |
| 151 | assert!(targets.is_empty(), "whole count group sheds at {width}"); |
| 152 | } |
| 153 | if width >= 8 { |
| 154 | assert_eq!(buf[(4, 2)].symbol(), if ascii { "." } else { "●" }); |
| 155 | assert_eq!(buf[(6, 2)].symbol(), "許"); |
| 156 | assert_eq!(buf[(8, 2)].symbol(), "可"); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn posture_cap_warning_keeps_its_live_attention_ink_and_priority() { |
| 164 | let theme = custom_theme(); |
| 165 | for percent in [79, 80, 100, 255] { |
| 166 | let area = Rect::new(0, 0, 100, 1); |
| 167 | let mut buf = Buffer::empty(area); |
| 168 | let footer = TidelineFooter::new(&theme, ("full access", ChromeInk::PermissionFullAccess)) |
| 169 | .hint(Some(("hint", ChromeInk::Failure))) |
| 170 | .context_percent(percent) |
| 171 | .right(Some(("failed", ChromeInk::Failure))); |
| 172 | render_tideline_footer(area, &mut buf, &footer); |
| 173 | let row: String = buf.content().iter().map(|cell| cell.symbol()).collect(); |
| 174 | if percent < 80 { |
| 175 | assert!(row.contains("hint")); |
| 176 | assert!(!row.contains("surface soon")); |
| 177 | } else { |
| 178 | assert!(!row.contains("hint")); |
| 179 | assert!(row.contains("▲ surface soon — /compact")); |
| 180 | let warning = (0..area.width) |
| 181 | .find(|x| buf[(*x, 0)].symbol() == "▲") |
| 182 | .unwrap(); |
| 183 | assert_eq!(buf[(warning, 0)].fg, theme.warning); |
| 184 | } |
| 185 | assert_eq!(buf[(1, 0)].fg, theme.permission_full_access); |
| 186 | assert_eq!(buf[(area.right() - 2, 0)].fg, theme.error_fg); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn cap_warning_shares_a_narrow_row_with_an_urgent_notice_when_the_action_fits() { |
| 192 | let area = Rect::new(0, 0, 45, 1); |
| 193 | let mut buf = Buffer::empty(area); |
| 194 | let footer = TidelineFooter::new(&UI_THEME, ("Full Access", ChromeInk::PermissionFullAccess)) |
| 195 | .context_percent(84) |
| 196 | .right(Some(("Tool failed", ChromeInk::Failure))); |
| 197 | render_tideline_footer(area, &mut buf, &footer); |
| 198 | let row: String = buf.content.iter().map(|cell| cell.symbol()).collect(); |
| 199 | for fact in ["Full Access", "/compact", "Tool failed"] { |
| 200 | assert!(row.contains(fact), "{row}"); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn compact_posture_keeps_a_pending_decision_without_a_running_clock() { |
| 206 | for (ask, permission, ascii) in [ |
| 207 | ("needs you", "Full Access", false), |
| 208 | ("需要確認", "完全存取", true), |
| 209 | ] { |
| 210 | let area = Rect::new(2, 1, 42, 1); |
| 211 | let mut buf = Buffer::empty(area); |
| 212 | let footer = TidelineFooter::new(&UI_THEME, (permission, ChromeInk::PermissionFullAccess)) |
| 213 | .turn_clock(Some((ask, ChromeInk::Waiting))) |
| 214 | .context_percent(85) |
| 215 | .compact(true) |
| 216 | .ascii_safe(ascii); |
| 217 | render_tideline_footer(area, &mut buf, &footer); |
| 218 | let row = buf |
| 219 | .content |
| 220 | .iter() |
| 221 | .map(|cell| cell.symbol()) |
| 222 | .collect::<String>(); |
| 223 | let words = row.replace(' ', ""); |
| 224 | assert!( |
| 225 | words.contains(&ask.replace(' ', "")) && words.contains(&permission.replace(' ', "")), |
| 226 | "{row}" |
| 227 | ); |
| 228 | assert!(row.contains(if ascii { " | " } else { " │ " })); |
| 229 | assert!(row.contains("/compact"), "{row}"); |
| 230 | assert!(footer.working_clock_visible(area)); |
| 231 | assert!( |
| 232 | buf.content |
| 233 | .iter() |
| 234 | .all(|cell| !cell.modifier.contains(Modifier::REVERSED)) |
| 235 | ); |
| 236 | } |
| 237 | let area = Rect::new(0, 0, 45, 1); |
| 238 | let mut buf = Buffer::empty(area); |
| 239 | let footer = TidelineFooter::new(&UI_THEME, ("Full Access", ChromeInk::PermissionFullAccess)) |
| 240 | .turn_clock(Some(("needs you", ChromeInk::Waiting))) |
| 241 | .context_percent(85) |
| 242 | .right(Some(("Tool failed", ChromeInk::Failure))); |
| 243 | render_tideline_footer(area, &mut buf, &footer); |
| 244 | let row: String = buf.content.iter().map(|cell| cell.symbol()).collect(); |
| 245 | for fact in ["Full Access", "needs you", "Tool failed"] { |
| 246 | assert!(row.contains(fact), "{row}"); |
| 247 | } |
| 248 | } |
| 249 |