| 1 | //! Workspace trust prompt for onboarding. |
| 2 | //! |
| 3 | //! One decision: trust the instructions and files in this folder, or |
| 4 | //! continue without trust. The explicit 1 · 2 · 3 keys stay — trusting |
| 5 | //! a workspace is a security boundary and must never happen by reflex. |
| 6 | |
| 7 | use crossterm::event::{KeyCode, KeyModifiers}; |
| 8 | use ratatui::style::{Modifier, Style}; |
| 9 | use ratatui::text::{Line, Span}; |
| 10 | |
| 11 | use crate::tui::app::App; |
| 12 | use codewhale_localization::MessageId; |
| 13 | use codewhale_palette as palette; |
| 14 | |
| 15 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 16 | pub(crate) enum TrustChoice { |
| 17 | Trust, |
| 18 | ContinueUntrusted, |
| 19 | Quit, |
| 20 | } |
| 21 | |
| 22 | pub(crate) fn choice_for_key(code: KeyCode, modifiers: KeyModifiers) -> Option<TrustChoice> { |
| 23 | if !(modifiers - KeyModifiers::SHIFT).is_empty() { |
| 24 | return None; |
| 25 | } |
| 26 | match code { |
| 27 | KeyCode::Char('1') => Some(TrustChoice::Trust), |
| 28 | KeyCode::Char('2') => Some(TrustChoice::ContinueUntrusted), |
| 29 | KeyCode::Char('3') | KeyCode::Esc => Some(TrustChoice::Quit), |
| 30 | _ => None, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Wrap a path-bearing line at `/` boundaries so a deep workspace never |
| 35 | /// hard-splits mid-component under ratatui's whitespace-only `Wrap`. |
| 36 | /// Continuation lines are indented to read as one location. |
| 37 | fn wrap_on_path_separators(text: &str, width: usize) -> Vec<String> { |
| 38 | let width = width.max(8); |
| 39 | let mut out: Vec<String> = Vec::new(); |
| 40 | let mut current = String::new(); |
| 41 | let mut chunk = String::new(); |
| 42 | let flush = |current: &mut String, chunk: &mut String, out: &mut Vec<String>| { |
| 43 | if chunk.is_empty() { |
| 44 | return; |
| 45 | } |
| 46 | let candidate_len = current.chars().count() + chunk.chars().count(); |
| 47 | if candidate_len > width && !current.is_empty() { |
| 48 | out.push(std::mem::take(current)); |
| 49 | current.push_str(" "); |
| 50 | } |
| 51 | current.push_str(chunk); |
| 52 | chunk.clear(); |
| 53 | }; |
| 54 | for ch in text.chars() { |
| 55 | chunk.push(ch); |
| 56 | if ch == '/' { |
| 57 | flush(&mut current, &mut chunk, &mut out); |
| 58 | } |
| 59 | } |
| 60 | flush(&mut current, &mut chunk, &mut out); |
| 61 | if !current.is_empty() { |
| 62 | out.push(current); |
| 63 | } |
| 64 | if out.is_empty() { |
| 65 | vec![String::new()] |
| 66 | } else { |
| 67 | out |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | pub fn lines(app: &App, content_width: usize) -> Vec<Line<'static>> { |
| 72 | let accent = super::theme_or(app.ui_theme.accent_primary, palette::WHALE_ACTION); |
| 73 | let body_fg = super::theme_or(app.ui_theme.text_body, palette::TEXT_PRIMARY); |
| 74 | let soft_fg = super::theme_or(app.ui_theme.text_soft, palette::TEXT_SOFT); |
| 75 | let muted_fg = super::theme_or(app.ui_theme.text_muted, palette::TEXT_MUTED); |
| 76 | let warn_fg = super::theme_or(app.ui_theme.warning, palette::STATUS_WARNING); |
| 77 | |
| 78 | let mut lines = Vec::new(); |
| 79 | lines.push(Line::from(Span::styled( |
| 80 | app.tr(MessageId::OnboardTrustTitle).to_string(), |
| 81 | Style::default().fg(accent).add_modifier(Modifier::BOLD), |
| 82 | ))); |
| 83 | lines.push(Line::from("")); |
| 84 | for segment in super::wrap_words( |
| 85 | app.tr(MessageId::OnboardTrustQuestion).as_ref(), |
| 86 | content_width, |
| 87 | ) { |
| 88 | lines.push(Line::from(Span::styled( |
| 89 | segment, |
| 90 | Style::default().fg(body_fg), |
| 91 | ))); |
| 92 | } |
| 93 | let location = format!( |
| 94 | "{}{}", |
| 95 | app.tr(MessageId::OnboardTrustLocationPrefix), |
| 96 | codewhale_util::utils::display_path(&app.workspace) |
| 97 | ); |
| 98 | for segment in wrap_on_path_separators(&location, content_width) { |
| 99 | lines.push(Line::from(Span::styled( |
| 100 | segment, |
| 101 | Style::default().fg(soft_fg).add_modifier(Modifier::BOLD), |
| 102 | ))); |
| 103 | } |
| 104 | lines.push(Line::from("")); |
| 105 | for id in [ |
| 106 | MessageId::OnboardTrustRiskHint, |
| 107 | MessageId::OnboardTrustEffectHint, |
| 108 | ] { |
| 109 | for segment in super::wrap_words(app.tr(id).as_ref(), content_width) { |
| 110 | lines.push(Line::from(Span::styled( |
| 111 | segment, |
| 112 | Style::default().fg(muted_fg), |
| 113 | ))); |
| 114 | } |
| 115 | } |
| 116 | if let Some(message) = app.setup_notice().map(|notice| notice.text.as_str()) { |
| 117 | lines.push(Line::from("")); |
| 118 | for segment in super::wrap_words(message, content_width) { |
| 119 | lines.push(Line::from(Span::styled( |
| 120 | segment, |
| 121 | Style::default().fg(warn_fg), |
| 122 | ))); |
| 123 | } |
| 124 | } |
| 125 | lines |
| 126 | } |
| 127 | |
| 128 | #[cfg(test)] |
| 129 | mod tests { |
| 130 | use super::*; |
| 131 | use crate::tui::app::TuiOptions; |
| 132 | use crate::tui::views::action_footer_lines; |
| 133 | use codewhale_runtime::config::Config; |
| 134 | use std::path::PathBuf; |
| 135 | |
| 136 | #[test] |
| 137 | fn only_the_digits_and_esc_act_on_the_trust_screen() { |
| 138 | for sentence in [ |
| 139 | "not yet", |
| 140 | "no thank you", |
| 141 | "nope, maybe later", |
| 142 | "Let me look first. No thanks, n N y Y u U", |
| 143 | ] { |
| 144 | for ch in sentence.chars() { |
| 145 | assert_eq!( |
| 146 | choice_for_key(KeyCode::Char(ch), KeyModifiers::NONE), |
| 147 | None, |
| 148 | "{ch:?} in {sentence:?}" |
| 149 | ); |
| 150 | } |
| 151 | } |
| 152 | for modifier in [KeyModifiers::SUPER, KeyModifiers::META] { |
| 153 | assert_eq!(choice_for_key(KeyCode::Char('1'), modifier), None); |
| 154 | } |
| 155 | for ch in ['y', 'Y'] { |
| 156 | assert_eq!( |
| 157 | choice_for_key(KeyCode::Char(ch), KeyModifiers::CONTROL), |
| 158 | None |
| 159 | ); |
| 160 | assert_eq!(choice_for_key(KeyCode::Char(ch), KeyModifiers::ALT), None); |
| 161 | } |
| 162 | assert_eq!( |
| 163 | choice_for_key(KeyCode::Char('1'), KeyModifiers::NONE), |
| 164 | Some(TrustChoice::Trust) |
| 165 | ); |
| 166 | assert_eq!( |
| 167 | choice_for_key(KeyCode::Char('2'), KeyModifiers::NONE), |
| 168 | Some(TrustChoice::ContinueUntrusted) |
| 169 | ); |
| 170 | assert_eq!( |
| 171 | choice_for_key(KeyCode::Char('3'), KeyModifiers::NONE), |
| 172 | Some(TrustChoice::Quit) |
| 173 | ); |
| 174 | assert_eq!( |
| 175 | choice_for_key(KeyCode::Esc, KeyModifiers::NONE), |
| 176 | Some(TrustChoice::Quit) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn prompt_names_the_workspace_boundary_and_effects() { |
| 182 | let options = TuiOptions { |
| 183 | model: "test-model".to_string(), |
| 184 | ..crate::tui::test_support::test_tui_options(PathBuf::from("workspace-fixture")) |
| 185 | }; |
| 186 | let mut app = App::new(options, &Config::default()); |
| 187 | app.ui_locale = codewhale_localization::Locale::En; |
| 188 | let body = lines(&app, 70) |
| 189 | .into_iter() |
| 190 | .flat_map(|line| line.spans.into_iter().map(|span| span.content.to_string())) |
| 191 | .collect::<Vec<_>>() |
| 192 | .join("\n"); |
| 193 | // The prose wraps, so a phrase can straddle a line break. Match on |
| 194 | // collapsed whitespace: this test is about which facts the screen |
| 195 | // states, not about where the lane happens to break them. |
| 196 | let flat = body.split_whitespace().collect::<Vec<_>>().join(" "); |
| 197 | |
| 198 | assert!(flat.contains("Know this workspace"), "{body}"); |
| 199 | assert!(flat.contains("instructions and files"), "{body}"); |
| 200 | assert!(flat.contains("prompt injection"), "{body}"); |
| 201 | assert!(flat.contains("tools and hooks"), "{body}"); |
| 202 | } |
| 203 | |
| 204 | /// Trust keys stay explicit and in the action rail: Enter must never |
| 205 | /// grant trust, and the three choices must each advertise their own key. |
| 206 | #[test] |
| 207 | fn trust_actions_are_explicit_keys_in_the_action_rail() { |
| 208 | let mut app = App::new( |
| 209 | TuiOptions { |
| 210 | model: "test-model".to_string(), |
| 211 | ..crate::tui::test_support::test_tui_options(PathBuf::from("workspace-fixture")) |
| 212 | }, |
| 213 | &Config::default(), |
| 214 | ); |
| 215 | app.ui_locale = codewhale_localization::Locale::En; |
| 216 | app.onboarding = crate::tui::app::OnboardingState::TrustDirectory; |
| 217 | |
| 218 | let rail = super::super::action_hints(&app) |
| 219 | .iter() |
| 220 | .flat_map(|hint| action_footer_lines(std::slice::from_ref(hint), 60)) |
| 221 | .flat_map(|line| { |
| 222 | line.spans |
| 223 | .into_iter() |
| 224 | .map(|span| span.content.to_string()) |
| 225 | .collect::<Vec<_>>() |
| 226 | }) |
| 227 | .collect::<Vec<_>>() |
| 228 | .join(" "); |
| 229 | |
| 230 | for expected in ["1", "2", "3"] { |
| 231 | assert!( |
| 232 | rail.contains(expected), |
| 233 | "missing {expected} in rail: {rail}" |
| 234 | ); |
| 235 | } |
| 236 | assert!(rail.contains("trust and continue"), "{rail}"); |
| 237 | assert!(rail.contains("continue without trusting"), "{rail}"); |
| 238 | assert!(rail.contains("quit Codewhale"), "{rail}"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #[cfg(test)] |
| 243 | mod narrow_terminal_tests { |
| 244 | use super::*; |
| 245 | use crate::tui::app::TuiOptions; |
| 246 | use codewhale_localization::{Locale, MessageId, tr}; |
| 247 | use codewhale_runtime::config::Config; |
| 248 | use std::path::PathBuf; |
| 249 | use unicode_width::UnicodeWidthStr; |
| 250 | |
| 251 | fn app_at(workspace: &str) -> App { |
| 252 | let options = TuiOptions { |
| 253 | model: "test-model".to_string(), |
| 254 | ..crate::tui::test_support::test_tui_options(PathBuf::from(workspace)) |
| 255 | }; |
| 256 | App::new(options, &Config::default()) |
| 257 | } |
| 258 | |
| 259 | /// The trust screen asks for filesystem trust. Every locale's prose has to |
| 260 | /// survive a small terminal intact: a question cut mid-word is not a |
| 261 | /// question the reader can answer. |
| 262 | #[test] |
| 263 | fn every_locale_keeps_the_trust_prose_whole_at_forty_columns() { |
| 264 | let mut app = app_at("/tmp/probe/ws"); |
| 265 | for locale in Locale::shipped().iter().copied() { |
| 266 | app.ui_locale = locale; |
| 267 | for width in [40usize, 60, 80, 120] { |
| 268 | let rendered: Vec<String> = lines(&app, width) |
| 269 | .iter() |
| 270 | .map(|line| { |
| 271 | line.spans |
| 272 | .iter() |
| 273 | .map(|span| span.content.as_ref()) |
| 274 | .collect::<String>() |
| 275 | }) |
| 276 | .collect(); |
| 277 | |
| 278 | for row in &rendered { |
| 279 | assert!( |
| 280 | row.width() <= width, |
| 281 | "{locale:?} at {width}: row overflows the lane: {row:?}", |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | // The question must be present in full, not clipped. Compare on |
| 286 | // the rejoined prose so a wrap is fine and a cut is not. |
| 287 | let joined = rendered.join(" "); |
| 288 | let question = tr(locale, MessageId::OnboardTrustQuestion); |
| 289 | // Compare with whitespace removed entirely, not collapsed. |
| 290 | // Japanese and Chinese wrap between characters that have no |
| 291 | // space between them in the source, so rejoining with a space |
| 292 | // would make a correctly wrapped line look like a changed one. |
| 293 | let normalize = |
| 294 | |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::<String>(); |
| 295 | assert!( |
| 296 | normalize(&joined).contains(&normalize(question.as_ref())) |
| 297 | || question.as_ref().chars().all(|c| c.is_whitespace()), |
| 298 | "{locale:?} at {width}: the trust question was cut.\nwanted: {question}\ngot: {joined}", |
| 299 | ); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 |