| 1 | //! Paste-burst handling — turn rapid keystrokes (terminals without bracketed |
| 2 | //! paste) into a single committed buffer instead of N individual chars. |
| 3 | //! |
| 4 | //! Extracted from `tui/ui.rs` (P1.2). The owning state machine lives on |
| 5 | //! `App.paste_burst` (`tui::paste_burst`); these helpers wire it to the key |
| 6 | //! event loop and the composer's text buffer. |
| 7 | |
| 8 | use std::time::{Duration, Instant}; |
| 9 | |
| 10 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 11 | |
| 12 | use super::app::{App, looks_like_slash_command_input}; |
| 13 | use super::clipboard::{ClipboardContent, ClipboardRead}; |
| 14 | use super::shell_key_routing::Focus; |
| 15 | use super::views::ModalKind; |
| 16 | use codewhale_localization::MessageId; |
| 17 | use codewhale_tui_kit::paste_burst::CharDecision; |
| 18 | |
| 19 | /// Terminals known to deliver `Event::Paste` for a real paste. |
| 20 | /// |
| 21 | /// `bracketed_paste_seen` alone waits for proof: it only flips after the |
| 22 | /// person has pasted once, so every session ran the rapid-keystroke |
| 23 | /// heuristic until then. That heuristic re-arms a ~120 ms Enter-suppression |
| 24 | /// window on each character, and the composer's send cue honestly reports it |
| 25 | /// — so the `[↵]` control strobed dim-to-bold once per keystroke while |
| 26 | /// someone typed at an ordinary pace. Reported from a live session. |
| 27 | /// |
| 28 | /// Naming the terminals we have verified is deliberately narrower than |
| 29 | /// trusting `EnableBracketedPaste` to have succeeded: `execute!` returning |
| 30 | /// `Ok` only means the escape was written, not that the terminal honors it, |
| 31 | /// and a terminal that ignores it would submit on a pasted newline. That is |
| 32 | /// data loss, so the fallback stays for everything not on this list. |
| 33 | /// Windows input keeps the fallback until an actual `Event::Paste`: Windows |
| 34 | /// Terminal can advertise bracketed paste while the input path still delivers |
| 35 | /// individual key events (#6427). Its session marker is not delivery proof. |
| 36 | /// |
| 37 | /// Known limitation: a non-Windows terminal that sets one of the remaining |
| 38 | /// identifiers while not honoring bracketed paste would still lose the guard. |
| 39 | pub(crate) fn terminal_delivers_bracketed_paste() -> bool { |
| 40 | use std::io::IsTerminal; |
| 41 | |
| 42 | // Only a real interactive terminal gets the trust. Under `cargo test` |
| 43 | // stdout is captured, so every unit test keeps the heuristic and stays |
| 44 | // hermetic regardless of the `TERM_PROGRAM` the developer happens to be |
| 45 | // running in — the first version of this read the ambient environment |
| 46 | // and broke five paste tests on a Mac running Terminal.app. |
| 47 | terminal_identity_allows_paste_bypass( |
| 48 | std::io::stdout().is_terminal(), |
| 49 | cfg!(windows) || std::env::var_os("WT_SESSION").is_some(), |
| 50 | std::env::var("TERM_PROGRAM").ok().as_deref(), |
| 51 | ) |
| 52 | } |
| 53 | |
| 54 | fn terminal_identity_allows_paste_bypass( |
| 55 | interactive: bool, |
| 56 | windows_input: bool, |
| 57 | program: Option<&str>, |
| 58 | ) -> bool { |
| 59 | interactive |
| 60 | && !windows_input |
| 61 | && program.is_some_and(|program| { |
| 62 | matches!( |
| 63 | program.trim(), |
| 64 | "ghostty" | "Ghostty" | "iTerm.app" | "WezTerm" | "Apple_Terminal" |
| 65 | ) |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | /// Process a key in the context of paste-burst detection. Returns `true` |
| 70 | /// when the key was fully handled by the paste machinery (caller skips |
| 71 | /// further input handling); `false` when the key still needs the normal |
| 72 | /// composer path. |
| 73 | pub fn handle_paste_burst_key(app: &mut App, key: &KeyEvent, now: Instant) -> bool { |
| 74 | if !app.use_paste_burst_detection { |
| 75 | return false; |
| 76 | } |
| 77 | // Once we've observed a real `Event::Paste` in this session, bracketed |
| 78 | // paste is verified working and the rapid-keystroke heuristic is |
| 79 | // unnecessary. Skipping it eliminates false positives on fast typing / |
| 80 | // IME commits / autocomplete on terminals with reliable bracketed |
| 81 | // paste (the dominant case on iTerm2 / Ghostty / WezTerm / Windows |
| 82 | // Terminal). |
| 83 | if app.bracketed_paste_seen || (app.bracketed_paste_trusted && app.use_bracketed_paste) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | let has_ctrl_alt_or_super = key.modifiers.contains(KeyModifiers::CONTROL) |
| 88 | || key.modifiers.contains(KeyModifiers::ALT) |
| 89 | || key.modifiers.contains(KeyModifiers::SUPER); |
| 90 | |
| 91 | match key.code { |
| 92 | KeyCode::Enter => { |
| 93 | if app.paste_burst.is_active() |
| 94 | && in_command_context(app) |
| 95 | && let Some(pending) = app.paste_burst.flush_before_modified_input() |
| 96 | { |
| 97 | app.insert_str(&pending); |
| 98 | } |
| 99 | if app.paste_burst.append_newline_if_active(now) { |
| 100 | if let Some(chunk) = app.paste_burst.take_chunk_if_full() { |
| 101 | app.insert_str(&chunk); |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | if app.paste_burst.newline_should_insert_instead_of_submit(now) { |
| 106 | app.insert_char('\n'); |
| 107 | // Deliberately no `extend_window` here. This Enter arrived |
| 108 | // with no burst being assembled, so it is only *maybe* a |
| 109 | // pasted newline. Re-arming on that guess let each absorbed |
| 110 | // Enter buy another 120ms, so a user pressing Enter to send |
| 111 | // never submitted — every press just added a newline. The |
| 112 | // window now always expires 120ms after the last real |
| 113 | // keystroke; newlines genuinely inside a paste are absorbed |
| 114 | // by `append_newline_if_active` above, which does re-arm. |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | KeyCode::Char(c) if !has_ctrl_alt_or_super => { |
| 119 | if !c.is_ascii() { |
| 120 | // IME-committed characters (Chinese, Japanese, Korean) |
| 121 | // arrive as individual KeyCode::Char events, typically with |
| 122 | // tens-of-milliseconds gaps between each committed character, |
| 123 | // so a lone commit still inserts straight into the composer. |
| 124 | // A run at paste speed is a raw paste: it buffers like an |
| 125 | // ASCII burst and keeps the full Enter-suppression window |
| 126 | // armed (#1302) while a lone commit earns the short window |
| 127 | // (`PasteBurst::arm_window_for_direct_char`). |
| 128 | let rapid_chars = app.paste_burst.note_plain_char(now); |
| 129 | if app.paste_burst.is_buffering() { |
| 130 | app.paste_burst.append_char_to_buffer(c, now); |
| 131 | if let Some(chunk) = app.paste_burst.take_chunk_if_full() { |
| 132 | app.insert_str(&chunk); |
| 133 | } |
| 134 | } else { |
| 135 | if let Some(pending) = app.paste_burst.flush_before_modified_input() { |
| 136 | app.insert_str(&pending); |
| 137 | } |
| 138 | if rapid_chars >= 2 { |
| 139 | app.paste_burst.begin_buffer_from_now(c, now); |
| 140 | return true; |
| 141 | } |
| 142 | app.paste_burst.arm_window_for_direct_char(now, rapid_chars); |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | let decision = app.paste_burst.on_plain_char(c, now); |
| 149 | return handle_paste_burst_decision(app, decision, c, now); |
| 150 | } |
| 151 | _ => {} |
| 152 | } |
| 153 | |
| 154 | false |
| 155 | } |
| 156 | |
| 157 | /// Apply a paste-burst decision to the composer buffer. The burst never |
| 158 | /// rewrites text the user already has on the composer line: chars before |
| 159 | /// the burst stay exactly as typed, and buffering starts from the current |
| 160 | /// char (Y-7 — the old retro-grab deleted and reinserted typed text on a |
| 161 | /// timing guess and scrambled fast input). |
| 162 | pub fn handle_paste_burst_decision( |
| 163 | app: &mut App, |
| 164 | decision: CharDecision, |
| 165 | c: char, |
| 166 | now: Instant, |
| 167 | ) -> bool { |
| 168 | match decision { |
| 169 | CharDecision::RetainFirstChar => true, |
| 170 | CharDecision::BeginBufferFromPending | CharDecision::BufferAppend => { |
| 171 | app.paste_burst.append_char_to_buffer(c, now); |
| 172 | if let Some(chunk) = app.paste_burst.take_chunk_if_full() { |
| 173 | app.insert_str(&chunk); |
| 174 | } |
| 175 | true |
| 176 | } |
| 177 | CharDecision::BeginBuffer => { |
| 178 | app.paste_burst.begin_buffer_from_now(c, now); |
| 179 | true |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | fn in_command_context(app: &App) -> bool { |
| 185 | let mut composite = app.input.clone(); |
| 186 | composite.push_str(&app.paste_burst.held_text()); |
| 187 | looks_like_slash_command_input(&composite) |
| 188 | } |
| 189 | |
| 190 | /// Where a paste came from. Every paste, terminal-delivered or read from the |
| 191 | /// clipboard, reaches [`handle_paste`]: the one place that decides who owns |
| 192 | /// it and whether the clipboard has to be asked at all. |
| 193 | pub(crate) enum PasteSource<'a> { |
| 194 | /// `Event::Paste`: the terminal already delivered the text. Windows |
| 195 | /// Terminal sends an empty one for an image-only clipboard ("send an empty |
| 196 | /// bracketed paste even if the clipboard was empty"), which is the only |
| 197 | /// signal a TUI gets that the person pasted. |
| 198 | Terminal(&'a str), |
| 199 | /// Cmd/Ctrl+V reached Codewhale, or the mouse menu's Paste: read the |
| 200 | /// clipboard. |
| 201 | Shortcut, |
| 202 | /// Only the release of Ctrl+V arrived: the terminal took the chord and |
| 203 | /// delivers any text itself, so only an image is Codewhale's to attach. |
| 204 | ReleaseOnly, |
| 205 | /// Middle-click on the composer under X11/Wayland: `text` is the PRIMARY |
| 206 | /// selection, inserted at the pointer's char index `position` and never |
| 207 | /// anywhere but the composer. |
| 208 | Primary { text: &'a str, position: usize }, |
| 209 | } |
| 210 | |
| 211 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 212 | enum PasteTarget { |
| 213 | TerminalPanel, |
| 214 | HistorySearch, |
| 215 | ProviderPicker, |
| 216 | Modal, |
| 217 | Composer, |
| 218 | } |
| 219 | |
| 220 | /// A clipboard read still running on the reader thread. One at a time: a |
| 221 | /// paste while it runs is the other edge of the same key press, or a repeat |
| 222 | /// the person will see land once the read answers. |
| 223 | pub(crate) struct PendingPaste { |
| 224 | target: PasteTarget, |
| 225 | image_only: bool, |
| 226 | started: Instant, |
| 227 | rx: std::sync::mpsc::Receiver<Option<ClipboardContent>>, |
| 228 | } |
| 229 | |
| 230 | /// The slowest read finishes inside this: two `wl-paste` waits of 2 s, or |
| 231 | /// PowerShell's 10 s image fallback plus the backend open. A reader stuck |
| 232 | /// past it is abandoned so the next paste can start a fresh one. |
| 233 | const PENDING_PASTE_LIMIT: Duration = Duration::from_secs(15); |
| 234 | const PENDING_PASTE_POLL: Duration = Duration::from_millis(16); |
| 235 | |
| 236 | fn paste_target(app: &App) -> PasteTarget { |
| 237 | if app.focus() == Focus::TerminalPanel && app.work_surface.last_area.is_some() { |
| 238 | PasteTarget::TerminalPanel |
| 239 | } else if app.view_stack.top_kind() == Some(ModalKind::ProviderPicker) { |
| 240 | PasteTarget::ProviderPicker |
| 241 | } else if !app.view_stack.is_empty() { |
| 242 | PasteTarget::Modal |
| 243 | } else if app.is_history_search_active() { |
| 244 | PasteTarget::HistorySearch |
| 245 | } else { |
| 246 | PasteTarget::Composer |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /// Route one paste. Returns whether the paste was taken; a clipboard shortcut |
| 251 | /// the focused surface does not paste into (a terminal panel, a non-credential |
| 252 | /// modal) is left for that surface's own key handling. |
| 253 | pub(crate) fn handle_paste(app: &mut App, source: PasteSource<'_>) -> bool { |
| 254 | let target = paste_target(app); |
| 255 | match source { |
| 256 | PasteSource::Terminal(text) => { |
| 257 | tracing::debug!( |
| 258 | paste_bytes = text.len(), |
| 259 | paste_chars = text.chars().count(), |
| 260 | "Received bracketed paste event" |
| 261 | ); |
| 262 | app.bracketed_paste_seen = true; |
| 263 | if text.is_empty() && target == PasteTarget::Composer { |
| 264 | request_clipboard(app, target, false, true); |
| 265 | } else { |
| 266 | deliver_text(app, target, text); |
| 267 | } |
| 268 | true |
| 269 | } |
| 270 | PasteSource::Shortcut => match target { |
| 271 | PasteTarget::TerminalPanel | PasteTarget::Modal => false, |
| 272 | _ => { |
| 273 | request_clipboard(app, target, false, false); |
| 274 | true |
| 275 | } |
| 276 | }, |
| 277 | PasteSource::ReleaseOnly => { |
| 278 | if target == PasteTarget::Composer { |
| 279 | request_clipboard(app, target, true, true); |
| 280 | } |
| 281 | true |
| 282 | } |
| 283 | PasteSource::Primary { text, position } => { |
| 284 | if target != PasteTarget::Composer || app.pending_paste.is_some() { |
| 285 | return false; |
| 286 | } |
| 287 | app.selection_anchor = None; |
| 288 | app.cursor_position = position; |
| 289 | deliver_text(app, target, text); |
| 290 | true |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | fn request_clipboard(app: &mut App, target: PasteTarget, image_only: bool, quiet: bool) { |
| 296 | if app.pending_paste.is_some() { |
| 297 | return; |
| 298 | } |
| 299 | // In SSH sessions without a forwarded display the terminal client owns |
| 300 | // paste: a direct shortcut gets the local terminal-paste instruction and |
| 301 | // `Event::Paste` stays the data path. |
| 302 | if app.clipboard.requires_terminal_paste() { |
| 303 | if !quiet { |
| 304 | app.show_input_hint(app.tr(MessageId::ClipboardSshPasteHint).into_owned()); |
| 305 | } |
| 306 | return; |
| 307 | } |
| 308 | let prefer_markdown = target == PasteTarget::Composer && !image_only; |
| 309 | match app.clipboard.read(app.workspace.as_path(), prefer_markdown) { |
| 310 | ClipboardRead::Ready(content) => finish_paste(app, target, image_only, content), |
| 311 | ClipboardRead::Pending(rx) => { |
| 312 | app.pending_paste = Some(PendingPaste { |
| 313 | target, |
| 314 | image_only, |
| 315 | started: Instant::now(), |
| 316 | rx, |
| 317 | }); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /// Apply a finished clipboard read. Called from the event loop every tick. |
| 323 | pub(crate) fn poll_pending_paste(app: &mut App) { |
| 324 | let Some(pending) = app.pending_paste.as_ref() else { |
| 325 | return; |
| 326 | }; |
| 327 | let content = match pending.rx.try_recv() { |
| 328 | Ok(content) => content, |
| 329 | Err(std::sync::mpsc::TryRecvError::Empty) |
| 330 | if pending.started.elapsed() < PENDING_PASTE_LIMIT => |
| 331 | { |
| 332 | return; |
| 333 | } |
| 334 | Err(_) => { |
| 335 | tracing::warn!( |
| 336 | target: "codewhale_tui::clipboard", |
| 337 | "clipboard read did not answer; abandoned" |
| 338 | ); |
| 339 | app.pending_paste = None; |
| 340 | return; |
| 341 | } |
| 342 | }; |
| 343 | let Some(pending) = app.pending_paste.take() else { |
| 344 | return; |
| 345 | }; |
| 346 | if paste_target(app) == pending.target { |
| 347 | finish_paste(app, pending.target, pending.image_only, content); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /// Shortens the event loop's wait while a clipboard read is running. |
| 352 | pub(crate) fn pending_paste_poll_delay(app: &App) -> Option<Duration> { |
| 353 | app.pending_paste.as_ref().map(|_| PENDING_PASTE_POLL) |
| 354 | } |
| 355 | |
| 356 | fn finish_paste( |
| 357 | app: &mut App, |
| 358 | target: PasteTarget, |
| 359 | image_only: bool, |
| 360 | content: Option<ClipboardContent>, |
| 361 | ) { |
| 362 | app.needs_redraw = true; |
| 363 | match content { |
| 364 | // Images go only to the composer; an open credential modal consumes |
| 365 | // and ignores one rather than leaking it to the draft beneath. |
| 366 | Some(ClipboardContent::Image(pasted)) if target == PasteTarget::Composer => { |
| 367 | crate::tui::work_surface::release_focus(app); |
| 368 | let description = format!("{} ({})", pasted.short_label(), pasted.size_label()); |
| 369 | app.insert_media_attachment("image", &pasted.path, Some(&description)); |
| 370 | app.show_input_hint(format!("Attached image: {description}")); |
| 371 | } |
| 372 | Some(ClipboardContent::Text(text)) if !image_only => deliver_text(app, target, &text), |
| 373 | // The read cannot tell an empty clipboard from an unreadable one; |
| 374 | // either way the person asked to paste and nothing happened. |
| 375 | None if !image_only && target == PasteTarget::Composer => { |
| 376 | app.show_input_hint(app.tr(MessageId::ClipboardNothingToPaste).into_owned()); |
| 377 | } |
| 378 | _ => {} |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | fn deliver_text(app: &mut App, target: PasteTarget, text: &str) { |
| 383 | match target { |
| 384 | PasteTarget::TerminalPanel => { |
| 385 | crate::tui::work_surface::handle_terminal_paste(app, text); |
| 386 | } |
| 387 | PasteTarget::HistorySearch => app.history_search_insert_str(text), |
| 388 | PasteTarget::ProviderPicker | PasteTarget::Modal => { |
| 389 | let _ = app.view_stack.handle_paste(text); |
| 390 | } |
| 391 | PasteTarget::Composer => { |
| 392 | crate::tui::work_surface::release_focus(app); |
| 393 | app.insert_paste_text(text); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | #[cfg(test)] |
| 399 | mod bracketed_paste_trust_tests { |
| 400 | use super::{terminal_delivers_bracketed_paste, terminal_identity_allows_paste_bypass}; |
| 401 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 402 | |
| 403 | /// Under `cargo test` stdout is captured, so the probe refuses to trust |
| 404 | /// anything — the heuristic stays on and every existing paste test keeps |
| 405 | /// the behaviour it was written against, whatever terminal the developer |
| 406 | /// is in. This is the hermeticity the first version of this check broke. |
| 407 | #[test] |
| 408 | fn a_captured_stdout_is_never_trusted() { |
| 409 | let _env = lock_test_env(); |
| 410 | for program in ["ghostty", "iTerm.app", "WezTerm", "some-new-terminal"] { |
| 411 | let _guard = EnvVarGuard::set("TERM_PROGRAM", program); |
| 412 | assert!( |
| 413 | !terminal_delivers_bracketed_paste(), |
| 414 | "{program} must not be trusted without a real terminal" |
| 415 | ); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | #[test] |
| 420 | fn windows_input_requires_observed_paste_even_with_a_known_terminal_identity() { |
| 421 | for program in [ |
| 422 | None, |
| 423 | Some("Windows Terminal"), |
| 424 | Some("iTerm.app"), |
| 425 | Some("ghostty"), |
| 426 | ] { |
| 427 | assert!( |
| 428 | !terminal_identity_allows_paste_bypass(true, true, program), |
| 429 | "Windows input must retain the fallback before Event::Paste: {program:?}" |
| 430 | ); |
| 431 | } |
| 432 | assert!(terminal_identity_allows_paste_bypass( |
| 433 | true, |
| 434 | false, |
| 435 | Some("iTerm.app") |
| 436 | )); |
| 437 | assert!(!terminal_identity_allows_paste_bypass( |
| 438 | false, |
| 439 | false, |
| 440 | Some("iTerm.app") |
| 441 | )); |
| 442 | assert!(!terminal_identity_allows_paste_bypass( |
| 443 | true, |
| 444 | false, |
| 445 | Some("unknown") |
| 446 | )); |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn windows_terminal_is_trusted_through_its_session_marker() { |
| 451 | let _env = lock_test_env(); |
| 452 | let _program = EnvVarGuard::remove("TERM_PROGRAM"); |
| 453 | let _wt = EnvVarGuard::set("WT_SESSION", "abc-123"); |
| 454 | assert!( |
| 455 | !terminal_delivers_bracketed_paste(), |
| 456 | "still gated on a real terminal, which a test never has" |
| 457 | ); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | #[cfg(test)] |
| 462 | mod tests { |
| 463 | use super::*; |
| 464 | use crate::tui::app::TuiOptions; |
| 465 | use codewhale_runtime::config::Config; |
| 466 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 467 | use std::path::PathBuf; |
| 468 | use std::time::{Duration, Instant}; |
| 469 | |
| 470 | fn test_app() -> App { |
| 471 | let options = TuiOptions { |
| 472 | ..crate::tui::test_support::test_tui_options(PathBuf::from(".")) |
| 473 | }; |
| 474 | App::new(options, &Config::default()) |
| 475 | } |
| 476 | |
| 477 | fn plain(ch: char) -> KeyEvent { |
| 478 | KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE) |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn a_clipboard_read_lands_when_it_answers_and_absorbs_the_second_edge() { |
| 483 | let mut app = test_app(); |
| 484 | let (clipboard, tx) = crate::tui::clipboard::ClipboardHandler::with_pending_read_for_test(); |
| 485 | app.clipboard = clipboard; |
| 486 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 487 | assert!(pending_paste_poll_delay(&app).is_some()); |
| 488 | |
| 489 | // The other edge of the same chord arrives while the read runs. |
| 490 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 491 | poll_pending_paste(&mut app); |
| 492 | assert!(app.input.is_empty()); |
| 493 | |
| 494 | tx.send(Some(ClipboardContent::Text("pasted".to_string()))) |
| 495 | .unwrap(); |
| 496 | poll_pending_paste(&mut app); |
| 497 | assert_eq!(app.input, "pasted"); |
| 498 | assert!(app.pending_paste.is_none()); |
| 499 | } |
| 500 | |
| 501 | #[test] |
| 502 | fn rapid_consecutive_shortcut_pastes_are_not_dropped() { |
| 503 | let mut app = test_app(); |
| 504 | app.clipboard = crate::tui::clipboard::ClipboardHandler::with_content_for_test( |
| 505 | ClipboardContent::Text("chunk".to_string()), |
| 506 | ); |
| 507 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 508 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 509 | assert_eq!(app.input, "chunkchunk"); |
| 510 | } |
| 511 | |
| 512 | #[test] |
| 513 | fn release_without_press_suppresses_text_and_press_release_edge_pastes_once() { |
| 514 | use crate::tui::key_shortcuts::{PasteEdge, paste_edge}; |
| 515 | use crossterm::event::{KeyEventKind, KeyEventState}; |
| 516 | |
| 517 | let press = KeyEvent { |
| 518 | code: KeyCode::Char('v'), |
| 519 | modifiers: KeyModifiers::CONTROL, |
| 520 | kind: KeyEventKind::Press, |
| 521 | state: KeyEventState::NONE, |
| 522 | }; |
| 523 | let release = KeyEvent { |
| 524 | code: KeyCode::Char('v'), |
| 525 | modifiers: KeyModifiers::CONTROL, |
| 526 | kind: KeyEventKind::Release, |
| 527 | state: KeyEventState::NONE, |
| 528 | }; |
| 529 | |
| 530 | let mut app = test_app(); |
| 531 | app.clipboard = crate::tui::clipboard::ClipboardHandler::with_content_for_test( |
| 532 | ClipboardContent::Text("hello".to_string()), |
| 533 | ); |
| 534 | assert!(handle_paste(&mut app, PasteSource::Terminal("hello"))); |
| 535 | assert_eq!( |
| 536 | paste_edge(&release, &mut app.paste_shortcut_press_seen), |
| 537 | PasteEdge::ImageOnly |
| 538 | ); |
| 539 | assert!(handle_paste(&mut app, PasteSource::ReleaseOnly)); |
| 540 | assert_eq!(app.input, "hello"); |
| 541 | |
| 542 | let mut app = test_app(); |
| 543 | app.clipboard = crate::tui::clipboard::ClipboardHandler::with_content_for_test( |
| 544 | ClipboardContent::Text("xy".to_string()), |
| 545 | ); |
| 546 | assert_eq!( |
| 547 | paste_edge(&press, &mut app.paste_shortcut_press_seen), |
| 548 | PasteEdge::Route |
| 549 | ); |
| 550 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 551 | assert_eq!( |
| 552 | paste_edge(&release, &mut app.paste_shortcut_press_seen), |
| 553 | PasteEdge::Ignore |
| 554 | ); |
| 555 | assert_eq!(app.input, "xy"); |
| 556 | |
| 557 | assert_eq!( |
| 558 | paste_edge(&press, &mut app.paste_shortcut_press_seen), |
| 559 | PasteEdge::Route |
| 560 | ); |
| 561 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 562 | assert_eq!( |
| 563 | paste_edge(&release, &mut app.paste_shortcut_press_seen), |
| 564 | PasteEdge::Ignore |
| 565 | ); |
| 566 | assert_eq!(app.input, "xyxy"); |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn a_clipboard_read_for_a_surface_that_closed_is_dropped() { |
| 571 | let mut app = test_app(); |
| 572 | let (clipboard, tx) = crate::tui::clipboard::ClipboardHandler::with_pending_read_for_test(); |
| 573 | app.clipboard = clipboard; |
| 574 | assert!(handle_paste(&mut app, PasteSource::Shortcut)); |
| 575 | // Focus moves to history search before the clipboard answers. |
| 576 | app.start_history_search(); |
| 577 | |
| 578 | tx.send(Some(ClipboardContent::Text("sk-secret".to_string()))) |
| 579 | .unwrap(); |
| 580 | poll_pending_paste(&mut app); |
| 581 | |
| 582 | assert!(app.input.is_empty(), "{}", app.input); |
| 583 | assert!(app.pending_paste.is_none()); |
| 584 | assert!( |
| 585 | !app.history_search_query() |
| 586 | .unwrap_or_default() |
| 587 | .contains("sk-secret") |
| 588 | ); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn requested_bracketed_paste_keeps_raw_multiline_fallback_until_verified() { |
| 593 | for pasted in [ |
| 594 | "first line\nsecond line\nthird line", |
| 595 | "多行粘贴测试\n行1\n行2\n行3", |
| 596 | ] { |
| 597 | let mut app = test_app(); |
| 598 | assert!(app.use_bracketed_paste); |
| 599 | assert!(!app.bracketed_paste_seen); |
| 600 | let t0 = Instant::now(); |
| 601 | for (i, ch) in pasted.chars().enumerate() { |
| 602 | let key = if ch == '\n' { |
| 603 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 604 | } else { |
| 605 | plain(ch) |
| 606 | }; |
| 607 | let handled = |
| 608 | handle_paste_burst_key(&mut app, &key, t0 + Duration::from_millis(i as u64)); |
| 609 | if i == 0 && !ch.is_ascii() { |
| 610 | assert!(!handled); |
| 611 | app.insert_char(ch); |
| 612 | } else { |
| 613 | assert!(handled, "raw paste {ch:?} must not reach the submit path"); |
| 614 | } |
| 615 | } |
| 616 | app.flush_paste_burst_if_due(t0 + Duration::from_secs(1)); |
| 617 | assert_eq!(app.input, pasted); |
| 618 | assert!( |
| 619 | !handle_paste_burst_key( |
| 620 | &mut app, |
| 621 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 622 | t0 + Duration::from_secs(2), |
| 623 | ), |
| 624 | "a deliberate Enter after the paste must reach the submit path" |
| 625 | ); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | #[test] |
| 630 | fn mixed_ascii_cjk_24_line_paste_keeps_newlines() { |
| 631 | let expected: String = (1..=24) |
| 632 | .map(|n| format!("cw-clipboard-{n:02} 漢字 🐳 line {n}")) |
| 633 | .collect::<Vec<_>>() |
| 634 | .join("\n"); |
| 635 | let mut app = test_app(); |
| 636 | let start = Instant::now(); |
| 637 | for (index, ch) in expected.chars().enumerate() { |
| 638 | let now = start + Duration::from_millis(index as u64); |
| 639 | app.flush_paste_burst_if_due(now); |
| 640 | let key = if ch == '\n' { |
| 641 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 642 | } else { |
| 643 | plain(ch) |
| 644 | }; |
| 645 | assert!( |
| 646 | handle_paste_burst_key(&mut app, &key, now), |
| 647 | "char {index} {ch:?} not handled" |
| 648 | ); |
| 649 | } |
| 650 | app.flush_paste_burst_if_due(start + Duration::from_secs(1)); |
| 651 | assert_eq!(app.input, expected); |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | #[cfg(windows)] |
| 656 | fn slow_paced_mixed_ascii_cjk_paste_keeps_newlines() { |
| 657 | let expected: String = (1..=6) |
| 658 | .map(|n| format!("cw-clipboard-{n:02} 漢字 🐳 line {n}")) |
| 659 | .collect::<Vec<_>>() |
| 660 | .join("\n"); |
| 661 | let mut app = test_app(); |
| 662 | let start = Instant::now(); |
| 663 | for (index, ch) in expected.chars().enumerate() { |
| 664 | let now = start + Duration::from_millis(30 * index as u64); |
| 665 | app.flush_paste_burst_if_due(now); |
| 666 | let key = if ch == '\n' { |
| 667 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 668 | } else { |
| 669 | plain(ch) |
| 670 | }; |
| 671 | let handled = handle_paste_burst_key(&mut app, &key, now); |
| 672 | if ch == '\n' || ch.is_ascii() { |
| 673 | assert!(handled, "char {index} {ch:?} not handled"); |
| 674 | } else { |
| 675 | assert!( |
| 676 | !handled, |
| 677 | "char {index} {ch:?} should route through direct insert" |
| 678 | ); |
| 679 | app.insert_char(ch); |
| 680 | } |
| 681 | } |
| 682 | app.flush_paste_burst_if_due(start + Duration::from_secs(60)); |
| 683 | assert_eq!(app.input, expected); |
| 684 | } |
| 685 | |
| 686 | #[cfg(windows)] |
| 687 | #[test] |
| 688 | fn fifty_ms_windows_typing_and_ime_commits_leave_send_available() { |
| 689 | for text in ["abc", "你好啊", "hello world", "你好世界你好世界"] { |
| 690 | let mut app = test_app(); |
| 691 | let start = Instant::now(); |
| 692 | let mut last = start; |
| 693 | for (i, ch) in text.chars().enumerate() { |
| 694 | last = start + Duration::from_millis(50 * i as u64); |
| 695 | app.flush_paste_burst_if_due(last); |
| 696 | let handled = handle_paste_burst_key(&mut app, &plain(ch), last); |
| 697 | if ch.is_ascii() { |
| 698 | assert!(handled); |
| 699 | } else { |
| 700 | assert!(!handled); |
| 701 | app.insert_char(ch); |
| 702 | } |
| 703 | } |
| 704 | let enter = last + Duration::from_millis(30); |
| 705 | app.flush_paste_burst_if_due(enter); |
| 706 | assert!(app.composer_enter_would_submit(true, enter), "{text}"); |
| 707 | assert!(!handle_paste_burst_key( |
| 708 | &mut app, |
| 709 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 710 | enter, |
| 711 | )); |
| 712 | assert_eq!(app.input, text); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | fn composer_enter_uses_event_observed_time_not_processing_time() { |
| 718 | let mut app = test_app(); |
| 719 | let t0 = Instant::now() - Duration::from_millis(880); |
| 720 | for (i, ch) in "cw-clipboard-01".chars().enumerate() { |
| 721 | let now = t0 + Duration::from_millis(5 * i as u64); |
| 722 | app.flush_paste_burst_if_due(now); |
| 723 | assert!(handle_paste_burst_key(&mut app, &plain(ch), now)); |
| 724 | } |
| 725 | let enter_observed_at = t0 + Duration::from_millis(5 * 15 + 40); |
| 726 | app.flush_paste_burst_if_due(enter_observed_at); |
| 727 | assert!( |
| 728 | app.handle_composer_enter(true, enter_observed_at).is_none(), |
| 729 | "a mid-stream Enter must insert a newline, not submit" |
| 730 | ); |
| 731 | assert!( |
| 732 | !app.composer_enter_would_submit(true, enter_observed_at), |
| 733 | "the would-submit probe must agree on the same clock" |
| 734 | ); |
| 735 | app.flush_paste_burst_if_due(enter_observed_at + Duration::from_secs(1)); |
| 736 | assert_eq!(app.input, "cw-clipboard-01\n"); |
| 737 | } |
| 738 | |
| 739 | #[test] |
| 740 | fn windows_terminal_raw_five_line_paste_never_reaches_submit() { |
| 741 | // #6427: test the interactive startup decision, not captured stdout, |
| 742 | // then send the raw keys that the Windows input backend can produce. |
| 743 | for pasted in [ |
| 744 | "first line\nsecond line\nthird line\nfourth line\nfifth line", |
| 745 | "第一行\n第二行\n第三行\n第四行\n第五行", |
| 746 | ] { |
| 747 | let mut app = test_app(); |
| 748 | app.bracketed_paste_trusted = |
| 749 | terminal_identity_allows_paste_bypass(true, true, Some("iTerm.app")); |
| 750 | let start = Instant::now(); |
| 751 | for (index, ch) in pasted.chars().enumerate() { |
| 752 | let key = if ch == '\n' { |
| 753 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 754 | } else { |
| 755 | plain(ch) |
| 756 | }; |
| 757 | let handled = handle_paste_burst_key( |
| 758 | &mut app, |
| 759 | &key, |
| 760 | start + Duration::from_millis(index as u64), |
| 761 | ); |
| 762 | if index == 0 && !ch.is_ascii() { |
| 763 | assert!(!handled); |
| 764 | app.insert_char(ch); |
| 765 | } else { |
| 766 | assert!(handled, "raw pasted {ch:?} must not reach submit"); |
| 767 | } |
| 768 | } |
| 769 | app.flush_paste_burst_if_due(start + Duration::from_secs(1)); |
| 770 | assert_eq!(app.input, pasted); |
| 771 | assert!(!handle_paste_burst_key( |
| 772 | &mut app, |
| 773 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 774 | start + Duration::from_secs(2), |
| 775 | )); |
| 776 | app.bracketed_paste_seen = true; |
| 777 | assert!(!handle_paste_burst_key( |
| 778 | &mut app, |
| 779 | &plain('x'), |
| 780 | start + Duration::from_secs(3), |
| 781 | )); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | #[test] |
| 786 | fn raw_paste_enter_bypasses_slash_completion_and_deliberate_enter_submits() { |
| 787 | let mut app = test_app(); |
| 788 | let start = Instant::now(); |
| 789 | let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE); |
| 790 | |
| 791 | for (index, ch) in "/mo\nsecond line".chars().enumerate() { |
| 792 | let now = start + Duration::from_millis(2 * index as u64); |
| 793 | app.flush_paste_burst_if_due(now); |
| 794 | let key = if ch == '\n' { enter } else { plain(ch) }; |
| 795 | assert!(handle_paste_burst_key(&mut app, &key, now)); |
| 796 | } |
| 797 | app.flush_paste_burst_if_due(start + Duration::from_secs(1)); |
| 798 | assert_eq!(app.input, "/mo\nsecond line"); |
| 799 | |
| 800 | let mut app = test_app(); |
| 801 | for (index, ch) in "/new".chars().enumerate() { |
| 802 | let now = start + Duration::from_millis(50 * index as u64); |
| 803 | app.flush_paste_burst_if_due(now); |
| 804 | assert!(handle_paste_burst_key(&mut app, &plain(ch), now)); |
| 805 | } |
| 806 | let now = start + Duration::from_millis(175); |
| 807 | app.flush_paste_burst_if_due(now); |
| 808 | assert!(!handle_paste_burst_key(&mut app, &enter, now)); |
| 809 | assert_eq!( |
| 810 | app.handle_composer_enter(true, now).as_deref(), |
| 811 | Some("/new") |
| 812 | ); |
| 813 | } |
| 814 | |
| 815 | /// The same burst stream ending in a normal prompt must still absorb |
| 816 | /// its Enter on terminals without bracketed paste — that absorption is |
| 817 | /// the heuristic's whole job for real multi-line pastes (#1073). |
| 818 | #[test] |
| 819 | fn enter_on_buffered_plain_text_is_still_absorbed() { |
| 820 | let mut app = test_app(); |
| 821 | let t0 = Instant::now(); |
| 822 | |
| 823 | for (i, ch) in "hello world".chars().enumerate() { |
| 824 | assert!(handle_paste_burst_key( |
| 825 | &mut app, |
| 826 | &plain(ch), |
| 827 | t0 + Duration::from_millis(2 * i as u64) |
| 828 | )); |
| 829 | } |
| 830 | assert!(handle_paste_burst_key( |
| 831 | &mut app, |
| 832 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 833 | t0 + Duration::from_millis(60), |
| 834 | )); |
| 835 | } |
| 836 | |
| 837 | #[test] |
| 838 | fn raw_short_cjk_multiline_paste_buffers_enter_instead_of_submitting() { |
| 839 | // #1302: pasting short CJK content like "请联网搜索:\nSTM32 …" used |
| 840 | // to silently submit the first line because the heuristic decided |
| 841 | // it wasn't paste-like (no whitespace + under 16 chars). The |
| 842 | // non-ASCII bypass now classifies it as a paste so the Enter is |
| 843 | // absorbed into the burst buffer. |
| 844 | let mut app = test_app(); |
| 845 | let t0 = Instant::now(); |
| 846 | |
| 847 | let pasted = "请联网搜索:\nSTM32 商业应用案例"; |
| 848 | for (i, ch) in pasted.chars().enumerate() { |
| 849 | let key = if ch == '\n' { |
| 850 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 851 | } else { |
| 852 | plain(ch) |
| 853 | }; |
| 854 | let handled = |
| 855 | handle_paste_burst_key(&mut app, &key, t0 + Duration::from_millis(i as u64)); |
| 856 | if i == 0 && !ch.is_ascii() { |
| 857 | assert!(!handled); |
| 858 | app.insert_char(ch); |
| 859 | } else { |
| 860 | assert!( |
| 861 | handled, |
| 862 | "raw paste character {ch:?} must be handled by paste-burst detection" |
| 863 | ); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | app.flush_paste_burst_if_due(t0 + Duration::from_secs(1)); |
| 868 | assert_eq!(app.input, pasted); |
| 869 | } |
| 870 | |
| 871 | #[test] |
| 872 | fn raw_multiline_paste_buffers_enter_instead_of_submitting() { |
| 873 | let mut app = test_app(); |
| 874 | let t0 = Instant::now(); |
| 875 | |
| 876 | assert!(handle_paste_burst_key(&mut app, &plain('a'), t0)); |
| 877 | assert!(handle_paste_burst_key( |
| 878 | &mut app, |
| 879 | &plain('b'), |
| 880 | t0 + Duration::from_millis(1) |
| 881 | )); |
| 882 | assert!(handle_paste_burst_key( |
| 883 | &mut app, |
| 884 | &plain('c'), |
| 885 | t0 + Duration::from_millis(2) |
| 886 | )); |
| 887 | assert!(handle_paste_burst_key( |
| 888 | &mut app, |
| 889 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 890 | t0 + Duration::from_millis(3) |
| 891 | )); |
| 892 | |
| 893 | assert!(app.input.is_empty(), "paste remains buffered until idle"); |
| 894 | assert!(app.flush_paste_burst_if_due( |
| 895 | t0 + Duration::from_millis(3) |
| 896 | + codewhale_tui_kit::paste_burst::PasteBurst::recommended_active_flush_delay() |
| 897 | )); |
| 898 | assert_eq!(app.input, "abc\n"); |
| 899 | } |
| 900 | |
| 901 | /// A raw CJK paste can open with a one-character line |
| 902 | /// ("好\n…"). That single character never forms a paste-speed *run*, so |
| 903 | /// the short window is all that protects the embedded newline — the |
| 904 | /// newline arrives within the burst interval, so it must still be |
| 905 | /// absorbed rather than submitting "好" on its own (#1302). |
| 906 | #[test] |
| 907 | fn raw_paste_with_single_char_first_line_still_absorbs_its_newline() { |
| 908 | let mut app = test_app(); |
| 909 | let t0 = Instant::now(); |
| 910 | |
| 911 | assert!(!handle_paste_burst_key(&mut app, &plain('好'), t0)); |
| 912 | app.insert_char('好'); |
| 913 | assert!( |
| 914 | handle_paste_burst_key( |
| 915 | &mut app, |
| 916 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 917 | t0 + Duration::from_millis(1), |
| 918 | ), |
| 919 | "the newline of a raw paste lands within the burst interval and \ |
| 920 | must be absorbed, not submitted" |
| 921 | ); |
| 922 | assert_eq!(app.input, "好\n"); |
| 923 | } |
| 924 | |
| 925 | /// The IME half of the same ambiguity: one committed character followed |
| 926 | /// by a human-speed Enter is a send gesture. `handle_paste_burst_key` |
| 927 | /// must decline the Enter so it reaches the normal submit path. |
| 928 | #[test] |
| 929 | fn ime_commit_then_human_enter_falls_through_to_submit() { |
| 930 | let mut app = test_app(); |
| 931 | let t0 = Instant::now(); |
| 932 | |
| 933 | assert!(!handle_paste_burst_key(&mut app, &plain('好'), t0)); |
| 934 | app.insert_char('好'); |
| 935 | assert_eq!(app.input, "好"); |
| 936 | |
| 937 | assert!( |
| 938 | !handle_paste_burst_key( |
| 939 | &mut app, |
| 940 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 941 | t0 + Duration::from_millis(30), |
| 942 | ), |
| 943 | "Enter 30ms after an IME candidate commit is a send, not a \ |
| 944 | pasted newline" |
| 945 | ); |
| 946 | assert_eq!(app.input, "好", "no stray newline may be inserted"); |
| 947 | } |
| 948 | |
| 949 | /// A whole IME-typed CJK sentence, one commit at a time at human speed, |
| 950 | /// followed by Enter: every character lands verbatim and the Enter still |
| 951 | /// reaches the submit path. A 50ms cadence alone must not establish a |
| 952 | /// paste stream (#7398). |
| 953 | #[test] |
| 954 | fn ime_typed_sentence_then_enter_falls_through_to_submit() { |
| 955 | let mut app = test_app(); |
| 956 | let t0 = Instant::now(); |
| 957 | |
| 958 | for (i, ch) in "你好世界".chars().enumerate() { |
| 959 | let now = t0 + Duration::from_millis(200 * i as u64); |
| 960 | assert!(!handle_paste_burst_key(&mut app, &plain(ch), now)); |
| 961 | app.insert_char(ch); |
| 962 | } |
| 963 | assert_eq!(app.input, "你好世界"); |
| 964 | |
| 965 | assert!( |
| 966 | !handle_paste_burst_key( |
| 967 | &mut app, |
| 968 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 969 | t0 + Duration::from_millis(700), |
| 970 | ), |
| 971 | "Enter after an IME-typed CJK message must submit" |
| 972 | ); |
| 973 | assert_eq!(app.input, "你好世界"); |
| 974 | } |
| 975 | |
| 976 | /// Absorbing an Enter outside an active burst must not re-arm the |
| 977 | /// suppression window. It used to, so each swallowed Enter bought |
| 978 | /// another 120ms and a user pressing Enter to send only ever added |
| 979 | /// newlines. The first Enter is still absorbed (#1073 trailing-newline |
| 980 | /// protection); the next one submits. |
| 981 | #[test] |
| 982 | fn absorbed_enter_does_not_extend_the_suppression_window() { |
| 983 | let mut app = test_app(); |
| 984 | let t0 = Instant::now(); |
| 985 | |
| 986 | // Unbracketed paste of "abc" with no trailing newline. |
| 987 | for (i, ch) in "abc".chars().enumerate() { |
| 988 | let now = t0 + Duration::from_millis(i as u64); |
| 989 | assert!(handle_paste_burst_key(&mut app, &plain(ch), now)); |
| 990 | } |
| 991 | let last_char = t0 + Duration::from_millis(2); |
| 992 | assert!(app.flush_paste_burst_if_due( |
| 993 | last_char |
| 994 | + codewhale_tui_kit::paste_burst::PasteBurst::recommended_active_flush_delay() |
| 995 | )); |
| 996 | assert_eq!(app.input, "abc"); |
| 997 | |
| 998 | // Still inside the window: this could be the paste's trailing |
| 999 | // newline, so it is absorbed. |
| 1000 | assert!(handle_paste_burst_key( |
| 1001 | &mut app, |
| 1002 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 1003 | last_char + Duration::from_millis(60), |
| 1004 | )); |
| 1005 | assert_eq!(app.input, "abc\n"); |
| 1006 | |
| 1007 | // Past the window measured from the last *keystroke* — the absorbed |
| 1008 | // Enter bought no extra time, so this one submits. |
| 1009 | assert!( |
| 1010 | !handle_paste_burst_key( |
| 1011 | &mut app, |
| 1012 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 1013 | last_char + Duration::from_millis(121), |
| 1014 | ), |
| 1015 | "the second Enter must reach the submit path" |
| 1016 | ); |
| 1017 | assert_eq!(app.input, "abc\n", "no second newline may be inserted"); |
| 1018 | } |
| 1019 | |
| 1020 | #[test] |
| 1021 | fn paste_buffered_question_mark_does_not_fall_through_to_help_shortcut() { |
| 1022 | let mut app = test_app(); |
| 1023 | let t0 = Instant::now(); |
| 1024 | |
| 1025 | assert!(handle_paste_burst_key(&mut app, &plain('?'), t0)); |
| 1026 | |
| 1027 | assert!(app.input.is_empty(), "shortcut char stays buffered first"); |
| 1028 | assert!(app.view_stack.is_empty(), "help modal must not open"); |
| 1029 | assert!(app.flush_paste_burst_if_due( |
| 1030 | t0 + codewhale_tui_kit::paste_burst::PasteBurst::recommended_flush_delay() |
| 1031 | )); |
| 1032 | assert_eq!(app.input, "?"); |
| 1033 | } |
| 1034 | |
| 1035 | /// Pin the IME-input contract: macOS/Windows input methods commit |
| 1036 | /// each Chinese character as a single `KeyCode::Char(c)` event |
| 1037 | /// after the candidate popup closes. Each codepoint fits in a |
| 1038 | /// `char` (no surrogate pair concerns for BMP chars), so a |
| 1039 | /// straightforward sequence of plain-char events must land in |
| 1040 | /// `app.input` verbatim — no ASCII filter, no byte-vs-char index |
| 1041 | /// drift, no paste-burst false-positive that buffers the chars |
| 1042 | /// indefinitely. |
| 1043 | #[test] |
| 1044 | fn ime_chinese_chars_route_through_to_composer() { |
| 1045 | let mut app = test_app(); |
| 1046 | let t0 = Instant::now(); |
| 1047 | |
| 1048 | // Type the four Chinese codepoints "你好世界" one event at a |
| 1049 | // time, with realistic ~50ms gaps so the paste-burst heuristic |
| 1050 | // doesn't classify them as a paste burst. |
| 1051 | for (i, ch) in "你好世界".chars().enumerate() { |
| 1052 | let now = t0 + Duration::from_millis(50 * i as u64); |
| 1053 | assert!(!handle_paste_burst_key(&mut app, &plain(ch), now)); |
| 1054 | app.insert_char(ch); |
| 1055 | } |
| 1056 | |
| 1057 | // Past the active-flush delay so any buffered burst commits. |
| 1058 | let after = t0 |
| 1059 | + Duration::from_millis(50 * 4) |
| 1060 | + codewhale_tui_kit::paste_burst::PasteBurst::recommended_active_flush_delay(); |
| 1061 | let _ = app.flush_paste_burst_if_due(after); |
| 1062 | |
| 1063 | assert_eq!( |
| 1064 | app.input, "你好世界", |
| 1065 | "IME-typed Chinese characters must land in composer verbatim" |
| 1066 | ); |
| 1067 | assert_eq!( |
| 1068 | app.cursor_position, 4, |
| 1069 | "cursor advances by one per codepoint, not per UTF-8 byte" |
| 1070 | ); |
| 1071 | } |
| 1072 | |
| 1073 | /// Pin the bracketed-paste contract for CJK content: pasted |
| 1074 | /// Chinese text (e.g. when a user copies a question from a |
| 1075 | /// Chinese website and pastes into the composer) must preserve |
| 1076 | /// every codepoint and not double-count multi-byte chars in the |
| 1077 | /// cursor position. |
| 1078 | #[test] |
| 1079 | fn bracketed_paste_preserves_chinese_and_mixed_text() { |
| 1080 | let mut app = test_app(); |
| 1081 | app.insert_paste_text("你好世界 hello 世界 café"); |
| 1082 | assert_eq!(app.input, "你好世界 hello 世界 café"); |
| 1083 | // 4 + 1 + 5 + 1 + 2 + 1 + 4 = 18 codepoints (counting é as one). |
| 1084 | assert_eq!(app.cursor_position, 18); |
| 1085 | } |
| 1086 | |
| 1087 | #[test] |
| 1088 | fn paste_burst_detection_can_be_disabled_without_disabling_bracketed_paste() { |
| 1089 | let mut app = test_app(); |
| 1090 | app.use_paste_burst_detection = false; |
| 1091 | |
| 1092 | assert!(!handle_paste_burst_key( |
| 1093 | &mut app, |
| 1094 | &plain('a'), |
| 1095 | Instant::now() |
| 1096 | )); |
| 1097 | assert!(app.input.is_empty()); |
| 1098 | |
| 1099 | app.insert_paste_text("line 1\r\nline 2"); |
| 1100 | assert_eq!(app.input, "line 1\nline 2"); |
| 1101 | assert!(app.use_bracketed_paste); |
| 1102 | } |
| 1103 | |
| 1104 | /// Once the session has observed a real `Event::Paste`, the |
| 1105 | /// rapid-keystroke heuristic must short-circuit. This pins the new |
| 1106 | /// "auto-disable paste-burst on verified bracketed paste" behavior so |
| 1107 | /// fast typing / IME commits / autocomplete on capable terminals can't |
| 1108 | /// be mis-classified as a paste burst. |
| 1109 | #[test] |
| 1110 | fn paste_burst_short_circuits_after_bracketed_paste_observed() { |
| 1111 | let mut app = test_app(); |
| 1112 | app.use_paste_burst_detection = true; |
| 1113 | app.bracketed_paste_seen = true; |
| 1114 | |
| 1115 | let t0 = Instant::now(); |
| 1116 | for (i, ch) in "abcdefgh".chars().enumerate() { |
| 1117 | // Type fast enough that paste-burst would normally fire. |
| 1118 | let now = t0 + Duration::from_millis(i as u64); |
| 1119 | assert!( |
| 1120 | !handle_paste_burst_key(&mut app, &plain(ch), now), |
| 1121 | "paste-burst must NOT consume keys once bracketed paste verified" |
| 1122 | ); |
| 1123 | } |
| 1124 | // No buffering — every char fell through to the normal composer |
| 1125 | // path (the test harness doesn't insert chars when the burst |
| 1126 | // handler returns false; we only assert the short-circuit |
| 1127 | // contract here). |
| 1128 | assert!(app.input.is_empty()); |
| 1129 | } |
| 1130 | |
| 1131 | #[test] |
| 1132 | fn oversized_raw_and_bracketed_pastes_preserve_every_char_and_newline() { |
| 1133 | use codewhale_tui_kit::paste_burst::{FlushResult, PasteBurst}; |
| 1134 | |
| 1135 | // The 31-byte line is deliberate. Chunk boundaries drift against the |
| 1136 | // 64 KiB cap instead of landing on it, so the "does this character |
| 1137 | // still fit" branch is actually exercised. A line whose cycles divide |
| 1138 | // the cap exactly — `cw-7403-漢字-🐳-line\n` is 25 bytes, and |
| 1139 | // `25 × 2621` hits 65 525 with 11 bytes to spare — drains at the exact |
| 1140 | // boundary every time and never trips that branch, so #7403's original |
| 1141 | // drop rule could come back without failing this test (verified by |
| 1142 | // injecting it: 0 bytes lost on the 25-byte line, 10 bytes lost on |
| 1143 | // this one). |
| 1144 | let payload = "cw-7403-漢字-🐳-paste-line\n".repeat(11_000); |
| 1145 | let character_count = payload.chars().count(); |
| 1146 | assert!(payload.len() > 256 * 1024); |
| 1147 | |
| 1148 | let mut raw = test_app(); |
| 1149 | raw.use_paste_burst_detection = true; |
| 1150 | raw.bracketed_paste_seen = false; |
| 1151 | raw.bracketed_paste_trusted = false; |
| 1152 | |
| 1153 | let mut now = Instant::now(); |
| 1154 | for (index, ch) in payload.chars().enumerate() { |
| 1155 | now += Duration::from_micros(100); |
| 1156 | let key = if ch == '\n' { |
| 1157 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 1158 | } else { |
| 1159 | plain(ch) |
| 1160 | }; |
| 1161 | let handled = handle_paste_burst_key(&mut raw, &key, now); |
| 1162 | if index == 0 && !ch.is_ascii() { |
| 1163 | assert!(!handled); |
| 1164 | raw.insert_char(ch); |
| 1165 | } else { |
| 1166 | assert!(handled); |
| 1167 | } |
| 1168 | } |
| 1169 | let flush_at = now + PasteBurst::recommended_active_flush_delay(); |
| 1170 | let FlushResult::TextContinuation(tail) = raw.take_paste_burst_flush_if_enabled(flush_at) |
| 1171 | else { |
| 1172 | panic!("oversized raw paste must flush its final continuation"); |
| 1173 | }; |
| 1174 | raw.insert_str(&tail); |
| 1175 | assert_eq!(raw.input, payload); |
| 1176 | assert_eq!(raw.cursor_position, character_count); |
| 1177 | |
| 1178 | let mut bracketed = test_app(); |
| 1179 | bracketed.insert_paste_text(&payload); |
| 1180 | assert_eq!(bracketed.input, raw.input); |
| 1181 | assert_eq!(bracketed.cursor_position, character_count); |
| 1182 | } |
| 1183 | |
| 1184 | /// Drive a raw (non-bracketed) paste one key event per character. |
| 1185 | /// |
| 1186 | /// Intermediate chunks are committed by `handle_paste_burst_key` itself |
| 1187 | /// (`insert_str` right after each `take_chunk_if_full`); this helper only |
| 1188 | /// commits the tail, through `App::take_paste_burst_flush_if_enabled` — the |
| 1189 | /// same non-test entry `event_loop.rs` calls, so the fixture cannot pass |
| 1190 | /// against a test-only stub. |
| 1191 | /// |
| 1192 | /// `input == payload` alone cannot prove chunking: a buffer that never |
| 1193 | /// flushes still ends up correct once the tail lands, so deleting a |
| 1194 | /// `take_chunk_if_full()` check would pass unnoticed. The probe at 60% |
| 1195 | /// fails in exactly that case — the composer is still nearly empty |
| 1196 | /// because everything is sitting in the burst buffer. |
| 1197 | /// |
| 1198 | /// The probe names no cap, but it is not cap-independent: it needs |
| 1199 | /// `floor(0.6·P / cap) · cap ≥ P/3`, i.e. roughly `cap < 4P/15 ≈ 0.27·P`. |
| 1200 | /// For these fixtures that is ~72 KiB against the 64 KiB cap, so there is |
| 1201 | /// margin — but raising `PASTE_BURST_MAX_BYTES` past that fraction of a |
| 1202 | /// fixture would fail this test on a healthy build. Widen the payload |
| 1203 | /// along with the cap. |
| 1204 | /// |
| 1205 | /// # What this cannot see |
| 1206 | /// |
| 1207 | /// The third `take_chunk_if_full()` call, the one after |
| 1208 | /// `append_newline_if_active` in the `KeyCode::Enter` arm, is out of reach |
| 1209 | /// here: this loop sends no newline, and the 60% probe would fail on a run |
| 1210 | /// that deliberately parks the buffer just under the cap. That site has its |
| 1211 | /// own fixture — `pasted_newline_crossing_the_chunk_cap_flushes_immediately` |
| 1212 | /// — which fills to `CAP - 1` bytes and then sends one newline, so the |
| 1213 | /// composer length directly reports whether the Enter arm flushed. |
| 1214 | /// |
| 1215 | /// An earlier revision of this comment argued the site needed no test at |
| 1216 | /// all because deleting it "leaves the text complete". That was half right |
| 1217 | /// and dangerous: the *text* survives, but the flush moves from the Enter |
| 1218 | /// to the next plain character, and an assertion placed between the two |
| 1219 | /// sees it. What was untestable was content equality, not the behaviour. |
| 1220 | fn feed_raw_paste(app: &mut App, payload: &str) { |
| 1221 | use codewhale_tui_kit::paste_burst::{FlushResult, PasteBurst}; |
| 1222 | |
| 1223 | let probe_at = payload.chars().count() * 3 / 5; |
| 1224 | let mut now = Instant::now(); |
| 1225 | for (index, ch) in payload.chars().enumerate() { |
| 1226 | now += Duration::from_micros(100); |
| 1227 | let key = if ch == '\n' { |
| 1228 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 1229 | } else { |
| 1230 | plain(ch) |
| 1231 | }; |
| 1232 | let handled = handle_paste_burst_key(app, &key, now); |
| 1233 | if index == 0 && !ch.is_ascii() { |
| 1234 | assert!(!handled); |
| 1235 | app.insert_char(ch); |
| 1236 | } else { |
| 1237 | assert!( |
| 1238 | handled, |
| 1239 | "raw paste character {ch:?} must be handled by paste-burst detection" |
| 1240 | ); |
| 1241 | } |
| 1242 | if index + 1 == probe_at { |
| 1243 | assert!( |
| 1244 | app.input.len() * 3 >= payload.len(), |
| 1245 | "60% into a multi-chunk paste the composer holds {} of {} bytes: the \ |
| 1246 | burst buffer is accumulating the paste instead of flushing chunks", |
| 1247 | app.input.len(), |
| 1248 | payload.len() |
| 1249 | ); |
| 1250 | } |
| 1251 | } |
| 1252 | let flush_at = now + PasteBurst::recommended_active_flush_delay(); |
| 1253 | let FlushResult::TextContinuation(tail) = app.take_paste_burst_flush_if_enabled(flush_at) |
| 1254 | else { |
| 1255 | panic!("an oversized raw paste must flush its final continuation, not a first `Paste`"); |
| 1256 | }; |
| 1257 | app.insert_str(&tail); |
| 1258 | } |
| 1259 | |
| 1260 | /// #7403, ASCII append path only: every character here takes |
| 1261 | /// `KeyCode::Char` → `on_plain_char` → `handle_paste_burst_decision`, and |
| 1262 | /// nothing else. |
| 1263 | /// |
| 1264 | /// The mixed-script fixture above cannot stand in for this. Each CJK or |
| 1265 | /// emoji character re-enters the separate `!c.is_ascii()` branch in |
| 1266 | /// `handle_paste_burst_key`, whose own `take_chunk_if_full()` call flushes |
| 1267 | /// the buffer and hides a missing check on the ASCII side — delete only |
| 1268 | /// the ASCII-path check and that test stays green. This payload has no |
| 1269 | /// non-ASCII byte and, deliberately, no newline either: an Enter reaches |
| 1270 | /// `append_newline_if_active`, whose chunk check would stand in again. |
| 1271 | /// |
| 1272 | /// # What this one is for |
| 1273 | /// |
| 1274 | /// One failure mode, precisely: the ASCII-path chunk check going away. |
| 1275 | /// Delete it and the buffer accumulates the whole paste, so the 60% probe |
| 1276 | /// finds the composer empty (0 of 273 000 bytes) and fails. |
| 1277 | /// |
| 1278 | /// It is the *wrong* fixture for #7403's original drop rule, and that is |
| 1279 | /// arithmetic rather than an oversight: every byte here is one UTF-8 byte |
| 1280 | /// wide, so the buffer fills to exactly the cap every cycle and a |
| 1281 | /// `len + ch.len_utf8() <= cap` guard always admits the character. Injecting |
| 1282 | /// that rule leaves this test green (0 bytes lost); the CJK fixture below, |
| 1283 | /// whose characters are 3–4 bytes wide, loses its tail and fails. |
| 1284 | #[test] |
| 1285 | fn ascii_only_raw_paste_over_the_chunk_cap_lands_complete() { |
| 1286 | let mut app = test_app(); |
| 1287 | app.use_paste_burst_detection = true; |
| 1288 | app.bracketed_paste_seen = false; |
| 1289 | app.bracketed_paste_trusted = false; |
| 1290 | |
| 1291 | let payload = "cw-7403-ascii-paste-chunking".repeat(9_750); |
| 1292 | assert!( |
| 1293 | payload.len() > 256 * 1024, |
| 1294 | "fixture must exceed four 64 KiB chunk caps, got {} bytes", |
| 1295 | payload.len() |
| 1296 | ); |
| 1297 | |
| 1298 | feed_raw_paste(&mut app, &payload); |
| 1299 | |
| 1300 | assert_eq!(app.input, payload); |
| 1301 | assert_eq!(app.cursor_position, payload.chars().count()); |
| 1302 | } |
| 1303 | |
| 1304 | /// #7403, non-ASCII append path only: a CJK-only burst stays in the |
| 1305 | /// `!c.is_ascii()` branch for its whole length, so this is the only |
| 1306 | /// fixture that exercises that branch's `take_chunk_if_full()` call in |
| 1307 | /// isolation. A mixed payload keeps ASCII nearby and that check would |
| 1308 | /// flush on the next ASCII character, masking the gap. Again no newline, |
| 1309 | /// for the same reason as the ASCII case. |
| 1310 | /// |
| 1311 | /// # What this one is for |
| 1312 | /// |
| 1313 | /// Two failure modes: the non-ASCII-path chunk check going away (the 60% |
| 1314 | /// probe then finds 3 of 342 000 bytes), and #7403's original drop rule |
| 1315 | /// coming back. The second works here and not in the ASCII fixture because |
| 1316 | /// these characters are 3–4 bytes wide: the buffer no longer fills to |
| 1317 | /// exactly the cap, the guard rejects the character that would cross it, |
| 1318 | /// and the paste loses its tail. |
| 1319 | #[test] |
| 1320 | fn cjk_only_raw_paste_over_the_chunk_cap_lands_complete() { |
| 1321 | let mut app = test_app(); |
| 1322 | app.use_paste_burst_detection = true; |
| 1323 | app.bracketed_paste_seen = false; |
| 1324 | app.bracketed_paste_trusted = false; |
| 1325 | |
| 1326 | let payload = "漢字テスト🐳".repeat(18_000); |
| 1327 | assert!( |
| 1328 | payload.len() > 256 * 1024, |
| 1329 | "fixture must exceed four 64 KiB chunk caps, got {} bytes", |
| 1330 | payload.len() |
| 1331 | ); |
| 1332 | |
| 1333 | feed_raw_paste(&mut app, &payload); |
| 1334 | |
| 1335 | assert_eq!(app.input, payload); |
| 1336 | assert_eq!(app.cursor_position, payload.chars().count()); |
| 1337 | } |
| 1338 | |
| 1339 | /// #7403, Enter append path: the third `take_chunk_if_full`, the one right |
| 1340 | /// after `append_newline_if_active`. |
| 1341 | /// |
| 1342 | /// The other fixtures cannot reach it. They carry no newline, and their |
| 1343 | /// 60% probe would fail on a run that deliberately keeps the composer |
| 1344 | /// empty — so this test fills the burst buffer to just under the cap and |
| 1345 | /// then sends exactly one newline. |
| 1346 | /// |
| 1347 | /// That newline is the only thing that can flush the buffer at this point: |
| 1348 | /// no plain character follows, so neither the ASCII nor the non-ASCII |
| 1349 | /// check gets a turn. The composer length is therefore a direct readout of |
| 1350 | /// whether the Enter arm flushed. Delete that `take_chunk_if_full` and the |
| 1351 | /// buffer sits at the cap while the composer stays empty, and the |
| 1352 | /// assertion below fails. |
| 1353 | /// |
| 1354 | /// This is why the tail flush in `feed_raw_paste` is not the only place |
| 1355 | /// chunking can be observed — an earlier revision of these docs claimed |
| 1356 | /// no content assertion could see this site, which was wrong. |
| 1357 | #[test] |
| 1358 | fn pasted_newline_crossing_the_chunk_cap_flushes_immediately() { |
| 1359 | // Mirrors `PASTE_BURST_MAX_BYTES`, which is private to `codewhale-tui-kit`. |
| 1360 | const CAP: usize = 64 * 1024; |
| 1361 | |
| 1362 | let mut app = test_app(); |
| 1363 | app.use_paste_burst_detection = true; |
| 1364 | app.bracketed_paste_seen = false; |
| 1365 | app.bracketed_paste_trusted = false; |
| 1366 | |
| 1367 | // Fill to CAP - 1 bytes. The plain-character checks fire at `>= CAP`, |
| 1368 | // so nothing flushes yet and the composer must stay empty. |
| 1369 | let mut now = Instant::now(); |
| 1370 | while app.paste_burst.held_text().len() < CAP - 1 { |
| 1371 | now += Duration::from_micros(100); |
| 1372 | assert!(handle_paste_burst_key(&mut app, &plain('x'), now)); |
| 1373 | assert!( |
| 1374 | app.input.is_empty(), |
| 1375 | "the buffer must not cross the cap while filling; it holds {} bytes", |
| 1376 | app.paste_burst.held_text().len() |
| 1377 | ); |
| 1378 | } |
| 1379 | let buffered = app.paste_burst.held_text().len(); |
| 1380 | assert_eq!( |
| 1381 | buffered, |
| 1382 | CAP - 1, |
| 1383 | "fill loop must stop one byte under the cap" |
| 1384 | ); |
| 1385 | |
| 1386 | now += Duration::from_micros(100); |
| 1387 | assert!(handle_paste_burst_key( |
| 1388 | &mut app, |
| 1389 | &KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 1390 | now |
| 1391 | )); |
| 1392 | |
| 1393 | assert_eq!( |
| 1394 | app.input.len(), |
| 1395 | CAP, |
| 1396 | "a pasted newline that crosses the cap must flush the buffer at once instead \ |
| 1397 | of waiting for the next plain character" |
| 1398 | ); |
| 1399 | } |
| 1400 | |
| 1401 | #[test] |
| 1402 | fn launch_composer_preserves_newlines_during_active_paste_burst() { |
| 1403 | let mut app = test_app(); |
| 1404 | app.use_paste_burst_detection = true; |
| 1405 | app.bracketed_paste_seen = false; |
| 1406 | app.bracketed_paste_trusted = false; |
| 1407 | app.launch.visible = true; |
| 1408 | app.launch.composer_focus = true; |
| 1409 | |
| 1410 | let payload = "cw-clipboard-01 漢字 🐳 line 1\ncw-clipboard-02 漢字 🐳 line 2\ncw-clipboard-03 漢字 🐳 line 3"; |
| 1411 | let mut now = Instant::now(); |
| 1412 | for ch in payload.chars() { |
| 1413 | now += Duration::from_millis(1); |
| 1414 | let key = if ch == '\n' { |
| 1415 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) |
| 1416 | } else { |
| 1417 | plain(ch) |
| 1418 | }; |
| 1419 | let launch_verdict = crate::tui::underwater::handle_launch_composer_key(&mut app, key); |
| 1420 | assert!( |
| 1421 | matches!( |
| 1422 | launch_verdict, |
| 1423 | crate::tui::underwater::LaunchComposerKey::ComposerAuthority |
| 1424 | ), |
| 1425 | "raw paste key {ch:?} on launch screen must route to ComposerAuthority" |
| 1426 | ); |
| 1427 | assert!(handle_paste_burst_key(&mut app, &key, now)); |
| 1428 | } |
| 1429 | let flush_at = |
| 1430 | now + codewhale_tui_kit::paste_burst::PasteBurst::recommended_active_flush_delay(); |
| 1431 | if let codewhale_tui_kit::paste_burst::FlushResult::Paste(rest) = |
| 1432 | app.paste_burst.flush_if_due(flush_at) |
| 1433 | { |
| 1434 | app.insert_str(&rest); |
| 1435 | } |
| 1436 | assert_eq!(app.input, payload); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | #[cfg(test)] |
| 1441 | mod primary_paste_tests { |
| 1442 | use super::{PasteSource, PasteTarget, PendingPaste, handle_paste}; |
| 1443 | use std::time::Instant; |
| 1444 | |
| 1445 | fn app() -> crate::tui::app::App { |
| 1446 | crate::tui::test_support::test_app_with_options(crate::tui::test_support::test_tui_options( |
| 1447 | std::env::temp_dir(), |
| 1448 | )) |
| 1449 | } |
| 1450 | |
| 1451 | #[test] |
| 1452 | fn primary_paste_lands_at_position_in_composer() { |
| 1453 | let mut app = app(); |
| 1454 | app.input = "abc".into(); |
| 1455 | assert!(handle_paste( |
| 1456 | &mut app, |
| 1457 | PasteSource::Primary { |
| 1458 | text: "X", |
| 1459 | position: 1 |
| 1460 | } |
| 1461 | )); |
| 1462 | assert_eq!(app.input, "aXbc"); |
| 1463 | } |
| 1464 | |
| 1465 | #[test] |
| 1466 | fn primary_paste_is_refused_while_a_clipboard_read_is_pending() { |
| 1467 | let mut app = app(); |
| 1468 | app.input = "abc".into(); |
| 1469 | let (_tx, rx) = std::sync::mpsc::channel(); |
| 1470 | app.pending_paste = Some(PendingPaste { |
| 1471 | target: PasteTarget::Composer, |
| 1472 | image_only: false, |
| 1473 | started: Instant::now(), |
| 1474 | rx, |
| 1475 | }); |
| 1476 | assert!(!handle_paste( |
| 1477 | &mut app, |
| 1478 | PasteSource::Primary { |
| 1479 | text: "X", |
| 1480 | position: 1 |
| 1481 | } |
| 1482 | )); |
| 1483 | assert_eq!(app.input, "abc"); |
| 1484 | } |
| 1485 | } |
| 1486 |